Skip to content

Commit 474626a

Browse files
committed
Eagerly replace {integer}/{float} with i32/f64 for suggestion
1 parent 3fe3b89 commit 474626a

File tree

5 files changed

+49
-12
lines changed

5 files changed

+49
-12
lines changed

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 { infcx: self };
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<'a, 'tcx> {
1802+
infcx: &'a InferCtxt<'a, 'tcx>,
1803+
}
1804+
1805+
impl<'a, 'tcx> TypeFolder<'tcx> for InferenceLiteralEraser<'a, 'tcx> {
1806+
fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
1807+
self.infcx.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/check/fn_ctxt/suggestions.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,8 @@ 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()) {

src/test/ui/issues/issue-66706.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,23 @@ error[E0308]: mismatched types
3636
--> $DIR/issue-66706.rs:2:5
3737
|
3838
LL | fn a() {
39-
| - possibly return type missing here?
39+
| - help: try adding a return type: `-> [i32; _]`
4040
LL | [0; [|_: _ &_| ()].len()]
4141
| ^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found array `[{integer}; _]`
4242

4343
error[E0308]: mismatched types
4444
--> $DIR/issue-66706.rs:14:5
4545
|
4646
LL | fn c() {
47-
| - possibly return type missing here?
47+
| - help: try adding a return type: `-> [i32; _]`
4848
LL | [0; [|&_: _ &_| {}; 0 ].len()]
4949
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found array `[{integer}; _]`
5050

5151
error[E0308]: mismatched types
5252
--> $DIR/issue-66706.rs:20:5
5353
|
5454
LL | fn d() {
55-
| - possibly return type missing here?
55+
| - help: try adding a return type: `-> [i32; _]`
5656
LL | [0; match [|f @ &ref _| () ] {} ]
5757
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found array `[{integer}; _]`
5858

src/test/ui/proc-macro/span-preservation.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,31 +38,31 @@ error[E0308]: mismatched types
3838
--> $DIR/span-preservation.rs:39:5
3939
|
4040
LL | extern "C" fn bar() {
41-
| - possibly return type missing here?
41+
| - help: try adding a return type: `-> i32`
4242
LL | 0
4343
| ^ expected `()`, found integer
4444

4545
error[E0308]: mismatched types
4646
--> $DIR/span-preservation.rs:44:5
4747
|
4848
LL | extern "C" fn baz() {
49-
| - possibly return type missing here?
49+
| - help: try adding a return type: `-> i32`
5050
LL | 0
5151
| ^ expected `()`, found integer
5252

5353
error[E0308]: mismatched types
5454
--> $DIR/span-preservation.rs:49:5
5555
|
5656
LL | extern "Rust" fn rust_abi() {
57-
| - possibly return type missing here?
57+
| - help: try adding a return type: `-> i32`
5858
LL | 0
5959
| ^ expected `()`, found integer
6060

6161
error[E0308]: mismatched types
6262
--> $DIR/span-preservation.rs:54:5
6363
|
6464
LL | extern "\x43" fn c_abi_escaped() {
65-
| - possibly return type missing here?
65+
| - help: try adding a return type: `-> i32`
6666
LL | 0
6767
| ^ expected `()`, found integer
6868

src/test/ui/return/return-type.stderr

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
error[E0308]: mismatched types
22
--> $DIR/return-type.rs:10:5
33
|
4-
LL | fn bar() {
5-
| - possibly return type missing here?
64
LL | foo(4 as usize)
7-
| ^^^^^^^^^^^^^^^- help: consider using a semicolon here: `;`
8-
| |
9-
| expected `()`, found struct `S`
5+
| ^^^^^^^^^^^^^^^ expected `()`, found struct `S`
106
|
117
= note: expected unit type `()`
128
found struct `S<usize>`
9+
help: consider using a semicolon here
10+
|
11+
LL | foo(4 as usize);
12+
| +
13+
help: try adding a return type
14+
|
15+
LL | fn bar() -> S<usize> {
16+
| +++++++++++
1317

1418
error: aborting due to previous error
1519

0 commit comments

Comments
 (0)