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

+1-1
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

+3-3
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

+2-1
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

+10-15
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

+1-1
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

+2-2
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

+4-4
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

+4-4
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

+1-1
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

+1-1
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)

src/test/ui/async-await/issues/issue-65436-raw-ptr-not-send.no_drop_tracking.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | | bar(Foo(std::ptr::null())).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 `*const u8`
11+
= help: within `[async block@$DIR/issue-65436-raw-ptr-not-send.rs:16:17: 19:6]`, the trait `Send` is not implemented for `*const u8`
1212
note: future is not `Send` as this value is used across an await
1313
--> $DIR/issue-65436-raw-ptr-not-send.rs:18:35
1414
|

src/test/ui/chalkify/bugs/async.stderr

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0277]: `impl Future<Output = u32>` is not a future
1+
error[E0277]: `[async fn body@$DIR/async.rs:7:29: 9:2]` is not a future
22
--> $DIR/async.rs:7:29
33
|
44
LL | async fn foo(x: u32) -> u32 {
@@ -7,18 +7,18 @@ LL | | x
77
LL | | }
88
| | ^
99
| | |
10-
| |_`impl Future<Output = u32>` is not a future
10+
| |_`[async fn body@$DIR/async.rs:7:29: 9:2]` is not a future
1111
| required by a bound introduced by this call
1212
|
13-
= help: the trait `Future` is not implemented for `impl Future<Output = u32>`
14-
= note: impl Future<Output = u32> must be a future or must implement `IntoFuture` to be awaited
13+
= help: the trait `Future` is not implemented for `[async fn body@$DIR/async.rs:7:29: 9:2]`
14+
= note: [async fn body@$DIR/async.rs:7:29: 9:2] must be a future or must implement `IntoFuture` to be awaited
1515
note: required by a bound in `identity_future`
1616
--> $SRC_DIR/core/src/future/mod.rs:LL:COL
1717
|
1818
LL | pub const fn identity_future<O, Fut: Future<Output = O>>(f: Fut) -> Fut {
1919
| ^^^^^^^^^^^^^^^^^^ required by this bound in `identity_future`
2020

21-
error[E0277]: the size for values of type `<impl Future<Output = u32> as Future>::Output` cannot be known at compilation time
21+
error[E0277]: the size for values of type `<[async fn body@$DIR/async.rs:7:29: 9:2] as Future>::Output` cannot be known at compilation time
2222
--> $DIR/async.rs:7:29
2323
|
2424
LL | async fn foo(x: u32) -> u32 {
@@ -27,23 +27,23 @@ LL | | x
2727
LL | | }
2828
| |_^ doesn't have a size known at compile-time
2929
|
30-
= help: the trait `Sized` is not implemented for `<impl Future<Output = u32> as Future>::Output`
30+
= help: the trait `Sized` is not implemented for `<[async fn body@$DIR/async.rs:7:29: 9:2] as Future>::Output`
3131
note: required by a bound in `identity_future`
3232
--> $SRC_DIR/core/src/future/mod.rs:LL:COL
3333
|
3434
LL | pub const fn identity_future<O, Fut: Future<Output = O>>(f: Fut) -> Fut {
3535
| ^ required by this bound in `identity_future`
3636

37-
error[E0277]: `impl Future<Output = u32>` is not a future
37+
error[E0277]: `[async fn body@$DIR/async.rs:7:29: 9:2]` is not a future
3838
--> $DIR/async.rs:7:25
3939
|
4040
LL | async fn foo(x: u32) -> u32 {
41-
| ^^^ `impl Future<Output = u32>` is not a future
41+
| ^^^ `[async fn body@$DIR/async.rs:7:29: 9:2]` is not a future
4242
|
43-
= help: the trait `Future` is not implemented for `impl Future<Output = u32>`
44-
= note: impl Future<Output = u32> must be a future or must implement `IntoFuture` to be awaited
43+
= help: the trait `Future` is not implemented for `[async fn body@$DIR/async.rs:7:29: 9:2]`
44+
= note: [async fn body@$DIR/async.rs:7:29: 9:2] must be a future or must implement `IntoFuture` to be awaited
4545

46-
error[E0280]: the requirement `<impl Future<Output = u32> as Future>::Output == u32` is not satisfied
46+
error[E0280]: the requirement `<[async fn body@$DIR/async.rs:7:29: 9:2] as Future>::Output == u32` is not satisfied
4747
--> $DIR/async.rs:7:25
4848
|
4949
LL | async fn foo(x: u32) -> u32 {

src/test/ui/generator/clone-impl-async.rs

+13-14
Original file line numberDiff line numberDiff line change
@@ -15,42 +15,42 @@ fn main() {
1515
drop(non_clone);
1616
};
1717
check_copy(&inner_non_clone);
18-
//~^ ERROR the trait bound `impl Future<Output = ()>: Copy` is not satisfied
18+
//~^ ERROR : Copy` is not satisfied
1919
check_clone(&inner_non_clone);
20-
//~^ ERROR the trait bound `impl Future<Output = ()>: Clone` is not satisfied
20+
//~^ ERROR : Clone` is not satisfied
2121

2222
let non_clone = NonClone;
2323
let outer_non_clone = async move {
2424
drop(non_clone);
2525
};
2626
check_copy(&outer_non_clone);
27-
//~^ ERROR the trait bound `impl Future<Output = ()>: Copy` is not satisfied
27+
//~^ ERROR : Copy` is not satisfied
2828
check_clone(&outer_non_clone);
29-
//~^ ERROR the trait bound `impl Future<Output = ()>: Clone` is not satisfied
29+
//~^ ERROR : Clone` is not satisfied
3030

3131
let maybe_copy_clone = async move {};
3232
check_copy(&maybe_copy_clone);
33-
//~^ ERROR the trait bound `impl Future<Output = ()>: Copy` is not satisfied
33+
//~^ ERROR : Copy` is not satisfied
3434
check_clone(&maybe_copy_clone);
35-
//~^ ERROR the trait bound `impl Future<Output = ()>: Clone` is not satisfied
35+
//~^ ERROR : Clone` is not satisfied
3636

3737
let inner_non_clone_fn = the_inner_non_clone_fn();
3838
check_copy(&inner_non_clone_fn);
39-
//~^ ERROR the trait bound `impl Future<Output = ()>: Copy` is not satisfied
39+
//~^ ERROR : Copy` is not satisfied
4040
check_clone(&inner_non_clone_fn);
41-
//~^ ERROR the trait bound `impl Future<Output = ()>: Clone` is not satisfied
41+
//~^ ERROR : Clone` is not satisfied
4242

4343
let outer_non_clone_fn = the_outer_non_clone_fn(NonClone);
4444
check_copy(&outer_non_clone_fn);
45-
//~^ ERROR the trait bound `impl Future<Output = ()>: Copy` is not satisfied
45+
//~^ ERROR : Copy` is not satisfied
4646
check_clone(&outer_non_clone_fn);
47-
//~^ ERROR the trait bound `impl Future<Output = ()>: Clone` is not satisfied
47+
//~^ ERROR : Clone` is not satisfied
4848

4949
let maybe_copy_clone_fn = the_maybe_copy_clone_fn();
5050
check_copy(&maybe_copy_clone_fn);
51-
//~^ ERROR the trait bound `impl Future<Output = ()>: Copy` is not satisfied
51+
//~^ ERROR : Copy` is not satisfied
5252
check_clone(&maybe_copy_clone_fn);
53-
//~^ ERROR the trait bound `impl Future<Output = ()>: Clone` is not satisfied
53+
//~^ ERROR : Clone` is not satisfied
5454
}
5555

5656
async fn the_inner_non_clone_fn() {
@@ -64,8 +64,7 @@ async fn the_outer_non_clone_fn(non_clone: NonClone) {
6464
drop(non_clone);
6565
}
6666

67-
async fn the_maybe_copy_clone_fn() {
68-
}
67+
async fn the_maybe_copy_clone_fn() {}
6968

7069
fn check_copy<T: Copy>(_x: &T) {}
7170
fn check_clone<T: Clone>(_x: &T) {}

0 commit comments

Comments
 (0)