Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion kani-compiler/src/kani_middle/codegen_units.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,12 @@ fn has_const_generic_precondition(tcx: TyCtxt, def: FnDef) -> bool {
return true;
};
budget = remaining;
let body = tcx.optimized_mir(def_id);
// Not `optimized_mir`: rustc's provider refuses a def whose body is a const context, and
// a `#[rustc_comptime]` intrinsic is one while still reporting `is_mir_available`
// (`core::intrinsics::size_of` and eight others), which aborted the whole run.
// `instance_mir` is rustc's own dispatcher between `optimized_mir` and `mir_for_ctfe`.
// See <https://github.com/model-checking/kani/issues/4839>.
let body = tcx.instance_mir(rustc_middle::ty::InstanceKind::Item(def_id));
if body_has_const_param_block(body) {
return true;
}
Expand Down Expand Up @@ -1253,6 +1258,21 @@ fn automatic_harness_partition(
continue;
}

// A `#[rustc_comptime]` function can only be called at compile time: rustc rejects any
// other call (`enforce_context_effects`), so a harness calling it is not a program rustc
// accepts. Check before instantiating, so that generic ones are skipped for this reason
// too, and none reaches the const-block precondition search (#4839).
if matches!(
tcx.constness(rustc_internal::internal(tcx, func.def_id())),
rustc_hir::Constness::Const { always: true }
) {
skipped.insert(
crate::kani_middle::strip_local_crate_prefix(func.name()),
AutoHarnessSkipReason::Comptime,
);
continue;
}

// For generic functions, try to find a monomorphic instantiation whose bounds are
// satisfied; the generated harness verifies the function for that instantiation only,
// and its name (e.g. `foo::<i32>`) reflects that.
Expand Down
4 changes: 3 additions & 1 deletion kani-driver/src/autoharness/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ fn print_autoharness_metadata(metadata: Vec<KaniMetadata>) {
AutoHarnessSkipReason::GenericFn(ref detail) => {
Some(vec![md.crate_name.clone(), func, format!("{reason}: {detail}")])
}
AutoHarnessSkipReason::NoBody | AutoHarnessSkipReason::UserFilter => {
AutoHarnessSkipReason::Comptime
| AutoHarnessSkipReason::NoBody
| AutoHarnessSkipReason::UserFilter => {
Some(vec![md.crate_name.clone(), func, reason.to_string()])
}
// We don't report Kani implementations to the user to avoid exposing Kani functions we insert during instrumentation.
Expand Down
4 changes: 4 additions & 0 deletions kani_metadata/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ pub struct AutoHarnessMetadata {
/// Reasons that Kani does not generate an automatic harness for a function.
#[derive(Debug, Clone, Serialize, Deserialize, Display, EnumString)]
pub enum AutoHarnessSkipReason {
/// A `#[rustc_comptime]` function, which rustc only lets const items, statics and const
/// blocks call; a harness calling it would not be a program rustc accepts.
#[strum(serialize = "Can only be called at compile time")]
Comptime,
/// The function is generic and autoharness could not find a monomorphic instantiation to
/// verify. The payload gives the specific reason (e.g. const generic parameters, or trait
/// bounds that no candidate type satisfies).
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash
# Copyright Kani Contributors
# SPDX-License-Identifier: Apache-2.0 OR MIT

kani autoharness -Z autoharness --only-codegen --output-format=terse comptime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright Kani Contributors
// SPDX-License-Identifier: Apache-2.0 OR MIT

//! A `#[rustc_comptime]` function's body is a const context, which `optimized_mir` refuses to
//! serve while `is_mir_available` still reports `true`. The const-block precondition search added
//! in #4820 fetched it that way and aborted the whole run on any crate declaring one -- `core`
//! declares 27: https://github.com/model-checking/kani/issues/4839
//!
//! rustc also rejects any call to a comptime function outside a const item, static or const
//! block, so autoharness skips all of them ("Can only be called at compile time") rather than
//! verifying a harness rustc would not accept. The three shapes below are the ones `core` has;
//! `guarded_const_fn` keeps the const-block precondition search covered.

#![feature(intrinsics, rustc_attrs)]

// Body-less, declared exactly as `core::intrinsics::size_of` is.
#[rustc_nounwind]
#[rustc_intrinsic]
#[rustc_comptime]
pub fn size_of<T>() -> usize;

// With a body, as `core::any::TypeId::trait_info_of` is.
#[rustc_comptime]
Comment thread
feliperodri marked this conversation as resolved.
Resolved
pub fn with_body(x: u32) -> u32 {
x + 1
}

// With a body, and a const-block precondition on a const generic parameter.
#[rustc_comptime]
pub fn guarded<const N: usize>() -> usize {
const { assert!(N >= 4) };
N
}

// A plain `const fn` with the same precondition: not a const *context*, so it is served by
// `optimized_mir`. Kept here so that a future change to how the body is fetched cannot lose the
// detection for the shape `core::escape` uses, which is why #4820 exists.
pub const fn guarded_const_fn<const N: usize>(x: u8) -> u8 {
const { assert!(N >= 4) };
x
}

pub fn plain(x: u8) -> u8 {
x
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright Kani Contributors
# SPDX-License-Identifier: Apache-2.0 OR MIT
script: check.sh
expected: expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
| comptime | plain |
| comptime | guarded | Can only be called at compile time
| comptime | guarded_const_fn | Generic Function: the function has a `const {}` block that may constrain its const generic parameter(s), and the value autoharness substitutes (2) is not guaranteed to satisfy it |
| comptime | size_of | Can only be called at compile time
| comptime | with_body | Can only be called at compile time
Loading