Skip to content

Commit c570b97

Browse files
Rollup merge of #154225 - Unique-Usman:ua/rustc_confusable, r=Kivooeo
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.
2 parents 43654df + fa3e85a commit c570b97

3 files changed

Lines changed: 59 additions & 1 deletion

File tree

compiler/rustc_hir_typeck/src/method/suggest.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2291,8 +2291,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
22912291
fn_sig,
22922292
);
22932293
let name = inherent_method.name();
2294+
let inputs = fn_sig.inputs();
2295+
let expected_inputs =
2296+
if inherent_method.is_method() { &inputs[1..] } else { inputs };
22942297
if let Some(ref args) = call_args
2295-
&& fn_sig.inputs()[1..]
2298+
&& expected_inputs
22962299
.iter()
22972300
.eq_by(args, |expected, found| self.may_coerce(*expected, *found))
22982301
{
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#![feature(rustc_attrs)]
2+
3+
struct S;
4+
5+
impl S {
6+
#[rustc_confusables("bar")]
7+
fn foo() {}
8+
9+
#[rustc_confusables("baz")]
10+
fn qux(&self, x: i32) {}
11+
}
12+
13+
fn main() {
14+
S::bar();
15+
//~^ ERROR no function or associated item named `bar`
16+
//~| HELP you might have meant to use `foo`
17+
18+
let s = S;
19+
s.baz(10);
20+
//~^ ERROR no method named `baz`
21+
//~| HELP you might have meant to use `qux`
22+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
error[E0599]: no function or associated item named `bar` found for struct `S` in the current scope
2+
--> $DIR/rustc_confusables_assoc_fn.rs:14:8
3+
|
4+
LL | struct S;
5+
| -------- function or associated item `bar` not found for this struct
6+
...
7+
LL | S::bar();
8+
| ^^^ function or associated item not found in `S`
9+
|
10+
help: you might have meant to use `foo`
11+
|
12+
LL - S::bar();
13+
LL + S::foo();
14+
|
15+
16+
error[E0599]: no method named `baz` found for struct `S` in the current scope
17+
--> $DIR/rustc_confusables_assoc_fn.rs:19:7
18+
|
19+
LL | struct S;
20+
| -------- method `baz` not found for this struct
21+
...
22+
LL | s.baz(10);
23+
| ^^^ method not found in `S`
24+
|
25+
help: you might have meant to use `qux`
26+
|
27+
LL - s.baz(10);
28+
LL + s.qux(10);
29+
|
30+
31+
error: aborting due to 2 previous errors
32+
33+
For more information about this error, try `rustc --explain E0599`.

0 commit comments

Comments
 (0)