Skip to content

Commit 6ee8595

Browse files
authored
Merge pull request #147 from oli-obk/rustup
rustup to rustc 1.17.0-nightly (60a0edc6c 2017-02-26)
2 parents ed0feee + 80be25e commit 6ee8595

File tree

12 files changed

+357
-176
lines changed

12 files changed

+357
-176
lines changed

src/error.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
use std::error::Error;
22
use std::fmt;
33
use rustc::mir;
4-
use rustc::ty::{BareFnTy, Ty, FnSig, layout};
5-
use syntax::abi::Abi;
4+
use rustc::ty::{FnSig, Ty, layout};
65
use memory::{Pointer, Function};
76
use rustc_const_math::ConstMathErr;
87
use syntax::codemap::Span;
98

109
#[derive(Clone, Debug)]
1110
pub enum EvalError<'tcx> {
12-
FunctionPointerTyMismatch(Abi, &'tcx FnSig<'tcx>, &'tcx BareFnTy<'tcx>),
11+
FunctionPointerTyMismatch(FnSig<'tcx>, FnSig<'tcx>),
1312
NoMirFor(String),
1413
UnterminatedCString(Pointer),
1514
DanglingPointerDeref,
@@ -151,8 +150,8 @@ impl<'tcx> fmt::Display for EvalError<'tcx> {
151150
ptr.offset, ptr.offset + size, ptr.alloc_id, allocation_size)
152151
},
153152
EvalError::NoMirFor(ref func) => write!(f, "no mir for `{}`", func),
154-
EvalError::FunctionPointerTyMismatch(abi, sig, got) =>
155-
write!(f, "tried to call a function with abi {:?} and sig {:?} through a function pointer of type {:?}", abi, sig, got),
153+
EvalError::FunctionPointerTyMismatch(sig, got) =>
154+
write!(f, "tried to call a function with sig {} through a function pointer of type {}", sig, got),
156155
EvalError::ArrayIndexOutOfBounds(span, len, index) =>
157156
write!(f, "index out of bounds: the len is {} but the index is {} at {:?}", len, index, span),
158157
EvalError::Math(span, ref err) =>

src/eval_context.rs

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc::mir;
99
use rustc::traits::Reveal;
1010
use rustc::ty::layout::{self, Layout, Size};
1111
use rustc::ty::subst::{self, Subst, Substs};
12-
use rustc::ty::{self, Ty, TyCtxt, TypeFoldable};
12+
use rustc::ty::{self, Ty, TyCtxt, TypeFoldable, Binder};
1313
use rustc_data_structures::indexed_vec::Idx;
1414
use syntax::codemap::{self, DUMMY_SP};
1515

@@ -181,8 +181,6 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
181181

182182
Float(ConstFloat::F32(f)) => PrimVal::from_f32(f),
183183
Float(ConstFloat::F64(f)) => PrimVal::from_f64(f),
184-
Float(ConstFloat::FInfer { .. }) =>
185-
bug!("uninferred constants only exist before typeck"),
186184

187185
Bool(b) => PrimVal::from_bool(b),
188186
Char(c) => PrimVal::from_char(c),
@@ -196,7 +194,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
196194

197195
Struct(_) => unimplemented!(),
198196
Tuple(_) => unimplemented!(),
199-
Function(_) => unimplemented!(),
197+
Function(_, _) => unimplemented!(),
200198
Array(_) => unimplemented!(),
201199
Repeat(_, _) => unimplemented!(),
202200
};
@@ -227,6 +225,13 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
227225
self.tcx.normalize_associated_type(&substituted)
228226
}
229227

