Skip to content

Commit 3a7abc3

Browse files
danielsntedinski
authored andcommitted
Squash warnings: remove final use of insert_unchecked (rust-lang#29)
* Squash warnings: remove final use of insert_unchecked * wWe only use ensure_global_var as an expr, so make it return that * cleanup how codegen_alloc_in_memory generates global vars
1 parent e9381e0 commit 3a7abc3

File tree

5 files changed

+68
-86
lines changed

5 files changed

+68
-86
lines changed

compiler/rustc_codegen_llvm/src/gotoc/cbmc/goto_program/symbol_table.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,6 @@ impl SymbolTable {
4848
self.symbol_table.insert(symbol.name.to_string(), symbol);
4949
}
5050

51-
#[deprecated(note = "Instead, use the `insert()` function.")]
52-
pub fn insert_unchecked(&mut self, symbol: Symbol) {
53-
self.symbol_table.insert(symbol.name.to_string(), symbol);
54-
}
55-
5651
/// Validates the previous value of the symbol using the validator function, then replaces it.
5752
/// Useful to replace declarations with the actual definition.
5853
pub fn replace<F: FnOnce(Option<&Symbol>) -> bool>(

compiler/rustc_codegen_llvm/src/gotoc/metadata.rs

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,32 @@ impl<'tcx> GotocCtx<'tcx> {
264264
Type::union_tag(union_name)
265265
}
266266

267+
/// Ensures that a global variable `name` appears in the Symbol table.
268+
/// If it doesn't, inserts it.
269+
/// If `init_fn` returns `Some(body)`, creates an initializer for the variable using `body`.
270+
/// Otherwise, leaves the variable uninitialized .
271+
pub fn ensure_global_var<F: FnOnce(&mut GotocCtx<'tcx>, Expr) -> Option<Stmt>>(
272+
&mut self,
273+
name: &str,
274+
is_file_local: bool,
275+
t: Type,
276+
loc: Location,
277+
init_fn: F,
278+
) -> Expr {
279+
if !self.symbol_table.contains(name) {
280+
let sym = Symbol::variable(name.to_string(), name.to_string(), t.clone(), loc)
281+
.with_is_file_local(is_file_local)
282+
.with_is_thread_local(false)
283+
.with_is_static_lifetime(true);
284+
let var = sym.to_expr();
285+
self.symbol_table.insert(sym);
286+
if let Some(body) = init_fn(self, var) {
287+
self.register_initializer(name, body);
288+
}
289+
}
290+
self.symbol_table.lookup(name).unwrap().to_expr()
291+
}
292+
267293
/// Ensures that the `name` appears in the Symbol table.
268294
/// If it doesn't, inserts it using `f`.
269295
pub fn ensure<F: FnOnce(&mut GotocCtx<'tcx>, &str) -> Symbol>(
@@ -273,8 +299,7 @@ impl<'tcx> GotocCtx<'tcx> {
273299
) -> &Symbol {
274300
if !self.symbol_table.contains(name) {
275301
let sym = f(self, name);
276-
// TODO, using `insert` here causes regression failures.
277-
self.symbol_table.insert_unchecked(sym);
302+
self.symbol_table.insert(sym);
278303
}
279304
self.symbol_table.lookup(name).unwrap()
280305
}
@@ -400,32 +425,6 @@ impl<'tcx> GotocCtx<'tcx> {
400425
self.gen_stack_variable(c, &self.fname(), "temp", t, loc)
401426
}
402427

403-
/// Generate a global variable if it doens't already exist.
404-
/// Otherwise, returns the existing variable.
405-
///
406-
pub fn gen_global_variable(
407-
&mut self,
408-
name: &str,
409-
is_file_local: bool,
410-
t: Type,
411-
loc: Location,
412-
) -> Symbol {
413-
debug!(
414-
"gen_global_variable\n\tname:\t{}\n\tis_file_local\t{}\n\tt\t{:?}\n\tloc\t{:?}",
415-
name, is_file_local, t, loc
416-
);
417-
418-
let sym = self.ensure(name, |_ctx, _name| {
419-
Symbol::variable(name.to_string(), name.to_string(), t.clone(), loc)
420-
.with_is_file_local(is_file_local)
421-
.with_is_thread_local(false)
422-
.with_is_static_lifetime(true)
423-
});
424-
debug!("{}\n{:?}\n{:?}\n", name, sym.typ, t);
425-
assert!(sym.typ == t);
426-
sym.to_owned()
427-
}
428-
429428
pub fn find_function(&mut self, fname: &str) -> Option<Expr> {
430429
self.symbol_table.lookup(&fname).map(|s| s.to_expr())
431430
}

compiler/rustc_codegen_llvm/src/gotoc/operand.rs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -439,29 +439,22 @@ impl<'tcx> GotocCtx<'tcx> {
439439
.collect()
440440
});
441441

442-
// The type of the global static variable may not be in the symbol table if we are dealing
442+
// The global static variable may not be in the symbol table if we are dealing
443443
// with a literal that can be statically allocated.
444-
let typ = match self.symbol_table.lookup(&name) {
445-
Some(x) => x.typ.clone(),
446-
None => {
447-
debug!(
448-
"Could not find {} in symbol_table; inserting alloc type ref {:?}",
449-
&name, alloc_typ_ref
450-
);
451-
self.symbol_table.insert(Symbol::variable(
452-
name.clone(),
453-
name.clone(),
454-
alloc_typ_ref.clone(),
455-
Location::none(),
456-
));
457-
alloc_typ_ref.clone()
458-
}
459-
};
444+
// We need to make a constructor whether it was in the table or not, so we can't use the
445+
// closure argument to ensure_global_var to do that here.
446+
let var = self.ensure_global_var(
447+
&name,
448+
false, //TODO is this correct?
449+
alloc_typ_ref.clone(),
450+
Location::none(),
451+
|_, _| None,
452+
);
453+
let var_typ = var.typ().clone();
460454

461455
// Assign the initial value `val` to `var` via an intermediate `temp_var` to allow for
462456
// transmuting the allocation type to the global static variable type.
463457
let alloc_data = self.codegen_allocation_data(alloc);
464-
let var = self.gen_global_variable(&name, false, typ.clone(), Location::none());
465458
let val = Expr::struct_expr_from_values(
466459
alloc_typ_ref.clone(),
467460
alloc_data
@@ -483,7 +476,7 @@ impl<'tcx> GotocCtx<'tcx> {
483476
let temp_var = self.gen_function_local_variable(0, &fn_name, alloc_typ_ref).to_expr();
484477
let body = Stmt::block(vec![
485478
Stmt::decl(temp_var.clone(), Some(val), Location::none()),
486-
var.to_expr().assign(temp_var.transmute_to(var.typ.clone(), &self.symbol_table)),
479+
var.assign(temp_var.transmute_to(var_typ, &self.symbol_table)),
487480
]);
488481
self.register_initializer(&name, body);
489482

compiler/rustc_codegen_llvm/src/gotoc/rvalue.rs

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ impl<'tcx> GotocCtx<'tcx> {
643643
}
644644
// Casting to a Box<dyn Trait> from a Box<Adt>
645645
(ty::Dynamic(..), ty::Adt(..)) => {
646-
let vtable = self.codegen_vtable(o, t).to_expr();
646+
let vtable = self.codegen_vtable(o, t);
647647
let codegened_operand = self.codegen_operand(o);
648648
let box_inner_data =
649649
self.deref_box(codegened_operand).cast_to(Type::void_pointer());
@@ -668,7 +668,7 @@ impl<'tcx> GotocCtx<'tcx> {
668668
dynamic_fat_ptr(
669669
self.codegen_ty(t),
670670
self.codegen_operand(o).cast_to(Type::void_pointer()),
671-
self.codegen_vtable(o, t).to_expr().address_of(),
671+
self.codegen_vtable(o, t).address_of(),
672672
&self.symbol_table,
673673
)
674674
}
@@ -787,7 +787,7 @@ impl<'tcx> GotocCtx<'tcx> {
787787
(vt_size, vt_align)
788788
}
789789

790-
fn codegen_vtable(&mut self, operand: &Operand<'tcx>, dst_mir_type: Ty<'tcx>) -> &Symbol {
790+
fn codegen_vtable(&mut self, operand: &Operand<'tcx>, dst_mir_type: Ty<'tcx>) -> Expr {
791791
let src_mir_type = self.monomorphize(self.operand_ty(operand));
792792
return self.codegen_vtable_from_types(src_mir_type, dst_mir_type);
793793
}
@@ -796,7 +796,7 @@ impl<'tcx> GotocCtx<'tcx> {
796796
&mut self,
797797
src_mir_type: Ty<'tcx>,
798798
dst_mir_type: Ty<'tcx>,
799-
) -> &Symbol {
799+
) -> Expr {
800800
let trait_type = match dst_mir_type.kind() {
801801
// dst is pointer type
802802
ty::Ref(_, pointee_type, ..) => pointee_type,
@@ -819,33 +819,28 @@ impl<'tcx> GotocCtx<'tcx> {
819819
let vtable_name = self.vtable_name(trait_type);
820820
let vtable_impl_name = format!("{}_impl_for_{}", vtable_name, src_name);
821821

822-
self.ensure(&vtable_impl_name, |ctx, _| {
823-
// Build the vtable
824-
let drop_irep = ctx.codegen_vtable_drop_in_place();
825-
let (vt_size, vt_align) = ctx.codegen_vtable_size_and_align(&src_mir_type);
826-
let mut vtable_fields = vec![drop_irep, vt_size, vt_align];
827-
let concrete_type = binders.principal().unwrap().with_self_ty(ctx.tcx, src_mir_type);
828-
let mut methods = ctx.codegen_vtable_methods(concrete_type, trait_type);
829-
vtable_fields.append(&mut methods);
830-
let vtable = Expr::struct_expr_from_values(
831-
Type::struct_tag(&vtable_name),
832-
vtable_fields,
833-
&ctx.symbol_table,
834-
);
835-
836-
// Store vtable in a static variable (compare codegen_alloc_in_memory)
837-
let vtable_var = ctx.gen_global_variable(
838-
&vtable_impl_name,
839-
true, // REVISIT: static-scope https://github.com/model-checking/rmc/issues/10
840-
Type::struct_tag(&vtable_name),
841-
Location::none(),
842-
);
843-
844-
// Add the code initializing vtable variable
845-
ctx.register_initializer(&vtable_impl_name, vtable_var.to_expr().assign(vtable));
846-
847-
// Return the vtable variable
848-
vtable_var
849-
})
822+
self.ensure_global_var(
823+
&vtable_impl_name,
824+
true, // REVISIT: static-scope https://github.com/model-checking/rmc/issues/10
825+
Type::struct_tag(&vtable_name),
826+
Location::none(),
827+
|ctx, var| {
828+
// Build the vtable
829+
let drop_irep = ctx.codegen_vtable_drop_in_place();
830+
let (vt_size, vt_align) = ctx.codegen_vtable_size_and_align(&src_mir_type);
831+
let mut vtable_fields = vec![drop_irep, vt_size, vt_align];
832+
let concrete_type =
833+
binders.principal().unwrap().with_self_ty(ctx.tcx, src_mir_type);
834+
let mut methods = ctx.codegen_vtable_methods(concrete_type, trait_type);
835+
vtable_fields.append(&mut methods);
836+
let vtable = Expr::struct_expr_from_values(
837+
Type::struct_tag(&vtable_name),
838+
vtable_fields,
839+
&ctx.symbol_table,
840+
);
841+
let body = var.assign(vtable);
842+
Some(body)
843+
},
844+
)
850845
}
851846
}

compiler/rustc_codegen_llvm/src/gotoc/statement.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ impl<'tcx> GotocCtx<'tcx> {
2020
let name = &FN_RETURN_VOID_VAR_NAME.to_string();
2121
let is_file_local = false;
2222
let ty = self.codegen_ty_unit();
23-
let var = self.gen_global_variable(name, is_file_local, ty, Location::none());
24-
Stmt::ret(Some(var.to_expr()), Location::none())
23+
let var = self.ensure_global_var(name, is_file_local, ty, Location::none(), |_, _| None);
24+
Stmt::ret(Some(var), Location::none())
2525
}
2626

2727
pub fn codegen_terminator(&mut self, term: &Terminator<'tcx>) -> Stmt {

0 commit comments

Comments
 (0)