Skip to content

Commit

Permalink
Only lookup types in one interner
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoxc committed Apr 30, 2018
1 parent ede7f94 commit 3d038cc
Showing 1 changed file with 52 additions and 8 deletions.
60 changes: 52 additions & 8 deletions src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2268,15 +2268,59 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
self.mk_fn_ptr(converted_sig)
}

// Interns a type/name combination, stores the resulting box in cx.interners,
// and returns the box as cast to an unsafe ptr (see comments for Ty above).
pub fn mk_ty(self, st: TypeVariants<'tcx>) -> Ty<'tcx> {
let global_interners = if !self.is_global() {
Some(&self.global_interners)
pub fn mk_ty(&self, st: TypeVariants<'tcx>) -> Ty<'tcx> {
let flags = super::flags::FlagComputation::for_sty(&st);

// HACK(eddyb) Depend on flags being accurate to
// determine that all contents are in the global tcx.
// See comments on Lift for why we can't use that.
if flags.flags.intersects(ty::TypeFlags::KEEP_IN_LOCAL_TCX) {
let mut interner = self.interners.type_.borrow_mut();
if let Some(&Interned(ty)) = interner.get(&st) {
return ty;
}

let ty_struct = TyS {
sty: st,
flags: flags.flags,
region_depth: flags.depth,
};

// Make sure we don't end up with inference
// types/regions in the global tcx.
if self.is_global() {
bug!("Attempted to intern `{:?}` which contains \
inference types/regions in the global type context",
&ty_struct);
}

// Don't be &mut TyS.
let ty: Ty<'tcx> = self.interners.arena.alloc(ty_struct);
interner.insert(Interned(ty));
ty
} else {
None
};
self.interners.intern_ty(st, global_interners)
let mut interner = self.global_interners.type_.borrow_mut();
if let Some(&Interned(ty)) = interner.get(&st) {
return ty;
}

let ty_struct = TyS {
sty: st,
flags: flags.flags,
region_depth: flags.depth,
};

// This is safe because all the types the ty_struct can point to
// already is in the global arena
let ty_struct: TyS<'gcx> = unsafe {
mem::transmute(ty_struct)
};

// Don't be &mut TyS.
let ty: Ty<'gcx> = self.global_interners.arena.alloc(ty_struct);
interner.insert(Interned(ty));
ty
}
}

pub fn mk_mach_int(self, tm: ast::IntTy) -> Ty<'tcx> {
Expand Down

0 comments on commit 3d038cc

Please sign in to comment.