Skip to content

Commit ea8b82e

Browse files
committed
Auto merge of #23376 - eddyb:die-tydesc-die, r=nikomatsakis
Final remnant of reflection is gone. Also, virtual `Trait` destructors are no longer tied to `Box`. That means they can be used to drop any instance of the type (used in libarena to replace TyDesc). This is [breaking-change] for direct users of intrinsics: * use `intrinsics::type_name::<T>()` instead of `(*intrinsics::get_tydesc::<T>()).name` * the only way to get the destructor is from a trait object's vtable (see libarena changes) r? @pcwalton f? @dotdash
2 parents bfac337 + e256b7f commit ea8b82e

File tree

20 files changed

+177
-528
lines changed

20 files changed

+177
-528
lines changed

src/libarena/lib.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ extern crate alloc;
4242

4343
use std::cell::{Cell, RefCell};
4444
use std::cmp;
45-
use std::intrinsics::{TyDesc, get_tydesc};
4645
use std::intrinsics;
46+
#[cfg(stage0)] // SNAP 270a677
47+
use std::intrinsics::{get_tydesc, TyDesc};
4748
use std::marker;
4849
use std::mem;
4950
#[cfg(stage0)]
@@ -186,6 +187,27 @@ fn un_bitpack_tydesc_ptr(p: usize) -> (*const TyDesc, bool) {
186187
((p & !1) as *const TyDesc, p & 1 == 1)
187188
}
188189

