From 43ff8855ac55da567a5e2966e0942700d120e067 Mon Sep 17 00:00:00 2001 From: xizheyin Date: Fri, 16 May 2025 16:49:56 +0800 Subject: [PATCH 1/3] Add ui test macro-shorthand-issue-140659 Signed-off-by: xizheyin --- .../ui/typeck/macro-shorthand-issue-140659.rs | 57 +++++++++++++++++++ .../macro-shorthand-issue-140659.stderr | 20 +++++++ 2 files changed, 77 insertions(+) create mode 100644 tests/ui/typeck/macro-shorthand-issue-140659.rs create mode 100644 tests/ui/typeck/macro-shorthand-issue-140659.stderr diff --git a/tests/ui/typeck/macro-shorthand-issue-140659.rs b/tests/ui/typeck/macro-shorthand-issue-140659.rs new file mode 100644 index 0000000000000..dba06b43d3931 --- /dev/null +++ b/tests/ui/typeck/macro-shorthand-issue-140659.rs @@ -0,0 +1,57 @@ +trait Reencode { + type Error; + fn tag_index(&mut self, tag: u32) -> Result; +} + +struct Reencoder; +impl Reencode for Reencoder { + type Error = &'static str; + fn tag_index(&mut self, tag: u32) -> Result { + 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(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 but we're using it where u32 is expected + Instruction::Suspend { tag_index } //~ ERROR mismatched types [E0308] + } + } + }; + } + + // This should give the specific error message with the wrong help suggestion + 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/macro-shorthand-issue-140659.stderr b/tests/ui/typeck/macro-shorthand-issue-140659.stderr new file mode 100644 index 0000000000000..419db9475710c --- /dev/null +++ b/tests/ui/typeck/macro-shorthand-issue-140659.stderr @@ -0,0 +1,20 @@ +error[E0308]: mismatched types + --> $DIR/macro-shorthand-issue-140659.rs:42:44 + | +LL | Instruction::Suspend { tag_index } + | ^^^^^^^^^ expected `u32`, found `Result::Error>` +... +LL | for_each_operator!(translate) + | ----------------------------- in this macro invocation + | + = note: expected type `u32` + found enum `Result::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) +help: consider using `Result::expect` to unwrap the `Result::Error>` value, panicking if the value is a `Result::Err` + | +LL | }: tag_index.expect("REASON") + | ++++++++++++++++++++++++++++ + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0308`. From 742c27bc524b806d04c34f22810f88cbf5fa4483 Mon Sep 17 00:00:00 2001 From: xizheyin Date: Fri, 16 May 2025 16:55:46 +0800 Subject: [PATCH 2/3] Do not emit help when shorthand from macro when suggest `?` or `expect` Signed-off-by: xizheyin --- compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs | 1 + tests/ui/typeck/macro-shorthand-issue-140659.rs | 1 - tests/ui/typeck/macro-shorthand-issue-140659.stderr | 4 ---- 3 files changed, 1 insertion(+), 5 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs index 91eb1989864ff..d3618a6ae69a3 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs @@ -2034,6 +2034,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/macro-shorthand-issue-140659.rs b/tests/ui/typeck/macro-shorthand-issue-140659.rs index dba06b43d3931..d71a7ff1d3d4d 100644 --- a/tests/ui/typeck/macro-shorthand-issue-140659.rs +++ b/tests/ui/typeck/macro-shorthand-issue-140659.rs @@ -45,7 +45,6 @@ fn process(op: &Operator, reencoder: &mut T) -> Instruction { }; } - // This should give the specific error message with the wrong help suggestion for_each_operator!(translate) } diff --git a/tests/ui/typeck/macro-shorthand-issue-140659.stderr b/tests/ui/typeck/macro-shorthand-issue-140659.stderr index 419db9475710c..12537754d8029 100644 --- a/tests/ui/typeck/macro-shorthand-issue-140659.stderr +++ b/tests/ui/typeck/macro-shorthand-issue-140659.stderr @@ -10,10 +10,6 @@ LL | for_each_operator!(translate) = note: expected type `u32` found enum `Result::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) -help: consider using `Result::expect` to unwrap the `Result::Error>` value, panicking if the value is a `Result::Err` - | -LL | }: tag_index.expect("REASON") - | ++++++++++++++++++++++++++++ error: aborting due to 1 previous error From 1c17324c7d82812b3e23902cd118b8ce77e9592c Mon Sep 17 00:00:00 2001 From: xizheyin Date: Sat, 17 May 2025 00:59:09 +0800 Subject: [PATCH 3/3] Create tests/ui/typeck/suggestions/ and move some tests in tests/ui/typeck/ to it Signed-off-by: xizheyin --- tests/ui/typeck/{ => suggestions}/macro-shorthand-issue-140659.rs | 0 .../typeck/{ => suggestions}/macro-shorthand-issue-140659.stderr | 0 .../suggest-adding-missing-zero-to-floating-point-number.fixed | 0 .../suggest-adding-missing-zero-to-floating-point-number.rs | 0 .../suggest-adding-missing-zero-to-floating-point-number.stderr | 0 tests/ui/typeck/{ => suggestions}/suggest-arg-comma-delete-ice.rs | 0 .../typeck/{ => suggestions}/suggest-arg-comma-delete-ice.stderr | 0 .../{ => suggestions}/suggest-box-on-divergent-if-else-arms.fixed | 0 .../{ => suggestions}/suggest-box-on-divergent-if-else-arms.rs | 0 .../suggest-box-on-divergent-if-else-arms.stderr | 0 .../suggest-similar-impls-for-root-obligation.rs | 0 .../suggest-similar-impls-for-root-obligation.stderr | 0 12 files changed, 0 insertions(+), 0 deletions(-) rename tests/ui/typeck/{ => suggestions}/macro-shorthand-issue-140659.rs (100%) rename tests/ui/typeck/{ => suggestions}/macro-shorthand-issue-140659.stderr (100%) rename tests/ui/typeck/{ => suggestions}/suggest-adding-missing-zero-to-floating-point-number.fixed (100%) rename tests/ui/typeck/{ => suggestions}/suggest-adding-missing-zero-to-floating-point-number.rs (100%) rename tests/ui/typeck/{ => suggestions}/suggest-adding-missing-zero-to-floating-point-number.stderr (100%) rename tests/ui/typeck/{ => suggestions}/suggest-arg-comma-delete-ice.rs (100%) rename tests/ui/typeck/{ => suggestions}/suggest-arg-comma-delete-ice.stderr (100%) rename tests/ui/typeck/{ => suggestions}/suggest-box-on-divergent-if-else-arms.fixed (100%) rename tests/ui/typeck/{ => suggestions}/suggest-box-on-divergent-if-else-arms.rs (100%) rename tests/ui/typeck/{ => suggestions}/suggest-box-on-divergent-if-else-arms.stderr (100%) rename tests/ui/typeck/{ => suggestions}/suggest-similar-impls-for-root-obligation.rs (100%) rename tests/ui/typeck/{ => suggestions}/suggest-similar-impls-for-root-obligation.stderr (100%) diff --git a/tests/ui/typeck/macro-shorthand-issue-140659.rs b/tests/ui/typeck/suggestions/macro-shorthand-issue-140659.rs similarity index 100% rename from tests/ui/typeck/macro-shorthand-issue-140659.rs rename to tests/ui/typeck/suggestions/macro-shorthand-issue-140659.rs diff --git a/tests/ui/typeck/macro-shorthand-issue-140659.stderr b/tests/ui/typeck/suggestions/macro-shorthand-issue-140659.stderr similarity index 100% rename from tests/ui/typeck/macro-shorthand-issue-140659.stderr rename to tests/ui/typeck/suggestions/macro-shorthand-issue-140659.stderr 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 similarity index 100% rename from tests/ui/typeck/suggest-adding-missing-zero-to-floating-point-number.fixed rename to 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 similarity index 100% rename from tests/ui/typeck/suggest-adding-missing-zero-to-floating-point-number.rs rename to 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 similarity index 100% rename from tests/ui/typeck/suggest-adding-missing-zero-to-floating-point-number.stderr rename to 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 similarity index 100% rename from tests/ui/typeck/suggest-arg-comma-delete-ice.rs rename to 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 similarity index 100% rename from tests/ui/typeck/suggest-arg-comma-delete-ice.stderr rename to 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 similarity index 100% rename from tests/ui/typeck/suggest-box-on-divergent-if-else-arms.fixed rename to 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 similarity index 100% rename from tests/ui/typeck/suggest-box-on-divergent-if-else-arms.rs rename to 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 similarity index 100% rename from tests/ui/typeck/suggest-box-on-divergent-if-else-arms.stderr rename to 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 similarity index 100% rename from tests/ui/typeck/suggest-similar-impls-for-root-obligation.rs rename to 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 similarity index 100% rename from tests/ui/typeck/suggest-similar-impls-for-root-obligation.stderr rename to tests/ui/typeck/suggestions/suggest-similar-impls-for-root-obligation.stderr