Skip to content

Commit c700d01

Browse files
committed
fix: Reject async assoc fns of #[const_trait] in ast_passes
1 parent e1b9081 commit c700d01

File tree

5 files changed

+40
-3
lines changed

5 files changed

+40
-3
lines changed

compiler/rustc_ast_passes/messages.ftl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ ast_passes_assoc_type_without_body =
3232
associated type in `impl` without body
3333
.suggestion = provide a definition for the type
3434
35+
ast_passes_async_fn_in_const_trait =
36+
async functions are not allowed in `const` traits
37+
.label = associated functions of `const` cannot be declared `async`
38+
3539
ast_passes_at_least_one_trait = at least one trait must be specified
3640
3741
ast_passes_auto_generic = auto traits cannot have generic parameters

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,20 @@ impl<'a> AstValidator<'a> {
294294
});
295295
}
296296

297+
fn check_async_fn_in_const_trait(&self, sig: &FnSig, parent: &TraitOrTraitImpl) {
298+
let TraitOrTraitImpl::Trait { constness: Const::Yes(const_trait), .. } = parent else {
299+
return;
300+
};
301+
302+
let Some(CoroutineKind::Async { span: async_keyword, .. }) = sig.header.coroutine_kind
303+
else {
304+
return;
305+
};
306+
307+
self.dcx()
308+
.emit_err(errors::AsyncFnInConstTrait { async_keyword, const_trait: *const_trait });
309+
}
310+
297311
fn check_fn_decl(&self, fn_decl: &FnDecl, self_semantic: SelfSemantic) {
298312
self.check_decl_num_args(fn_decl);
299313
self.check_decl_cvariadic_pos(fn_decl);
@@ -1586,6 +1600,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
15861600
self.visibility_not_permitted(&item.vis, errors::VisibilityNotPermittedNote::TraitImpl);
15871601
if let AssocItemKind::Fn(box Fn { sig, .. }) = &item.kind {
15881602
self.check_trait_fn_not_const(sig.header.constness, parent);
1603+
self.check_async_fn_in_const_trait(sig, parent);
15891604
}
15901605
}
15911606

compiler/rustc_ast_passes/src/errors.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ pub(crate) struct TraitFnConst {
6262
pub make_trait_const_sugg: Option<Span>,
6363
}
6464

65+
#[derive(Diagnostic)]
66+
#[diag(ast_passes_async_fn_in_const_trait)]
67+
pub(crate) struct AsyncFnInConstTrait {
68+
#[primary_span]
69+
pub async_keyword: Span,
70+
#[label]
71+
pub const_trait: Span,
72+
}
73+
6574
#[derive(Diagnostic)]
6675
#[diag(ast_passes_forbidden_bound)]
6776
pub(crate) struct ForbiddenBound {
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
//@ known-bug: #117629
2-
//@ edition:2021
3-
1+
//@ edition: 2021
42
#![feature(const_trait_impl)]
53

64
const trait Tr {
75
async fn ft1() {}
6+
//~^ ERROR async functions are not allowed in `const` traits
87
}
98

109
fn main() {}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: async functions are not allowed in `const` traits
2+
--> $DIR/const-trait-async-assoc-fn.rs:5:5
3+
|
4+
LL | const trait Tr {
5+
| ----- associated functions of `const` cannot be declared `async`
6+
LL | async fn ft1() {}
7+
| ^^^^^
8+
9+
error: aborting due to 1 previous error
10+

0 commit comments

Comments
 (0)