Skip to content

Commit f8ebea9

Browse files
avanhatttedinski
authored andcommitted
Refactor pretty name to restore passable dyn trait functionality (rust-lang#171)
1 parent d86c4a4 commit f8ebea9

File tree

8 files changed

+35
-12
lines changed

8 files changed

+35
-12
lines changed

compiler/rustc_codegen_llvm/src/gotoc/metadata.rs

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_middle::mir::interpret::Allocation;
1717
use rustc_middle::mir::{BasicBlock, Body, HasLocalDecls, Local, Operand, Place, Rvalue};
1818
use rustc_middle::ty::layout::{HasParamEnv, HasTyCtxt, TyAndLayout};
1919
use rustc_middle::ty::print::with_no_trimmed_paths;
20-
use rustc_middle::ty::{self, Instance, Ty, TyCtxt, TypeFoldable};
20+
use rustc_middle::ty::{self, Binder, Instance, TraitRef, Ty, TyCtxt, TypeFoldable};
2121
use rustc_target::abi::{HasDataLayout, LayoutOf, TargetDataLayout};
2222
use rustc_target::spec::Target;
2323
use std::iter;
@@ -186,10 +186,35 @@ impl<'tcx> GotocCtx<'tcx> {
186186
self.current_fn.as_ref().map(|x| self.symbol_name(x.instance))
187187
}
188188

189-
/// a path to the def id. the difference from [instance_name] is that it does not consider generics.
190-
pub fn pretty_name(&self, did: DefId) -> String {
191-
let def_path = self.tcx.def_path(did);
192-
format!("{}{}", self.tcx.crate_name(did.krate), def_path.to_string_no_crate_verbose())
189+
/// Pretty name including crate path and trait information. For example:
190+
/// boxtrait_fail::<Concrete as Trait>::increment
191+
/// Generated from the fn instance to insert _into_ the symbol table.
192+
/// Must match the format of pretty_name_from_dynamic_object.
193+
/// TODO: internal unit tests https://github.com/model-checking/rmc/issues/172
194+
pub fn pretty_name_from_instance(&self, instance: Instance<'tcx>) -> String {
195+
format!(
196+
"{}::{}",
197+
self.tcx.crate_name(instance.def_id().krate),
198+
with_no_trimmed_paths(|| instance.to_string())
199+
)
200+
}
201+
202+
/// Pretty name including crate path and trait information. For example:
203+
/// boxtrait_fail::<Concrete as Trait>::increment
204+
/// Generated from the dynamic object type for _lookup_ from the symbol table.
205+
/// Must match the format of pretty_name_from_instance.
206+
pub fn pretty_name_from_dynamic_object(
207+
&self,
208+
def_id: DefId,
209+
trait_ref_t: Binder<'_, TraitRef<'tcx>>,
210+
) -> String {
211+
let normalized_object_type_name = self.normalized_name_of_dynamic_object_type(trait_ref_t);
212+
format!(
213+
"{}::{}::{}",
214+
self.tcx.crate_name(def_id.krate),
215+
normalized_object_type_name,
216+
self.tcx.item_name(def_id)
217+
)
193218
}
194219

195220
/// a human readable name in rust for reference
@@ -208,8 +233,8 @@ impl<'tcx> GotocCtx<'tcx> {
208233
llvm_mangled,
209234
rustc_demangle::demangle(&llvm_mangled).to_string()
210235
);
211-
let did = instance.def.def_id();
212-
let pretty = self.pretty_name(did);
236+
237+
let pretty = self.pretty_name_from_instance(instance);
213238

214239
// make main function a special case for easy CBMC entry
215240
if pretty.ends_with("::main") {

compiler/rustc_codegen_llvm/src/gotoc/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ impl<'tcx> GotocCtx<'tcx> {
166166
self.ensure(&self.fname(), |ctx, fname| {
167167
let mir = ctx.mir();
168168
Symbol::function(fname, ctx.fn_typ(), None, ctx.codegen_span2(&mir.span))
169-
.with_pretty_name(&ctx.pretty_name(instance.def_id()))
169+
.with_pretty_name(&ctx.pretty_name_from_instance(instance))
170170
});
171171
self.reset_current_fn();
172172
}

compiler/rustc_codegen_llvm/src/gotoc/rvalue.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -679,10 +679,8 @@ impl<'tcx> GotocCtx<'tcx> {
679679
.cloned()
680680
.unwrap();
681681

682-
// TODO: this name lookup does not work with rust-tests/cbmc-reg/DynTrait/main.rs
683-
let normalized_object_type_name = self.normalized_name_of_dynamic_object_type(trait_ref_t);
684-
let pretty_function_name = format!("{}::{}", normalized_object_type_name, function_name);
685-
let matching_symbols = self.symbol_table.find_by_pretty_name(&pretty_function_name); //("<Rectangle as Vol>::vol");
682+
let pretty_function_name = self.pretty_name_from_dynamic_object(def_id, trait_ref_t);
683+
let matching_symbols = self.symbol_table.find_by_pretty_name(&pretty_function_name); //("<path>::<Rectangle as Vol>::vol");
686684
match matching_symbols.len() {
687685
0 => {
688686
warn!(

0 commit comments

Comments
 (0)