Skip to content

Commit aad47a0

Browse files
authored
Unrolled build for rust-lang#120272
Rollup merge of rust-lang#120272 - long-long-float:suppress-suggestions-in-derive-macro, r=oli-obk Suppress suggestions in derive macro close rust-lang#118809 I suppress warnings inside derive macros. For example, the compiler emits following error by a program described in rust-lang#118809 (comment) with a suggestion that indicates invalid syntax. ``` error[E0308]: `?` operator has incompatible types --> src/main.rs:3:17 | 3 | #[derive(Debug, Deserialize)] | ^^^^^^^^^^^ expected `u32`, found `u64` | = note: `?` operator cannot convert from `u64` to `u32` = note: this error originates in the derive macro `Deserialize` (in Nightly builds, run with -Z macro-backtrace for more info) help: you can convert a `u64` to a `u32` and panic if the converted value doesn't fit | 3 | #[derive(Debug, Deserialize.try_into().unwrap())] | ++++++++++++++++++++ For more information about this error, try `rustc --explain E0308`. error: could not compile `serde_test` (bin "serde_test") due to 2 previous errors ``` In this PR, suggestions to cast are suppressed. ``` error[E0308]: `?` operator has incompatible types --> src/main.rs:3:17 | 3 | #[derive(Debug, Deserialize)] | ^^^^^^^^^^^ expected `u32`, found `u64` | = note: `?` operator cannot convert from `u64` to `u32` = note: this error originates in the derive macro `Deserialize` (in Nightly builds, run with -Z macro-backtrace for more info) For more information about this error, try `rustc --explain E0308`. error: could not compile `serde_test` (bin "serde_test") due to 2 previous errors ```
2 parents 980cf08 + 1e59e66 commit aad47a0

File tree

5 files changed

+69
-0
lines changed

5 files changed

+69
-0
lines changed

compiler/rustc_errors/src/diagnostic.rs

+11
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,17 @@ impl Diagnostic {
516516

517517
/// Helper for pushing to `self.suggestions`, if available (not disable).
518518
fn push_suggestion(&mut self, suggestion: CodeSuggestion) {
519+
for subst in &suggestion.substitutions {
520+
for part in &subst.parts {
521+
let span = part.span;
522+
let call_site = span.ctxt().outer_expn_data().call_site;
523+
if span.in_derive_expansion() && span.overlaps_or_adjacent(call_site) {
524+
// Ignore if spans is from derive macro.
525+
return;
526+
}
527+
}
528+
}
529+
519530
if let Ok(suggestions) = &mut self.suggestions {
520531
suggestions.push(suggestion);
521532
}

compiler/rustc_span/src/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,13 @@ impl Span {
625625
span.lo < other.hi && other.lo < span.hi
626626
}
627627

628+
/// Returns `true` if `self` touches or adjoins `other`.
629+
pub fn overlaps_or_adjacent(self, other: Span) -> bool {
630+
let span = self.data();
631+
let other = other.data();
632+
span.lo <= other.hi && other.lo <= span.hi
633+
}
634+
628635
/// Returns `true` if the spans are equal with regards to the source text.
629636
///
630637
/// Use this instead of `==` when either span could be generated code,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// force-host
2+
// no-prefer-dynamic
3+
4+
#![crate_type = "proc-macro"]
5+
6+
extern crate proc_macro;
7+
use proc_macro::TokenStream;
8+
9+
#[proc_macro_derive(Deserialize)]
10+
pub fn deserialize_derive(input: TokenStream) -> TokenStream {
11+
"impl Build {
12+
fn deserialize() -> Option<u64> {
13+
let x: Option<u32> = Some(0);
14+
Some(x? + 1)
15+
}
16+
}
17+
"
18+
.parse()
19+
.unwrap()
20+
}

tests/ui/proc-macro/issue-118809.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// aux-build: issue-118809.rs
2+
3+
#[macro_use]
4+
extern crate issue_118809;
5+
6+
#[derive(Deserialize)] //~ ERROR mismatched types [E0308]
7+
pub struct Build {
8+
}
9+
10+
fn main() {}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/issue-118809.rs:6:10
3+
|
4+
LL | #[derive(Deserialize)]
5+
| ^^^^^^^^^^^
6+
| |
7+
| expected `u64`, found `u32`
8+
| arguments to this enum variant are incorrect
9+
|
10+
help: the type constructed contains `u32` due to the type of the argument passed
11+
--> $DIR/issue-118809.rs:6:10
12+
|
13+
LL | #[derive(Deserialize)]
14+
| ^^^^^^^^^^^ this argument influences the type of `Some`
15+
note: tuple variant defined here
16+
--> $SRC_DIR/core/src/option.rs:LL:COL
17+
= note: this error originates in the derive macro `Deserialize` (in Nightly builds, run with -Z macro-backtrace for more info)
18+
19+
error: aborting due to 1 previous error
20+
21+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)