Skip to content

Commit 0172d15

Browse files
committed
Fix #90557
1 parent d7c0bcd commit 0172d15

File tree

4 files changed

+61
-2
lines changed

4 files changed

+61
-2
lines changed

compiler/rustc_hir_analysis/src/structured_errors/wrong_number_of_generic_args.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
594594

595595
match self.angle_brackets {
596596
AngleBrackets::Missing => {
597-
let span = self.path_segment.ident.span;
597+
let span = self.tcx.mark_span_for_resize(self.path_segment.ident.span);
598598

599599
// insert a suggestion of the form "Y<'a, 'b>"
600600
let sugg = format!("<{}>", suggested_args);
@@ -618,6 +618,15 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
618618
let last_lt = &self.gen_args.args[self.num_provided_lifetime_args() - 1];
619619
(self.tcx.mark_span_for_resize(last_lt.span()).shrink_to_hi(), false)
620620
};
621+
let path_sp = self.path_segment.ident.span.peel_ctxt();
622+
if !self.gen_args.args.iter().all(|arg| {
623+
arg.span().can_be_used_for_suggestions()
624+
&& arg.span().peel_ctxt().ctxt() == path_sp.ctxt()
625+
}) || !path_sp.can_be_used_for_suggestions()
626+
{
627+
// Do not suggest syntax when macros are involved. (#90557)
628+
return;
629+
}
621630
let has_non_lt_args = self.num_provided_type_or_const_args() != 0;
622631
let has_bindings = !self.gen_args.bindings.is_empty();
623632

@@ -647,7 +656,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
647656

648657
match self.angle_brackets {
649658
AngleBrackets::Missing | AngleBrackets::Implied => {
650-
let span = self.path_segment.ident.span;
659+
let span = self.tcx.mark_span_for_resize(self.path_segment.ident.span);
651660

652661
// insert a suggestion of the form "Y<T, U>"
653662
let sugg = format!("<{}>", suggested_args);
@@ -661,6 +670,15 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
661670
);
662671
}
663672
AngleBrackets::Available => {
673+
let path_sp = self.path_segment.ident.span.peel_ctxt();
674+
if !self.gen_args.args.iter().all(|arg| {
675+
arg.span().can_be_used_for_suggestions()
676+
&& arg.span().peel_ctxt().ctxt() == path_sp.ctxt()
677+
}) || !path_sp.can_be_used_for_suggestions()
678+
{
679+
// Do not suggest syntax when macros are involved. (#90557)
680+
return;
681+
}
664682
let gen_args_span = self.tcx.mark_span_for_resize(self.gen_args.span().unwrap());
665683
let sugg_offset =
666684
self.get_lifetime_args_offset() + self.num_provided_type_or_const_args();

tests/ui/lifetimes/missing-lifetime-in-alias.rs

+1
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ type C<'a, 'b> = <A<'a> as Trait>::Bar;
2727
//~| NOTE expected named lifetime parameter
2828
//~| NOTE these named lifetimes are available to use
2929
//~| NOTE expected 1 lifetime argument
30+
//~| NOTE in this expansion of desugaring of a resized `Span`
3031

3132
fn main() {}

tests/ui/macros/issue-90557.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
struct Example<T, U> {
2+
foo: T,
3+
bar: U,
4+
}
5+
6+
macro_rules! impl_example {
7+
($($t:ty)+) => {$(
8+
impl Example<$t> { //~ ERROR struct takes 2 generic arguments but 1 generic argument was supplied
9+
fn baz() {
10+
println!(":)");
11+
}
12+
}
13+
)+}
14+
}
15+
16+
impl_example! { u8 }
17+
18+
fn main() {}

tests/ui/macros/issue-90557.stderr

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0107]: struct takes 2 generic arguments but 1 generic argument was supplied
2+
--> $DIR/issue-90557.rs:8:14
3+
|
4+
LL | impl Example<$t> {
5+
| ^^^^^^^ expected 2 generic arguments
6+
...
7+
LL | impl_example! { u8 }
8+
| --------------------
9+
| | |
10+
| | supplied 1 generic argument
11+
| in this macro invocation
12+
|
13+
note: struct defined here, with 2 generic parameters: `T`, `U`
14+
--> $DIR/issue-90557.rs:1:8
15+
|
16+
LL | struct Example<T, U> {
17+
| ^^^^^^^ - -
18+
= note: this error originates in the macro `impl_example` (in Nightly builds, run with -Z macro-backtrace for more info)
19+
20+
error: aborting due to previous error
21+
22+
For more information about this error, try `rustc --explain E0107`.

0 commit comments

Comments
 (0)