From 0935df7829c466f8ef94248429095ea79dc0fa73 Mon Sep 17 00:00:00 2001 From: yukang Date: Wed, 15 Oct 2025 09:05:09 +0800 Subject: [PATCH] Fix ICE when using contracts on async functions --- compiler/rustc_builtin_macros/src/contracts.rs | 8 ++++++++ .../contracts/async-fn-contract-ice-145333.rs | 10 ++++++++++ .../async-fn-contract-ice-145333.stderr | 17 +++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 tests/ui/contracts/async-fn-contract-ice-145333.rs create mode 100644 tests/ui/contracts/async-fn-contract-ice-145333.stderr diff --git a/compiler/rustc_builtin_macros/src/contracts.rs b/compiler/rustc_builtin_macros/src/contracts.rs index 6a24af361fe78..13c63268e1e87 100644 --- a/compiler/rustc_builtin_macros/src/contracts.rs +++ b/compiler/rustc_builtin_macros/src/contracts.rs @@ -71,6 +71,14 @@ fn expand_contract_clause( .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. let next_tt = loop { let Some(tt) = cursor.next() else { diff --git a/tests/ui/contracts/async-fn-contract-ice-145333.rs b/tests/ui/contracts/async-fn-contract-ice-145333.rs new file mode 100644 index 0000000000000..a6de8a786af96 --- /dev/null +++ b/tests/ui/contracts/async-fn-contract-ice-145333.rs @@ -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 +} diff --git a/tests/ui/contracts/async-fn-contract-ice-145333.stderr b/tests/ui/contracts/async-fn-contract-ice-145333.stderr new file mode 100644 index 0000000000000..77f5379e6fb54 --- /dev/null +++ b/tests/ui/contracts/async-fn-contract-ice-145333.stderr @@ -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 for more information + = note: `#[warn(incomplete_features)]` on by default + +error: aborting due to 1 previous error; 1 warning emitted +