Skip to content

Commit a496d1d

Browse files
Rollup merge of #150585 - issue-149559, r=petrochenkov
Add a context-consistency check before emitting redundant generic-argument suggestions Fixes #149559 Add a context-consistency check before emitting redundant generic-argument suggestions. If the redundant argument spans come from mixed hygiene contexts (e.g., macro definition + macro callsite), we skip the suggestion to avoid malformed `shrink_to_hi().to(...)` spans and potential ICEs.
2 parents 86a49fd + 2c31b0d commit a496d1d

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

‎compiler/rustc_hir_analysis/src/errors/wrong_number_of_generic_args.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,9 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
988988
gen_arg_spans[self.num_expected_type_or_const_args() - 1]
989989
};
990990
let span_hi_redundant_type_or_const_args = gen_arg_spans[gen_arg_spans.len() - 1];
991+
if !span_lo_redundant_type_or_const_args.eq_ctxt(span_hi_redundant_type_or_const_args) {
992+
return;
993+
}
991994
let span_redundant_type_or_const_args = span_lo_redundant_type_or_const_args
992995
.shrink_to_hi()
993996
.to(span_hi_redundant_type_or_const_args);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Regression test for #149559
2+
3+
struct Foo<T>; //~ ERROR type parameter `T` is never used
4+
5+
macro_rules! foo_ty {
6+
($a:ty, $b:ty) => {
7+
Foo<a, $b>
8+
//~^ ERROR cannot find type `a` in this scope
9+
//~| ERROR struct takes 1 generic argument but 2 generic arguments were supplied
10+
};
11+
}
12+
13+
fn foo<'a, 'b>() -> foo_ty!(&'b (), &'b ()) {}
14+
15+
fn main() {}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
error[E0425]: cannot find type `a` in this scope
2+
--> $DIR/wrong-number-of-args-in-macro.rs:7:13
3+
|
4+
LL | Foo<a, $b>
5+
| ^ not found in this scope
6+
...
7+
LL | fn foo<'a, 'b>() -> foo_ty!(&'b (), &'b ()) {}
8+
| ----------------------- in this macro invocation
9+
|
10+
= note: this error originates in the macro `foo_ty` (in Nightly builds, run with -Z macro-backtrace for more info)
11+
help: you might be missing a type parameter
12+
|
13+
LL | fn foo<'a, 'b, a>() -> foo_ty!(&'b (), &'b ()) {}
14+
| +++
15+
16+
error[E0107]: struct takes 1 generic argument but 2 generic arguments were supplied
17+
--> $DIR/wrong-number-of-args-in-macro.rs:7:9
18+
|
19+
LL | Foo<a, $b>
20+
| ^^^ expected 1 generic argument
21+
...
22+
LL | fn foo<'a, 'b>() -> foo_ty!(&'b (), &'b ()) {}
23+
| ----------------------- in this macro invocation
24+
|
25+
note: struct defined here, with 1 generic parameter: `T`
26+
--> $DIR/wrong-number-of-args-in-macro.rs:3:8
27+
|
28+
LL | struct Foo<T>;
29+
| ^^^ -
30+
= note: this error originates in the macro `foo_ty` (in Nightly builds, run with -Z macro-backtrace for more info)
31+
32+
error[E0392]: type parameter `T` is never used
33+
--> $DIR/wrong-number-of-args-in-macro.rs:3:12
34+
|
35+
LL | struct Foo<T>;
36+
| ^ unused type parameter
37+
|
38+
= help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`
39+
= help: if you intended `T` to be a const parameter, use `const T: /* Type */` instead
40+
41+
error: aborting due to 3 previous errors
42+
43+
Some errors have detailed explanations: E0107, E0392, E0425.
44+
For more information about an error, try `rustc --explain E0107`.

0 commit comments

Comments
 (0)