Skip to content

Commit 924898f

Browse files
committed
Auto merge of #41917 - arielb1:mir-array, r=nagisa
Translate array drop glue using MIR I was a bit lazy here and used a usize-based index instead of a pointer iteration. Do you think this is important @eddyb? r? @eddyb
2 parents 41e74a2 + ee982d4 commit 924898f

File tree

37 files changed

+999
-454
lines changed

37 files changed

+999
-454
lines changed

src/librustc/ich/impls_mir.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,8 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for mir::Rvalue<'tcx>
315315
mir::Rvalue::Discriminant(ref lvalue) => {
316316
lvalue.hash_stable(hcx, hasher);
317317
}
318-
mir::Rvalue::Box(ty) => {
318+
mir::Rvalue::NullaryOp(op, ty) => {
319+
op.hash_stable(hcx, hasher);
319320
ty.hash_stable(hcx, hasher);
320321
}
321322
mir::Rvalue::Aggregate(ref kind, ref operands) => {
@@ -374,14 +375,19 @@ impl_stable_hash_for!(enum mir::BinOp {
374375
Le,
375376
Ne,
376377
Ge,
377-
Gt
378+
Gt,
379+
Offset
378380
});
379381

380382
impl_stable_hash_for!(enum mir::UnOp {
381383
Not,
382384
Neg
383385
});
384386

387+
impl_stable_hash_for!(enum mir::NullOp {
388+
Box,
389+
SizeOf
390+
});
385391

386392
impl_stable_hash_for!(struct mir::Constant<'tcx> { span, ty, literal });
387393

src/librustc/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
#![cfg_attr(stage0, feature(staged_api))]
4848
#![cfg_attr(stage0, feature(loop_break_value))]
4949

50-
#![recursion_limit="128"]
50+
#![recursion_limit="192"]
5151

5252
extern crate arena;
5353
extern crate core;

src/librustc/middle/cstore.rs

-2
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,6 @@ pub trait CrateStore {
245245

246246
// flags
247247
fn is_const_fn(&self, did: DefId) -> bool;
248-
fn is_default_impl(&self, impl_did: DefId) -> bool;
249248
fn is_dllimport_foreign_item(&self, def: DefId) -> bool;
250249
fn is_statically_included_foreign_item(&self, def_id: DefId) -> bool;
251250

@@ -364,7 +363,6 @@ impl CrateStore for DummyCrateStore {
364363

365364
// flags
366365
fn is_const_fn(&self, did: DefId) -> bool { bug!("is_const_fn") }
367-
fn is_default_impl(&self, impl_did: DefId) -> bool { bug!("is_default_impl") }
368366
fn is_dllimport_foreign_item(&self, id: DefId) -> bool { false }
369367
fn is_statically_included_foreign_item(&self, def_id: DefId) -> bool { false }
370368

src/librustc/mir/mod.rs

+14-6
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,7 @@ pub enum Rvalue<'tcx> {
10461046
BinaryOp(BinOp, Operand<'tcx>, Operand<'tcx>),
10471047
CheckedBinaryOp(BinOp, Operand<'tcx>, Operand<'tcx>),
10481048

1049+
NullaryOp(NullOp, Ty<'tcx>),
10491050
UnaryOp(UnOp, Operand<'tcx>),
10501051

10511052
/// Read the discriminant of an ADT.
@@ -1054,9 +1055,6 @@ pub enum Rvalue<'tcx> {
10541055
/// be defined to return, say, a 0) if ADT is not an enum.
10551056
Discriminant(Lvalue<'tcx>),
10561057

1057-
/// Creates an *uninitialized* Box
1058-
Box(Ty<'tcx>),
1059-
10601058
/// Create an aggregate value, like a tuple or struct. This is
10611059
/// only needed because we want to distinguish `dest = Foo { x:
10621060
/// ..., y: ... }` from `dest.x = ...; dest.y = ...;` in the case
@@ -1132,6 +1130,8 @@ pub enum BinOp {
11321130
Ge,
11331131
/// The `>` operator (greater than)
11341132
Gt,
1133+
/// The `ptr.offset` operator
1134+
Offset,
11351135
}
11361136

11371137
impl BinOp {
@@ -1144,6 +1144,14 @@ impl BinOp {
11441144
}
11451145
}
11461146

1147+
#[derive(Copy, Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
1148+
pub enum NullOp {
1149+
/// Return the size of a value of that type
1150+
SizeOf,
1151+
/// Create a new uninitialized box for a value of that type
1152+
Box,
1153+
}
1154+
11471155
#[derive(Copy, Clone, Debug, PartialEq, Eq, RustcEncodable, RustcDecodable)]
11481156
pub enum UnOp {
11491157
/// The `!` operator for logical inversion
@@ -1167,7 +1175,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
11671175
}
11681176
UnaryOp(ref op, ref a) => write!(fmt, "{:?}({:?})", op, a),
11691177
Discriminant(ref lval) => write!(fmt, "discriminant({:?})", lval),
1170-
Box(ref t) => write!(fmt, "Box({:?})", t),
1178+
NullaryOp(ref op, ref t) => write!(fmt, "{:?}({:?})", op, t),
11711179
Ref(_, borrow_kind, ref lv) => {
11721180
let kind_str = match borrow_kind {
11731181
BorrowKind::Shared => "",
@@ -1601,7 +1609,7 @@ impl<'tcx> TypeFoldable<'tcx> for Rvalue<'tcx> {
16011609
CheckedBinaryOp(op, rhs.fold_with(folder), lhs.fold_with(folder)),
16021610
UnaryOp(op, ref val) => UnaryOp(op, val.fold_with(folder)),
16031611
Discriminant(ref lval) => Discriminant(lval.fold_with(folder)),
1604-
Box(ty) => Box(ty.fold_with(folder)),
1612+
NullaryOp(op, ty) => NullaryOp(op, ty.fold_with(folder)),
16051613
Aggregate(ref kind, ref fields) => {
16061614
let kind = box match **kind {
16071615
AggregateKind::Array(ty) => AggregateKind::Array(ty.fold_with(folder)),
@@ -1629,7 +1637,7 @@ impl<'tcx> TypeFoldable<'tcx> for Rvalue<'tcx> {
16291637
rhs.visit_with(visitor) || lhs.visit_with(visitor),
16301638
UnaryOp(_, ref val) => val.visit_with(visitor),
16311639
Discriminant(ref lval) => lval.visit_with(visitor),
1632-
Box(ty) => ty.visit_with(visitor),
1640+
NullaryOp(_, ty) => ty.visit_with(visitor),
16331641
Aggregate(ref kind, ref fields) => {
16341642
(match **kind {
16351643
AggregateKind::Array(ty) => ty.visit_with(visitor),

src/librustc/mir/tcx.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ impl<'tcx> Rvalue<'tcx> {
166166
let ty = op.ty(tcx, lhs_ty, rhs_ty);
167167
tcx.intern_tup(&[ty, tcx.types.bool], false)
168168
}
169-
Rvalue::UnaryOp(_, ref operand) => {
169+
Rvalue::UnaryOp(UnOp::Not, ref operand) |
170+
Rvalue::UnaryOp(UnOp::Neg, ref operand) => {
170171
operand.ty(mir, tcx)
171172
}
172173
Rvalue::Discriminant(ref lval) => {
@@ -179,9 +180,8 @@ impl<'tcx> Rvalue<'tcx> {
179180
bug!("Rvalue::Discriminant on Lvalue of type {:?}", ty);
180181
}
181182
}
182-
Rvalue::Box(t) => {
183-
tcx.mk_box(t)
184-
}
183+
Rvalue::NullaryOp(NullOp::Box, t) => tcx.mk_box(t),
184+
Rvalue::NullaryOp(NullOp::SizeOf, _) => tcx.types.usize,
185185
Rvalue::Aggregate(ref ak, ref ops) => {
186186
match **ak {
187187
AggregateKind::Array(ty) => {
@@ -227,7 +227,7 @@ impl<'tcx> BinOp {
227227
assert_eq!(lhs_ty, rhs_ty);
228228
lhs_ty
229229
}
230-
&BinOp::Shl | &BinOp::Shr => {
230+
&BinOp::Shl | &BinOp::Shr | &BinOp::Offset => {
231231
lhs_ty // lhs_ty can be != rhs_ty
232232
}
233233
&BinOp::Eq | &BinOp::Lt | &BinOp::Le |
@@ -270,7 +270,8 @@ impl BinOp {
270270
BinOp::Lt => hir::BinOp_::BiLt,
271271
BinOp::Gt => hir::BinOp_::BiGt,
272272
BinOp::Le => hir::BinOp_::BiLe,
273-
BinOp::Ge => hir::BinOp_::BiGe
273+
BinOp::Ge => hir::BinOp_::BiGe,
274+
BinOp::Offset => unreachable!()
274275
}
275276
}
276277
}

src/librustc/mir/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ macro_rules! make_mir_visitor {
509509
self.visit_lvalue(lvalue, LvalueContext::Inspect, location);
510510
}
511511

512-
Rvalue::Box(ref $($mutability)* ty) => {
512+
Rvalue::NullaryOp(_op, ref $($mutability)* ty) => {
513513
self.visit_ty(ty);
514514
}
515515

src/librustc/ty/item_path.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -218,15 +218,15 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
218218

219219
// Always use types for non-local impls, where types are always
220220
// available, and filename/line-number is mostly uninteresting.
221-
let use_types = !impl_def_id.is_local() || {
221+
let use_types = !self.is_default_impl(impl_def_id) && (!impl_def_id.is_local() || {
222222
// Otherwise, use filename/line-number if forced.
223223
let force_no_types = FORCE_IMPL_FILENAME_LINE.with(|f| f.get());
224224
!force_no_types && {
225225
// Otherwise, use types if we can query them without inducing a cycle.
226226
ty::queries::impl_trait_ref::try_get(self, DUMMY_SP, impl_def_id).is_ok() &&
227227
ty::queries::type_of::try_get(self, DUMMY_SP, impl_def_id).is_ok()
228228
}
229-
};
229+
});
230230

231231
if !use_types {
232232
return self.push_impl_path_fallback(buffer, impl_def_id);

src/librustc/ty/maps.rs

+3
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,9 @@ define_maps! { <'tcx>
774774
/// True if this is a foreign item (i.e., linked via `extern { ... }`).
775775
[] is_foreign_item: IsForeignItem(DefId) -> bool,
776776

777+
/// True if this is a default impl (aka impl Foo for ..)
778+
[] is_default_impl: ItemSignature(DefId) -> bool,
779+
777780
/// Get a map with the variance of every item; use `item_variance`
778781
/// instead.
779782
[] crate_variances: crate_variances(CrateNum) -> Rc<ty::CrateVariancesMap>,

src/librustc/ty/util.rs

+9
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,15 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
600600
bug!("empty_substs_for_def_id: {:?} has type parameters", item_def_id)
601601
})
602602
}
603+
604+
pub fn const_usize(&self, val: u16) -> ConstInt {
605+
match self.sess.target.uint_type {
606+
ast::UintTy::U16 => ConstInt::Usize(ConstUsize::Us16(val as u16)),
607+
ast::UintTy::U32 => ConstInt::Usize(ConstUsize::Us32(val as u32)),
608+
ast::UintTy::U64 => ConstInt::Usize(ConstUsize::Us64(val as u64)),
609+
_ => bug!(),
610+
}
611+
}
603612
}
604613

605614
pub struct TypeIdHasher<'a, 'gcx: 'a+'tcx, 'tcx: 'a, W> {

src/librustc_borrowck/borrowck/mir/elaborate_drops.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc::util::nodemap::FxHashMap;
2222
use rustc_data_structures::indexed_set::IdxSetBuf;
2323
use rustc_data_structures::indexed_vec::Idx;
2424
use rustc_mir::util::patch::MirPatch;
25-
use rustc_mir::util::elaborate_drops::{DropFlagState, elaborate_drop};
25+
use rustc_mir::util::elaborate_drops::{DropFlagState, Unwind, elaborate_drop};
2626
use rustc_mir::util::elaborate_drops::{DropElaborator, DropStyle, DropFlagMode};
2727
use syntax::ast;
2828
use syntax_pos::Span;
@@ -399,14 +399,13 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
399399
ctxt: self
400400
},
401401
terminator.source_info,
402-
data.is_cleanup,
403402
location,
404403
path,
405404
target,
406405
if data.is_cleanup {
407-
None
406+
Unwind::InCleanup
408407
} else {
409-
Some(Option::unwrap_or(unwind, resume_block))
408+
Unwind::To(Option::unwrap_or(unwind, resume_block))
410409
},
411410
bb)
412411
}
@@ -455,6 +454,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
455454
let bb = loc.block;
456455
let data = &self.mir[bb];
457456
let terminator = data.terminator();
457+
assert!(!data.is_cleanup, "DropAndReplace in unwind path not supported");
458458

459459
let assign = Statement {
460460
kind: StatementKind::Assign(location.clone(), Rvalue::Use(value.clone())),
@@ -477,7 +477,7 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
477477
kind: TerminatorKind::Goto { target: target },
478478
..*terminator
479479
}),
480-
is_cleanup: data.is_cleanup,
480+
is_cleanup: false,
481481
});
482482

483483
match self.move_data().rev_lookup.find(location) {
@@ -491,11 +491,10 @@ impl<'b, 'tcx> ElaborateDropsCtxt<'b, 'tcx> {
491491
ctxt: self
492492
},
493493
terminator.source_info,
494-
data.is_cleanup,
495494
location,
496495
path,
497496
target,
498-
Some(unwind),
497+
Unwind::To(unwind),
499498
bb);
500499
on_all_children_bits(self.tcx, self.mir, self.move_data(), path, |child| {
501500
self.set_drop_flag(Location { block: target, statement_index: 0 },

src/librustc_borrowck/borrowck/mir/gather_moves.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,8 @@ impl<'a, 'tcx> MoveDataBuilder<'a, 'tcx> {
438438
Rvalue::Ref(..) |
439439
Rvalue::Discriminant(..) |
440440
Rvalue::Len(..) |
441-
Rvalue::Box(..) => {
441+
Rvalue::NullaryOp(NullOp::SizeOf, _) |
442+
Rvalue::NullaryOp(NullOp::Box, _) => {
442443
// This returns an rvalue with uninitialized contents. We can't
443444
// move out of it here because it is an rvalue - assignments always
444445
// completely initialize their lvalue.

src/librustc_borrowck/borrowck/mir/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub fn borrowck_mir(bcx: &mut BorrowckCtxt,
5959
attributes: &[ast::Attribute]) {
6060
let tcx = bcx.tcx;
6161
let def_id = tcx.hir.local_def_id(id);
62-
debug!("borrowck_mir({}) UNIMPLEMENTED", tcx.item_path_str(def_id));
62+
debug!("borrowck_mir({:?}) UNIMPLEMENTED", def_id);
6363

6464
// It is safe for us to borrow `mir_validated()`: `optimized_mir`
6565
// steals it, but it forces the `borrowck` query.

src/librustc_data_structures/indexed_vec.rs

+7
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,13 @@ impl<I: Idx, T> IndexMut<I> for IndexVec<I, T> {
212212
}
213213
}
214214

215+
impl<I: Idx, T> Default for IndexVec<I, T> {
216+
#[inline]
217+
fn default() -> Self {
218+
Self::new()
219+
}
220+
}
221+
215222
impl<I: Idx, T> Extend<T> for IndexVec<I, T> {
216223
#[inline]
217224
fn extend<J: IntoIterator<Item = T>>(&mut self, iter: J) {

src/librustc_metadata/cstore_impl.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ provide! { <'tcx> tcx, def_id, cdata
106106
closure_type => { cdata.closure_ty(def_id.index, tcx) }
107107
inherent_impls => { Rc::new(cdata.get_inherent_implementations_for_type(def_id.index)) }
108108
is_foreign_item => { cdata.is_foreign_item(def_id.index) }
109+
is_default_impl => { cdata.is_default_impl(def_id.index) }
109110
describe_def => { cdata.get_def(def_id.index) }
110111
def_span => { cdata.get_span(def_id.index, &tcx.sess) }
111112
stability => { cdata.get_stability(def_id.index) }
@@ -176,11 +177,6 @@ impl CrateStore for cstore::CStore {
176177
self.get_crate_data(did.krate).is_const_fn(did.index)
177178
}
178179

179-
fn is_default_impl(&self, impl_did: DefId) -> bool {
180-
self.dep_graph.read(DepNode::MetaData(impl_did));
181-
self.get_crate_data(impl_did.krate).is_default_impl(impl_did.index)
182-
}
183-
184180
fn is_statically_included_foreign_item(&self, def_id: DefId) -> bool
185181
{
186182
self.do_is_statically_included_foreign_item(def_id)
@@ -403,7 +399,7 @@ impl CrateStore for cstore::CStore {
403399
}
404400

405401
self.dep_graph.read(DepNode::MetaData(def_id));
406-
debug!("item_body({}): inlining item", tcx.item_path_str(def_id));
402+
debug!("item_body({:?}): inlining item", def_id);
407403

408404
self.get_crate_data(def_id.krate).item_body(tcx, def_id.index)
409405
}
@@ -515,4 +511,4 @@ impl CrateStore for cstore::CStore {
515511
drop(visible_parent_map);
516512
self.visible_parent_map.borrow()
517513
}
518-
}
514+
}

src/librustc_mir/build/expr/as_rvalue.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
9797
let value = this.hir.mirror(value);
9898
let result = this.temp(expr.ty, expr_span);
9999
// to start, malloc some memory of suitable type (thus far, uninitialized):
100-
this.cfg.push_assign(block, source_info, &result, Rvalue::Box(value.ty));
100+
let box_ = Rvalue::NullaryOp(NullOp::Box, value.ty);
101+
this.cfg.push_assign(block, source_info, &result, box_);
101102
this.in_scope(value_extents, block, |this| {
102103
// schedule a shallow free of that memory, lest we unwind:
103104
this.schedule_box_free(expr_span, value_extents, &result, value.ty);

src/librustc_mir/shim.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,10 @@ fn build_drop_shim<'a, 'tcx>(tcx: ty::TyCtxt<'a, 'tcx, 'tcx>,
198198
elaborate_drops::elaborate_drop(
199199
&mut elaborator,
200200
source_info,
201-
false,
202201
&dropee,
203202
(),
204203
return_block,
205-
Some(resume_block),
204+
elaborate_drops::Unwind::To(resume_block),
206205
START_BLOCK
207206
);
208207
elaborator.patch

src/librustc_mir/transform/erase_regions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl<'a, 'tcx> MutVisitor<'tcx> for EraseRegionsVisitor<'a, 'tcx> {
5353
Rvalue::CheckedBinaryOp(..) |
5454
Rvalue::UnaryOp(..) |
5555
Rvalue::Discriminant(..) |
56-
Rvalue::Box(..) |
56+
Rvalue::NullaryOp(..) |
5757
Rvalue::Aggregate(..) => {
5858
// These variants don't contain regions.
5959
}

0 commit comments

Comments
 (0)