Skip to content

Commit b9b0b6a

Browse files
authored
Unrolled build for #144907
Rollup merge of #144907 - ShoyuVanilla:no-const-async, r=fmease fix: Reject async assoc fns of const traits/impls in ast_passes Fixes #117629
2 parents cd7cbe8 + 2218ff1 commit b9b0b6a

File tree

6 files changed

+69
-10
lines changed

6 files changed

+69
-10
lines changed

compiler/rustc_ast_passes/messages.ftl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ 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_or_trait_impl =
36+
async functions are not allowed in `const` {$in_impl ->
37+
[true] trait impls
38+
*[false] traits
39+
}
40+
.label = associated functions of `const` cannot be declared `async`
41+
3542
ast_passes_at_least_one_trait = at least one trait must be specified
3643
3744
ast_passes_auto_generic = auto traits cannot have generic parameters

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,21 @@ impl<'a> AstValidator<'a> {
293293
});
294294
}
295295

296+
fn check_async_fn_in_const_trait_or_impl(&self, sig: &FnSig, parent: &TraitOrTraitImpl) {
297+
let Some(const_keyword) = parent.constness() else { return };
298+
299+
let Some(CoroutineKind::Async { span: async_keyword, .. }) = sig.header.coroutine_kind
300+
else {
301+
return;
302+
};
303+
304+
self.dcx().emit_err(errors::AsyncFnInConstTraitOrTraitImpl {
305+
async_keyword,
306+
in_impl: matches!(parent, TraitOrTraitImpl::TraitImpl { .. }),
307+
const_keyword,
308+
});
309+
}
310+
296311
fn check_fn_decl(&self, fn_decl: &FnDecl, self_semantic: SelfSemantic) {
297312
self.check_decl_num_args(fn_decl);
298313
self.check_decl_cvariadic_pos(fn_decl);
@@ -1578,6 +1593,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
15781593
self.visibility_not_permitted(&item.vis, errors::VisibilityNotPermittedNote::TraitImpl);
15791594
if let AssocItemKind::Fn(box Fn { sig, .. }) = &item.kind {
15801595
self.check_trait_fn_not_const(sig.header.constness, parent);
1596+
self.check_async_fn_in_const_trait_or_impl(sig, parent);
15811597
}
15821598
}
15831599

compiler/rustc_ast_passes/src/errors.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ 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_or_trait_impl)]
67+
pub(crate) struct AsyncFnInConstTraitOrTraitImpl {
68+
#[primary_span]
69+
pub async_keyword: Span,
70+
pub in_impl: bool,
71+
#[label]
72+
pub const_keyword: Span,
73+
}
74+
6575
#[derive(Diagnostic)]
6676
#[diag(ast_passes_forbidden_bound)]
6777
pub(crate) struct ForbiddenBound {

tests/crashes/117629.rs

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//@ edition: 2021
2+
#![feature(const_trait_impl)]
3+
4+
const trait Tr {
5+
async fn ft1() {}
6+
//~^ ERROR async functions are not allowed in `const` traits
7+
}
8+
9+
const trait Tr2 {
10+
fn f() -> impl std::future::Future<Output = ()>;
11+
}
12+
13+
impl const Tr2 for () {
14+
async fn f() {}
15+
//~^ ERROR async functions are not allowed in `const` trait impls
16+
}
17+
18+
fn main() {}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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: async functions are not allowed in `const` trait impls
10+
--> $DIR/const-trait-async-assoc-fn.rs:14:5
11+
|
12+
LL | impl const Tr2 for () {
13+
| ----- associated functions of `const` cannot be declared `async`
14+
LL | async fn f() {}
15+
| ^^^^^
16+
17+
error: aborting due to 2 previous errors
18+

0 commit comments

Comments
 (0)