Skip to content

Fixed ICE when using a function with anonymous type parameters as a const generic argument#158329

Open
kytoaa wants to merge 1 commit into
rust-lang:mainfrom
kytoaa:issue-158152
Open

Fixed ICE when using a function with anonymous type parameters as a const generic argument#158329
kytoaa wants to merge 1 commit into
rust-lang:mainfrom
kytoaa:issue-158152

Conversation

@kytoaa

@kytoaa kytoaa commented Jun 23, 2026

Copy link
Copy Markdown

Detects anonymous type parameters in functions being used as const generic arguments, and returns the DefId of the parameter for emitting an error.

I don't love the solution of adding an Option<DefId> to the tuple returned by lower_generic_args_of_path, and it might be nicer to wrap it in some kind of larger result type with the GenericArgCountResult, but changes like that would probably be more suitable for a larger refactor that takes into account delegation's requirements for lower_generic_args_of_path.

Fixes #158152

Added a test with a more minimal example

@rustbot

rustbot commented Jun 23, 2026

Copy link
Copy Markdown
Collaborator

HIR ty lowering was modified

cc @fmease

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 23, 2026
@rustbot

rustbot commented Jun 23, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome! The Rust Project is excited to review your changes, and you should hear from @nnethercote (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue
Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler, types
  • compiler, types expanded to 73 candidates
  • Random selection from 20 candidates

@fmease fmease assigned fmease and unassigned nnethercote Jun 23, 2026
Comment on lines +2850 to +2865
if let Some(param_id) = inferred_synth_arg {
let fn_name = path.segments.last().unwrap().ident;
let anon_param = tcx.def_path_str(param_id);
let fn_span = tcx.hir_span_if_local(did);

let e = tcx
.dcx()
.create_err(ConstParamCannotInferAnonTypeParam {
span,
fn_name,
anon_param,
fn_span,
})
.emit();

ty::Const::new_error(tcx, e)

@fmease fmease Jun 23, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a hyper specific band-aid solution to a lowering step that's simply incorrect (introduced in #155096) and only happens to work for delegation because of the way it gets lowered.

This function should not need to know which APITs were inferred uprooted.

Copy/pasting https://github.com/rust-lang/rust/pull/155096/changes#r3462851183:

+ } else if synthetic {
+     Ty::new_param(tcx, param.index, param.name).into()

This is not correct. It's injecting type parameters into a whole different context, it's uprooting a parameter from the def site and planting it at the use site (semi related: things like EarlyBinder solely exist to force people to think about this context change..).

I guess it only happens to work for delegation specifically because the generated delegate either reuses or has a copy of the generic parameters of the delegated function.

It's basically doing this:

fn f() {
    g();
    // ==>
    g::<T>(); // but `T` is not in scope!
}

fn g<T>() {}

View changes since the review

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 23, 2026
@rustbot

rustbot commented Jun 23, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ICE]: ...has parameters, but no args were provided in instantiate

4 participants