check if redundant args spans belong to the same context

This commit is contained in:
Takayuki Maeda
2026-01-02 15:59:03 +09:00
parent 68f11a11b6
commit 2c31b0deb1
3 changed files with 62 additions and 0 deletions
@@ -988,6 +988,9 @@ fn suggest_removing_args_or_generics(&self, err: &mut Diag<'_, impl EmissionGuar
gen_arg_spans[self.num_expected_type_or_const_args() - 1]
};
let span_hi_redundant_type_or_const_args = gen_arg_spans[gen_arg_spans.len() - 1];
if !span_lo_redundant_type_or_const_args.eq_ctxt(span_hi_redundant_type_or_const_args) {
return;
}
let span_redundant_type_or_const_args = span_lo_redundant_type_or_const_args
.shrink_to_hi()
.to(span_hi_redundant_type_or_const_args);
@@ -0,0 +1,15 @@
// Regression test for #149559
struct Foo<T>; //~ ERROR type parameter `T` is never used
macro_rules! foo_ty {
($a:ty, $b:ty) => {
Foo<a, $b>
//~^ ERROR cannot find type `a` in this scope
//~| ERROR struct takes 1 generic argument but 2 generic arguments were supplied
};
}
fn foo<'a, 'b>() -> foo_ty!(&'b (), &'b ()) {}
fn main() {}
@@ -0,0 +1,44 @@
error[E0425]: cannot find type `a` in this scope
--> $DIR/wrong-number-of-args-in-macro.rs:7:13
|
LL | Foo<a, $b>
| ^ not found in this scope
...
LL | fn foo<'a, 'b>() -> foo_ty!(&'b (), &'b ()) {}
| ----------------------- in this macro invocation
|
= note: this error originates in the macro `foo_ty` (in Nightly builds, run with -Z macro-backtrace for more info)
help: you might be missing a type parameter
|
LL | fn foo<'a, 'b, a>() -> foo_ty!(&'b (), &'b ()) {}
| +++
error[E0107]: struct takes 1 generic argument but 2 generic arguments were supplied
--> $DIR/wrong-number-of-args-in-macro.rs:7:9
|
LL | Foo<a, $b>
| ^^^ expected 1 generic argument
...
LL | fn foo<'a, 'b>() -> foo_ty!(&'b (), &'b ()) {}
| ----------------------- in this macro invocation
|
note: struct defined here, with 1 generic parameter: `T`
--> $DIR/wrong-number-of-args-in-macro.rs:3:8
|
LL | struct Foo<T>;
| ^^^ -
= note: this error originates in the macro `foo_ty` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0392]: type parameter `T` is never used
--> $DIR/wrong-number-of-args-in-macro.rs:3:12
|
LL | struct Foo<T>;
| ^ unused type parameter
|
= help: consider removing `T`, referring to it in a field, or using a marker such as `PhantomData`
= help: if you intended `T` to be a const parameter, use `const T: /* Type */` instead
error: aborting due to 3 previous errors
Some errors have detailed explanations: E0107, E0392, E0425.
For more information about an error, try `rustc --explain E0107`.