Skip to content

Commit c28d86c

Browse files
committed
name async generators something more human friendly in type error diagnostics
1 parent b05fd2a commit c28d86c

File tree

8 files changed

+103
-17
lines changed

8 files changed

+103
-17
lines changed

compiler/rustc_hir/src/hir.rs

+21-2
Original file line numberDiff line numberDiff line change
@@ -1274,7 +1274,7 @@ impl Body<'hir> {
12741274
}
12751275

12761276
/// The type of source expression that caused this generator to be created.
1277-
#[derive(Clone, PartialEq, Eq, HashStable_Generic, Encodable, Decodable, Debug, Copy)]
1277+
#[derive(Clone, PartialEq, Eq, Hash, HashStable_Generic, Encodable, Decodable, Debug, Copy)]
12781278
pub enum GeneratorKind {
12791279
/// An explicit `async` block or the body of an async function.
12801280
Async(AsyncGeneratorKind),
@@ -1292,12 +1292,21 @@ impl fmt::Display for GeneratorKind {
12921292
}
12931293
}
12941294

1295+
impl GeneratorKind {
1296+
pub fn descr(&self) -> &'static str {
1297+
match self {
1298+
GeneratorKind::Async(ask) => ask.descr(),
1299+
GeneratorKind::Gen => "generator",
1300+
}
1301+
}
1302+
}
1303+
12951304
/// In the case of a generator created as part of an async construct,
12961305
/// which kind of async construct caused it to be created?
12971306
///
12981307
/// This helps error messages but is also used to drive coercions in
12991308
/// type-checking (see #60424).
1300-
#[derive(Clone, PartialEq, Eq, HashStable_Generic, Encodable, Decodable, Debug, Copy)]
1309+
#[derive(Clone, PartialEq, Eq, Hash, HashStable_Generic, Encodable, Decodable, Debug, Copy)]
13011310
pub enum AsyncGeneratorKind {
13021311
/// An explicit `async` block written by the user.
13031312
Block,
@@ -1319,6 +1328,16 @@ impl fmt::Display for AsyncGeneratorKind {
13191328
}
13201329
}
13211330

