Skip to content

Avoid empty identifiers for delegate params and args. #139614

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions compiler/rustc_ast_lowering/src/delegation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use rustc_errors::ErrorGuaranteed;
use rustc_hir::def_id::DefId;
use rustc_middle::span_bug;
use rustc_middle::ty::{Asyncness, ResolverAstLowering};
use rustc_span::{Ident, Span};
use rustc_span::{Ident, Span, Symbol};
use {rustc_ast as ast, rustc_hir as hir};

use super::{GenericArgsMode, ImplTraitContext, LoweringContext, ParamMode};
Expand Down Expand Up @@ -234,22 +234,23 @@ impl<'hir> LoweringContext<'_, 'hir> {
hir::FnSig { decl, header, span }
}

fn generate_param(&mut self, span: Span) -> (hir::Param<'hir>, NodeId) {
fn generate_param(&mut self, idx: usize, span: Span) -> (hir::Param<'hir>, NodeId) {
let pat_node_id = self.next_node_id();
let pat_id = self.lower_node_id(pat_node_id);
let ident = Ident::with_dummy_span(Symbol::intern(&format!("arg{idx}")));
let pat = self.arena.alloc(hir::Pat {
hir_id: pat_id,
kind: hir::PatKind::Binding(hir::BindingMode::NONE, pat_id, Ident::empty(), None),
kind: hir::PatKind::Binding(hir::BindingMode::NONE, pat_id, ident, None),
span,
default_binding_modes: false,
});

(hir::Param { hir_id: self.next_id(), pat, ty_span: span, span }, pat_node_id)
}

fn generate_arg(&mut self, param_id: HirId, span: Span) -> hir::Expr<'hir> {
fn generate_arg(&mut self, idx: usize, param_id: HirId, span: Span) -> hir::Expr<'hir> {
let segments = self.arena.alloc_from_iter(iter::once(hir::PathSegment {
ident: Ident::empty(),
ident: Ident::with_dummy_span(Symbol::intern(&format!("arg{idx}"))),
hir_id: self.next_id(),
res: Res::Local(param_id),
args: None,
Expand All @@ -273,7 +274,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let mut args: Vec<hir::Expr<'_>> = Vec::with_capacity(param_count);

for idx in 0..param_count {
let (param, pat_node_id) = this.generate_param(span);
let (param, pat_node_id) = this.generate_param(idx, span);
parameters.push(param);

let arg = if let Some(block) = block
Expand All @@ -289,7 +290,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
this.ident_and_label_to_local_id.insert(pat_node_id, param.pat.hir_id.local_id);
this.lower_target_expr(&block)
} else {
this.generate_arg(param.pat.hir_id, span)
this.generate_arg(idx, param.pat.hir_id, span)
};
args.push(arg);
}
Expand Down
23 changes: 23 additions & 0 deletions tests/pretty/hir-delegation.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//@ pretty-compare-only
//@ pretty-mode:hir
//@ pp-exact:hir-delegation.pp

#![allow(incomplete_features)]#![feature(fn_delegation)]
#[prelude_import]
use ::std::prelude::rust_2015::*;
#[macro_use]
extern crate std;

fn b<C>(e: C) { }

trait G {
fn b(arg0: _) -> _ { b({ }) }
}

mod m {
fn add(a: u32, b: u32) -> u32 { a + b }
}

fn add(arg0: _, arg1: _) -> _ { m::add(arg0, arg1) }

fn main() { { let _ = add(1, 2); }; }
22 changes: 22 additions & 0 deletions tests/pretty/hir-delegation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//@ pretty-compare-only
//@ pretty-mode:hir
//@ pp-exact:hir-delegation.pp

#![allow(incomplete_features)]
#![feature(fn_delegation)]

fn b<C>(e: C) {}

trait G {
reuse b {}
}

mod m {
pub fn add(a: u32, b: u32) -> u32 { a + b }
}

reuse m::add;

fn main() {
_ = add(1, 2);
}
Loading