Skip to content

Commit c2d12a6

Browse files
authored
Unrolled build for #149144
Rollup merge of #149144 - lapla-cogito:ice_149083, r=oli-obk Reject `async fn` in `const impl` during AST validation closes #149083 Fixes the ICE when using `async fn` inside `const impl` blocks by adding AST validation. Currently, inherent `impl`s does not perform any checks to verify whether it contains `async fn` declarations. In this PR, I have modified the `visit_assoc_item` function to call `check_async_fn_in_const_trait_or_impl` within the `TraitOrImpl::Impl` case to handle this requirement. Additionally, this change has introduced three possible contexts for the corresponding error messages, so I have updated to properly distinguish between these different contexts when generating messages. r? oli-obk
2 parents 53732d5 + c108451 commit c2d12a6

File tree

5 files changed

+33
-6
lines changed

5 files changed

+33
-6
lines changed

compiler/rustc_ast_passes/messages.ftl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ ast_passes_assoc_type_without_body =
3737
.suggestion = provide a definition for the type
3838
3939
ast_passes_async_fn_in_const_trait_or_trait_impl =
40-
async functions are not allowed in `const` {$in_impl ->
41-
[true] trait impls
42-
*[false] traits
40+
async functions are not allowed in `const` {$context ->
41+
[trait_impl] trait impls
42+
[impl] impls
43+
*[trait] traits
4344
}
4445
.label = associated functions of `const` cannot be declared `async`
4546

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,15 @@ impl<'a> AstValidator<'a> {
312312
return;
313313
};
314314

315+
let context = match parent {
316+
TraitOrImpl::Trait { .. } => "trait",
317+
TraitOrImpl::TraitImpl { .. } => "trait_impl",
318+
TraitOrImpl::Impl { .. } => "impl",
319+
};
320+
315321
self.dcx().emit_err(errors::AsyncFnInConstTraitOrTraitImpl {
316322
async_keyword,
317-
in_impl: matches!(parent, TraitOrImpl::TraitImpl { .. }),
323+
context,
318324
const_keyword,
319325
});
320326
}
@@ -1714,9 +1720,10 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
17141720
self.check_async_fn_in_const_trait_or_impl(sig, parent);
17151721
}
17161722
}
1717-
Some(TraitOrImpl::Impl { constness }) => {
1723+
Some(parent @ TraitOrImpl::Impl { constness }) => {
17181724
if let AssocItemKind::Fn(box Fn { sig, .. }) = &item.kind {
17191725
self.check_impl_fn_not_const(sig.header.constness, *constness);
1726+
self.check_async_fn_in_const_trait_or_impl(sig, parent);
17201727
}
17211728
}
17221729
None => {}

compiler/rustc_ast_passes/src/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ pub(crate) struct TraitFnConst {
7676
pub(crate) struct AsyncFnInConstTraitOrTraitImpl {
7777
#[primary_span]
7878
pub async_keyword: Span,
79-
pub in_impl: bool,
79+
pub context: &'static str,
8080
#[label]
8181
pub const_keyword: Span,
8282
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//@ edition:2024
2+
3+
#![feature(const_trait_impl)]
4+
struct Foo;
5+
const impl Foo {
6+
async fn e() {}
7+
//~^ ERROR async functions are not allowed in `const` impls
8+
}
9+
fn main() {}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: async functions are not allowed in `const` impls
2+
--> $DIR/ice-149083-async-in-const-impl.rs:6:5
3+
|
4+
LL | const impl Foo {
5+
| ----- associated functions of `const` cannot be declared `async`
6+
LL | async fn e() {}
7+
| ^^^^^
8+
9+
error: aborting due to 1 previous error
10+

0 commit comments

Comments
 (0)