Skip to content
Closed
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
4 changes: 2 additions & 2 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The suggestion is still incorrect for

macro_rules! make {
    () => { let x: vec![]; };
}

fn main() {
    make!();
}

namely

error: expected type, found associated function call
  --> u.rs:5:16
   |
 5 |         let x: vec![];
   |                ^^^^^^
...
12 |     make!();
   |     ------- in this macro invocation
   |
   = note: this error originates in the macro `vec` which comes from the expansion of the macro `make` (in Nightly builds, run with -Z macro-backtrace for more info)
help: use `=` if you meant to assign
   |
 5 -         let x: vec![];
 5 +         let  = : vec![];
   |

@fmease fmease Jun 28, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moreover for

macro_rules! make {
    ($pat:pat, $ty:ty) => {
        let $pat: $ty;
    };
}

fn main() {
    make!(_, vec![]);
}

your branch now suggests

error: expected type, found associated function call
 --> u.rs:8:14
  |
8 |     make!(_, vec![]);
  |              ^^^^^^
  |
help: use `=` if you meant to assign
  |
8 -     make!(_, vec![]);
8 +     make!(_ = vec![]);
  |

main also emits an incorrect suggestion here but it's a different one.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's more, for the following input

macro_rules! make {
    ($pat:pat) => {
        let $pat: Vec::new();
    };
}

fn main() {
    make!(_);
}

main correctly emits

error: expected type, found associated function call
 --> u.rs:3:19
  |
3 |         let $pat: Vec::new();
  |                   ^^^^^^^^^^
...
8 |     make!(_);
  |     -------- in this macro invocation
  |
  = note: this error originates in the macro `make` (in Nightly builds, run with -Z macro-backtrace for more info)
help: use `=` if you meant to assign
  |
3 -         let $pat: Vec::new();
3 +         let $pat = Vec::new();

but your branch incorrectly emits

error: expected type, found associated function call
 --> u.rs:3:19
  |
3 |         let $pat: Vec::new();
  |                   ^^^^^^^^^^
...
8 |     make!(_);
  |     -------- in this macro invocation
  |
  = note: this error originates in the macro `make` (in Nightly builds, run with -Z macro-backtrace for more info)
help: use `=` if you meant to assign
  |
8 -     make!(_);
8 +     make!( = );
  |

Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions tests/ui/suggestions/let-binding-init-expr-as-ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
24 changes: 18 additions & 6 deletions tests/ui/suggestions/let-binding-init-expr-as-ty.stderr
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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`.
Loading