Reject async fn in const impl during AST validation

This commit is contained in:
lapla 2025-11-20 21:25:54 +09:00
parent 683dd08db5
commit c108451faf
No known key found for this signature in database
GPG key ID: B39C71D9F127FF9F
5 changed files with 33 additions and 6 deletions

View file

@ -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`

View file

@ -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,
});
}
@ -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 => {}

View file

@ -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,
}

View file

@ -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() {}

View file

@ -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