Skip to content

fix: Reject async assoc fns of #[const_trait] 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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions compiler/rustc_ast_passes/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ 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 =
async functions are not allowed in `#[const_trait]`
.label = associated functions of `#[const_trait]` 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
15 changes: 15 additions & 0 deletions compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,20 @@ impl<'a> AstValidator<'a> {
});
}

fn check_async_fn_in_const_trait(&self, sig: &FnSig, parent: &TraitOrTraitImpl) {
let TraitOrTraitImpl::Trait { constness: Const::Yes(const_trait), .. } = parent else {
return;
};

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

self.dcx()
.emit_err(errors::AsyncFnInConstTrait { async_keyword, const_trait: *const_trait });
}

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 @@ -1586,6 +1600,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(sig, parent);
}
}

Expand Down
9 changes: 9 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,15 @@ pub(crate) struct TraitFnConst {
pub make_trait_const_sugg: Option<Span>,
}

#[derive(Diagnostic)]
#[diag(ast_passes_async_fn_in_const_trait)]
pub(crate) struct AsyncFnInConstTrait {
#[primary_span]
pub async_keyword: Span,
#[label]
pub const_trait: 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.

10 changes: 10 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,10 @@
//@ edition: 2021
#![feature(const_trait_impl)]

#[const_trait]
trait Tr {
async fn ft1() {}
//~^ ERROR async functions are not allowed in `#[const_trait]`
}

fn main() {}
11 changes: 11 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,11 @@
error: async functions are not allowed in `#[const_trait]`
--> $DIR/const-trait-async-assoc-fn.rs:6:5
|
LL | #[const_trait]
| -------------- associated functions of `#[const_trait]` cannot be declared async
LL | trait Tr {
LL | async fn ft1() {}
| ^^^^^

error: aborting due to 1 previous error

Loading