From 0172d15fc1a0e68b03de2fcd44e91e5d88d1ea0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Wed, 15 Mar 2023 03:48:28 +0000 Subject: [PATCH] Fix #90557 --- .../wrong_number_of_generic_args.rs | 22 +++++++++++++++++-- .../ui/lifetimes/missing-lifetime-in-alias.rs | 1 + tests/ui/macros/issue-90557.rs | 18 +++++++++++++++ tests/ui/macros/issue-90557.stderr | 22 +++++++++++++++++++ 4 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 tests/ui/macros/issue-90557.rs create mode 100644 tests/ui/macros/issue-90557.stderr diff --git a/compiler/rustc_hir_analysis/src/structured_errors/wrong_number_of_generic_args.rs b/compiler/rustc_hir_analysis/src/structured_errors/wrong_number_of_generic_args.rs index 821b8f75da5eb..359f3beeaa12b 100644 --- a/compiler/rustc_hir_analysis/src/structured_errors/wrong_number_of_generic_args.rs +++ b/compiler/rustc_hir_analysis/src/structured_errors/wrong_number_of_generic_args.rs @@ -594,7 +594,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> { match self.angle_brackets { AngleBrackets::Missing => { - let span = self.path_segment.ident.span; + let span = self.tcx.mark_span_for_resize(self.path_segment.ident.span); // insert a suggestion of the form "Y<'a, 'b>" let sugg = format!("<{}>", suggested_args); @@ -618,6 +618,15 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> { let last_lt = &self.gen_args.args[self.num_provided_lifetime_args() - 1]; (self.tcx.mark_span_for_resize(last_lt.span()).shrink_to_hi(), false) }; + let path_sp = self.path_segment.ident.span.peel_ctxt(); + if !self.gen_args.args.iter().all(|arg| { + arg.span().can_be_used_for_suggestions() + && arg.span().peel_ctxt().ctxt() == path_sp.ctxt() + }) || !path_sp.can_be_used_for_suggestions() + { + // Do not suggest syntax when macros are involved. (#90557) + return; + } let has_non_lt_args = self.num_provided_type_or_const_args() != 0; let has_bindings = !self.gen_args.bindings.is_empty(); @@ -647,7 +656,7 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> { match self.angle_brackets { AngleBrackets::Missing | AngleBrackets::Implied => { - let span = self.path_segment.ident.span; + let span = self.tcx.mark_span_for_resize(self.path_segment.ident.span); // insert a suggestion of the form "Y" let sugg = format!("<{}>", suggested_args); @@ -661,6 +670,15 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> { ); } AngleBrackets::Available => { + let path_sp = self.path_segment.ident.span.peel_ctxt(); + if !self.gen_args.args.iter().all(|arg| { + arg.span().can_be_used_for_suggestions() + && arg.span().peel_ctxt().ctxt() == path_sp.ctxt() + }) || !path_sp.can_be_used_for_suggestions() + { + // Do not suggest syntax when macros are involved. (#90557) + return; + } let gen_args_span = self.tcx.mark_span_for_resize(self.gen_args.span().unwrap()); let sugg_offset = self.get_lifetime_args_offset() + self.num_provided_type_or_const_args(); diff --git a/tests/ui/lifetimes/missing-lifetime-in-alias.rs b/tests/ui/lifetimes/missing-lifetime-in-alias.rs index 51c564c011a86..44be06e1fef7c 100644 --- a/tests/ui/lifetimes/missing-lifetime-in-alias.rs +++ b/tests/ui/lifetimes/missing-lifetime-in-alias.rs @@ -27,5 +27,6 @@ type C<'a, 'b> = as Trait>::Bar; //~| NOTE expected named lifetime parameter //~| NOTE these named lifetimes are available to use //~| NOTE expected 1 lifetime argument +//~| NOTE in this expansion of desugaring of a resized `Span` fn main() {} diff --git a/tests/ui/macros/issue-90557.rs b/tests/ui/macros/issue-90557.rs new file mode 100644 index 0000000000000..5a9aa5dabb70c --- /dev/null +++ b/tests/ui/macros/issue-90557.rs @@ -0,0 +1,18 @@ +struct Example { + foo: T, + bar: U, +} + +macro_rules! impl_example { + ($($t:ty)+) => {$( + impl Example<$t> { //~ ERROR struct takes 2 generic arguments but 1 generic argument was supplied + fn baz() { + println!(":)"); + } + } + )+} +} + +impl_example! { u8 } + +fn main() {} diff --git a/tests/ui/macros/issue-90557.stderr b/tests/ui/macros/issue-90557.stderr new file mode 100644 index 0000000000000..f28521efe75c1 --- /dev/null +++ b/tests/ui/macros/issue-90557.stderr @@ -0,0 +1,22 @@ +error[E0107]: struct takes 2 generic arguments but 1 generic argument was supplied + --> $DIR/issue-90557.rs:8:14 + | +LL | impl Example<$t> { + | ^^^^^^^ expected 2 generic arguments +... +LL | impl_example! { u8 } + | -------------------- + | | | + | | supplied 1 generic argument + | in this macro invocation + | +note: struct defined here, with 2 generic parameters: `T`, `U` + --> $DIR/issue-90557.rs:1:8 + | +LL | struct Example { + | ^^^^^^^ - - + = note: this error originates in the macro `impl_example` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0107`.