Skip to content

Commit 1b48f4d

Browse files
authored
Rollup merge of #87673 - estebank:opaque-ty-mismatch, r=davidtwco
Tweak opaque type mismatch error
2 parents 4380056 + 052084a commit 1b48f4d

10 files changed

+114
-82
lines changed

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

+43-25
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ use rustc_middle::ty::{
7171
subst::{GenericArgKind, Subst, SubstsRef},
7272
Region, Ty, TyCtxt, TypeFoldable,
7373
};
74-
use rustc_span::{sym, BytePos, DesugaringKind, Pos, Span};
74+
use rustc_span::{sym, BytePos, DesugaringKind, MultiSpan, Pos, Span};
7575
use rustc_target::spec::abi;
7676
use std::ops::ControlFlow;
7777
use std::{cmp, fmt, iter};
@@ -1485,31 +1485,49 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
14851485
let count = values.len();
14861486
let kind = key.descr();
14871487
let mut returned_async_output_error = false;
1488-
for sp in values {
1489-
err.span_label(
1490-
*sp,
1491-
format!(
1492-
"{}{}{} {}{}",
1493-
if sp.is_desugaring(DesugaringKind::Async)
1494-
&& !returned_async_output_error
1495-
{
1496-
"checked the `Output` of this `async fn`, "
1497-
} else if count == 1 {
1498-
"the "
1499-
} else {
1500-
""
1501-
},
1502-
if count > 1 { "one of the " } else { "" },
1503-
target,
1504-
kind,
1505-
pluralize!(count),
1506-
),
1507-
);
1508-
if sp.is_desugaring(DesugaringKind::Async)
1509-
&& returned_async_output_error == false
1510-
{
1511-
err.note("while checking the return type of the `async fn`");
1488+
for &sp in values {
1489+
if sp.is_desugaring(DesugaringKind::Async) && !returned_async_output_error {
1490+
if &[sp] != err.span.primary_spans() {
1491+
let mut span: MultiSpan = sp.into();
1492+
span.push_span_label(
1493+
sp,
1494+
format!(
1495+
"checked the `Output` of this `async fn`, {}{} {}{}",
1496+
if count > 1 { "one of the " } else { "" },
1497+
target,
1498+
kind,
1499+
pluralize!(count),
1500+
),
1501+
);
1502+
err.span_note(
1503+
span,
1504+
"while checking the return type of the `async fn`",
1505+
);
1506+
} else {
1507+
err.span_label(
1508+
sp,
1509+
format!(
1510+
"checked the `Output` of this `async fn`, {}{} {}{}",
1511+
if count > 1 { "one of the " } else { "" },
1512+
target,
1513+
kind,
1514+
pluralize!(count),
1515+
),
1516+
);
1517+
err.note("while checking the return type of the `async fn`");
1518+
}
15121519
returned_async_output_error = true;
1520+
} else {
1521+
err.span_label(
1522+
sp,
1523+
format!(
1524+
"{}{} {}{}",
1525+
if count == 1 { "the " } else { "one of the " },
1526+
target,
1527+
kind,
1528+
pluralize!(count),
1529+
),
1530+
);
15131531
}
15141532
}
15151533
}

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
error[E0308]: mismatched types
22
--> $DIR/dont-suggest-missing-await.rs:14:18
33
|
4-
LL | async fn make_u32() -> u32 {
5-
| --- checked the `Output` of this `async fn`, found opaque type
6-
...
74
LL | take_u32(x)
85
| ^ expected `u32`, found opaque type
96
|
10-
= note: while checking the return type of the `async fn`
7+
note: while checking the return type of the `async fn`
8+
--> $DIR/dont-suggest-missing-await.rs:7:24
9+
|
10+
LL | async fn make_u32() -> u32 {
11+
| ^^^ checked the `Output` of this `async fn`, found opaque type
1112
= note: expected type `u32`
1213
found opaque type `impl Future`
1314
help: consider `await`ing on the `Future`

src/test/ui/async-await/generator-desc.stderr

+10-7
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,19 @@ LL | fun(async {}, async {});
1212
error[E0308]: mismatched types
1313
--> $DIR/generator-desc.rs:12:16
1414
|
15-
LL | async fn one() {}
16-
| - checked the `Output` of this `async fn`, expected opaque type
17-
LL | async fn two() {}
18-
| - checked the `Output` of this `async fn`, found opaque type
19-
...
2015
LL | fun(one(), two());
2116
| ^^^^^ expected opaque type, found a different opaque type
2217
|
23-
= note: while checking the return type of the `async fn`
24-
= note: while checking the return type of the `async fn`
18+
note: while checking the return type of the `async fn`
19+
--> $DIR/generator-desc.rs:5:16
20+
|
21+
LL | async fn one() {}
22+
| ^ checked the `Output` of this `async fn`, expected opaque type
23+
note: while checking the return type of the `async fn`
24+
--> $DIR/generator-desc.rs:6:16
25+
|
26+
LL | async fn two() {}
27+
| ^ checked the `Output` of this `async fn`, found opaque type
2528
= note: expected opaque type `impl Future` (opaque type at <$DIR/generator-desc.rs:5:16>)
2629
found opaque type `impl Future` (opaque type at <$DIR/generator-desc.rs:6:16>)
2730
= help: consider `await`ing on both `Future`s

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ async fn struct_() -> Struct {
5757

5858
async fn tuple() -> Tuple {
5959
//~^ NOTE checked the `Output` of this `async fn`, expected opaque type
60+
//~| NOTE while checking the return type of the `async fn`
61+
//~| NOTE in this expansion of desugaring of `async` block or function
6062
Tuple(1i32)
6163
}
6264

@@ -92,7 +94,6 @@ async fn match_() {
9294
Tuple(_) => {} //~ ERROR mismatched types
9395
//~^ NOTE expected opaque type, found struct `Tuple`
9496
//~| NOTE expected opaque type `impl Future`
95-
//~| NOTE while checking the return type of the `async fn`
9697
}
9798
}
9899

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

+10-9
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ LL | foo().await?;
1616
| ^^^^^^
1717

1818
error[E0277]: the `?` operator can only be applied to values that implement `Try`
19-
--> $DIR/issue-61076.rs:65:5
19+
--> $DIR/issue-61076.rs:67:5
2020
|
2121
LL | t?;
2222
| ^^ the `?` operator cannot be applied to type `T`
@@ -33,7 +33,7 @@ LL | t.await?;
3333
| ^^^^^^
3434

3535
error[E0609]: no field `0` on type `impl Future`
36-
--> $DIR/issue-61076.rs:76:26
36+
--> $DIR/issue-61076.rs:78:26
3737
|
3838
LL | let _: i32 = tuple().0;
3939
| ^ field not available in `impl Future`, but it is available in its `Output`
@@ -44,7 +44,7 @@ LL | let _: i32 = tuple().await.0;
4444
| ^^^^^^
4545

4646
error[E0609]: no field `a` on type `impl Future`
47-
--> $DIR/issue-61076.rs:80:28
47+
--> $DIR/issue-61076.rs:82:28
4848
|
4949
LL | let _: i32 = struct_().a;
5050
| ^ field not available in `impl Future`, but it is available in its `Output`
@@ -55,7 +55,7 @@ LL | let _: i32 = struct_().await.a;
5555
| ^^^^^^
5656

5757
error[E0599]: no method named `method` found for opaque type `impl Future` in the current scope
58-
--> $DIR/issue-61076.rs:84:15
58+
--> $DIR/issue-61076.rs:86:15
5959
|
6060
LL | struct_().method();
6161
| ^^^^^^ method not found in `impl Future`
@@ -66,15 +66,16 @@ LL | struct_().await.method();
6666
| ^^^^^^
6767

6868
error[E0308]: mismatched types
69-
--> $DIR/issue-61076.rs:92:9
69+
--> $DIR/issue-61076.rs:94:9
7070
|
71-
LL | async fn tuple() -> Tuple {
72-
| ----- checked the `Output` of this `async fn`, expected opaque type
73-
...
7471
LL | Tuple(_) => {}
7572
| ^^^^^^^^ expected opaque type, found struct `Tuple`
7673
|
77-
= note: while checking the return type of the `async fn`
74+
note: while checking the return type of the `async fn`
75+
--> $DIR/issue-61076.rs:58:21
76+
|
77+
LL | async fn tuple() -> Tuple {
78+
| ^^^^^ checked the `Output` of this `async fn`, expected opaque type
7879
= note: expected opaque type `impl Future`
7980
found struct `Tuple`
8081
help: consider `await`ing on the `Future`

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
error[E0308]: mismatched types
22
--> $DIR/suggest-missing-await-closure.rs:16:18
33
|
4-
LL | async fn make_u32() -> u32 {
5-
| --- checked the `Output` of this `async fn`, found opaque type
6-
...
74
LL | take_u32(x)
85
| ^ expected `u32`, found opaque type
96
|
10-
= note: while checking the return type of the `async fn`
7+
note: while checking the return type of the `async fn`
8+
--> $DIR/suggest-missing-await-closure.rs:8:24
9+
|
10+
LL | async fn make_u32() -> u32 {
11+
| ^^^ checked the `Output` of this `async fn`, found opaque type
1112
= note: expected type `u32`
1213
found opaque type `impl Future`
1314
help: consider `await`ing on the `Future`

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

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
error[E0308]: mismatched types
22
--> $DIR/suggest-missing-await.rs:12:14
33
|
4-
LL | async fn make_u32() -> u32 {
5-
| --- checked the `Output` of this `async fn`, found opaque type
6-
...
74
LL | take_u32(x)
85
| ^ expected `u32`, found opaque type
96
|
10-
= note: while checking the return type of the `async fn`
7+
note: while checking the return type of the `async fn`
8+
--> $DIR/suggest-missing-await.rs:5:24
9+
|
10+
LL | async fn make_u32() -> u32 {
11+
| ^^^ checked the `Output` of this `async fn`, found opaque type
1112
= note: expected type `u32`
1213
found opaque type `impl Future`
1314
help: consider `await`ing on the `Future`
@@ -18,13 +19,14 @@ LL | take_u32(x.await)
1819
error[E0308]: mismatched types
1920
--> $DIR/suggest-missing-await.rs:22:5
2021
|
21-
LL | async fn dummy() {}
22-
| - checked the `Output` of this `async fn`, found opaque type
23-
...
2422
LL | dummy()
2523
| ^^^^^^^ expected `()`, found opaque type
2624
|
27-
= note: while checking the return type of the `async fn`
25+
note: while checking the return type of the `async fn`
26+
--> $DIR/suggest-missing-await.rs:18:18
27+
|
28+
LL | async fn dummy() {}
29+
| ^ checked the `Output` of this `async fn`, found opaque type
2830
= note: expected unit type `()`
2931
found opaque type `impl Future`
3032
help: consider `await`ing on the `Future`

src/test/ui/suggestions/issue-81839.stderr

+4-5
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@ LL | | _ => cx.answer_str("hi"),
1313
| | ^^^^^^^^^^^^^^^^^^^ expected `()`, found opaque type
1414
LL | | }
1515
| |_____- `match` arms have incompatible types
16-
|
17-
::: $DIR/auxiliary/issue-81839.rs:6:49
1816
|
19-
LL | pub async fn answer_str(&self, _s: &str) -> Test {
20-
| ---- checked the `Output` of this `async fn`, found opaque type
17+
note: while checking the return type of the `async fn`
18+
--> $DIR/auxiliary/issue-81839.rs:6:49
2119
|
22-
= note: while checking the return type of the `async fn`
20+
LL | pub async fn answer_str(&self, _s: &str) -> Test {
21+
| ^^^^ checked the `Output` of this `async fn`, found opaque type
2322
= note: expected type `()`
2423
found opaque type `impl Future`
2524

src/test/ui/suggestions/match-prev-arm-needing-semi.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@ fn extra_semicolon() {
1414
}
1515

1616
async fn async_dummy() {} //~ NOTE checked the `Output` of this `async fn`, found opaque type
17+
//~| NOTE while checking the return type of the `async fn`
18+
//~| NOTE in this expansion of desugaring of `async` block or function
1719
async fn async_dummy2() {} //~ NOTE checked the `Output` of this `async fn`, found opaque type
1820
//~| NOTE checked the `Output` of this `async fn`, found opaque type
21+
//~| NOTE while checking the return type of the `async fn`
22+
//~| NOTE in this expansion of desugaring of `async` block or function
23+
//~| NOTE while checking the return type of the `async fn`
24+
//~| NOTE in this expansion of desugaring of `async` block or function
1925

2026
async fn async_extra_semicolon_same() {
2127
let _ = match true { //~ NOTE `match` arms have incompatible types
@@ -26,7 +32,6 @@ async fn async_extra_semicolon_same() {
2632
false => async_dummy(), //~ ERROR `match` arms have incompatible types
2733
//~^ NOTE expected `()`, found opaque type
2834
//~| NOTE expected type `()`
29-
//~| NOTE while checking the return type of the `async fn`
3035
//~| HELP consider `await`ing on the `Future`
3136
};
3237
}
@@ -40,7 +45,6 @@ async fn async_extra_semicolon_different() {
4045
false => async_dummy2(), //~ ERROR `match` arms have incompatible types
4146
//~^ NOTE expected `()`, found opaque type
4247
//~| NOTE expected type `()`
43-
//~| NOTE while checking the return type of the `async fn`
4448
//~| HELP consider `await`ing on the `Future`
4549
};
4650
}
@@ -53,7 +57,6 @@ async fn async_different_futures() {
5357
//~^ NOTE expected opaque type, found a different opaque type
5458
//~| NOTE expected type `impl Future`
5559
//~| NOTE distinct uses of `impl Trait` result in different opaque types
56-
//~| NOTE while checking the return type of the `async fn`
5760
};
5861
}
5962

src/test/ui/suggestions/match-prev-arm-needing-semi.stderr

+19-16
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
error[E0308]: `match` arms have incompatible types
2-
--> $DIR/match-prev-arm-needing-semi.rs:26:18
2+
--> $DIR/match-prev-arm-needing-semi.rs:32:18
33
|
4-
LL | async fn async_dummy() {}
5-
| - checked the `Output` of this `async fn`, found opaque type
6-
...
74
LL | let _ = match true {
85
| _____________-
96
LL | | true => {
@@ -18,7 +15,11 @@ LL | |
1815
LL | | };
1916
| |_____- `match` arms have incompatible types
2017
|
21-
= note: while checking the return type of the `async fn`
18+
note: while checking the return type of the `async fn`
19+
--> $DIR/match-prev-arm-needing-semi.rs:16:24
20+
|
21+
LL | async fn async_dummy() {}
22+
| ^ checked the `Output` of this `async fn`, found opaque type
2223
= note: expected type `()`
2324
found opaque type `impl Future`
2425
help: consider `await`ing on the `Future`
@@ -31,11 +32,8 @@ LL | async_dummy()
3132
| --
3233

3334
error[E0308]: `match` arms have incompatible types
34-
--> $DIR/match-prev-arm-needing-semi.rs:40:18
35+
--> $DIR/match-prev-arm-needing-semi.rs:45:18
3536
|
36-
LL | async fn async_dummy2() {}
37-
| - checked the `Output` of this `async fn`, found opaque type
38-
...
3937
LL | let _ = match true {
4038
| _____________-
4139
LL | | true => {
@@ -50,7 +48,11 @@ LL | |
5048
LL | | };
5149
| |_____- `match` arms have incompatible types
5250
|
53-
= note: while checking the return type of the `async fn`
51+
note: while checking the return type of the `async fn`
52+
--> $DIR/match-prev-arm-needing-semi.rs:19:25
53+
|
54+
LL | async fn async_dummy2() {}
55+
| ^ checked the `Output` of this `async fn`, found opaque type
5456
= note: expected type `()`
5557
found opaque type `impl Future`
5658
help: consider `await`ing on the `Future`
@@ -66,11 +68,8 @@ LL | false => Box::new(async_dummy2()),
6668
|
6769

6870
error[E0308]: `match` arms have incompatible types
69-
--> $DIR/match-prev-arm-needing-semi.rs:52:18
71+
--> $DIR/match-prev-arm-needing-semi.rs:56:18
7072
|
71-
LL | async fn async_dummy2() {}
72-
| - checked the `Output` of this `async fn`, found opaque type
73-
...
7473
LL | let _ = match true {
7574
| _____________-
7675
LL | | true => async_dummy(),
@@ -83,9 +82,13 @@ LL | |
8382
LL | | };
8483
| |_____- `match` arms have incompatible types
8584
|
86-
= note: while checking the return type of the `async fn`
85+
note: while checking the return type of the `async fn`
86+
--> $DIR/match-prev-arm-needing-semi.rs:19:25
87+
|
88+
LL | async fn async_dummy2() {}
89+
| ^ checked the `Output` of this `async fn`, found opaque type
8790
= note: expected type `impl Future` (opaque type at <$DIR/match-prev-arm-needing-semi.rs:16:24>)
88-
found opaque type `impl Future` (opaque type at <$DIR/match-prev-arm-needing-semi.rs:17:25>)
91+
found opaque type `impl Future` (opaque type at <$DIR/match-prev-arm-needing-semi.rs:19:25>)
8992
= note: distinct uses of `impl Trait` result in different opaque types
9093
help: consider `await`ing on both `Future`s
9194
|

0 commit comments

Comments
 (0)