190+
// HACK(eddyb) TyDesc replacement using a trait object vtable.
191+
// This could be replaced in the future with a custom DST layout,
192+
// or `&'static (drop_glue, size, align)` created by a `const fn`.
193+
#[cfg(not(stage0))] // SNAP 270a677
194+
struct TyDesc {
195+
drop_glue: fn(*const i8),
196+
size: usize,
197+
align: usize
198+
}
199+
200+
#[cfg(not(stage0))] // SNAP 270a677
201+
unsafe fn get_tydesc<T>() -> *const TyDesc {
202+
use std::raw::TraitObject;
203+
204+
let ptr = &*(1 as *const T);
205+
206+
// Can use any trait that is implemented for all types.
207+
let obj = mem::transmute::<&marker::MarkerTrait, TraitObject>(ptr);
208+
obj.vtable as *const TyDesc
209+
}
210+
189211
impl<'longer_than_self> Arena<'longer_than_self> {
190212
fn chunk_size(&self) -> usize {
191213
self.copy_head.borrow().capacity()

src/libcore/intrinsics.rs

+7
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,12 @@
4444

4545
use marker::Sized;
4646

47+
#[cfg(stage0)] // SNAP 270a677
4748
pub type GlueFn = extern "Rust" fn(*const i8);
4849

4950
#[lang="ty_desc"]
5051
#[derive(Copy)]
52+
#[cfg(stage0)] // SNAP 270a677
5153
pub struct TyDesc {
5254
// sizeof(T)
5355
pub size: usize,
@@ -197,8 +199,13 @@ extern "rust-intrinsic" {
197199
pub fn pref_align_of<T>() -> usize;
198200

199201
/// Get a static pointer to a type descriptor.
202+
#[cfg(stage0)] // SNAP 270a677
200203
pub fn get_tydesc<T: ?Sized>() -> *const TyDesc;
201204

205+
/// Gets a static string slice containing the name of a type.
206+
#[cfg(not(stage0))] // SNAP 270a677
207+
pub fn type_name<T: ?Sized>() -> &'static str;
208+
202209
/// Gets an identifier which is globally unique to the specified type. This
203210
/// function will return the same value for a type regardless of whichever
204211
/// crate it is invoked in.

src/librustc/middle/lang_items.rs

-3
Original file line numberDiff line numberDiff line change
@@ -316,9 +316,6 @@ lets_do_this! {
316316

317317
StartFnLangItem, "start", start_fn;
318318

319-
TyDescStructLangItem, "ty_desc", ty_desc;
320-
OpaqueStructLangItem, "opaque", opaque;
321-
322319
EhPersonalityLangItem, "eh_personality", eh_personality;
323320

324321
ExchangeHeapLangItem, "exchange_heap", exchange_heap;

src/librustc/middle/ty.rs

+1-11
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ use middle::check_const;
4747
use middle::const_eval;
4848
use middle::def::{self, DefMap, ExportMap};
4949
use middle::dependency_format;
50-
use middle::lang_items::{FnTraitLangItem, FnMutTraitLangItem};
51-
use middle::lang_items::{FnOnceTraitLangItem, TyDescStructLangItem};
50+
use middle::lang_items::{FnTraitLangItem, FnMutTraitLangItem, FnOnceTraitLangItem};
5251
use middle::mem_categorization as mc;
5352
use middle::region;
5453
use middle::resolve_lifetime;
@@ -723,7 +722,6 @@ pub struct ctxt<'tcx> {
723722
pub object_cast_map: ObjectCastMap<'tcx>,
724723

725724
pub map: ast_map::Map<'tcx>,
726-
pub intrinsic_defs: RefCell<DefIdMap<Ty<'tcx>>>,
727725
pub freevars: RefCell<FreevarMap>,
728726
pub tcache: RefCell<DefIdMap<TypeScheme<'tcx>>>,
729727
pub rcache: RefCell<FnvHashMap<creader_cache_key, Ty<'tcx>>>,
@@ -2575,7 +2573,6 @@ pub fn mk_ctxt<'tcx>(s: Session,
25752573
super_predicates: RefCell::new(DefIdMap()),
25762574
object_cast_map: RefCell::new(NodeMap()),
25772575
map: map,
2578-
intrinsic_defs: RefCell::new(DefIdMap()),
25792576
freevars: freevars,
25802577
tcache: RefCell::new(DefIdMap()),
25812578
rcache: RefCell::new(FnvHashMap()),
@@ -5951,13 +5948,6 @@ pub fn required_region_bounds<'tcx>(tcx: &ctxt<'tcx>,
59515948
.collect()
59525949
}
59535950

5954-
pub fn get_tydesc_ty<'tcx>(tcx: &ctxt<'tcx>) -> Result<Ty<'tcx>, String> {
5955-
tcx.lang_items.require(TyDescStructLangItem).map(|tydesc_lang_item| {
5956-
tcx.intrinsic_defs.borrow().get(&tydesc_lang_item).cloned()
5957-
.expect("Failed to resolve TyDesc")
5958-
})
5959-
}
5960-
59615951
pub fn item_variances(tcx: &ctxt, item_id: ast::DefId) -> Rc<ItemVariances> {
59625952
lookup_locally_or_in_crate_store(
59635953
"item_variance_map", item_id, &mut *tcx.item_variance_map.borrow_mut(),

src/librustc_trans/trans/_match.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ fn bind_subslice_pat(bcx: Block,
660660
offset_right: uint) -> ValueRef {
661661
let _icx = push_ctxt("match::bind_subslice_pat");
662662
let vec_ty = node_id_type(bcx, pat_id);
663-
let vt = tvec::vec_types(bcx, ty::sequence_element_type(bcx.tcx(), ty::type_content(vec_ty)));
663+
let unit_ty = ty::sequence_element_type(bcx.tcx(), ty::type_content(vec_ty));
664664
let vec_datum = match_datum(val, vec_ty);
665665
let (base, len) = vec_datum.get_vec_base_and_len(bcx);
666666

@@ -669,7 +669,7 @@ fn bind_subslice_pat(bcx: Block,
669669
let slice_len = Sub(bcx, len, slice_len_offset, DebugLoc::None);
670670
let slice_ty = ty::mk_slice(bcx.tcx(),
671671
bcx.tcx().mk_region(ty::ReStatic),
672-
ty::mt {ty: vt.unit_ty, mutbl: ast::MutImmutable});
672+
ty::mt {ty: unit_ty, mutbl: ast::MutImmutable});
673673
let scratch = rvalue_scratch_datum(bcx, slice_ty, "");
674674
Store(bcx, slice_begin,
675675
GEPi(bcx, scratch.val, &[0, abi::FAT_PTR_ADDR]));

src/librustc_trans/trans/base.rs

+5-21
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ use trans::common::{C_null, C_struct_in_context, C_u64, C_u8, C_undef};
5757
use trans::common::{CrateContext, ExternMap, FunctionContext};
5858
use trans::common::{Result, NodeIdAndSpan};
5959
use trans::common::{node_id_type, return_type_is_void};
60-
use trans::common::{tydesc_info, type_is_immediate};
61-
use trans::common::{type_is_zero_size, val_ty};
60+
use trans::common::{type_is_immediate, type_is_zero_size, val_ty};
6261
use trans::common;
6362
use trans::consts;
6463
use trans::context::SharedCrateContext;
@@ -90,7 +89,6 @@ use std::ffi::{CStr, CString};
9089
use std::cell::{Cell, RefCell};
9190
use std::collections::HashSet;
9291
use std::mem;
93-
use std::rc::Rc;
9492
use std::str;
9593
use std::{i8, i16, i32, i64};
9694
use syntax::abi::{Rust, RustCall, RustIntrinsic, Abi};
@@ -392,22 +390,6 @@ pub fn malloc_raw_dyn<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
392390
Result::new(r.bcx, PointerCast(r.bcx, r.val, llty_ptr))
393391
}
394392

395-
// Type descriptor and type glue stuff
396-
397-
pub fn get_tydesc<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
398-
t: Ty<'tcx>) -> Rc<tydesc_info<'tcx>> {
399-
match ccx.tydescs().borrow().get(&t) {
400-
Some(inf) => return inf.clone(),
401-
_ => { }
402-
}
403-
404-
ccx.stats().n_static_tydescs.set(ccx.stats().n_static_tydescs.get() + 1);
405-
let inf = Rc::new(glue::declare_tydesc(ccx, t));
406-
407-
ccx.tydescs().borrow_mut().insert(t, inf.clone());
408-
inf
409-
}
410-
411393
#[allow(dead_code)] // useful
412394
pub fn set_optimize_for_size(f: ValueRef) {
413395
llvm::SetFunctionAttribute(f, llvm::OptimizeForSizeAttribute)
@@ -702,6 +684,10 @@ pub fn iter_structural_ty<'blk, 'tcx, F>(cx: Block<'blk, 'tcx>,
702684
let unit_ty = ty::sequence_element_type(cx.tcx(), t);
703685
cx = tvec::iter_vec_raw(cx, base, unit_ty, len, f);
704686
}
687+
ty::ty_vec(_, None) | ty::ty_str => {
688+
let unit_ty = ty::sequence_element_type(cx.tcx(), t);
689+
cx = tvec::iter_vec_raw(cx, data_ptr, unit_ty, info.unwrap(), f);
690+
}
705691
ty::ty_tup(ref args) => {
706692
let repr = adt::represent_type(cx.ccx(), t);
707693
for (i, arg) in args.iter().enumerate() {
@@ -3133,7 +3119,6 @@ pub fn trans_crate<'tcx>(analysis: ty::CrateAnalysis<'tcx>)
31333119
}
31343120

31353121
for ccx in shared_ccx.iter() {
3136-
glue::emit_tydescs(&ccx);
31373122
if ccx.sess().opts.debuginfo != NoDebugInfo {
31383123
debuginfo::finalize(&ccx);
31393124
}
@@ -3145,7 +3130,6 @@ pub fn trans_crate<'tcx>(analysis: ty::CrateAnalysis<'tcx>)
31453130
if shared_ccx.sess().trans_stats() {
31463131
let stats = shared_ccx.stats();
31473132
println!("--- trans stats ---");
3148-
println!("n_static_tydescs: {}", stats.n_static_tydescs.get());
31493133
println!("n_glues_created: {}", stats.n_glues_created.get());
31503134
println!("n_null_glues: {}", stats.n_null_glues.get());
31513135
println!("n_real_glues: {}", stats.n_real_glues.get());

src/librustc_trans/trans/common.rs

-9
Original file line numberDiff line numberDiff line change
@@ -316,15 +316,6 @@ pub fn gensym_name(name: &str) -> PathElem {
316316
PathName(token::gensym(&format!("{}:{}", name, num)))
317317
}
318318

319-
#[derive(Copy)]
320-
pub struct tydesc_info<'tcx> {
321-
pub ty: Ty<'tcx>,
322-
pub tydesc: ValueRef,
323-
pub size: ValueRef,
324-
pub align: ValueRef,
325-
pub name: ValueRef,
326-
}
327-
328319
/*
329320
* A note on nomenclature of linking: "extern", "foreign", and "upcall".
330321
*

src/librustc_trans/trans/consts.rs

-1
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,6 @@ pub fn const_expr<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
314314
let info =
315315
expr::unsized_info(
316316
cx, k, e.id, ty, param_substs,
317-
|t| ty::mk_imm_rptr(cx.tcx(), cx.tcx().mk_region(ty::ReStatic), t),
318317
|| const_get_elt(cx, llconst, &[abi::FAT_PTR_EXTRA as u32]));
319318

320319
let unsized_ty = ty::unsize_ty(cx.tcx(), ty, k, e.span);

src/librustc_trans/trans/context.rs

+3-26
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use middle::traits;
1818
use trans::adt;
1919
use trans::base;
2020
use trans::builder::Builder;
21-
use trans::common::{ExternMap,tydesc_info,BuilderRef_res};
21+
use trans::common::{ExternMap,BuilderRef_res};
2222
use trans::debuginfo;
2323
use trans::monomorphize::MonoId;
2424
use trans::type_::{Type, TypeNames};
@@ -38,7 +38,6 @@ use syntax::ast;
3838
use syntax::parse::token::InternedString;
3939

4040
pub struct Stats {
41-
pub n_static_tydescs: Cell<uint>,
4241
pub n_glues_created: Cell<uint>,
4342
pub n_null_glues: Cell<uint>,
4443
pub n_real_glues: Cell<uint>,
@@ -89,10 +88,6 @@ pub struct LocalCrateContext<'tcx> {
8988
needs_unwind_cleanup_cache: RefCell<FnvHashMap<Ty<'tcx>, bool>>,
9089
fn_pointer_shims: RefCell<FnvHashMap<Ty<'tcx>, ValueRef>>,
9190
drop_glues: RefCell<FnvHashMap<Ty<'tcx>, ValueRef>>,
92-
tydescs: RefCell<FnvHashMap<Ty<'tcx>, Rc<tydesc_info<'tcx>>>>,
93-
/// Set when running emit_tydescs to enforce that no more tydescs are
94-
/// created.
95-
finished_tydescs: Cell<bool>,
9691
/// Track mapping of external ids to local items imported for inlining
9792
external: RefCell<DefIdMap<Option<ast::NodeId>>>,
9893
/// Backwards version of the `external` map (inlined items to where they
@@ -102,7 +97,7 @@ pub struct LocalCrateContext<'tcx> {
10297
monomorphized: RefCell<FnvHashMap<MonoId<'tcx>, ValueRef>>,
10398
monomorphizing: RefCell<DefIdMap<uint>>,
10499
/// Cache generated vtables
105-
vtables: RefCell<FnvHashMap<(Ty<'tcx>, ty::PolyTraitRef<'tcx>), ValueRef>>,
100+
vtables: RefCell<FnvHashMap<ty::PolyTraitRef<'tcx>, ValueRef>>,
106101
/// Cache of constant strings,
107102
const_cstr_cache: RefCell<FnvHashMap<InternedString, ValueRef>>,
108103

@@ -264,7 +259,6 @@ impl<'tcx> SharedCrateContext<'tcx> {
264259
symbol_hasher: RefCell::new(symbol_hasher),
265260
tcx: tcx,
266261
stats: Stats {
267-
n_static_tydescs: Cell::new(0),
268262
n_glues_created: Cell::new(0),
269263
n_null_glues: Cell::new(0),
270264
n_real_glues: Cell::new(0),
@@ -399,8 +393,6 @@ impl<'tcx> LocalCrateContext<'tcx> {
399393
needs_unwind_cleanup_cache: RefCell::new(FnvHashMap()),
400394
fn_pointer_shims: RefCell::new(FnvHashMap()),
401395
drop_glues: RefCell::new(FnvHashMap()),
402-
tydescs: RefCell::new(FnvHashMap()),
403-
finished_tydescs: Cell::new(false),
404396
external: RefCell::new(DefIdMap()),
405397
external_srcs: RefCell::new(NodeMap()),
406398
monomorphized: RefCell::new(FnvHashMap()),
@@ -442,8 +434,6 @@ impl<'tcx> LocalCrateContext<'tcx> {
442434
str_slice_ty.set_struct_body(&[Type::i8p(&ccx), ccx.int_type()], false);
443435
ccx.tn().associate_type("str_slice", &str_slice_ty);
444436

445-
ccx.tn().associate_type("tydesc", &Type::tydesc(&ccx, str_slice_ty));
446-
447437
if ccx.sess().count_llvm_insns() {
448438
base::init_insn_ctxt()
449439
}
@@ -519,10 +509,6 @@ impl<'b, 'tcx> CrateContext<'b, 'tcx> {
519509
self.local.builder.b
520510
}
521511

522-
pub fn tydesc_type(&self) -> Type {
523-
self.local.tn.find_type("tydesc").unwrap()
524-
}
525-
526512
pub fn get_intrinsic(&self, key: & &'static str) -> ValueRef {
527513
if let Some(v) = self.intrinsics().borrow().get(key).cloned() {
528514
return v;
@@ -590,14 +576,6 @@ impl<'b, 'tcx> CrateContext<'b, 'tcx> {
590576
&self.local.drop_glues
591577
}
592578

593-
pub fn tydescs<'a>(&'a self) -> &'a RefCell<FnvHashMap<Ty<'tcx>, Rc<tydesc_info<'tcx>>>> {
594-
&self.local.tydescs
595-
}
596-
597-
pub fn finished_tydescs<'a>(&'a self) -> &'a Cell<bool> {
598-
&self.local.finished_tydescs
599-
}
600-
601579
pub fn external<'a>(&'a self) -> &'a RefCell<DefIdMap<Option<ast::NodeId>>> {
602580
&self.local.external
603581
}
@@ -614,8 +592,7 @@ impl<'b, 'tcx> CrateContext<'b, 'tcx> {
614592
&self.local.monomorphizing
615593
}
616594

617-
pub fn vtables<'a>(&'a self) -> &'a RefCell<FnvHashMap<(Ty<'tcx>, ty::PolyTraitRef<'tcx>),
618-
ValueRef>> {
595+
pub fn vtables<'a>(&'a self) -> &'a RefCell<FnvHashMap<ty::PolyTraitRef<'tcx>, ValueRef>> {
619596
&self.local.vtables
620597
}
621598

0 commit comments

Comments
 (0)