Rollup merge of #147710 - chenyukang:yukang-fix-ice-contracts-async, r=jackh726

Fix ICE when using contracts on async functions

Fixes rust-lang/rust#145333

contract is not supported for async functions right now, it's not properly lowered and getting HirId will ICE.

This PR adds checking for async function in expanding AST phase, it's better until we want to fully support async for contracts feature.
This commit is contained in:
Matthias Krüger
2025-10-15 23:41:04 +02:00
committed by GitHub
3 changed files with 35 additions and 0 deletions

View File

@@ -71,6 +71,14 @@ fn expand_contract_clause(
.span_err(attr_span, "contract annotations can only be used on functions")); .span_err(attr_span, "contract annotations can only be used on functions"));
} }
// Contracts are not yet supported on async/gen functions
if new_tts.iter().any(|tt| is_kw(tt, kw::Async) || is_kw(tt, kw::Gen)) {
return Err(ecx.sess.dcx().span_err(
attr_span,
"contract annotations are not yet supported on async or gen functions",
));
}
// Found the `fn` keyword, now find either the `where` token or the function body. // Found the `fn` keyword, now find either the `where` token or the function body.
let next_tt = loop { let next_tt = loop {
let Some(tt) = cursor.next() else { let Some(tt) = cursor.next() else {

View File

@@ -0,0 +1,10 @@
//@ compile-flags: --crate-type=lib
//@ edition: 2021
#![feature(contracts)]
//~^ WARN the feature `contracts` is incomplete
#[core::contracts::ensures(|ret| *ret)]
//~^ ERROR contract annotations are not yet supported on async or gen functions
async fn _always_true(b: bool) -> bool {
b
}

View File

@@ -0,0 +1,17 @@
error: contract annotations are not yet supported on async or gen functions
--> $DIR/async-fn-contract-ice-145333.rs:6:1
|
LL | #[core::contracts::ensures(|ret| *ret)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
warning: the feature `contracts` is incomplete and may not be safe to use and/or cause compiler crashes
--> $DIR/async-fn-contract-ice-145333.rs:3:12
|
LL | #![feature(contracts)]
| ^^^^^^^^^
|
= note: see issue #128044 <https://github.com/rust-lang/rust/issues/128044> for more information
= note: `#[warn(incomplete_features)]` on by default
error: aborting due to 1 previous error; 1 warning emitted