Skip to content

Commit 4435bb0

Browse files
authored
Rollup merge of #91981 - estebank:tweakaroo, r=lcnr
Recover suggestions and useful information lost in previous PR Follow up to #91898.
2 parents 551b4fa + f479e26 commit 4435bb0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+209
-60
lines changed

compiler/rustc_infer/src/infer/error_reporting/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,9 @@ pub fn same_type_modulo_infer<'tcx>(a: Ty<'tcx>, b: Ty<'tcx>) -> bool {
333333
)
334334
| (&ty::Infer(ty::InferTy::TyVar(_)), _)
335335
| (_, &ty::Infer(ty::InferTy::TyVar(_))) => true,
336+
(&ty::Ref(reg_a, ty_a, mut_a), &ty::Ref(reg_b, ty_b, mut_b)) => {
337+
reg_a == reg_b && mut_a == mut_b && same_type_modulo_infer(*ty_a, *ty_b)
338+
}
336339
_ => a == b,
337340
}
338341
}
@@ -602,7 +605,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
602605
match *cause.code() {
603606
ObligationCauseCode::Pattern { origin_expr: true, span: Some(span), root_ty } => {
604607
let ty = self.resolve_vars_if_possible(root_ty);
605-
if ty.is_suggestable() {
608+
if !matches!(ty.kind(), ty::Infer(ty::InferTy::TyVar(_) | ty::InferTy::FreshTy(_)))
609+
{
606610
// don't show type `_`
607611
err.span_label(span, format!("this expression has type `{}`", ty));
608612
}

compiler/rustc_infer/src/infer/mod.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,6 +1434,17 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
14341434
value.fold_with(&mut r)
14351435
}
14361436

1437+
pub fn resolve_numeric_literals_with_default<T>(&self, value: T) -> T
1438+
where
1439+
T: TypeFoldable<'tcx>,
1440+
{
1441+
if !value.needs_infer() {
1442+
return value; // Avoid duplicated subst-folding.
1443+
}
1444+
let mut r = InferenceLiteralEraser { tcx: self.tcx };
1445+
value.fold_with(&mut r)
1446+
}
1447+
14371448
/// Returns the first unresolved variable contained in `T`. In the
14381449
/// process of visiting `T`, this will resolve (where possible)
14391450
/// type variables in `T`, but it never constructs the final,
@@ -1785,6 +1796,26 @@ impl<'tcx> TyOrConstInferVar<'tcx> {
17851796
}
17861797
}
17871798

1799+
/// Replace `{integer}` with `i32` and `{float}` with `f64`.
1800+
/// Used only for diagnostics.
1801+
struct InferenceLiteralEraser<'tcx> {
1802+
tcx: TyCtxt<'tcx>,
1803+
}
1804+
1805+
impl<'tcx> TypeFolder<'tcx> for InferenceLiteralEraser<'tcx> {
1806+
fn tcx(&self) -> TyCtxt<'tcx> {
1807+
self.tcx
1808+
}
1809+
1810+
fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
1811+
match ty.kind() {
1812+
ty::Infer(ty::IntVar(_) | ty::FreshIntTy(_)) => self.tcx.types.i32,
1813+
ty::Infer(ty::FloatVar(_) | ty::FreshFloatTy(_)) => self.tcx.types.f64,
1814+
_ => ty.super_fold_with(self),
1815+
}
1816+
}
1817+
}
1818+
17881819
struct ShallowResolver<'a, 'tcx> {
17891820
infcx: &'a InferCtxt<'a, 'tcx>,
17901821
}

