diff options
| author | Matthias Krüger <476013+matthiaskrgr@users.noreply.github.com> | 2025-05-17 10:33:12 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-17 10:33:12 +0200 |
| commit | 14f3ef997f5650913f2f735c36f54da4d0f5f94a (patch) | |
| tree | af94a1ba60a8e458aa7fd60ed778029ab9be0cad | |
| parent | 59ad0cbd04597067cfb15aed6df2f7e20eeeab77 (diff) | |
| parent | 1c17324c7d82812b3e23902cd118b8ce77e9592c (diff) | |
| download | rust-14f3ef997f5650913f2f735c36f54da4d0f5f94a.tar.gz rust-14f3ef997f5650913f2f735c36f54da4d0f5f94a.zip | |
Rollup merge of #141070 - xizheyin:issue-140659, r=chenyukang
Do not emit help when shorthand from macro when suggest `?` or `expect` Fixes #140659 I didn't fully minimize the original bug, but I found a similar test case, and they have perhaps the same root cause. For the bug mentioned in #140659 , I also tested it locally and passed it. Jieyou has worked on this part before, maybe r? `@jieyouxu`
| -rw-r--r-- | compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs | 1 | ||||
| -rw-r--r-- | tests/ui/typeck/suggestions/macro-shorthand-issue-140659.rs | 56 | ||||
| -rw-r--r-- | tests/ui/typeck/suggestions/macro-shorthand-issue-140659.stderr | 16 | ||||
| -rw-r--r-- | tests/ui/typeck/suggestions/suggest-adding-missing-zero-to-floating-point-number.fixed (renamed from tests/ui/typeck/suggest-adding-missing-zero-to-floating-point-number.fixed) | 0 | ||||
| -rw-r--r-- | tests/ui/typeck/suggestions/suggest-adding-missing-zero-to-floating-point-number.rs (renamed from tests/ui/typeck/suggest-adding-missing-zero-to-floating-point-number.rs) | 0 | ||||
| -rw-r--r-- | tests/ui/typeck/suggestions/suggest-adding-missing-zero-to-floating-point-number.stderr (renamed from tests/ui/typeck/suggest-adding-missing-zero-to-floating-point-number.stderr) | 0 | ||||
| -rw-r--r-- | tests/ui/typeck/suggestions/suggest-arg-comma-delete-ice.rs (renamed from tests/ui/typeck/suggest-arg-comma-delete-ice.rs) | 0 | ||||
| -rw-r--r-- | tests/ui/typeck/suggestions/suggest-arg-comma-delete-ice.stderr (renamed from tests/ui/typeck/suggest-arg-comma-delete-ice.stderr) | 0 | ||||
| -rw-r--r-- | tests/ui/typeck/suggestions/suggest-box-on-divergent-if-else-arms.fixed (renamed from tests/ui/typeck/suggest-box-on-divergent-if-else-arms.fixed) | 0 | ||||
| -rw-r--r-- | tests/ui/typeck/suggestions/suggest-box-on-divergent-if-else-arms.rs (renamed from tests/ui/typeck/suggest-box-on-divergent-if-else-arms.rs) | 0 | ||||
| -rw-r--r-- | tests/ui/typeck/suggestions/suggest-box-on-divergent-if-else-arms.stderr (renamed from tests/ui/typeck/suggest-box-on-divergent-if-else-arms.stderr) | 0 | ||||
| -rw-r--r-- | tests/ui/typeck/suggestions/suggest-similar-impls-for-root-obligation.rs (renamed from tests/ui/typeck/suggest-similar-impls-for-root-obligation.rs) | 0 | ||||
| -rw-r--r-- | tests/ui/typeck/suggestions/suggest-similar-impls-for-root-obligation.stderr (renamed from tests/ui/typeck/suggest-similar-impls-for-root-obligation.stderr) | 0 |
13 files changed, 73 insertions, 0 deletions
diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs index 251801f479e..1079262b5af 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs @@ -2043,6 +2043,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { }; let sugg = match self.tcx.hir_maybe_get_struct_pattern_shorthand_field(expr) { + Some(_) if expr.span.from_expansion() => return false, Some(ident) => format!(": {ident}{sugg}"), None => sugg.to_string(), }; diff --git a/tests/ui/typeck/suggestions/macro-shorthand-issue-140659.rs b/tests/ui/typeck/suggestions/macro-shorthand-issue-140659.rs new file mode 100644 index 00000000000..d71a7ff1d3d --- /dev/null +++ b/tests/ui/typeck/suggestions/macro-shorthand-issue-140659.rs @@ -0,0 +1,56 @@ +trait Reencode { + type Error; + fn tag_index(&mut self, tag: u32) -> Result<u32, Self::Error>; +} + +struct Reencoder; +impl Reencode for Reencoder { + type Error = &'static str; + fn tag_index(&mut self, tag: u32) -> Result<u32, Self::Error> { + Ok(tag) + } +} + + +enum Operator { + Suspend { tag_index: u32 }, +} + +enum Instruction { + Suspend { tag_index: u32 }, +} + + +macro_rules! for_each_operator { + ($m:ident) => { + $m! { + Suspend { tag_index: u32 } => visit_suspend + } + }; +} + + +fn process<T: Reencode>(op: &Operator, reencoder: &mut T) -> Instruction { + macro_rules! translate { + (Suspend { tag_index: $ty:ty } => $visit:ident) => { + match op { + Operator::Suspend { tag_index } => { + let tag_index = reencoder.tag_index(*tag_index); + + // KEY POINT: Using field shorthand syntax where the compiler gets confused + // Here tag_index is a Result<u32, E> but we're using it where u32 is expected + Instruction::Suspend { tag_index } //~ ERROR mismatched types [E0308] + } + } + }; + } + + for_each_operator!(translate) +} + +fn main() { + let mut reencoder = Reencoder; + let op = Operator::Suspend { tag_index: 1 }; + + let _ = process(&op, &mut reencoder); +} diff --git a/tests/ui/typeck/suggestions/macro-shorthand-issue-140659.stderr b/tests/ui/typeck/suggestions/macro-shorthand-issue-140659.stderr new file mode 100644 index 00000000000..12537754d80 --- /dev/null +++ b/tests/ui/typeck/suggestions/macro-shorthand-issue-140659.stderr @@ -0,0 +1,16 @@ +error[E0308]: mismatched types + --> $DIR/macro-shorthand-issue-140659.rs:42:44 + | +LL | Instruction::Suspend { tag_index } + | ^^^^^^^^^ expected `u32`, found `Result<u32, <T as Reencode>::Error>` +... +LL | for_each_operator!(translate) + | ----------------------------- in this macro invocation + | + = note: expected type `u32` + found enum `Result<u32, <T as Reencode>::Error>` + = note: this error originates in the macro `translate` which comes from the expansion of the macro `for_each_operator` (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`. diff --git a/tests/ui/typeck/suggest-adding-missing-zero-to-floating-point-number.fixed b/tests/ui/typeck/suggestions/suggest-adding-missing-zero-to-floating-point-number.fixed index cb4a3967741..cb4a3967741 100644 --- a/tests/ui/typeck/suggest-adding-missing-zero-to-floating-point-number.fixed +++ b/tests/ui/typeck/suggestions/suggest-adding-missing-zero-to-floating-point-number.fixed diff --git a/tests/ui/typeck/suggest-adding-missing-zero-to-floating-point-number.rs b/tests/ui/typeck/suggestions/suggest-adding-missing-zero-to-floating-point-number.rs index e4cafc466c6..e4cafc466c6 100644 --- a/tests/ui/typeck/suggest-adding-missing-zero-to-floating-point-number.rs +++ b/tests/ui/typeck/suggestions/suggest-adding-missing-zero-to-floating-point-number.rs diff --git a/tests/ui/typeck/suggest-adding-missing-zero-to-floating-point-number.stderr b/tests/ui/typeck/suggestions/suggest-adding-missing-zero-to-floating-point-number.stderr index 503015f3bec..503015f3bec 100644 --- a/tests/ui/typeck/suggest-adding-missing-zero-to-floating-point-number.stderr +++ b/tests/ui/typeck/suggestions/suggest-adding-missing-zero-to-floating-point-number.stderr diff --git a/tests/ui/typeck/suggest-arg-comma-delete-ice.rs b/tests/ui/typeck/suggestions/suggest-arg-comma-delete-ice.rs index 48d02e13eca..48d02e13eca 100644 --- a/tests/ui/typeck/suggest-arg-comma-delete-ice.rs +++ b/tests/ui/typeck/suggestions/suggest-arg-comma-delete-ice.rs diff --git a/tests/ui/typeck/suggest-arg-comma-delete-ice.stderr b/tests/ui/typeck/suggestions/suggest-arg-comma-delete-ice.stderr index 0b899ad2712..0b899ad2712 100644 --- a/tests/ui/typeck/suggest-arg-comma-delete-ice.stderr +++ b/tests/ui/typeck/suggestions/suggest-arg-comma-delete-ice.stderr diff --git a/tests/ui/typeck/suggest-box-on-divergent-if-else-arms.fixed b/tests/ui/typeck/suggestions/suggest-box-on-divergent-if-else-arms.fixed index f562b24cc4c..f562b24cc4c 100644 --- a/tests/ui/typeck/suggest-box-on-divergent-if-else-arms.fixed +++ b/tests/ui/typeck/suggestions/suggest-box-on-divergent-if-else-arms.fixed diff --git a/tests/ui/typeck/suggest-box-on-divergent-if-else-arms.rs b/tests/ui/typeck/suggestions/suggest-box-on-divergent-if-else-arms.rs index e364e6daa6a..e364e6daa6a 100644 --- a/tests/ui/typeck/suggest-box-on-divergent-if-else-arms.rs +++ b/tests/ui/typeck/suggestions/suggest-box-on-divergent-if-else-arms.rs diff --git a/tests/ui/typeck/suggest-box-on-divergent-if-else-arms.stderr b/tests/ui/typeck/suggestions/suggest-box-on-divergent-if-else-arms.stderr index c58bf60e7d6..c58bf60e7d6 100644 --- a/tests/ui/typeck/suggest-box-on-divergent-if-else-arms.stderr +++ b/tests/ui/typeck/suggestions/suggest-box-on-divergent-if-else-arms.stderr diff --git a/tests/ui/typeck/suggest-similar-impls-for-root-obligation.rs b/tests/ui/typeck/suggestions/suggest-similar-impls-for-root-obligation.rs index d00b4f33132..d00b4f33132 100644 --- a/tests/ui/typeck/suggest-similar-impls-for-root-obligation.rs +++ b/tests/ui/typeck/suggestions/suggest-similar-impls-for-root-obligation.rs diff --git a/tests/ui/typeck/suggest-similar-impls-for-root-obligation.stderr b/tests/ui/typeck/suggestions/suggest-similar-impls-for-root-obligation.stderr index 5c0d98735f7..5c0d98735f7 100644 --- a/tests/ui/typeck/suggest-similar-impls-for-root-obligation.stderr +++ b/tests/ui/typeck/suggestions/suggest-similar-impls-for-root-obligation.stderr |
