diagnostics: avoid ICE in confusable_method_name for associated functions

Avoid unconditionally slicing `inputs()[1..]`, which assumes a `self`
parameter. Use `is_method()` to conditionally skip the receiver,
preventing out-of-bounds access and fixing suggestions for associated
functions.

Signed-off-by: Usman Akinyemi <usmanakinyemi202@gmail.com>
This commit is contained in:
Usman Akinyemi
2026-03-23 01:24:23 +05:30
parent ac7f9ec7da
commit fa3e85a7e8
3 changed files with 59 additions and 1 deletions
@@ -2291,8 +2291,11 @@ pub(crate) fn confusable_method_name(
fn_sig,
);
let name = inherent_method.name();
let inputs = fn_sig.inputs();
let expected_inputs =
if inherent_method.is_method() { &inputs[1..] } else { inputs };
if let Some(ref args) = call_args
&& fn_sig.inputs()[1..]
&& expected_inputs
.iter()
.eq_by(args, |expected, found| self.may_coerce(*expected, *found))
{
@@ -0,0 +1,22 @@
#![feature(rustc_attrs)]
struct S;
impl S {
#[rustc_confusables("bar")]
fn foo() {}
#[rustc_confusables("baz")]
fn qux(&self, x: i32) {}
}
fn main() {
S::bar();
//~^ ERROR no function or associated item named `bar`
//~| HELP you might have meant to use `foo`
let s = S;
s.baz(10);
//~^ ERROR no method named `baz`
//~| HELP you might have meant to use `qux`
}
@@ -0,0 +1,33 @@
error[E0599]: no function or associated item named `bar` found for struct `S` in the current scope
--> $DIR/rustc_confusables_assoc_fn.rs:14:8
|
LL | struct S;
| -------- function or associated item `bar` not found for this struct
...
LL | S::bar();
| ^^^ function or associated item not found in `S`
|
help: you might have meant to use `foo`
|
LL - S::bar();
LL + S::foo();
|
error[E0599]: no method named `baz` found for struct `S` in the current scope
--> $DIR/rustc_confusables_assoc_fn.rs:19:7
|
LL | struct S;
| -------- method `baz` not found for this struct
...
LL | s.baz(10);
| ^^^ method not found in `S`
|
help: you might have meant to use `qux`
|
LL - s.baz(10);
LL + s.qux(10);
|
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0599`.