Skip to content

Commit cf7df1e

Browse files
committed
auto merge of #18483 : eddyb/rust/safe-ty, r=nikomatsakis
After more than a month of sitting on this patch, rebasing and tracking down some nasty bugs (there's might be still one out there, but it only manifested in `middle::trans::reflect` which is now gone), I'd like to merge it as it is. This changeset makes middle::ty safe, linking the lifetime of a type to the type context it was created in. It's a prerequisite for introducing function-local type contexts to localize types with inference variables, in order to (potentially) free hundreds of MBs from rustc's memory usage peak.
2 parents e09d986 + bf0766a commit cf7df1e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+5115
-4974
lines changed

src/librustc/lint/builtin.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ use self::MethodContext::*;
2828

2929
use metadata::csearch;
3030
use middle::def::*;
31+
use middle::ty::{mod, Ty};
3132
use middle::typeck::astconv::ast_ty_to_ty;
32-
use middle::typeck::infer;
33-
use middle::{typeck, ty, def, pat_util, stability};
33+
use middle::typeck::{mod, infer};
34+
use middle::{def, pat_util, stability};
3435
use middle::const_eval::{eval_const_expr_partial, const_int, const_uint};
3536
use util::ppaux::{ty_to_string};
3637
use util::nodemap::{FnvHashMap, NodeSet};
@@ -99,7 +100,7 @@ impl LintPass for UnusedCasts {
99100
match e.node {
100101
ast::ExprCast(ref expr, ref ty) => {
101102
let t_t = ast_ty_to_ty(cx, &infer::new_infer_ctxt(cx.tcx), &**ty);
102-
if ty::get(ty::expr_ty(cx.tcx, &**expr)).sty == ty::get(t_t).sty {
103+
if ty::expr_ty(cx.tcx, &**expr) == t_t {
103104
cx.span_lint(UNUSED_TYPECASTS, ty.span, "unnecessary type cast");
104105
}
105106
}
@@ -155,7 +156,7 @@ impl LintPass for TypeLimits {
155156
},
156157
_ => {
157158
let t = ty::expr_ty(cx.tcx, &**expr);
158-
match ty::get(t).sty {
159+
match t.sty {
159160
ty::ty_uint(_) => {
160161
cx.span_lint(UNSIGNED_NEGATION, e.span,
161162
"negation of unsigned int variable may \
@@ -180,7 +181,7 @@ impl LintPass for TypeLimits {
180181
}
181182

182183
if is_shift_binop(binop) {
183-
let opt_ty_bits = match ty::get(ty::expr_ty(cx.tcx, &**l)).sty {
184+
let opt_ty_bits = match ty::expr_ty(cx.tcx, &**l).sty {
184185
ty::ty_int(t) => Some(int_ty_bits(t, cx.sess().target.int_type)),
185186
ty::ty_uint(t) => Some(uint_ty_bits(t, cx.sess().target.uint_type)),
186187
_ => None
@@ -205,7 +206,7 @@ impl LintPass for TypeLimits {
205206
}
206207
},
207208
ast::ExprLit(ref lit) => {
208-
match ty::get(ty::expr_ty(cx.tcx, e)).sty {
209+
match ty::expr_ty(cx.tcx, e).sty {
209210
ty::ty_int(t) => {
210211
match lit.node {
211212
ast::LitInt(v, ast::SignedIntLit(_, ast::Plus)) |
@@ -343,7 +344,7 @@ impl LintPass for TypeLimits {
343344
// Normalize the binop so that the literal is always on the RHS in
344345
// the comparison
345346
let norm_binop = if swap { rev_binop(binop) } else { binop };
346-
match ty::get(ty::expr_ty(tcx, expr)).sty {
347+
match ty::expr_ty(tcx, expr).sty {
347348
ty::ty_int(int_ty) => {
348349
let (min, max) = int_ty_range(int_ty);
349350
let lit_val: i64 = match lit.node {
@@ -473,10 +474,11 @@ declare_lint!(BOX_POINTERS, Allow,
473474
pub struct BoxPointers;
474475

475476
impl BoxPointers {
476-
fn check_heap_type(&self, cx: &Context, span: Span, ty: ty::t) {
477+
fn check_heap_type<'a, 'tcx>(&self, cx: &Context<'a, 'tcx>,
478+
span: Span, ty: Ty<'tcx>) {
477479
let mut n_uniq = 0i;
478480
ty::fold_ty(cx.tcx, ty, |t| {
479-
match ty::get(t).sty {
481+
match t.sty {
480482
ty::ty_uniq(_) |
481483
ty::ty_closure(box ty::ClosureTy {
482484
store: ty::UniqTraitStore,
@@ -576,7 +578,7 @@ impl LintPass for RawPointerDeriving {
576578
}
577579
let did = match item.node {
578580
ast::ItemImpl(..) => {
579-
match ty::get(ty::node_id_to_type(cx.tcx, item.id)).sty {
581+
match ty::node_id_to_type(cx.tcx, item.id).sty {
580582
ty::ty_enum(did, _) => did,
581583
ty::ty_struct(did, _) => did,
582584
_ => return,
@@ -738,7 +740,7 @@ impl LintPass for UnusedResults {
738740

739741
let t = ty::expr_ty(cx.tcx, expr);
740742
let mut warned = false;
741-
match ty::get(t).sty {
743+
match t.sty {
742744
ty::ty_tup(ref tys) if tys.is_empty() => return,
743745
ty::ty_bool => return,
744746
ty::ty_struct(did, _) |

src/librustc/lint/context.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use self::TargetLint::*;
2727

2828
use middle::privacy::ExportedItems;
2929
use middle::subst;
30-
use middle::ty;
30+
use middle::ty::{mod, Ty};
3131
use middle::typeck::astconv::AstConv;
3232
use middle::typeck::infer;
3333
use session::{early_error, Session};
@@ -546,30 +546,30 @@ impl<'a, 'tcx> Context<'a, 'tcx> {
546546
impl<'a, 'tcx> AstConv<'tcx> for Context<'a, 'tcx>{
547547
fn tcx<'a>(&'a self) -> &'a ty::ctxt<'tcx> { self.tcx }
548548

549-
fn get_item_ty(&self, id: ast::DefId) -> ty::Polytype {
549+
fn get_item_ty(&self, id: ast::DefId) -> ty::Polytype<'tcx> {
550550
ty::lookup_item_type(self.tcx, id)
551551
}
552552

553-
fn get_trait_def(&self, id: ast::DefId) -> Rc<ty::TraitDef> {
553+
fn get_trait_def(&self, id: ast::DefId) -> Rc<ty::TraitDef<'tcx>> {
554554
ty::lookup_trait_def(self.tcx, id)
555555
}
556556

557-
fn ty_infer(&self, _span: Span) -> ty::t {
557+
fn ty_infer(&self, _span: Span) -> Ty<'tcx> {
558558
infer::new_infer_ctxt(self.tcx).next_ty_var()
559559
}
560560

561-
fn associated_types_of_trait_are_valid(&self, _: ty::t, _: ast::DefId)
561+
fn associated_types_of_trait_are_valid(&self, _: Ty<'tcx>, _: ast::DefId)
562562
-> bool {
563563
// FIXME(pcwalton): This is wrong.
564564
true
565565
}
566566

567567
fn associated_type_binding(&self,
568568
_: Span,
569-
_: Option<ty::t>,
569+
_: Option<Ty<'tcx>>,
570570
trait_id: ast::DefId,
571571
associated_type_id: ast::DefId)
572-
-> ty::t {
572+
-> Ty<'tcx> {
573573
// FIXME(pcwalton): This is wrong.
574574
let trait_def = self.get_trait_def(trait_id);
575575
let index = ty::associated_type_parameter_index(self.tcx,

src/librustc/metadata/csearch.rs

+22-19
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ pub fn get_enum_variant_defs(cstore: &cstore::CStore, enum_id: ast::DefId)
123123
decoder::get_enum_variant_defs(&*cstore.intr, &*cdata, enum_id.node)
124124
}
125125

126-
pub fn get_enum_variants(tcx: &ty::ctxt, def: ast::DefId)
127-
-> Vec<Rc<ty::VariantInfo>> {
126+
pub fn get_enum_variants<'tcx>(tcx: &ty::ctxt<'tcx>, def: ast::DefId)
127+
-> Vec<Rc<ty::VariantInfo<'tcx>>> {
128128
let cstore = &tcx.sess.cstore;
129129
let cdata = cstore.get_crate_data(def.krate);
130130
decoder::get_enum_variants(cstore.intr.clone(), &*cdata, def.node, tcx)
@@ -137,8 +137,8 @@ pub fn get_impl_items(cstore: &cstore::CStore, impl_def_id: ast::DefId)
137137
decoder::get_impl_items(&*cdata, impl_def_id.node)
138138
}
139139

140-
pub fn get_impl_or_trait_item(tcx: &ty::ctxt, def: ast::DefId)
141-
-> ty::ImplOrTraitItem {
140+
pub fn get_impl_or_trait_item<'tcx>(tcx: &ty::ctxt<'tcx>, def: ast::DefId)
141+
-> ty::ImplOrTraitItem<'tcx> {
142142
let cdata = tcx.sess.cstore.get_crate_data(def.krate);
143143
decoder::get_impl_or_trait_item(tcx.sess.cstore.intr.clone(),
144144
&*cdata,
@@ -166,15 +166,17 @@ pub fn get_item_variances(cstore: &cstore::CStore,
166166
decoder::get_item_variances(&*cdata, def.node)
167167
}
168168

169-
pub fn get_provided_trait_methods(tcx: &ty::ctxt,
170-
def: ast::DefId)
171-
-> Vec<Rc<ty::Method>> {
169+
pub fn get_provided_trait_methods<'tcx>(tcx: &ty::ctxt<'tcx>,
170+
def: ast::DefId)
171+
-> Vec<Rc<ty::Method<'tcx>>> {
172172
let cstore = &tcx.sess.cstore;
173173
let cdata = cstore.get_crate_data(def.krate);
174174
decoder::get_provided_trait_methods(cstore.intr.clone(), &*cdata, def.node, tcx)
175175
}
176176

177-
pub fn get_supertraits(tcx: &ty::ctxt, def: ast::DefId) -> Vec<Rc<ty::TraitRef>> {
177+
pub fn get_supertraits<'tcx>(tcx: &ty::ctxt<'tcx>,
178+
def: ast::DefId)
179+
-> Vec<Rc<ty::TraitRef<'tcx>>> {
178180
let cstore = &tcx.sess.cstore;
179181
let cdata = cstore.get_crate_data(def.krate);
180182
decoder::get_supertraits(&*cdata, def.node, tcx)
@@ -213,22 +215,22 @@ pub fn get_struct_field_attrs(cstore: &cstore::CStore, def: ast::DefId) -> HashM
213215
decoder::get_struct_field_attrs(&*cdata)
214216
}
215217

216-
pub fn get_type(tcx: &ty::ctxt,
217-
def: ast::DefId)
218-
-> ty::Polytype {
218+
pub fn get_type<'tcx>(tcx: &ty::ctxt<'tcx>,
219+
def: ast::DefId)
220+
-> ty::Polytype<'tcx> {
219221
let cstore = &tcx.sess.cstore;
220222
let cdata = cstore.get_crate_data(def.krate);
221223
decoder::get_type(&*cdata, def.node, tcx)
222224
}
223225

224-
pub fn get_trait_def(tcx: &ty::ctxt, def: ast::DefId) -> ty::TraitDef {
226+
pub fn get_trait_def<'tcx>(tcx: &ty::ctxt<'tcx>, def: ast::DefId) -> ty::TraitDef<'tcx> {
225227
let cstore = &tcx.sess.cstore;
226228
let cdata = cstore.get_crate_data(def.krate);
227229
decoder::get_trait_def(&*cdata, def.node, tcx)
228230
}
229231

230-
pub fn get_field_type(tcx: &ty::ctxt, class_id: ast::DefId,
231-
def: ast::DefId) -> ty::Polytype {
232+
pub fn get_field_type<'tcx>(tcx: &ty::ctxt<'tcx>, class_id: ast::DefId,
233+
def: ast::DefId) -> ty::Polytype<'tcx> {
232234
let cstore = &tcx.sess.cstore;
233235
let cdata = cstore.get_crate_data(class_id.krate);
234236
let all_items = reader::get_doc(rbml::Doc::new(cdata.data()), tag_items);
@@ -255,17 +257,18 @@ pub fn get_field_type(tcx: &ty::ctxt, class_id: ast::DefId,
255257

256258
// Given a def_id for an impl, return the trait it implements,
257259
// if there is one.
258-
pub fn get_impl_trait(tcx: &ty::ctxt,
259-
def: ast::DefId) -> Option<Rc<ty::TraitRef>> {
260+
pub fn get_impl_trait<'tcx>(tcx: &ty::ctxt<'tcx>,
261+
def: ast::DefId)
262+
-> Option<Rc<ty::TraitRef<'tcx>>> {
260263
let cstore = &tcx.sess.cstore;
261264
let cdata = cstore.get_crate_data(def.krate);
262265
decoder::get_impl_trait(&*cdata, def.node, tcx)
263266
}
264267

265268
// Given a def_id for an impl, return information about its vtables
266-
pub fn get_impl_vtables(tcx: &ty::ctxt,
267-
def: ast::DefId)
268-
-> typeck::vtable_res {
269+
pub fn get_impl_vtables<'tcx>(tcx: &ty::ctxt<'tcx>,
270+
def: ast::DefId)
271+
-> typeck::vtable_res<'tcx> {
269272
let cstore = &tcx.sess.cstore;
270273
let cdata = cstore.get_crate_data(def.krate);
271274
decoder::get_impl_vtables(&*cdata, def.node, tcx)

0 commit comments

Comments
 (0)