Skip to content
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
7 changes: 4 additions & 3 deletions compiler/rustc_ast_passes/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ ast_passes_assoc_type_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
async functions are not allowed in `const` {$context ->
[trait_impl] trait impls
[impl] impls
*[trait] traits
}
.label = associated functions of `const` cannot be declared `async`
Expand Down
11 changes: 9 additions & 2 deletions compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,15 @@ impl<'a> AstValidator<'a> {
return;
};

let context = match parent {
TraitOrImpl::Trait { .. } => "trait",
TraitOrImpl::TraitImpl { .. } => "trait_impl",
TraitOrImpl::Impl { .. } => "impl",
};

self.dcx().emit_err(errors::AsyncFnInConstTraitOrTraitImpl {
async_keyword,
in_impl: matches!(parent, TraitOrImpl::TraitImpl { .. }),
context,
const_keyword,
});
}
Expand Down Expand Up @@ -1714,9 +1720,10 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
self.check_async_fn_in_const_trait_or_impl(sig, parent);
}
}
Some(TraitOrImpl::Impl { constness }) => {
Some(parent @ TraitOrImpl::Impl { constness }) => {
if let AssocItemKind::Fn(box Fn { sig, .. }) = &item.kind {
self.check_impl_fn_not_const(sig.header.constness, *constness);
self.check_async_fn_in_const_trait_or_impl(sig, parent);
}
}
None => {}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_passes/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub(crate) struct TraitFnConst {
pub(crate) struct AsyncFnInConstTraitOrTraitImpl {
#[primary_span]
pub async_keyword: Span,
pub in_impl: bool,
pub context: &'static str,
#[label]
pub const_keyword: Span,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//@ edition:2024

#![feature(const_trait_impl)]
struct Foo;
const impl Foo {
async fn e() {}
//~^ ERROR async functions are not allowed in `const` impls
}
fn main() {}
10 changes: 10 additions & 0 deletions tests/ui/traits/const-traits/ice-149083-async-in-const-impl.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: async functions are not allowed in `const` impls
--> $DIR/ice-149083-async-in-const-impl.rs:6:5
|
LL | const impl Foo {
| ----- associated functions of `const` cannot be declared `async`
LL | async fn e() {}
| ^^^^^

error: aborting due to 1 previous error

Loading