Skip to content

Commit a837b8a

Browse files
author
Saleem Jaffer
committed
cleaner code as per review
1 parent 7fb1c22 commit a837b8a

File tree

11 files changed

+158
-211
lines changed

11 files changed

+158
-211
lines changed

src/librustc/mir/visit.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -737,16 +737,6 @@ macro_rules! make_mir_visitor {
737737
self.visit_local(local, context, location);
738738
}
739739
Place::Base(PlaceBase::Static(static_)) => {
740-
match static_.promoted {
741-
None => {
742-
self.visit_static(static_, context, location);
743-
}
744-
Some(_) => {
745-
self.visit_ty(
746-
& $($mutability)? static_.ty, TyContext::Location(location)
747-
);
748-
}
749-
}
750740
self.visit_static(static_, context, location);
751741
}
752742
Place::Projection(proj) => {

src/librustc_mir/borrow_check/error_reporting.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,13 +1602,10 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
16021602
self.append_local_to_string(local, buf)?;
16031603
}
16041604
Place::Base(PlaceBase::Static(ref static_)) => {
1605-
match static_.promoted {
1606-
Some(_) => {
1607-
buf.push_str("promoted");
1608-
}
1609-
None => {
1610-
buf.push_str(&self.infcx.tcx.item_name(static_.def_id).to_string());
1611-
}
1605+
if static_.promoted.is_some() {
1606+
buf.push_str("promoted");
1607+
} else {
1608+
buf.push_str(&self.infcx.tcx.item_name(static_.def_id).to_string());
16121609
}
16131610
}
16141611
Place::Projection(ref proj) => {

src/librustc_mir/borrow_check/mod.rs

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,15 +1309,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
13091309
// FIXME: allow thread-locals to borrow other thread locals?
13101310
let (might_be_alive, will_be_dropped) = match root_place {
13111311
Place::Base(PlaceBase::Static(st)) => {
1312-
match st.promoted {
1313-
None => {
1314-
// Thread-locals might be dropped after the function exits, but
1315-
// "true" statics will never be.
1316-
let is_thread_local = self.is_place_thread_local(&root_place);
1317-
(true, is_thread_local)
1318-
}
1319-
Some(_) => (true, false),
1320-
}
1312+
(true, st.promoted.is_none() && self.is_place_thread_local(&root_place))
13211313
}
13221314
Place::Base(PlaceBase::Local(_)) => {
13231315
// Locals are always dropped at function exit, and if they
@@ -1990,28 +1982,21 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
19901982
}
19911983
// The rules for promotion are made by `qualify_consts`, there wouldn't even be a
19921984
// `Place::Promoted` if the promotion weren't 100% legal. So we just forward this
1993-
// Place::Base(PlaceBase::Promoted(_)) => Ok(RootPlace {
1994-
// place,
1995-
// is_local_mutation_allowed,
1996-
// }),
19971985
Place::Base(PlaceBase::Static(ref static_)) => {
1998-
match static_.promoted {
1999-
Some(_) => {
1986+
if static_.promoted.is_some() {
1987+
Ok(RootPlace {
1988+
place,
1989+
is_local_mutation_allowed,
1990+
})
1991+
} else {
1992+
if self.infcx.tcx.is_static(static_.def_id) != Some(hir::Mutability::MutMutable) {
1993+
Err(place)
1994+
} else {
20001995
Ok(RootPlace {
20011996
place,
20021997
is_local_mutation_allowed,
20031998
})
20041999
}
2005-
None => {
2006-
if self.infcx.tcx.is_static(static_.def_id) != Some(hir::Mutability::MutMutable) {
2007-
Err(place)
2008-
} else {
2009-
Ok(RootPlace {
2010-
place,
2011-
is_local_mutation_allowed,
2012-
})
2013-
}
2014-
}
20152000
}
20162001
}
20172002
Place::Projection(ref proj) => {

src/librustc_mir/borrow_check/mutability_errors.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,14 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
130130
}
131131

132132
Place::Base(PlaceBase::Static(box Static { def_id, ty: _, promoted })) => {
133-
match promoted {
134-
Some(_) => unreachable!(),
135-
None => {
136-
if let Place::Base(PlaceBase::Static(_)) = access_place {
137-
item_msg = format!("immutable static item `{}`", access_place_desc.unwrap());
138-
reason = String::new();
139-
} else {
140-
item_msg = format!("`{}`", access_place_desc.unwrap());
141-
let static_name = &self.infcx.tcx.item_name(*def_id);
142-
reason = format!(", as `{}` is an immutable static item", static_name);
143-
}
144-
}
133+
assert!(promoted.is_none());
134+
if let Place::Base(PlaceBase::Static(_)) = access_place {
135+
item_msg = format!("immutable static item `{}`", access_place_desc.unwrap());
136+
reason = String::new();
137+
} else {
138+
item_msg = format!("`{}`", access_place_desc.unwrap());
139+
let static_name = &self.infcx.tcx.item_name(*def_id);
140+
reason = format!(", as `{}` is an immutable static item", static_name);
145141
}
146142
}
147143

src/librustc_mir/borrow_check/nll/type_check/mod.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -454,10 +454,9 @@ impl<'a, 'b, 'gcx, 'tcx> TypeVerifier<'a, 'b, 'gcx, 'tcx> {
454454
ty: self.mir.local_decls[index].ty,
455455
},
456456
Place::Base(PlaceBase::Static(box Static { def_id, ty: sty, promoted })) => {
457+
let sty = self.sanitize_type(place, sty);
457458
match promoted {
458459
Some(pr) => {
459-
let sty = self.sanitize_type(place, sty);
460-
461460
if !self.errors_reported {
462461
let promoted_mir = &self.mir.promoted[pr];
463462
self.sanitize_promoted(promoted_mir, location);
@@ -480,15 +479,18 @@ impl<'a, 'b, 'gcx, 'tcx> TypeVerifier<'a, 'b, 'gcx, 'tcx> {
480479
);
481480
};
482481
}
483-
PlaceTy::Ty { ty: sty }
484482
}
485483
None => {
486-
let sty = self.sanitize_type(place, sty);
487484
let ty = self.tcx().type_of(def_id);
488485
let ty = self.cx.normalize(ty, location);
489486
if let Err(terr) =
490487
self.cx
491-
.eq_types(ty, sty, location.to_locations(), ConstraintCategory::Boring)
488+
.eq_types(
489+
ty,
490+
sty,
491+
location.to_locations(),
492+
ConstraintCategory::Boring
493+
)
492494
{
493495
span_mirbug!(
494496
self,
@@ -498,10 +500,10 @@ impl<'a, 'b, 'gcx, 'tcx> TypeVerifier<'a, 'b, 'gcx, 'tcx> {
498500
sty,
499501
terr
500502
);
501-
}
502-
PlaceTy::Ty { ty: sty }
503+
};
503504
}
504505
}
506+
PlaceTy::Ty { ty: sty }
505507
}
506508
Place::Projection(ref proj) => {
507509
let base_context = if context.is_mutating_use() {

src/librustc_mir/borrow_check/place_ext.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,10 @@ impl<'tcx> PlaceExt<'tcx> for Place<'tcx> {
5050
}
5151
}
5252
Place::Base(PlaceBase::Static(static_)) => {
53-
match static_.promoted {
54-
Some(_) => false,
55-
None => {
56-
tcx.is_static(static_.def_id) == Some(hir::Mutability::MutMutable)
57-
}
53+
if static_.promoted.is_none() {
54+
tcx.is_static(static_.def_id) == Some(hir::Mutability::MutMutable)
55+
} else {
56+
false
5857
}
5958
}
6059
Place::Projection(proj) => match proj.elem {

src/librustc_mir/borrow_check/places_conflict.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ fn place_element_conflict<'a, 'gcx: 'tcx, 'tcx>(
387387
},
388388
(Some(p1), Some(p2)) => {
389389
if p1 == p2 {
390-
if let ty::Array(_, size) =s1.ty.sty {
390+
if let ty::Array(_, size) = s1.ty.sty {
391391
if size.unwrap_usize(tcx) == 0 {
392392
// Ignore conflicts with promoted [T; 0].
393393
debug!("place_element_conflict: IGNORE-LEN-0-PROMOTED");
@@ -404,7 +404,7 @@ fn place_element_conflict<'a, 'gcx: 'tcx, 'tcx>(
404404
}
405405
},
406406
(p1_, p2_) => {
407-
debug!("place_element_conflict: DISJOINT-STATIC-LOCAL-PROMOTED");
407+
debug!("place_element_conflict: DISJOINT-STATIC-PROMOTED");
408408
Overlap::Disjoint
409409
}
410410
}

src/librustc_mir/interpret/place.rs

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -582,39 +582,37 @@ where
582582
) -> EvalResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
583583
use rustc::mir::Place::*;
584584
use rustc::mir::PlaceBase;
585+
use rustc::mir::Static;
585586
Ok(match *mir_place {
586-
Base(PlaceBase::Static(ref static_)) => {
587-
match static_.promoted {
588-
Some(promoted) => {
589-
let instance = self.frame().instance;
590-
self.const_eval_raw(GlobalId {
591-
instance,
592-
promoted: Some(promoted),
593-
})?
594-
}
595-
None => {
596-
assert!(!static_.ty.needs_subst());
597-
let layout = self.layout_of(static_.ty)?;
598-
let instance = ty::Instance::mono(*self.tcx, static_.def_id);
599-
let cid = GlobalId {
600-
instance,
601-
promoted: None
602-
};
603-
// Just create a lazy reference, so we can support recursive statics.
604-
// tcx takes are of assigning every static one and only one unique AllocId.
605-
// When the data here is ever actually used, memory will notice,
606-
// and it knows how to deal with alloc_id that are present in the
607-
// global table but not in its local memory: It calls back into tcx through
608-
// a query, triggering the CTFE machinery to actually turn this lazy reference
609-
// into a bunch of bytes. IOW, statics are evaluated with CTFE even when
610-
// this EvalContext uses another Machine (e.g., in miri). This is what we
611-
// want! This way, computing statics works concistently between codegen
612-
// and miri: They use the same query to eventually obtain a `ty::Const`
613-
// and use that for further computation.
614-
let alloc = self.tcx.alloc_map.lock().intern_static(cid.instance.def_id());
615-
MPlaceTy::from_aligned_ptr(Pointer::from(alloc).with_default_tag(), layout)
616-
}
617-
}
587+
Base(PlaceBase::Static(box Static {promoted: Some(promoted), ty, ..})) => {
588+
let instance = self.frame().instance;
589+
self.const_eval_raw(GlobalId {
590+
instance,
591+
promoted: Some(promoted),
592+
})?
593+
}
594+
595+
Base(PlaceBase::Static(box Static {promoted: None, ty, def_id})) => {
596+
assert!(!ty.needs_subst());
597+
let layout = self.layout_of(ty)?;
598+
let instance = ty::Instance::mono(*self.tcx, def_id);
599+
let cid = GlobalId {
600+
instance,
601+
promoted: None
602+
};
603+
// Just create a lazy reference, so we can support recursive statics.
604+
// tcx takes are of assigning every static one and only one unique AllocId.
605+
// When the data here is ever actually used, memory will notice,
606+
// and it knows how to deal with alloc_id that are present in the
607+
// global table but not in its local memory: It calls back into tcx through
608+
// a query, triggering the CTFE machinery to actually turn this lazy reference
609+
// into a bunch of bytes. IOW, statics are evaluated with CTFE even when
610+
// this EvalContext uses another Machine (e.g., in miri). This is what we
611+
// want! This way, computing statics works concistently between codegen
612+
// and miri: They use the same query to eventually obtain a `ty::Const`
613+
// and use that for further computation.
614+
let alloc = self.tcx.alloc_map.lock().intern_static(cid.instance.def_id());
615+
MPlaceTy::from_aligned_ptr(Pointer::from(alloc).with_default_tag(), layout)
618616
}
619617

620618
_ => bug!("eval_place_to_mplace called on {:?}", mir_place),

src/librustc_mir/transform/check_unsafety.rs

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -301,32 +301,27 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
301301
// locals are safe
302302
}
303303
&Place::Base(PlaceBase::Static(box Static { def_id, ty: _, promoted })) => {
304-
match promoted {
305-
Some(..) => {
306-
bug!("unsafety checking should happen before promotion")
307-
}
308-
None => {
309-
if self.tcx.is_static(def_id) == Some(hir::Mutability::MutMutable) {
310-
self.require_unsafe("use of mutable static",
311-
"mutable statics can be mutated by multiple threads: aliasing violations \
312-
or data races will cause undefined behavior",
313-
UnsafetyViolationKind::General);
314-
} else if self.tcx.is_foreign_item(def_id) {
315-
let source_info = self.source_info;
316-
let lint_root =
317-
self.source_scope_local_data[source_info.scope].lint_root;
318-
self.register_violations(&[UnsafetyViolation {
319-
source_info,
320-
description: Symbol::intern("use of extern static").as_interned_str(),
321-
details:
322-
Symbol::intern("extern statics are not controlled by the Rust type \
323-
system: invalid data, aliasing violations or data \
324-
races will cause undefined behavior")
325-
.as_interned_str(),
326-
kind: UnsafetyViolationKind::ExternStatic(lint_root)
327-
}], &[]);
328-
}
329-
}
304+
assert!(promoted.is_none(), "unsafety checking should happen before promotion");
305+
306+
if self.tcx.is_static(def_id) == Some(hir::Mutability::MutMutable) {
307+
self.require_unsafe("use of mutable static",
308+
"mutable statics can be mutated by multiple threads: aliasing violations \
309+
or data races will cause undefined behavior",
310+
UnsafetyViolationKind::General);
311+
} else if self.tcx.is_foreign_item(def_id) {
312+
let source_info = self.source_info;
313+
let lint_root =
314+
self.source_scope_local_data[source_info.scope].lint_root;
315+
self.register_violations(&[UnsafetyViolation {
316+
source_info,
317+
description: Symbol::intern("use of extern static").as_interned_str(),
318+
details:
319+
Symbol::intern("extern statics are not controlled by the Rust type \
320+
system: invalid data, aliasing violations or data \
321+
races will cause undefined behavior")
322+
.as_interned_str(),
323+
kind: UnsafetyViolationKind::ExternStatic(lint_root)
324+
}], &[]);
330325
}
331326
}
332327
};

src/librustc_mir/transform/const_prop.rs

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ impl<'a, 'mir, 'tcx> ConstPropagator<'a, 'mir, 'tcx> {
266266
}
267267

268268
fn eval_place(&mut self, place: &Place<'tcx>, source_info: SourceInfo) -> Option<Const<'tcx>> {
269+
use rustc::mir::Static;
269270
match *place {
270271
Place::Base(PlaceBase::Local(loc)) => self.places[loc].clone(),
271272
Place::Projection(ref proj) => match proj.elem {
@@ -282,31 +283,25 @@ impl<'a, 'mir, 'tcx> ConstPropagator<'a, 'mir, 'tcx> {
282283
// an `Index` projection would throw us off-track.
283284
_ => None,
284285
},
285-
Place::Base(PlaceBase::Static(ref static_)) => {
286-
match static_.promoted {
287-
Some(promoted) => {
288-
let generics = self.tcx.generics_of(self.source.def_id());
289-
if generics.requires_monomorphization(self.tcx) {
290-
// FIXME: can't handle code with generics
291-
return None;
292-
}
293-
let substs = InternalSubsts::identity_for_item(self.tcx, self.source.def_id());
294-
let instance = Instance::new(self.source.def_id(), substs);
295-
let cid = GlobalId {
296-
instance,
297-
promoted: Some(promoted),
298-
};
299-
// cannot use `const_eval` here, because that would require having the MIR
300-
// for the current function available, but we're producing said MIR right now
301-
let res = self.use_ecx(source_info, |this| {
302-
eval_promoted(this.tcx, cid, this.mir, this.param_env)
303-
})?;
304-
trace!("evaluated promoted {:?} to {:?}", promoted, res);
305-
Some((res.into(), source_info.span))
306-
}
307-
None => None
286+
Place::Base(PlaceBase::Static(box Static {promoted: Some(promoted), ty, ..})) => {
287+
let generics = self.tcx.generics_of(self.source.def_id());
288+
if generics.requires_monomorphization(self.tcx) {
289+
// FIXME: can't handle code with generics
290+
return None;
308291
}
309-
292+
let substs = InternalSubsts::identity_for_item(self.tcx, self.source.def_id());
293+
let instance = Instance::new(self.source.def_id(), substs);
294+
let cid = GlobalId {
295+
instance,
296+
promoted: Some(promoted),
297+
};
298+
// cannot use `const_eval` here, because that would require having the MIR
299+
// for the current function available, but we're producing said MIR right now
300+
let res = self.use_ecx(source_info, |this| {
301+
eval_promoted(this.tcx, cid, this.mir, this.param_env)
302+
})?;
303+
trace!("evaluated promoted {:?} to {:?}", promoted, res);
304+
Some((res.into(), source_info.span))
310305
},
311306
_ => None,
312307
}

0 commit comments

Comments
 (0)