From 503344ef2c06e1b81218501cf50787c034672cc5 Mon Sep 17 00:00:00 2001 From: Chronocoder Date: Sat, 27 Jun 2026 22:18:00 -0400 Subject: [PATCH] Use the call site for the assign suggestion on macro-expanded types When a `let` binding's type is actually a macro call such as `let x: vec![];`, lowering reports "expected type, found associated function call" and suggests writing `=` instead. The suggestion span was built from the expanded type span, which resolves inside the macro definition, so the suggestion pointed at the standard library source and proposed editing the expansion of `vec!`. Compute the span from `hir_ty.span.source_callsite()` so it maps back to the user's code. For non-macro types `source_callsite` returns the same span, so existing suggestions are unchanged. --- .../src/hir_ty_lowering/mod.rs | 4 ++-- .../let-binding-init-expr-as-ty.rs | 1 + .../let-binding-init-expr-as-ty.stderr | 24 ++++++++++++++----- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs index 4781525ad686e..0799230f637ca 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs @@ -3229,7 +3229,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { "expected type, found associated function call", ) .with_span_suggestion_verbose( - stmt.pat.span.between(hir_ty.span), + stmt.pat.span.between(hir_ty.span.source_callsite()), "use `=` if you meant to assign", " = ".to_string(), Applicability::MaybeIncorrect, @@ -3255,7 +3255,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ { "expected type, found associated function call", ) .with_span_suggestion_verbose( - stmt.pat.span.between(hir_ty.span), + stmt.pat.span.between(hir_ty.span.source_callsite()), "use `=` if you meant to assign", " = ".to_string(), Applicability::MaybeIncorrect, diff --git a/tests/ui/suggestions/let-binding-init-expr-as-ty.rs b/tests/ui/suggestions/let-binding-init-expr-as-ty.rs index c108b567b18b5..7897c5e545fac 100644 --- a/tests/ui/suggestions/let-binding-init-expr-as-ty.rs +++ b/tests/ui/suggestions/let-binding-init-expr-as-ty.rs @@ -27,6 +27,7 @@ fn main() { let x: S::new(..); //~ ERROR expected type, found associated function call //~^ ERROR return type notation is experimental let x: S::new(()); //~ ERROR expected type, found associated function call + let x: vec![]; //~ ERROR expected type, found associated function call // Literals let x: 42; //~ ERROR expected type, found `42` diff --git a/tests/ui/suggestions/let-binding-init-expr-as-ty.stderr b/tests/ui/suggestions/let-binding-init-expr-as-ty.stderr index 354d119769119..06ca102575d98 100644 --- a/tests/ui/suggestions/let-binding-init-expr-as-ty.stderr +++ b/tests/ui/suggestions/let-binding-init-expr-as-ty.stderr @@ -1,5 +1,5 @@ error: expected type, found `42` - --> $DIR/let-binding-init-expr-as-ty.rs:32:12 + --> $DIR/let-binding-init-expr-as-ty.rs:33:12 | LL | let x: 42; | - ^^ expected type @@ -13,7 +13,7 @@ LL + let x = 42; | error: expected type, found `""` - --> $DIR/let-binding-init-expr-as-ty.rs:33:12 + --> $DIR/let-binding-init-expr-as-ty.rs:34:12 | LL | let x: ""; | - ^^ expected type @@ -39,7 +39,7 @@ LL + let foo = i32::from_be(num); | error[E0573]: expected type, found function `bar` - --> $DIR/let-binding-init-expr-as-ty.rs:36:12 + --> $DIR/let-binding-init-expr-as-ty.rs:37:12 | LL | let x: bar(); | ^^^^^ not a type @@ -51,13 +51,13 @@ LL + let x = bar(); | error[E0573]: expected type, found function `bar` - --> $DIR/let-binding-init-expr-as-ty.rs:37:12 + --> $DIR/let-binding-init-expr-as-ty.rs:38:12 | LL | let x: bar; | ^^^ not a type error[E0573]: expected type, found local variable `x` - --> $DIR/let-binding-init-expr-as-ty.rs:40:12 + --> $DIR/let-binding-init-expr-as-ty.rs:41:12 | LL | struct K(S::new(())); | --------------------- similarly named struct `K` defined here @@ -153,7 +153,19 @@ LL - let x: S::new(()); LL + let x = S::new(()); | -error: aborting due to 13 previous errors +error: expected type, found associated function call + --> $DIR/let-binding-init-expr-as-ty.rs:30:12 + | +LL | let x: vec![]; + | ^^^^^^ + | +help: use `=` if you meant to assign + | +LL - let x: vec![]; +LL + let x = vec![]; + | + +error: aborting due to 14 previous errors Some errors have detailed explanations: E0573, E0658. For more information about an error, try `rustc --explain E0573`.