228+
pub fn erase_lifetimes<T>(&self, value: &Binder<T>) -> T
229+
where T : TypeFoldable<'tcx>
230+
{
231+
let value = self.tcx.erase_late_bound_regions(value);
232+
self.tcx.erase_regions(&value)
233+
}
234+
230235
pub(super) fn type_size(&self, ty: Ty<'tcx>) -> EvalResult<'tcx, Option<u64>> {
231236
self.type_size_with_substs(ty, self.substs())
232237
}
@@ -457,7 +462,10 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
457462

458463
General { discr, ref variants, .. } => {
459464
if let mir::AggregateKind::Adt(adt_def, variant, _, _) = *kind {
460-
let discr_val = adt_def.variants[variant].disr_val;
465+
let discr_val = adt_def.discriminants(self.tcx)
466+
.nth(variant)
467+
.expect("broken mir: Adt variant id invalid")
468+
.to_u128_unchecked();
461469
let discr_size = discr.size().bytes();
462470
if variants[variant].packed {
463471
let ptr = self.force_allocation(dest)?.to_ptr_and_extra().0;
@@ -530,7 +538,10 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
530538
CEnum { .. } => {
531539
assert_eq!(operands.len(), 0);
532540
if let mir::AggregateKind::Adt(adt_def, variant, _, _) = *kind {
533-
let n = adt_def.variants[variant].disr_val;
541+
let n = adt_def.discriminants(self.tcx)
542+
.nth(variant)
543+
.expect("broken mir: Adt variant index invalid")
544+
.to_u128_unchecked();
534545
self.write_primval(dest, PrimVal::Bytes(n), dest_ty)?;
535546
} else {
536547
bug!("tried to assign {:?} to Layout::CEnum", kind);
@@ -640,25 +651,29 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
640651
}
641652

642653
ReifyFnPointer => match self.operand_ty(operand).sty {
643-
ty::TyFnDef(def_id, substs, fn_ty) => {
644-
let fn_ty = self.tcx.erase_regions(&fn_ty);
645-
let fn_ptr = self.memory.create_fn_ptr(self.tcx,def_id, substs, fn_ty);
654+
ty::TyFnDef(def_id, substs, sig) => {
655+
let fn_ptr = self.memory.create_fn_ptr(def_id, substs, sig);
646656
self.write_value(Value::ByVal(PrimVal::Ptr(fn_ptr)), dest, dest_ty)?;
647657
},
648658
ref other => bug!("reify fn pointer on {:?}", other),
649659
},
650660

651661
UnsafeFnPointer => match dest_ty.sty {
652-
ty::TyFnPtr(unsafe_fn_ty) => {
662+
ty::TyFnPtr(_) => {
653663
let src = self.eval_operand(operand)?;
654-
let ptr = src.read_ptr(&self.memory)?;
655-
let fn_def = self.memory.get_fn(ptr.alloc_id)?.expect_concrete()?;
656-
let unsafe_fn_ty = self.tcx.erase_regions(&unsafe_fn_ty);
657-
let fn_ptr = self.memory.create_fn_ptr(self.tcx, fn_def.def_id, fn_def.substs, unsafe_fn_ty);
658-
self.write_value(Value::ByVal(PrimVal::Ptr(fn_ptr)), dest, dest_ty)?;
664+
self.write_value(src, dest, dest_ty)?;
659665
},
660666
ref other => bug!("fn to unsafe fn cast on {:?}", other),
661667
},
668+
669+
ClosureFnPointer => match self.operand_ty(operand).sty {
670+
ty::TyClosure(def_id, substs) => {
671+
let fn_ty = self.tcx.closure_type(def_id);
672+
let fn_ptr = self.memory.create_fn_ptr_from_noncapture_closure(def_id, substs, fn_ty);
673+
self.write_value(Value::ByVal(PrimVal::Ptr(fn_ptr)), dest, dest_ty)?;
674+
},
675+
ref other => bug!("reify fn pointer on {:?}", other),
676+
},
662677
}
663678
}
664679

@@ -668,7 +683,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
668683
let ptr = self.force_allocation(lval)?.to_ptr();
669684
let discr_val = self.read_discriminant_value(ptr, ty)?;
670685
if let ty::TyAdt(adt_def, _) = ty.sty {
671-
if adt_def.variants.iter().all(|v| discr_val != v.disr_val) {
686+
if adt_def.discriminants(self.tcx).all(|v| discr_val != v.to_u128_unchecked()) {
672687
return Err(EvalError::InvalidDiscriminant);
673688
}
674689
} else {

src/lvalue.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
137137
Local(mir::RETURN_POINTER) => self.frame().return_lvalue,
138138
Local(local) => Lvalue::Local { frame: self.stack.len() - 1, local, field: None },
139139

140-
Static(def_id) => {
140+
Static(ref static_) => {
141141
let substs = self.tcx.intern_substs(&[]);
142-
Lvalue::Global(GlobalId { def_id, substs, promoted: None })
142+
Lvalue::Global(GlobalId { def_id: static_.def_id, substs, promoted: None })
143143
}
144144

145145
Projection(ref proj) => return self.eval_lvalue_projection(proj),

src/memory.rs

Lines changed: 26 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ use std::collections::{btree_map, BTreeMap, HashMap, HashSet, VecDeque, BTreeSet
33
use std::{fmt, iter, ptr, mem, io};
44

55
use rustc::hir::def_id::DefId;
6-
use rustc::ty::{self, BareFnTy, ClosureTy, ClosureSubsts, TyCtxt};
6+
use rustc::ty::{self, PolyFnSig, ClosureSubsts};
77
use rustc::ty::subst::Substs;
88
use rustc::ty::layout::{self, TargetDataLayout};
99

10-
use syntax::abi::Abi;
11-
1210
use error::{EvalError, EvalResult};
1311
use value::PrimVal;
1412

@@ -109,8 +107,7 @@ impl Pointer {
109107
pub struct FunctionDefinition<'tcx> {
110108
pub def_id: DefId,
111109
pub substs: &'tcx Substs<'tcx>,
112-
pub abi: Abi,
113-
pub sig: &'tcx ty::FnSig<'tcx>,
110+
pub sig: PolyFnSig<'tcx>,
114111
}
115112

116113
/// Either a concrete function, or a glue function
@@ -127,18 +124,14 @@ pub enum Function<'tcx> {
127124
DropGlue(ty::Ty<'tcx>),
128125
/// Glue required to treat the ptr part of a fat pointer
129126
/// as a function pointer
130-
FnPtrAsTraitObject(&'tcx ty::FnSig<'tcx>),
127+
FnPtrAsTraitObject(PolyFnSig<'tcx>),
131128
/// Glue for Closures
132129
Closure(FunctionDefinition<'tcx>),
130+
/// Glue for noncapturing closures casted to function pointers
131+
NonCaptureClosureAsFnPtr(FunctionDefinition<'tcx>),
133132
}
134133

135134
impl<'tcx> Function<'tcx> {
136-
pub fn expect_concrete(self) -> EvalResult<'tcx, FunctionDefinition<'tcx>> {
137-
match self {
138-
Function::Concrete(fn_def) => Ok(fn_def),
139-
other => Err(EvalError::ExpectedConcreteFunction(other)),
140-
}
141-
}
142135
pub fn expect_drop_glue_real_ty(self) -> EvalResult<'tcx, ty::Ty<'tcx>> {
143136
match self {
144137
Function::DropGlue(real_ty) => Ok(real_ty),
@@ -221,50 +214,43 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
221214
self.alloc_map.iter()
222215
}
223216

224-
pub fn create_closure_ptr(&mut self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId, substs: ClosureSubsts<'tcx>, fn_ty: ClosureTy<'tcx>) -> Pointer {
225-
// FIXME: this is a hack
226-
let fn_ty = tcx.mk_bare_fn(ty::BareFnTy {
227-
unsafety: fn_ty.unsafety,
228-
abi: fn_ty.abi,
229-
sig: fn_ty.sig,
230-
});
217+
pub fn create_closure_ptr(&mut self, def_id: DefId, substs: ClosureSubsts<'tcx>, sig: PolyFnSig<'tcx>) -> Pointer {
231218
self.create_fn_alloc(Function::Closure(FunctionDefinition {
232219
def_id,
233220
substs: substs.substs,
234-
abi: fn_ty.abi,
235-
// FIXME: why doesn't this compile?
236-
//sig: tcx.erase_late_bound_regions(&fn_ty.sig),
237-
sig: fn_ty.sig.skip_binder(),
221+
sig,
222+
}))
223+
}
224+
225+
pub fn create_fn_ptr_from_noncapture_closure(&mut self, def_id: DefId, substs: ClosureSubsts<'tcx>, sig: PolyFnSig<'tcx>) -> Pointer {
226+
self.create_fn_alloc(Function::NonCaptureClosureAsFnPtr(FunctionDefinition {
227+
def_id,
228+
substs: substs.substs,
229+
sig,
238230
}))
239231
}
240232

241-
pub fn create_fn_as_trait_glue(&mut self, _tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId, substs: &'tcx Substs<'tcx>, fn_ty: &'tcx BareFnTy<'tcx>) -> Pointer {
233+
pub fn create_fn_as_trait_glue(&mut self, def_id: DefId, substs: &'tcx Substs, sig: PolyFnSig<'tcx>) -> Pointer {
242234
self.create_fn_alloc(Function::FnDefAsTraitObject(FunctionDefinition {
243235
def_id,
244236
substs,
245-
abi: fn_ty.abi,
246-
// FIXME: why doesn't this compile?
247-
//sig: tcx.erase_late_bound_regions(&fn_ty.sig),
248-
sig: fn_ty.sig.skip_binder(),
237+
sig,
249238
}))
250239
}
251240

252-
pub fn create_fn_ptr_as_trait_glue(&mut self, fn_ty: &'tcx BareFnTy<'tcx>) -> Pointer {
253-
self.create_fn_alloc(Function::FnPtrAsTraitObject(fn_ty.sig.skip_binder()))
241+
pub fn create_fn_ptr_as_trait_glue(&mut self, sig: PolyFnSig<'tcx>) -> Pointer {
242+
self.create_fn_alloc(Function::FnPtrAsTraitObject(sig))
254243
}
255244

256245
pub fn create_drop_glue(&mut self, ty: ty::Ty<'tcx>) -> Pointer {
257246
self.create_fn_alloc(Function::DropGlue(ty))
258247
}
259248

260-
pub fn create_fn_ptr(&mut self, _tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId, substs: &'tcx Substs<'tcx>, fn_ty: &'tcx BareFnTy<'tcx>) -> Pointer {
249+
pub fn create_fn_ptr(&mut self, def_id: DefId, substs: &'tcx Substs, sig: PolyFnSig<'tcx>) -> Pointer {
261250
self.create_fn_alloc(Function::Concrete(FunctionDefinition {
262251
def_id,
263252
substs,
264-
abi: fn_ty.abi,
265-
// FIXME: why doesn't this compile?
266-
//sig: tcx.erase_late_bound_regions(&fn_ty.sig),
267-
sig: fn_ty.sig.skip_binder(),
253+
sig,
268254
}))
269255
}
270256

@@ -535,6 +521,10 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
535521
trace!("{} closure glue for {}", msg, dump_fn_def(fn_def));
536522
continue;
537523
},
524+
(None, Some(&Function::NonCaptureClosureAsFnPtr(fn_def))) => {
525+
trace!("{} non-capture closure as fn ptr glue for {}", msg, dump_fn_def(fn_def));
526+
continue;
527+
},
538528
(None, None) => {
539529
trace!("{} (deallocated)", msg);
540530
continue;
@@ -606,12 +596,7 @@ impl<'a, 'tcx> Memory<'a, 'tcx> {
606596

607597
fn dump_fn_def<'tcx>(fn_def: FunctionDefinition<'tcx>) -> String {
608598
let name = ty::tls::with(|tcx| tcx.item_path_str(fn_def.def_id));
609-
let abi = if fn_def.abi == Abi::Rust {
610-
format!("")
611-
} else {
612-
format!("extern {} ", fn_def.abi)
613-
};
614-
format!("function pointer: {}: {}{}", name, abi, fn_def.sig)
599+
format!("function pointer: {}: {}", name, fn_def.sig.skip_binder())
615600
}
616601

617602
/// Byte accessors

src/step.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,8 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for ConstantExtractor<'a, 'b, 'tcx> {
242242
location: mir::Location
243243
) {
244244
self.super_lvalue(lvalue, context, location);
245-
if let mir::Lvalue::Static(def_id) = *lvalue {
245+
if let mir::Lvalue::Static(ref static_) = *lvalue {
246+
let def_id = static_.def_id;
246247
let substs = self.ecx.tcx.intern_substs(&[]);
247248
let span = self.span;
248249
if let Some(node_item) = self.ecx.tcx.hir.get_if_local(def_id) {

src/terminator/drop.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
9797
let (adt_ptr, extra) = lval.to_ptr_and_extra();
9898

9999
// run drop impl before the fields' drop impls
100-
if let Some(drop_def_id) = adt_def.destructor() {
100+
if let Some(destructor) = adt_def.destructor(self.tcx) {
101101
let trait_ref = ty::Binder(ty::TraitRef {
102102
def_id: self.tcx.lang_items.drop_trait().unwrap(),
103103
substs: self.tcx.mk_substs_trait(ty, &[]),
@@ -112,7 +112,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
112112
LvalueExtra::Length(n) => Value::ByValPair(PrimVal::Ptr(adt_ptr), PrimVal::from_u128(n as u128)),
113113
LvalueExtra::Vtable(vtable) => Value::ByValPair(PrimVal::Ptr(adt_ptr), PrimVal::Ptr(vtable)),
114114
};
115-
drop.push((drop_def_id, val, vtable.substs));
115+
drop.push((destructor.did, val, vtable.substs));
116116
}
117117

118118
let layout = self.type_layout(ty)?;
@@ -121,7 +121,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
121121
Layout::General { .. } => {
122122
let discr_val = self.read_discriminant_value(adt_ptr, ty)? as u128;
123123
let ptr = self.force_allocation(lval)?.to_ptr();
124-
match adt_def.variants.iter().position(|v| discr_val == v.disr_val) {
124+
match adt_def.discriminants(self.tcx).position(|v| discr_val == v.to_u128_unchecked()) {
125125
Some(i) => {
126126
lval = Lvalue::Ptr {
127127
ptr,

0 commit comments

Comments
 (0)