Skip to content

Suppress suggestions in derive macro #120272

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions compiler/rustc_errors/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,17 @@ impl Diagnostic {

/// Helper for pushing to `self.suggestions`, if available (not disable).
fn push_suggestion(&mut self, suggestion: CodeSuggestion) {
for subst in &suggestion.substitutions {
for part in &subst.parts {
let span = part.span;
let call_site = span.ctxt().outer_expn_data().call_site;
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 {
suggestions.push(suggestion);
}
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_span/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
20 changes: 20 additions & 0 deletions tests/ui/proc-macro/auxiliary/issue-118809.rs
Original file line number Diff line number Diff line change
@@ -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<u64> {
let x: Option<u32> = Some(0);
Some(x? + 1)
}
}
"
.parse()
.unwrap()
}
10 changes: 10 additions & 0 deletions tests/ui/proc-macro/issue-118809.rs
Original file line number Diff line number Diff line change
@@ -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() {}
21 changes: 21 additions & 0 deletions tests/ui/proc-macro/issue-118809.stderr
Original file line number Diff line number Diff line change
@@ -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`.