Skip to content

Commit 6e6c42c

Browse files
authored
Rollup merge of #104931 - Swatinem:async-pretty, r=eholk
Pretty-print generators with their `generator_kind` After removing `GenFuture`, I special-cased async generators to pretty-print as `impl Future<Output = X>` mainly to avoid too much diagnostics changes originally. This now reverses that change so that async fn/blocks are pretty-printed as `[$async-type@$source-position]` in various diagnostics, and updates the tests that this touches.
2 parents 9ebffb7 + c96d888 commit 6e6c42c

File tree

20 files changed

+85
-90
lines changed

20 files changed

+85
-90
lines changed

compiler/rustc_const_eval/src/transform/check_consts/ops.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ impl<'tcx> NonConstOp<'tcx> for Generator {
376376
ccx: &ConstCx<'_, 'tcx>,
377377
span: Span,
378378
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
379-
let msg = format!("{}s are not allowed in {}s", self.0, ccx.const_kind());
379+
let msg = format!("{}s are not allowed in {}s", self.0.descr(), ccx.const_kind());
380380
if let hir::GeneratorKind::Async(hir::AsyncGeneratorKind::Block) = self.0 {
381381
ccx.tcx.sess.create_feature_err(
382382
UnallowedOpInConstContext { span, msg },

compiler/rustc_hir/src/hir.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,9 +1514,9 @@ pub enum AsyncGeneratorKind {
15141514
impl fmt::Display for AsyncGeneratorKind {
15151515
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
15161516
f.write_str(match self {
1517-
AsyncGeneratorKind::Block => "`async` block",
1518-
AsyncGeneratorKind::Closure => "`async` closure body",
1519-
AsyncGeneratorKind::Fn => "`async fn` body",
1517+
AsyncGeneratorKind::Block => "async block",
1518+
AsyncGeneratorKind::Closure => "async closure body",
1519+
AsyncGeneratorKind::Fn => "async fn body",
15201520
})
15211521
}
15221522
}

compiler/rustc_hir_typeck/src/generator_interior/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ impl<'a, 'tcx> InteriorVisitor<'a, 'tcx> {
118118
} else {
119119
let note = format!(
120120
"the type is part of the {} because of this {}",
121-
self.kind, yield_data.source
121+
self.kind.descr(),
122+
yield_data.source
122123
);
123124

124125
self.fcx

compiler/rustc_middle/src/ty/print/pretty.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -681,25 +681,20 @@ pub trait PrettyPrinter<'tcx>:
681681
}
682682
ty::Str => p!("str"),
683683
ty::Generator(did, substs, movability) => {
684-
// FIXME(swatinem): async constructs used to be pretty printed
685-
// as `impl Future` previously due to the `from_generator` wrapping.
686-
// lets special case this here for now to avoid churn in diagnostics.
687-
let generator_kind = self.tcx().generator_kind(did);
688-
if matches!(generator_kind, Some(hir::GeneratorKind::Async(..))) {
689-
let return_ty = substs.as_generator().return_ty();
690-
p!(write("impl Future<Output = {}>", return_ty));
691-
692-
return Ok(self);
693-
}
694-
695684
p!(write("["));
696-
match movability {
697-
hir::Movability::Movable => {}
698-
hir::Movability::Static => p!("static "),
685+
let generator_kind = self.tcx().generator_kind(did).unwrap();
686+
let should_print_movability =
687+
self.should_print_verbose() || generator_kind == hir::GeneratorKind::Gen;
688+
689+
if should_print_movability {
690+
match movability {
691+
hir::Movability::Movable => {}
692+
hir::Movability::Static => p!("static "),
693+
}
699694
}
700695

701696
if !self.should_print_verbose() {
702-
p!("generator");
697+
p!(write("{}", generator_kind));
703698
// FIXME(eddyb) should use `def_span`.
704699
if let Some(did) = did.as_local() {
705700
let span = self.tcx().def_span(did);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2673,7 +2673,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
26732673
let sp = self.tcx.def_span(def_id);
26742674

26752675
// Special-case this to say "async block" instead of `[static generator]`.
2676-
let kind = tcx.generator_kind(def_id).unwrap();
2676+
let kind = tcx.generator_kind(def_id).unwrap().descr();
26772677
err.span_note(
26782678
sp,
26792679
&format!("required because it's used within this {}", kind),

src/test/ui/async-await/async-block-control-flow-static-semantics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn return_targets_async_block_not_fn() -> u8 {
1515
return 0u8;
1616
};
1717
let _: &dyn Future<Output = ()> = &block;
18-
//~^ ERROR expected `impl Future<Output = u8>` to be a future that resolves to `()`, but it resolves to `u8`
18+
//~^ ERROR to be a future that resolves to `()`, but it resolves to `u8`
1919
}
2020

2121
async fn return_targets_async_block_not_async_fn() -> u8 {
@@ -24,7 +24,7 @@ async fn return_targets_async_block_not_async_fn() -> u8 {
2424
return 0u8;
2525
};
2626
let _: &dyn Future<Output = ()> = &block;
27-
//~^ ERROR expected `impl Future<Output = u8>` to be a future that resolves to `()`, but it resolves to `u8`
27+
//~^ ERROR to be a future that resolves to `()`, but it resolves to `u8`
2828
}
2929

3030
fn no_break_in_async_block() {

src/test/ui/async-await/async-block-control-flow-static-semantics.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ LL | |
2929
LL | | }
3030
| |_^ expected `u8`, found `()`
3131

32-
error[E0271]: expected `impl Future<Output = u8>` to be a future that resolves to `()`, but it resolves to `u8`
32+
error[E0271]: expected `[async block@$DIR/async-block-control-flow-static-semantics.rs:23:17: 25:6]` to be a future that resolves to `()`, but it resolves to `u8`
3333
--> $DIR/async-block-control-flow-static-semantics.rs:26:39
3434
|
3535
LL | let _: &dyn Future<Output = ()> = &block;
3636
| ^^^^^^ expected `()`, found `u8`
3737
|
38-
= note: required for the cast from `impl Future<Output = u8>` to the object type `dyn Future<Output = ()>`
38+
= note: required for the cast from `[async block@$DIR/async-block-control-flow-static-semantics.rs:23:17: 25:6]` to the object type `dyn Future<Output = ()>`
3939

4040
error[E0308]: mismatched types
4141
--> $DIR/async-block-control-flow-static-semantics.rs:12:43
@@ -45,13 +45,13 @@ LL | fn return_targets_async_block_not_fn() -> u8 {
4545
| |
4646
| implicitly returns `()` as its body has no tail or `return` expression
4747

48-
error[E0271]: expected `impl Future<Output = u8>` to be a future that resolves to `()`, but it resolves to `u8`
48+
error[E0271]: expected `[async block@$DIR/async-block-control-flow-static-semantics.rs:14:17: 16:6]` to be a future that resolves to `()`, but it resolves to `u8`
4949
--> $DIR/async-block-control-flow-static-semantics.rs:17:39
5050
|
5151
LL | let _: &dyn Future<Output = ()> = &block;
5252
| ^^^^^^ expected `()`, found `u8`
5353
|
54-
= note: required for the cast from `impl Future<Output = u8>` to the object type `dyn Future<Output = ()>`
54+
= note: required for the cast from `[async block@$DIR/async-block-control-flow-static-semantics.rs:14:17: 16:6]` to the object type `dyn Future<Output = ()>`
5555

5656
error[E0308]: mismatched types
5757
--> $DIR/async-block-control-flow-static-semantics.rs:49:44

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ LL | fun(async {}, async {});
88
| | arguments to this function are incorrect
99
| the expected `async` block
1010
|
11-
= note: expected `async` block `impl Future<Output = ()>` (`async` block)
12-
found `async` block `impl Future<Output = ()>` (`async` block)
11+
= note: expected `async` block `[async block@$DIR/generator-desc.rs:10:9: 10:17]`
12+
found `async` block `[async block@$DIR/generator-desc.rs:10:19: 10:27]`
1313
note: function defined here
1414
--> $SRC_DIR/core/src/future/mod.rs:LL:COL
1515
|
@@ -53,8 +53,8 @@ LL | fun((async || {})(), (async || {})());
5353
| | the expected `async` closure body
5454
| arguments to this function are incorrect
5555
|
56-
= note: expected `async` closure body `impl Future<Output = ()>` (`async` closure body)
57-
found `async` closure body `impl Future<Output = ()>` (`async` closure body)
56+
= note: expected `async` closure body `[async closure body@$DIR/generator-desc.rs:14:19: 14:21]`
57+
found `async` closure body `[async closure body@$DIR/generator-desc.rs:14:36: 14:38]`
5858
note: function defined here
5959
--> $DIR/generator-desc.rs:8:4
6060
|

src/test/ui/async-await/issue-67252-unnamed-future.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | | AFuture.await;
88
LL | | });
99
| |_____^ future created by async block is not `Send`
1010
|
11-
= help: within `impl Future<Output = ()>`, the trait `Send` is not implemented for `*mut ()`
11+
= help: within `[async block@$DIR/issue-67252-unnamed-future.rs:18:11: 21:6]`, the trait `Send` is not implemented for `*mut ()`
1212
note: future is not `Send` as this value is used across an await
1313
--> $DIR/issue-67252-unnamed-future.rs:20:16
1414
|

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ note: captured value is not `Send` because `&` references cannot be sent unless
1313
|
1414
LL | let x = x;
1515
| ^ has type `&T` which is not `Send`, because `T` is not `Sync`
16-
= note: required for the cast from `impl Future<Output = ()>` to the object type `dyn Future<Output = ()> + Send`
16+
= note: required for the cast from `[async block@$DIR/issue-86507.rs:18:17: 20:18]` to the object type `dyn Future<Output = ()> + Send`
1717
help: consider further restricting this bound
1818
|
1919
LL | fn bar<'me, 'async_trait, T: Send + std::marker::Sync>(x: &'me T)

0 commit comments

Comments
 (0)