1331+
impl AsyncGeneratorKind {
1332+
pub fn descr(&self) -> &'static str {
1333+
match self {
1334+
AsyncGeneratorKind::Block => "`async` block",
1335+
AsyncGeneratorKind::Closure => "`async` closure body",
1336+
AsyncGeneratorKind::Fn => "`async fn` body",
1337+
}
1338+
}
1339+
}
1340+
13221341
#[derive(Copy, Clone, Debug)]
13231342
pub enum BodyOwnerKind {
13241343
/// Functions and methods.

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

+9-7
Original file line numberDiff line numberDiff line change
@@ -1509,7 +1509,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
15091509

15101510
impl<'tcx> ty::fold::TypeVisitor<'tcx> for OpaqueTypesVisitor<'tcx> {
15111511
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
1512-
if let Some((kind, def_id)) = TyCategory::from_ty(t) {
1512+
if let Some((kind, def_id)) = TyCategory::from_ty(self.tcx, t) {
15131513
let span = self.tcx.def_span(def_id);
15141514
// Avoid cluttering the output when the "found" and error span overlap:
15151515
//
@@ -1582,11 +1582,11 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
15821582
};
15831583
if let Some((expected, found)) = expected_found {
15841584
let expected_label = match exp_found {
1585-
Mismatch::Variable(ef) => ef.expected.prefix_string(),
1585+
Mismatch::Variable(ef) => ef.expected.prefix_string(self.tcx),
15861586
Mismatch::Fixed(s) => s.into(),
15871587
};
15881588
let found_label = match exp_found {
1589-
Mismatch::Variable(ef) => ef.found.prefix_string(),
1589+
Mismatch::Variable(ef) => ef.found.prefix_string(self.tcx),
15901590
Mismatch::Fixed(s) => s.into(),
15911591
};
15921592
let exp_found = match exp_found {
@@ -2436,7 +2436,7 @@ impl<'tcx> ObligationCauseExt<'tcx> for ObligationCause<'tcx> {
24362436
pub enum TyCategory {
24372437
Closure,
24382438
Opaque,
2439-
Generator,
2439+
Generator(hir::GeneratorKind),
24402440
Foreign,
24412441
}
24422442

@@ -2445,16 +2445,18 @@ impl TyCategory {
24452445
match self {
24462446
Self::Closure => "closure",
24472447
Self::Opaque => "opaque type",
2448-
Self::Generator => "generator",
2448+
Self::Generator(gk) => gk.descr(),
24492449
Self::Foreign => "foreign type",
24502450
}
24512451
}
24522452

2453-
pub fn from_ty(ty: Ty<'_>) -> Option<(Self, DefId)> {
2453+
pub fn from_ty(tcx: TyCtxt<'_>, ty: Ty<'_>) -> Option<(Self, DefId)> {
24542454
match *ty.kind() {
24552455
ty::Closure(def_id, _) => Some((Self::Closure, def_id)),
24562456
ty::Opaque(def_id, _) => Some((Self::Opaque, def_id)),
2457-
ty::Generator(def_id, ..) => Some((Self::Generator, def_id)),
2457+
ty::Generator(def_id, ..) => {
2458+
Some((Self::Generator(tcx.generator_kind(def_id).unwrap()), def_id))
2459+
}
24582460
ty::Foreign(def_id) => Some((Self::Foreign, def_id)),
24592461
_ => None,
24602462
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
383383
InferenceDiagnosticsData {
384384
name: s,
385385
span: None,
386-
kind: UnderspecifiedArgKind::Type { prefix: ty.prefix_string() },
386+
kind: UnderspecifiedArgKind::Type { prefix: ty.prefix_string(self.tcx) },
387387
parent: None,
388388
}
389389
}

compiler/rustc_middle/src/ty/error.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ impl<'tcx> ty::TyS<'tcx> {
264264
}
265265
}
266266
ty::Closure(..) => "closure".into(),
267-
ty::Generator(..) => "generator".into(),
267+
ty::Generator(def_id, ..) => tcx.generator_kind(def_id).unwrap().descr().into(),
268268
ty::GeneratorWitness(..) => "generator witness".into(),
269269
ty::Tuple(..) => "tuple".into(),
270270
ty::Infer(ty::TyVar(_)) => "inferred type".into(),
@@ -282,7 +282,7 @@ impl<'tcx> ty::TyS<'tcx> {
282282
}
283283
}
284284

285-
pub fn prefix_string(&self) -> Cow<'static, str> {
285+
pub fn prefix_string(&self, tcx: TyCtxt<'_>) -> Cow<'static, str> {
286286
match *self.kind() {
287287
ty::Infer(_)
288288
| ty::Error(_)
@@ -308,7 +308,7 @@ impl<'tcx> ty::TyS<'tcx> {
308308
ty::FnPtr(_) => "fn pointer".into(),
309309
ty::Dynamic(..) => "trait object".into(),
310310
ty::Closure(..) => "closure".into(),
311-
ty::Generator(..) => "generator".into(),
311+
ty::Generator(def_id, ..) => tcx.generator_kind(def_id).unwrap().descr().into(),
312312
ty::GeneratorWitness(..) => "generator witness".into(),
313313
ty::Tuple(..) => "tuple".into(),
314314
ty::Placeholder(..) => "higher-ranked type".into(),

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1366,8 +1366,8 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
13661366
Some(t) => Some(t),
13671367
None => {
13681368
let ty = parent_trait_ref.skip_binder().self_ty();
1369-
let span =
1370-
TyCategory::from_ty(ty).map(|(_, def_id)| self.tcx.def_span(def_id));
1369+
let span = TyCategory::from_ty(self.tcx, ty)
1370+
.map(|(_, def_id)| self.tcx.def_span(def_id));
13711371
Some((ty.to_string(), span))
13721372
}
13731373
}

compiler/rustc_typeck/src/check/method/suggest.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
390390
"no {} named `{}` found for {} `{}` in the current scope",
391391
item_kind,
392392
item_name,
393-
actual.prefix_string(),
393+
actual.prefix_string(self.tcx),
394394
ty_str,
395395
);
396396
if let Mode::MethodCall = mode {
@@ -728,7 +728,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
728728
.map(|(_, path)| path)
729729
.collect::<Vec<_>>()
730730
.join("\n");
731-
let actual_prefix = actual.prefix_string();
731+
let actual_prefix = actual.prefix_string(self.tcx);
732732
err.set_primary_message(&format!(
733733
"the {item_kind} `{item_name}` exists for {actual_prefix} `{ty_str}`, but its trait bounds were not satisfied"
734734
));
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// edition:2018
2+
#![feature(async_closure)]
3+
use std::future::Future;
4+
5+
async fn one() {}
6+
async fn two() {}
7+
8+
fn fun<F: Future<Output = ()>>(f1: F, f2: F) {}
9+
fn main() {
10+
fun(async {}, async {});
11+
//~^ ERROR mismatched types
12+
fun(one(), two());
13+
//~^ ERROR mismatched types
14+
fun((async || {})(), (async || {})());
15+
//~^ ERROR mismatched types
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/generator-desc.rs:10:25
3+
|
4+
LL | fun(async {}, async {});
5+
| -- ^^ expected `async` block, found a different `async` block
6+
| |
7+
| the expected `async` block
8+
|
9+
= note: expected `async` block `[static generator@$DIR/generator-desc.rs:10:15: 10:17]`
10+
found `async` block `[static generator@$DIR/generator-desc.rs:10:25: 10:27]`
11+
12+
error[E0308]: mismatched types
13+
--> $DIR/generator-desc.rs:12:16
14+
|
15+
LL | async fn one() {}
16+
| - the `Output` of this `async fn`'s expected opaque type
17+
LL | async fn two() {}
18+
| - the `Output` of this `async fn`'s found opaque type
19+
...
20+
LL | fun(one(), two());
21+
| ^^^^^ expected opaque type, found a different opaque type
22+
|
23+
= note: expected opaque type `impl Future` (opaque type at <$DIR/generator-desc.rs:5:16>)
24+
found opaque type `impl Future` (opaque type at <$DIR/generator-desc.rs:6:16>)
25+
= help: consider `await`ing on both `Future`s
26+
= note: distinct uses of `impl Trait` result in different opaque types
27+
28+
error[E0308]: mismatched types
29+
--> $DIR/generator-desc.rs:14:26
30+
|
31+
LL | fun((async || {})(), (async || {})());
32+
| -- ^^^^^^^^^^^^^^^ expected `async` closure body, found a different `async` closure body
33+
| |
34+
| the expected `async` closure body
35+
|
36+
::: $SRC_DIR/core/src/future/mod.rs:LL:COL
37+
|
38+
LL | pub const fn from_generator<T>(gen: T) -> impl Future<Output = T::Return>
39+
| -------------------------------
40+
| |
41+
| the expected opaque type
42+
| the found opaque type
43+
|
44+
= note: expected opaque type `impl Future` (`async` closure body)
45+
found opaque type `impl Future` (`async` closure body)
46+
47+
error: aborting due to 3 previous errors
48+
49+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)