Skip to content

Commit 165c9ae

Browse files
committed
typecheck unsafe pointers with a 'static region (rust-lang#7694)
1 parent a972633 commit 165c9ae

30 files changed

+149
-83
lines changed

src/librustc/metadata/tydecode.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,11 @@ fn parse_ty(st: &mut PState, conv: conv_did) -> ty::t {
326326
}
327327
'@' => return ty::mk_box(st.tcx, parse_mt(st, conv)),
328328
'~' => return ty::mk_uniq(st.tcx, parse_mt(st, conv)),
329-
'*' => return ty::mk_ptr(st.tcx, parse_mt(st, conv)),
329+
'*' => {
330+
let r = parse_region(st);
331+
let mt = parse_mt(st, conv);
332+
return ty::mk_ptr(st.tcx, r, mt);
333+
}
330334
'&' => {
331335
let r = parse_region(st);
332336
let mt = parse_mt(st, conv);

src/librustc/metadata/tyencode.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,11 @@ fn enc_sty(w: @io::Writer, cx: @ctxt, st: &ty::sty) {
278278
}
279279
ty::ty_box(mt) => { w.write_char('@'); enc_mt(w, cx, mt); }
280280
ty::ty_uniq(mt) => { w.write_char('~'); enc_mt(w, cx, mt); }
281-
ty::ty_ptr(mt) => { w.write_char('*'); enc_mt(w, cx, mt); }
281+
ty::ty_ptr(r, mt) => {
282+
w.write_char('*');
283+
enc_region(w, cx, r);
284+
enc_mt(w, cx, mt);
285+
}
282286
ty::ty_rptr(r, mt) => {
283287
w.write_char('&');
284288
enc_region(w, cx, r);

src/librustc/middle/borrowck/gather_loans/lifetime.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl GuaranteeLifetimeContext {
7474
mc::cat_arg(*) | // L-Local
7575
mc::cat_self(*) | // L-Local
7676
mc::cat_deref(_, _, mc::region_ptr(*)) | // L-Deref-Borrowed
77-
mc::cat_deref(_, _, mc::unsafe_ptr) => {
77+
mc::cat_deref(_, _, mc::unsafe_ptr(*)) => {
7878
let scope = self.scope(cmt);
7979
self.check_scope(scope)
8080
}

src/librustc/middle/borrowck/gather_loans/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ impl GatherLoanCtxt {
357357
m_imm,
358358
r)
359359
}
360-
ty::AutoUnsafe(_) => {}
360+
ty::AutoUnsafe(*) => {}
361361
}
362362
}
363363
}

src/librustc/middle/borrowck/gather_loans/restrictions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ impl RestrictionsContext {
194194
}
195195
}
196196