compiler/rustc_typeck/src/astconv/generics.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use rustc_hir as hir;
1111
use rustc_hir::def::{DefKind, Res};
1212
use rustc_hir::def_id::DefId;
1313
use rustc_hir::GenericArg;
14+
use rustc_infer::infer::TyCtxtInferExt;
1415
use rustc_middle::ty::{
1516
self, subst, subst::SubstsRef, GenericParamDef, GenericParamDefKind, Ty, TyCtxt,
1617
};
@@ -83,7 +84,9 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
8384
if let Some(param_local_id) = param.def_id.as_local() {
8485
let param_hir_id = tcx.hir().local_def_id_to_hir_id(param_local_id);
8586
let param_name = tcx.hir().ty_param_name(param_hir_id);
86-
let param_type = tcx.type_of(param.def_id);
87+
let param_type = tcx.infer_ctxt().enter(|infcx| {
88+
infcx.resolve_numeric_literals_with_default(tcx.type_of(param.def_id))
89+
});
8790
if param_type.is_suggestable() {
8891
err.span_suggestion(
8992
tcx.def_span(src_def_id),

compiler/rustc_typeck/src/check/fn_ctxt/suggestions.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -521,20 +521,29 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
521521
can_suggest: bool,
522522
fn_id: hir::HirId,
523523
) -> bool {
524+
let found =
525+
self.resolve_numeric_literals_with_default(self.resolve_vars_if_possible(found));
524526
// Only suggest changing the return type for methods that
525527
// haven't set a return type at all (and aren't `fn main()` or an impl).
526528
match (&fn_decl.output, found.is_suggestable(), can_suggest, expected.is_unit()) {
527529
(&hir::FnRetTy::DefaultReturn(span), true, true, true) => {
528530
err.span_suggestion(
529531
span,
530532
"try adding a return type",
531-
format!("-> {} ", self.resolve_vars_with_obligations(found)),
533+
format!("-> {} ", found),
532534
Applicability::MachineApplicable,
533535
);
534536
true
535537
}
536538
(&hir::FnRetTy::DefaultReturn(span), false, true, true) => {
537-
err.span_label(span, "possibly return type missing here?");
539+
// FIXME: if `found` could be `impl Iterator` or `impl Fn*`, we should suggest
540+
// that.
541+
err.span_suggestion(
542+
span,
543+
"a return type might be missing here",
544+
"-> _ ".to_string(),
545+
Applicability::HasPlaceholders,
546+
);
538547
true
539548
}
540549
(&hir::FnRetTy::DefaultReturn(span), _, false, true) => {

src/test/ui/async-await/issue-61076.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ async fn baz() -> Result<(), ()> {
8787

8888
async fn match_() {
8989
match tuple() { //~ HELP consider `await`ing on the `Future`
90+
//~^ NOTE this expression has type `impl Future<Output = Tuple>`
9091
Tuple(_) => {} //~ ERROR mismatched types
9192
//~^ NOTE expected opaque type, found struct `Tuple`
9293
//~| NOTE expected opaque type `impl Future<Output = Tuple>`

src/test/ui/async-await/issue-61076.stderr

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,11 @@ LL | struct_().await.method();
5656
| ++++++
5757

5858
error[E0308]: mismatched types
59-
--> $DIR/issue-61076.rs:90:9
59+
--> $DIR/issue-61076.rs:91:9
6060
|
61+
LL | match tuple() {
62+
| ------- this expression has type `impl Future<Output = Tuple>`
63+
LL |
6164
LL | Tuple(_) => {}
6265
| ^^^^^^^^ expected opaque type, found struct `Tuple`
6366
|

src/test/ui/async-await/suggest-missing-await.stderr

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ LL ~ 1 => dummy().await,
9191
error[E0308]: mismatched types
9292
--> $DIR/suggest-missing-await.rs:53:9
9393
|
94+
LL | let _x = match dummy() {
95+
| ------- this expression has type `impl Future<Output = ()>`
9496
LL | () => {}
9597
| ^^ expected opaque type, found `()`
9698
|
@@ -109,6 +111,9 @@ LL | let _x = match dummy().await {
109111
error[E0308]: mismatched types
110112
--> $DIR/suggest-missing-await.rs:67:9
111113
|
114+
LL | match dummy_result() {
115+
| -------------- this expression has type `impl Future<Output = Result<(), ()>>`
116+
...
112117
LL | Ok(_) => {}
113118
| ^^^^^ expected opaque type, found enum `Result`
114119
|
@@ -127,6 +132,9 @@ LL | match dummy_result().await {
127132
error[E0308]: mismatched types
128133
--> $DIR/suggest-missing-await.rs:69:9
129134
|
135+
LL | match dummy_result() {
136+
| -------------- this expression has type `impl Future<Output = Result<(), ()>>`
137+
...
130138
LL | Err(_) => {}
131139
| ^^^^^^ expected opaque type, found enum `Result`
132140
|

src/test/ui/blind/blind-item-block-middle.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | mod foo { pub struct bar; }
55
| --------------- unit struct defined here
66
...
77
LL | let bar = 5;
8-
| ^^^
8+
| ^^^ - this expression has type `{integer}`
99
| |
1010
| expected integer, found struct `bar`
1111
| `bar` is interpreted as a unit struct, not a new binding

src/test/ui/block-result/issue-20862.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0308]: mismatched types
22
--> $DIR/issue-20862.rs:2:5
33
|
44
LL | fn foo(x: i32) {
5-
| - possibly return type missing here?
5+
| - help: a return type might be missing here: `-> _`
66
LL | |y| x + y
77
| ^^^^^^^^^ expected `()`, found closure
88
|

src/test/ui/destructuring-assignment/default-match-bindings-forbidden.stderr

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ error[E0308]: mismatched types
22
--> $DIR/default-match-bindings-forbidden.rs:4:5
33
|
44
LL | (x, y) = &(1, 2);
5-
| ^^^^^^ expected reference, found tuple
5+
| ^^^^^^ ------- this expression has type `&({integer}, {integer})`
6+
| |
7+
| expected reference, found tuple
68
|
79
= note: expected type `&({integer}, {integer})`
810
found tuple `(_, _)`

0 commit comments

Comments
 (0)