Skip to content

Commit 9b6f0b4

Browse files
authored
Fix Kani crash with const-generic [e; N] expression (rust-lang#1770)
1 parent 674ae49 commit 9b6f0b4

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

kani-compiler/src/codegen_cprover_gotoc/codegen/rvalue.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ impl<'tcx> GotocCtx<'tcx> {
204204
fn codegen_rvalue_repeat(
205205
&mut self,
206206
op: &Operand<'tcx>,
207-
sz: &ty::Const<'tcx>,
207+
sz: ty::Const<'tcx>,
208208
res_ty: Ty<'tcx>,
209209
loc: Location,
210210
) -> Expr {
@@ -393,7 +393,10 @@ impl<'tcx> GotocCtx<'tcx> {
393393
debug!(?rv, "codegen_rvalue");
394394
match rv {
395395
Rvalue::Use(p) => self.codegen_operand(p),
396-
Rvalue::Repeat(op, sz) => self.codegen_rvalue_repeat(op, sz, res_ty, loc),
396+
Rvalue::Repeat(op, sz) => {
397+
let sz = self.monomorphize(*sz);
398+
self.codegen_rvalue_repeat(op, sz, res_ty, loc)
399+
}
397400
Rvalue::Ref(_, _, p) | Rvalue::AddressOf(_, p) => self.codegen_rvalue_ref(p, res_ty),
398401
Rvalue::Len(p) => self.codegen_rvalue_len(p),
399402
// Rust has begun distinguishing "ptr -> num" and "num -> ptr" (providence-relevant casts) but we do not yet:

kani-compiler/src/codegen_cprover_gotoc/codegen/typ.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ impl<'tcx> GotocCtx<'tcx> {
708708
}
709709

710710
/// codegen for types. it finds a C type which corresponds to a rust type.
711-
/// that means [ty] has to be monomorphized.
711+
/// that means [ty] has to be monomorphized before calling this function.
712712
///
713713
/// check [rustc_middle::ty::layout::LayoutCx::layout_of_uncached] for LLVM codegen
714714
///
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright Kani Contributors
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
// Ensure Kani can compile `[e; N]` when `N` is a const generic.
5+
// Test from <https://github.com/model-checking/kani/issues/1728>
6+
7+
struct Foo<const N: usize> {
8+
field: [u64; N],
9+
}
10+
11+
impl<const N: usize> Foo<N> {
12+
fn new() -> Self {
13+
// Was a crash during codegen because N hadn't been "monomorphized"
14+
// and so could not be evaluated to a compile-time constant.
15+
Self { field: [0; N] }
16+
}
17+
}
18+
19+
#[cfg(kani)]
20+
mod proofs {
21+
use super::*;
22+
23+
#[kani::proof]
24+
fn hope_kani_does_not_crash() {
25+
let x = Foo::<32>::new();
26+
assert!(x.field.len() == 32);
27+
}
28+
}

0 commit comments

Comments
 (0)