197-
mc::cat_deref(_, _, mc::unsafe_ptr) => {
197+
mc::cat_deref(_, _, mc::unsafe_ptr(*)) => {
198198
// We are very trusting when working with unsafe pointers.
199199
Safe
200200
}

src/librustc/middle/effect.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ pub fn check_crate(tcx: ty::ctxt,
133133
debug!("effect: unary case, base type is %s",
134134
ppaux::ty_to_str(tcx, base_type));
135135
match ty::get(base_type).sty {
136-
ty_ptr(_) => {
136+
ty_ptr(*) => {
137137
require_unsafe(expr.span,
138138
"dereference of unsafe pointer")
139139
}

src/librustc/middle/mem_categorization.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ pub enum ptr_kind {
8888
uniq_ptr(ast::mutability),
8989
gc_ptr(ast::mutability),
9090
region_ptr(ast::mutability, ty::Region),
91-
unsafe_ptr
91+
unsafe_ptr(ty::Region),
9292
}
9393

9494
// We use the term "interior" to mean "something reachable from the
@@ -188,8 +188,8 @@ pub fn opt_deref_kind(t: ty::t) -> Option<deref_kind> {
188188
Some(deref_ptr(gc_ptr(ast::m_imm)))
189189
}
190190

191-
ty::ty_ptr(*) => {
192-
Some(deref_ptr(unsafe_ptr))
191+
ty::ty_ptr(r, _) => {
192+
Some(deref_ptr(unsafe_ptr(r)))
193193
}
194194

195195
ty::ty_enum(*) |
@@ -678,7 +678,7 @@ impl mem_categorization_ctxt {
678678
uniq_ptr(*) => {
679679
self.inherited_mutability(base_cmt.mutbl, mt.mutbl)
680680
}
681-
gc_ptr(*) | region_ptr(_, _) | unsafe_ptr => {
681+
gc_ptr(*) | region_ptr(*) | unsafe_ptr(*) => {
682682
MutabilityCategory::from_mutbl(mt.mutbl)
683683
}
684684
};
@@ -759,7 +759,7 @@ impl mem_categorization_ctxt {
759759
uniq_ptr(*) => {
760760
self.inherited_mutability(base_cmt.mutbl, mt.mutbl)
761761
}
762-
gc_ptr(_) | region_ptr(_, _) | unsafe_ptr => {
762+
gc_ptr(_) | region_ptr(*) | unsafe_ptr(*) => {
763763
MutabilityCategory::from_mutbl(mt.mutbl)
764764
}
765765
};
@@ -1232,7 +1232,7 @@ pub fn ptr_sigil(ptr: ptr_kind) -> ~str {
12321232
uniq_ptr(_) => ~"~",
12331233
gc_ptr(_) => ~"@",
12341234
region_ptr(_, _) => ~"&",
1235-
unsafe_ptr => ~"*"
1235+
unsafe_ptr(*) => ~"*"
12361236
}
12371237
}
12381238

src/librustc/middle/region.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,16 @@ fn determine_rp_in_ty(ty: &ast::Ty,
748748
// locations)
749749
let sess = cx.sess;
750750
match ty.node {
751+
ast::ty_ptr(ref r, _) => {
752+
debug!("referenced ptr type %s",
753+
pprust::ty_to_str(ty, sess.intr()));
754+
755+
if cx.region_is_relevant(r) {
756+
let rv = cx.add_variance(rv_contravariant);
757+
cx.add_rp(cx.item_id, rv)
758+
}
759+
}
760+
751761
ast::ty_rptr(ref r, _) => {
752762
debug!("referenced rptr type %s",
753763
pprust::ty_to_str(ty, sess.intr()));

src/librustc/middle/trans/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ pub fn compare_scalar_types(cx: block,
572572

573573
match ty::get(t).sty {
574574
ty::ty_nil => rslt(cx, f(nil_type)),
575-
ty::ty_bool | ty::ty_ptr(_) => rslt(cx, f(unsigned_int)),
575+
ty::ty_bool | ty::ty_ptr(*) => rslt(cx, f(unsigned_int)),
576576
ty::ty_int(_) => rslt(cx, f(signed_int)),
577577
ty::ty_uint(_) => rslt(cx, f(unsigned_int)),
578578
ty::ty_float(_) => rslt(cx, f(floating_point)),

src/librustc/middle/trans/closure.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,9 @@ pub fn mk_closure_tys(tcx: ty::ctxt,
148148
let bound_tys = bound_values.map(|bv| {
149149
match bv.action {
150150
EnvCopy | EnvMove => bv.datum.ty,
151-
EnvRef => ty::mk_mut_ptr(tcx, bv.datum.ty)
151+
EnvRef => ty::mk_mut_ptr(tcx,
152+
ty::re_static,
153+
bv.datum.ty)
152154
}
153155
});
154156
let cdata_ty = ty::mk_tup(tcx, bound_tys);
@@ -210,7 +212,9 @@ pub fn store_environment(bcx: block,
210212
// tuple. This could be a ptr in uniq or a box or on stack,
211213
// whatever.
212214
let cbox_ty = tuplify_box_ty(tcx, cdata_ty);
213-
let cboxptr_ty = ty::mk_ptr(tcx, ty::mt {ty:cbox_ty, mutbl:ast::m_imm});
215+
let cboxptr_ty = ty::mk_ptr(tcx,
216+
ty::re_static,
217+
ty::mt {ty:cbox_ty, mutbl:ast::m_imm});
214218
let llboxptr_ty = type_of(ccx, cboxptr_ty);
215219

216220
// If there are no bound values, no point in allocating anything.

src/librustc/middle/trans/common.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,7 @@ impl block_ {
683683
pub fn tuplify_box_ty(tcx: ty::ctxt, t: ty::t) -> ty::t {
684684
let ptr = ty::mk_ptr(
685685
tcx,
686+
ty::re_static,
686687
ty::mt {ty: ty::mk_i8(), mutbl: ast::m_imm}
687688
);
688689
return ty::mk_tup(tcx, ~[ty::mk_uint(), ty::mk_type(tcx),

src/librustc/middle/trans/consts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ pub fn const_expr(cx: @mut CrateContext, e: @ast::expr) -> ValueRef {
205205
None => const_addr_of(cx, llconst)
206206
};
207207
match *autoref {
208-
ty::AutoUnsafe(m) |
208+
ty::AutoUnsafe(_, m) |
209209
ty::AutoPtr(ty::re_static, m) => {
210210
assert!(m != ast::m_mutbl);
211211
llconst = llptr;

src/librustc/middle/trans/datum.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ impl Datum {
631631
ty::ty_box(_) | ty::ty_uniq(_) => {
632632
return (Some(self.box_body(bcx)), bcx);
633633
}
634-
ty::ty_ptr(mt) => {
634+
ty::ty_ptr(_, mt) => {
635635
if is_auto { // unsafe ptrs are not AUTO-derefable
636636
return (None, bcx);
637637
} else {

src/librustc/middle/trans/debuginfo.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -850,7 +850,7 @@ fn boxed_type_metadata(cx: &mut CrateContext,
850850
assert!(box_layout_is_correct(cx, member_llvm_types, content_llvm_type));
851851

852852
let int_type = ty::mk_int();
853-
let nil_pointer_type = ty::mk_nil_ptr(cx.tcx);
853+
let nil_pointer_type = ty::mk_nil_ptr(cx.tcx, ty::re_static);
854854

855855
let member_types_metadata = [
856856
type_metadata(cx, int_type, span),
@@ -983,7 +983,9 @@ fn vec_slice_metadata(cx: &mut CrateContext,
983983

984984
assert!(slice_layout_is_correct(cx, member_llvm_types, element_type));
985985

986-
let data_ptr_type = ty::mk_ptr(cx.tcx, ty::mt { ty: element_type, mutbl: ast::m_imm });
986+
let data_ptr_type = ty::mk_ptr(cx.tcx,
987+
ty::re_static,
988+
ty::mt { ty: element_type, mutbl: ast::m_imm });
987989

988990
let member_type_metadata = &[type_metadata(cx, data_ptr_type, span),
989991
type_metadata(cx, ty::mk_uint(), span)];
@@ -1019,7 +1021,9 @@ fn bare_fn_metadata(cx: &mut CrateContext,
10191021
let loc = span_start(cx, span);
10201022
let file_metadata = file_metadata(cx, loc.file.name);
10211023

1022-
let nil_pointer_type_metadata = type_metadata(cx, ty::mk_nil_ptr(cx.tcx), span);
1024+
let nil_pointer_type_metadata = type_metadata(cx,
1025+
ty::mk_nil_ptr(cx.tcx, ty::re_static),
1026+
span);
10231027
let output_metadata = type_metadata(cx, output, span);
10241028
let output_ptr_metadata = pointer_type_metadata(cx, output, output_metadata);
10251029

@@ -1146,8 +1150,8 @@ fn type_metadata(cx: &mut CrateContext,
11461150
ty::ty_uniq(ref mt) if ty::type_contents(cx.tcx, mt.ty).contains_managed() => {
11471151
create_pointer_to_box_metadata(cx, t, mt.ty)
11481152
},
1149-
ty::ty_uniq(ref mt) |
1150-
ty::ty_ptr(ref mt) |
1153+
ty::ty_uniq(ref mt) |
1154+
ty::ty_ptr(_, ref mt) |
11511155
ty::ty_rptr(_, ref mt) => {
11521156
let pointee = type_metadata(cx, mt.ty, span);
11531157
pointer_type_metadata(cx, t, pointee)

src/librustc/middle/trans/expr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,7 @@ fn trans_def_datum_unadjusted(bcx: block,
784784
let (rust_ty, llval) = if is_extern {
785785
let rust_ty = ty::mk_ptr(
786786
bcx.tcx(),
787+
ty::re_static,
787788
ty::mt {
788789
ty: ty::mk_mach_uint(ast::ty_u8),
789790
mutbl: ast::m_imm

src/librustc/middle/trans/foreign.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,7 @@ pub fn trans_intrinsic(ccx: @mut CrateContext,
844844
let frameaddress_val = Call(bcx, frameaddress, [C_i32(0i32)]);
845845
let star_u8 = ty::mk_imm_ptr(
846846
bcx.tcx(),
847+
ty::re_static,
847848
ty::mk_mach_uint(ast::ty_u8));
848849
let fty = ty::mk_closure(bcx.tcx(), ty::ClosureTy {
849850
purity: ast::impure_fn,

src/librustc/middle/trans/monomorphize.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ pub fn normalize_for_monomorphization(tcx: ty::ctxt,
335335
// Traits have the same runtime representation as closures.
336336
Some(normalized_closure_ty(tcx, sigil))
337337
}
338-
ty::ty_ptr(_) => {
338+
ty::ty_ptr(*) => {
339339
Some(ty::mk_uint())
340340
}
341341
_ => {

src/librustc/middle/trans/reflect.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ impl Reflector {
201201
self.visit("uniq", extra)
202202
}
203203
}
204-
ty::ty_ptr(ref mt) => {
204+
ty::ty_ptr(_, ref mt) => {
205205
let extra = self.c_mt(mt);
206206
self.visit("ptr", extra)
207207
}
@@ -280,7 +280,9 @@ impl Reflector {
280280
let variants = ty::substd_enum_variants(ccx.tcx, did, substs);
281281
let llptrty = type_of(ccx, t).ptr_to();
282282
let opaquety = ty::get_opaque_ty(ccx.tcx).unwrap();
283-
let opaqueptrty = ty::mk_ptr(ccx.tcx, ty::mt { ty: opaquety, mutbl: ast::m_imm });
283+
let opaqueptrty = ty::mk_ptr(ccx.tcx,
284+
ty::re_static,
285+
ty::mt { ty: opaquety, mutbl: ast::m_imm });
284286

285287
let make_get_disr = || {
286288
let sub_path = bcx.fcx.path + &[path_name(special_idents::anon)];

src/librustc/middle/trans/type_of.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ pub fn type_of(cx: &mut CrateContext, t: ty::t) -> Type {
227227
let ty = type_of(cx, mt.ty);
228228
Type::vec(cx.sess.targ_cfg.arch, &ty)
229229
}
230-
ty::ty_ptr(ref mt) => type_of(cx, mt.ty).ptr_to(),
230+
ty::ty_ptr(_, ref mt) => type_of(cx, mt.ty).ptr_to(),
231231
ty::ty_rptr(_, ref mt) => type_of(cx, mt.ty).ptr_to(),
232232

233233
ty::ty_evec(ref mt, ty::vstore_slice(_)) => {

src/librustc/middle/trans/type_use.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,8 @@ pub fn type_needs_inner(cx: &Context,
233233
*/
234234
ty::ty_closure(*) |
235235
ty::ty_bare_fn(*) |
236-
ty::ty_ptr(_) |
237-
ty::ty_rptr(_, _) |
236+
ty::ty_ptr(*) |
237+
ty::ty_rptr(*) |
238238
ty::ty_trait(*) => false,
239239

240240
ty::ty_enum(did, ref substs) => {

0 commit comments

Comments
 (0)