From 87ea0d76fe1ae997a11fbc914daf33644d5c65bf Mon Sep 17 00:00:00 2001 From: long-long-float Date: Wed, 13 Dec 2023 01:43:10 +0900 Subject: [PATCH 1/7] Suppress suggestions in derive macro --- compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs index 5395ffda1d1..aeab3629ece 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs @@ -2698,6 +2698,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { return false; } + if expr.span.in_derive_expansion() { + // Ignore if span is from derive macro. + return false; + } + let Ok(src) = self.tcx.sess.source_map().span_to_snippet(expr.span) else { return false; }; From 4909258ae885a3762b6202f9f57a54d756b927ec Mon Sep 17 00:00:00 2001 From: long-long-float Date: Thu, 25 Jan 2024 00:12:50 +0900 Subject: [PATCH 2/7] Check in push_suggestion --- compiler/rustc_errors/src/diagnostic.rs | 9 +++++++++ compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs | 5 ----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_errors/src/diagnostic.rs b/compiler/rustc_errors/src/diagnostic.rs index 8ad4925cff2..b37aa4cf7c0 100644 --- a/compiler/rustc_errors/src/diagnostic.rs +++ b/compiler/rustc_errors/src/diagnostic.rs @@ -519,6 +519,15 @@ impl Diagnostic { /// Helper for pushing to `self.suggestions`, if available (not disable). fn push_suggestion(&mut self, suggestion: CodeSuggestion) { + let in_derive = suggestion + .substitutions + .iter() + .any(|subst| subst.parts.iter().any(|part| part.span.in_derive_expansion())); + if in_derive { + // Ignore if spans is from derive macro. + return; + } + if let Ok(suggestions) = &mut self.suggestions { suggestions.push(suggestion); } diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs index aeab3629ece..5395ffda1d1 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs @@ -2698,11 +2698,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { return false; } - if expr.span.in_derive_expansion() { - // Ignore if span is from derive macro. - return false; - } - let Ok(src) = self.tcx.sess.source_map().span_to_snippet(expr.span) else { return false; }; From 8cc50849500b2208a0c8faf39c31425267ecb941 Mon Sep 17 00:00:00 2001 From: long-long-float Date: Sun, 28 Jan 2024 01:52:37 +0900 Subject: [PATCH 3/7] Update UI tests that generates duplicated result --- ...ndeclared-lifetime-used-in-debug-macro-issue-70152.stderr | 1 - .../ui/pattern/usefulness/issue-119778-type-error-ice.stderr | 5 ----- tests/ui/traits/issue-50480.stderr | 5 ----- 3 files changed, 11 deletions(-) diff --git a/tests/ui/lifetimes/undeclared-lifetime-used-in-debug-macro-issue-70152.stderr b/tests/ui/lifetimes/undeclared-lifetime-used-in-debug-macro-issue-70152.stderr index 0d6ade41511..7b116e4f74f 100644 --- a/tests/ui/lifetimes/undeclared-lifetime-used-in-debug-macro-issue-70152.stderr +++ b/tests/ui/lifetimes/undeclared-lifetime-used-in-debug-macro-issue-70152.stderr @@ -12,7 +12,6 @@ error[E0261]: use of undeclared lifetime name `'b` LL | #[derive(Eq, PartialEq)] | -- lifetime `'b` is missing in item created through this procedural macro LL | struct Test { - | - help: consider introducing lifetime `'b` here: `<'b>` LL | a: &'b str, | ^^ undeclared lifetime diff --git a/tests/ui/pattern/usefulness/issue-119778-type-error-ice.stderr b/tests/ui/pattern/usefulness/issue-119778-type-error-ice.stderr index 7da384d613e..5b4c2bda738 100644 --- a/tests/ui/pattern/usefulness/issue-119778-type-error-ice.stderr +++ b/tests/ui/pattern/usefulness/issue-119778-type-error-ice.stderr @@ -14,11 +14,6 @@ error[E0425]: cannot find value `S` in this scope | LL | struct Foo([u8; S]); | ^ not found in this scope - | -help: you might be missing a const parameter - | -LL | struct Foo([u8; S]); - | +++++++++++++++++++++ error[E0658]: `impl Trait` in type aliases is unstable --> $DIR/issue-119778-type-error-ice.rs:9:14 diff --git a/tests/ui/traits/issue-50480.stderr b/tests/ui/traits/issue-50480.stderr index 4f72db60a16..aff75e805f9 100644 --- a/tests/ui/traits/issue-50480.stderr +++ b/tests/ui/traits/issue-50480.stderr @@ -20,11 +20,6 @@ error[E0412]: cannot find type `N` in this scope | LL | struct Foo(N, NotDefined, ::Item, Vec, String); | ^ not found in this scope - | -help: you might be missing a type parameter - | -LL | struct Foo(N, NotDefined, ::Item, Vec, String); - | +++ error[E0412]: cannot find type `NotDefined` in this scope --> $DIR/issue-50480.rs:3:15 From 4e7941c2c57441d2fe92a21c2951cab2926f5656 Mon Sep 17 00:00:00 2001 From: long-long-float Date: Fri, 9 Feb 2024 01:03:38 +0900 Subject: [PATCH 4/7] Check with overlaps_or_adjacent --- compiler/rustc_errors/src/diagnostic.rs | 11 +++++++---- compiler/rustc_span/src/lib.rs | 7 +++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_errors/src/diagnostic.rs b/compiler/rustc_errors/src/diagnostic.rs index b37aa4cf7c0..026b0222665 100644 --- a/compiler/rustc_errors/src/diagnostic.rs +++ b/compiler/rustc_errors/src/diagnostic.rs @@ -519,10 +519,13 @@ impl Diagnostic { /// Helper for pushing to `self.suggestions`, if available (not disable). fn push_suggestion(&mut self, suggestion: CodeSuggestion) { - let in_derive = suggestion - .substitutions - .iter() - .any(|subst| subst.parts.iter().any(|part| part.span.in_derive_expansion())); + let in_derive = suggestion.substitutions.iter().any(|subst| { + subst.parts.iter().any(|part| { + let span = part.span; + let call_site = span.ctxt().outer_expn_data().call_site; + span.in_derive_expansion() && span.overlaps_or_adjacent(call_site) + }) + }); if in_derive { // Ignore if spans is from derive macro. return; diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs index ea6766ea583..228d33bdba2 100644 --- a/compiler/rustc_span/src/lib.rs +++ b/compiler/rustc_span/src/lib.rs @@ -627,6 +627,13 @@ impl Span { span.lo < other.hi && other.lo < span.hi } + /// Returns `true` if `self` touches or adjoins `other`. + pub fn overlaps_or_adjacent(self, other: Span) -> bool { + let span = self.data(); + let other = other.data(); + span.lo <= other.hi && other.lo <= span.hi + } + /// Returns `true` if the spans are equal with regards to the source text. /// /// Use this instead of `==` when either span could be generated code, From 42c4f1024a6681c307f4b34e6ea4bc12ce38ce31 Mon Sep 17 00:00:00 2001 From: long-long-float Date: Sat, 10 Feb 2024 00:13:13 +0900 Subject: [PATCH 5/7] Fix test results --- ...ndeclared-lifetime-used-in-debug-macro-issue-70152.stderr | 1 + .../ui/pattern/usefulness/issue-119778-type-error-ice.stderr | 5 +++++ tests/ui/traits/issue-50480.stderr | 5 +++++ 3 files changed, 11 insertions(+) diff --git a/tests/ui/lifetimes/undeclared-lifetime-used-in-debug-macro-issue-70152.stderr b/tests/ui/lifetimes/undeclared-lifetime-used-in-debug-macro-issue-70152.stderr index 7b116e4f74f..0d6ade41511 100644 --- a/tests/ui/lifetimes/undeclared-lifetime-used-in-debug-macro-issue-70152.stderr +++ b/tests/ui/lifetimes/undeclared-lifetime-used-in-debug-macro-issue-70152.stderr @@ -12,6 +12,7 @@ error[E0261]: use of undeclared lifetime name `'b` LL | #[derive(Eq, PartialEq)] | -- lifetime `'b` is missing in item created through this procedural macro LL | struct Test { + | - help: consider introducing lifetime `'b` here: `<'b>` LL | a: &'b str, | ^^ undeclared lifetime diff --git a/tests/ui/pattern/usefulness/issue-119778-type-error-ice.stderr b/tests/ui/pattern/usefulness/issue-119778-type-error-ice.stderr index 5b4c2bda738..7da384d613e 100644 --- a/tests/ui/pattern/usefulness/issue-119778-type-error-ice.stderr +++ b/tests/ui/pattern/usefulness/issue-119778-type-error-ice.stderr @@ -14,6 +14,11 @@ error[E0425]: cannot find value `S` in this scope | LL | struct Foo([u8; S]); | ^ not found in this scope + | +help: you might be missing a const parameter + | +LL | struct Foo([u8; S]); + | +++++++++++++++++++++ error[E0658]: `impl Trait` in type aliases is unstable --> $DIR/issue-119778-type-error-ice.rs:9:14 diff --git a/tests/ui/traits/issue-50480.stderr b/tests/ui/traits/issue-50480.stderr index aff75e805f9..4f72db60a16 100644 --- a/tests/ui/traits/issue-50480.stderr +++ b/tests/ui/traits/issue-50480.stderr @@ -20,6 +20,11 @@ error[E0412]: cannot find type `N` in this scope | LL | struct Foo(N, NotDefined, ::Item, Vec, String); | ^ not found in this scope + | +help: you might be missing a type parameter + | +LL | struct Foo(N, NotDefined, ::Item, Vec, String); + | +++ error[E0412]: cannot find type `NotDefined` in this scope --> $DIR/issue-50480.rs:3:15 From 44616e11d080f7051a05ae6977c303b42efdcf89 Mon Sep 17 00:00:00 2001 From: long-long-float Date: Sun, 11 Feb 2024 02:43:44 +0900 Subject: [PATCH 6/7] Add test for the issue --- tests/ui/proc-macro/auxiliary/issue-118809.rs | 20 ++++++++++++++++++ tests/ui/proc-macro/issue-118809.rs | 10 +++++++++ tests/ui/proc-macro/issue-118809.stderr | 21 +++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 tests/ui/proc-macro/auxiliary/issue-118809.rs create mode 100644 tests/ui/proc-macro/issue-118809.rs create mode 100644 tests/ui/proc-macro/issue-118809.stderr diff --git a/tests/ui/proc-macro/auxiliary/issue-118809.rs b/tests/ui/proc-macro/auxiliary/issue-118809.rs new file mode 100644 index 00000000000..029b58c6d0c --- /dev/null +++ b/tests/ui/proc-macro/auxiliary/issue-118809.rs @@ -0,0 +1,20 @@ +// force-host +// no-prefer-dynamic + +#![crate_type = "proc-macro"] + +extern crate proc_macro; +use proc_macro::TokenStream; + +#[proc_macro_derive(Deserialize)] +pub fn deserialize_derive(input: TokenStream) -> TokenStream { + "impl Build { + fn deserialize() -> Option { + let x: Option = Some(0); + Some(x? + 1) + } + } + " + .parse() + .unwrap() +} diff --git a/tests/ui/proc-macro/issue-118809.rs b/tests/ui/proc-macro/issue-118809.rs new file mode 100644 index 00000000000..732bf19c173 --- /dev/null +++ b/tests/ui/proc-macro/issue-118809.rs @@ -0,0 +1,10 @@ +// aux-build: issue-118809.rs + +#[macro_use] +extern crate issue_118809; + +#[derive(Deserialize)] //~ ERROR mismatched types [E0308] +pub struct Build { +} + +fn main() {} diff --git a/tests/ui/proc-macro/issue-118809.stderr b/tests/ui/proc-macro/issue-118809.stderr new file mode 100644 index 00000000000..30b09fd4006 --- /dev/null +++ b/tests/ui/proc-macro/issue-118809.stderr @@ -0,0 +1,21 @@ +error[E0308]: mismatched types + --> $DIR/issue-118809.rs:6:10 + | +LL | #[derive(Deserialize)] + | ^^^^^^^^^^^ + | | + | expected `u64`, found `u32` + | arguments to this enum variant are incorrect + | +help: the type constructed contains `u32` due to the type of the argument passed + --> $DIR/issue-118809.rs:6:10 + | +LL | #[derive(Deserialize)] + | ^^^^^^^^^^^ this argument influences the type of `Some` +note: tuple variant defined here + --> $SRC_DIR/core/src/option.rs:LL:COL + = note: this error originates in the derive macro `Deserialize` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0308`. From 1e59e662258fc301dd7b396ac0e3686568a71164 Mon Sep 17 00:00:00 2001 From: long-long-float Date: Sun, 11 Feb 2024 02:43:55 +0900 Subject: [PATCH 7/7] Fix to use for loop --- compiler/rustc_errors/src/diagnostic.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_errors/src/diagnostic.rs b/compiler/rustc_errors/src/diagnostic.rs index 026b0222665..c48a8e12a00 100644 --- a/compiler/rustc_errors/src/diagnostic.rs +++ b/compiler/rustc_errors/src/diagnostic.rs @@ -519,16 +519,15 @@ impl Diagnostic { /// Helper for pushing to `self.suggestions`, if available (not disable). fn push_suggestion(&mut self, suggestion: CodeSuggestion) { - let in_derive = suggestion.substitutions.iter().any(|subst| { - subst.parts.iter().any(|part| { + for subst in &suggestion.substitutions { + for part in &subst.parts { let span = part.span; let call_site = span.ctxt().outer_expn_data().call_site; - span.in_derive_expansion() && span.overlaps_or_adjacent(call_site) - }) - }); - if in_derive { - // Ignore if spans is from derive macro. - return; + if span.in_derive_expansion() && span.overlaps_or_adjacent(call_site) { + // Ignore if spans is from derive macro. + return; + } + } } if let Ok(suggestions) = &mut self.suggestions {