Skip to content
Open
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
54 changes: 36 additions & 18 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3290,18 +3290,18 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
.next()
{
// `let x: S::new(valid_in_ty_ctxt);` -> `let x = S::new(valid_in_ty_ctxt);`
let err = tcx
.dcx()
.struct_span_err(
hir_ty.span,
"expected type, found associated function call",
)
.with_span_suggestion_verbose(
stmt.pat.span.between(hir_ty.span),
let mut err = tcx.dcx().struct_span_err(
hir_ty.span,
"expected type, found associated function call",
);
if let Some(between) = eq_ctxt_suggestion_span(stmt.pat.span, hir_ty.span) {
err.span_suggestion_verbose(
between,
"use `=` if you meant to assign",
" = ".to_string(),
" = ",
Applicability::MaybeIncorrect,
);
}
self.dcx().try_steal_replace_and_emit_err(
hir_ty.span,
StashKey::ReturnTypeNotation,
Expand All @@ -3316,18 +3316,18 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
{
// `let x: i32::something(valid_in_ty_ctxt);` -> `let x = i32::something(valid_in_ty_ctxt);`
// FIXME: Check that `something` is a valid function in `i32`.
let err = tcx
.dcx()
.struct_span_err(
hir_ty.span,
"expected type, found associated function call",
)
.with_span_suggestion_verbose(
stmt.pat.span.between(hir_ty.span),
let mut err = tcx.dcx().struct_span_err(
hir_ty.span,
"expected type, found associated function call",
);
if let Some(between) = eq_ctxt_suggestion_span(stmt.pat.span, hir_ty.span) {
err.span_suggestion_verbose(
between,
"use `=` if you meant to assign",
" = ".to_string(),
" = ",
Applicability::MaybeIncorrect,
);
}
self.dcx().try_steal_replace_and_emit_err(
hir_ty.span,
StashKey::ReturnTypeNotation,
Expand Down Expand Up @@ -3813,3 +3813,21 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
ty::Const::new_value(tcx, valtree, adt_ty)
}
}

/// Computes the `pat.between(ty)` span for the "use `=`" suggestion on `let pat: ty`.
/// Returns `None` if `pat` and `ty` are in incompatible macro contexts (e.g. `pat` is a
/// metavariable from the call site while `ty` lives in the macro body), in which case no
/// suggestion is emitted.
fn eq_ctxt_suggestion_span(pat: Span, ty: Span) -> Option<Span> {
if let Some(ty2) = ty.find_ancestor_in_same_ctxt(pat)
&& pat.hi() <= ty2.lo()
{
return Some(pat.between(ty2));
}
if let Some(pat2) = pat.find_ancestor_in_same_ctxt(ty)
&& pat2.hi() <= ty.lo()
{
return Some(pat2.between(ty));
}
None
}
11 changes: 11 additions & 0 deletions tests/ui/suggestions/let-binding-init-expr-as-ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ fn main() {
//~^ ERROR return type notation is experimental
let x: S::new(()); //~ ERROR expected type, found associated function call

// Macros — suggestion must point at user code, not the macro definition (#158834)
let x: vec![]; //~ ERROR expected type, found associated function call

// When the `let` is inside a macro, no suggestion should be emitted at the call site
macro_rules! make {
($pat:pat) => {
let $pat: Vec::new(); //~ ERROR expected type, found associated function call
};
}
make!(_);

// Literals
let x: 42; //~ ERROR expected type, found `42`
let x: ""; //~ ERROR expected type, found `""`
Expand Down
35 changes: 29 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:43: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:44: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:47: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:48: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:51:12
|
LL | struct K(S::new(()));
| --------------------- similarly named struct `K` defined here
Expand Down Expand Up @@ -153,7 +153,30 @@ 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:32:12
|
LL | let x: vec![];
| ^^^^^^
|
help: use `=` if you meant to assign
|
LL - let x: vec![];
LL + let x = vec![];
|

error: expected type, found associated function call
--> $DIR/let-binding-init-expr-as-ty.rs:37:23
|
LL | let $pat: Vec::new();
| ^^^^^^^^^^
...
LL | make!(_);
| -------- in this macro invocation
|
= note: this error originates in the macro `make` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 15 previous errors

Some errors have detailed explanations: E0573, E0658.
For more information about an error, try `rustc --explain E0573`.
Loading