mirror of
https://github.com/rust-lang/rust.git
synced 2026-04-27 18:57:42 +03:00
Reject async fn in const impl during AST validation
This commit is contained in:
@@ -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`
|
||||
|
||||
|
||||
@@ -312,9 +312,15 @@ fn check_async_fn_in_const_trait_or_impl(&self, sig: &FnSig, parent: &TraitOrImp
|
||||
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 @@ fn visit_assoc_item(&mut self, item: &'a AssocItem, ctxt: AssocCtxt) {
|
||||
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 => {}
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
|
||||
@@ -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() {}
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user