Skip to content

Commit e5b5745

Browse files
authored
Rollup merge of #84168 - cjgillot:asi, r=davidtwco
Lower async fn in traits. An error is already created by AST validation. Fixes #84149
2 parents 3b81ea8 + a54d575 commit e5b5745

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

compiler/rustc_ast_lowering/src/item.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -836,9 +836,17 @@ impl<'hir> LoweringContext<'_, 'hir> {
836836
(generics, hir::TraitItemKind::Fn(sig, hir::TraitFn::Required(names)))
837837
}
838838
AssocItemKind::Fn(box FnKind(_, ref sig, ref generics, Some(ref body))) => {
839-
let body_id = self.lower_fn_body_block(i.span, &sig.decl, Some(body));
840-
let (generics, sig) =
841-
self.lower_method_sig(generics, sig, trait_item_def_id, false, None, i.id);
839+
let asyncness = sig.header.asyncness;
840+
let body_id =
841+
self.lower_maybe_async_body(i.span, &sig.decl, asyncness, Some(&body));
842+
let (generics, sig) = self.lower_method_sig(
843+
generics,
844+
sig,
845+
trait_item_def_id,
846+
false,
847+
asyncness.opt_return_id(),
848+
i.id,
849+
);
842850
(generics, hir::TraitItemKind::Fn(sig, hir::TraitFn::Provided(body_id)))
843851
}
844852
AssocItemKind::TyAlias(box TyAliasKind(_, ref generics, ref bounds, ref default)) => {

src/test/ui/async-await/async-trait-fn.rs

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
trait T {
33
async fn foo() {} //~ ERROR functions in traits cannot be declared `async`
44
async fn bar(&self) {} //~ ERROR functions in traits cannot be declared `async`
5+
async fn baz() { //~ ERROR functions in traits cannot be declared `async`
6+
// Nested item must not ICE.
7+
fn a() {}
8+
}
59
}
610

711
fn main() {}

src/test/ui/async-await/async-trait-fn.stderr

+17-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,22 @@ LL | async fn bar(&self) {}
2020
= note: `async` trait functions are not currently supported
2121
= note: consider using the `async-trait` crate: https://crates.io/crates/async-trait
2222

23-
error: aborting due to 2 previous errors
23+
error[E0706]: functions in traits cannot be declared `async`
24+
--> $DIR/async-trait-fn.rs:5:5
25+
|
26+
LL | async fn baz() {
27+
| ^----
28+
| |
29+
| _____`async` because of this
30+
| |
31+
LL | | // Nested item must not ICE.
32+
LL | | fn a() {}
33+
LL | | }
34+
| |_____^
35+
|
36+
= note: `async` trait functions are not currently supported
37+
= note: consider using the `async-trait` crate: https://crates.io/crates/async-trait
38+
39+
error: aborting due to 3 previous errors
2440

2541
For more information about this error, try `rustc --explain E0706`.

0 commit comments

Comments
 (0)