@@ -131,7 +131,7 @@ impl<'tcx> GotocCtx<'tcx> {
131131 // First try to generate the constant without allocating memory.
132132 let expr = self . try_codegen_constant ( alloc, ty, loc) . unwrap_or_else ( || {
133133 debug ! ( "codegen_allocation try_fail" ) ;
134- let mem_var = self . codegen_const_allocation ( alloc, None , loc) ;
134+ let mem_var = self . codegen_const_allocation ( alloc, None , loc, true ) ;
135135 mem_var
136136 . cast_to ( Type :: unsigned_int ( 8 ) . to_pointer ( ) )
137137 . cast_to ( self . codegen_ty_stable ( ty) . to_pointer ( ) )
@@ -282,7 +282,7 @@ impl<'tcx> GotocCtx<'tcx> {
282282 let GlobalAlloc :: Memory ( data) = GlobalAlloc :: from ( alloc_id) else {
283283 unreachable ! ( )
284284 } ;
285- let mem_var = self . codegen_const_allocation ( & data, None , loc) ;
285+ let mem_var = self . codegen_const_allocation ( & data, None , loc, false ) ;
286286
287287 // Extract identifier for static variable.
288288 // codegen_allocation_auto_imm_name returns the *address* of
@@ -323,7 +323,7 @@ impl<'tcx> GotocCtx<'tcx> {
323323 let GlobalAlloc :: Memory ( data) = GlobalAlloc :: from ( alloc_id) else {
324324 unreachable ! ( )
325325 } ;
326- let mem_var = self . codegen_const_allocation ( & data, None , loc) ;
326+ let mem_var = self . codegen_const_allocation ( & data, None , loc, false ) ;
327327 let inner_typ = self . codegen_ty_stable ( inner_ty) ;
328328 let len = data. bytes . len ( ) / inner_typ. sizeof ( & self . symbol_table ) as usize ;
329329 let data_expr = mem_var. cast_to ( inner_typ. to_pointer ( ) ) ;
@@ -394,7 +394,7 @@ impl<'tcx> GotocCtx<'tcx> {
394394 // crates do not conflict. The name alone is insufficient because Rust
395395 // allows different versions of the same crate to be used.
396396 let name = format ! ( "{}::{alloc_id:?}" , self . full_crate_name( ) ) ;
397- self . codegen_const_allocation ( & alloc, Some ( name) , loc)
397+ self . codegen_const_allocation ( & alloc, Some ( name) , loc, false )
398398 }
399399 alloc @ GlobalAlloc :: VTable ( ..) => {
400400 // This is similar to GlobalAlloc::Memory but the type is opaque to rust and it
@@ -404,7 +404,7 @@ impl<'tcx> GotocCtx<'tcx> {
404404 unreachable ! ( )
405405 } ;
406406 let name = format ! ( "{}::{alloc_id:?}" , self . full_crate_name( ) ) ;
407- self . codegen_const_allocation ( & alloc, Some ( name) , loc)
407+ self . codegen_const_allocation ( & alloc, Some ( name) , loc, false )
408408 }
409409 GlobalAlloc :: TypeId { ty : _ } => todo ! ( ) ,
410410 } ;
@@ -486,6 +486,7 @@ impl<'tcx> GotocCtx<'tcx> {
486486 alloc : & Allocation ,
487487 name : Option < String > ,
488488 loc : Location ,
489+ is_definitely_const : bool ,
489490 ) -> Expr {
490491 debug ! ( ?name, ?alloc, "codegen_const_allocation" ) ;
491492 let alloc_name = match self . alloc_map . get ( alloc) {
@@ -497,6 +498,7 @@ impl<'tcx> GotocCtx<'tcx> {
497498 alloc_name. clone ( ) ,
498499 loc,
499500 has_interior_mutabity,
501+ is_definitely_const,
500502 ) ;
501503 alloc_name
502504 }
@@ -517,7 +519,7 @@ impl<'tcx> GotocCtx<'tcx> {
517519 // The memory behind this allocation isn't constant, but codegen_alloc_in_memory (which codegen_const_allocation calls)
518520 // uses alloc's mutability field to set the const-ness of the allocation in CBMC's symbol table,
519521 // so we can reuse the code and without worrying that the allocation is set as immutable.
520- self . codegen_const_allocation ( alloc, name, loc)
522+ self . codegen_const_allocation ( alloc, name, loc, false )
521523 }
522524
523525 /// Insert an allocation into the goto symbol table, and generate an init value.
@@ -530,6 +532,7 @@ impl<'tcx> GotocCtx<'tcx> {
530532 name : String ,
531533 loc : Location ,
532534 has_interior_mutabity : bool ,
535+ is_definitely_const : bool ,
533536 ) {
534537 debug ! ( ?name, ?alloc, "codegen_alloc_in_memory" ) ;
535538 let struct_name = & format ! ( "{name}::struct" ) ;
@@ -583,7 +586,7 @@ impl<'tcx> GotocCtx<'tcx> {
583586 let _var = self . ensure_global_var_init (
584587 & name,
585588 false , //TODO is this correct?
586- alloc. mutability == Mutability :: Not && !has_interior_mutabity,
589+ ( is_definitely_const || alloc. mutability == Mutability :: Not ) && !has_interior_mutabity,
587590 alloc_typ_ref. clone ( ) ,
588591 loc,
589592 init_fn,
0 commit comments