Skip to content

fix: Reject async assoc fns of const traits/impls in ast_passes #144907

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions compiler/rustc_ast_passes/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ ast_passes_assoc_type_without_body =
associated type in `impl` without body
.suggestion = provide a definition for the type
ast_passes_async_fn_in_const_trait_or_trait_impl =
async functions are not allowed in `const` {$in_impl ->
[true] trait impls
*[false] traits
}
.label = associated functions of `const` cannot be declared `async`
ast_passes_at_least_one_trait = at least one trait must be specified
ast_passes_auto_generic = auto traits cannot have generic parameters
Expand Down
16 changes: 16 additions & 0 deletions compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,21 @@ impl<'a> AstValidator<'a> {
});
}

fn check_async_fn_in_const_trait_or_impl(&self, sig: &FnSig, parent: &TraitOrTraitImpl) {
let Some(const_keyword) = parent.constness() else { return };

let Some(CoroutineKind::Async { span: async_keyword, .. }) = sig.header.coroutine_kind
else {
return;
};

self.dcx().emit_err(errors::AsyncFnInConstTraitOrTraitImpl {
async_keyword,
in_impl: matches!(parent, TraitOrTraitImpl::TraitImpl { .. }),
const_keyword,
});
}

fn check_fn_decl(&self, fn_decl: &FnDecl, self_semantic: SelfSemantic) {
self.check_decl_num_args(fn_decl);
self.check_decl_cvariadic_pos(fn_decl);
Expand Down Expand Up @@ -1566,6 +1581,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
self.visibility_not_permitted(&item.vis, errors::VisibilityNotPermittedNote::TraitImpl);
if let AssocItemKind::Fn(box Fn { sig, .. }) = &item.kind {
self.check_trait_fn_not_const(sig.header.constness, parent);
self.check_async_fn_in_const_trait_or_impl(sig, parent);
}
}

Expand Down
10 changes: 10 additions & 0 deletions compiler/rustc_ast_passes/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ pub(crate) struct TraitFnConst {
pub make_trait_const_sugg: Option<Span>,
}

#[derive(Diagnostic)]
#[diag(ast_passes_async_fn_in_const_trait_or_trait_impl)]
pub(crate) struct AsyncFnInConstTraitOrTraitImpl {
#[primary_span]
pub async_keyword: Span,
pub in_impl: bool,
#[label]
pub const_keyword: Span,
}

#[derive(Diagnostic)]
#[diag(ast_passes_forbidden_bound)]
pub(crate) struct ForbiddenBound {
Expand Down
10 changes: 0 additions & 10 deletions tests/crashes/117629.rs

This file was deleted.

18 changes: 18 additions & 0 deletions tests/ui/traits/const-traits/const-trait-async-assoc-fn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//@ edition: 2021
#![feature(const_trait_impl)]

const trait Tr {
async fn ft1() {}
//~^ ERROR async functions are not allowed in `const` traits
}

const trait Tr2 {
fn f() -> impl std::future::Future<Output = ()>;
}

impl const Tr2 for () {
async fn f() {}
//~^ ERROR async functions are not allowed in `const` trait impls
}

fn main() {}
18 changes: 18 additions & 0 deletions tests/ui/traits/const-traits/const-trait-async-assoc-fn.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error: async functions are not allowed in `const` traits
--> $DIR/const-trait-async-assoc-fn.rs:5:5
|
LL | const trait Tr {
| ----- associated functions of `const` cannot be declared `async`
LL | async fn ft1() {}
| ^^^^^

error: async functions are not allowed in `const` trait impls
--> $DIR/const-trait-async-assoc-fn.rs:14:5
|
LL | impl const Tr2 for () {
| ----- associated functions of `const` cannot be declared `async`
LL | async fn f() {}
| ^^^^^

error: aborting due to 2 previous errors

Loading