Skip to content

Commit 77413d7

Browse files
committed
Some cleanups
1 parent 054b0db commit 77413d7

File tree

5 files changed

+23
-41
lines changed

5 files changed

+23
-41
lines changed

miri/fn_call.rs

+4-19
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx> for EvalContext<'a, 'mir, '
115115
match ret_ty.sty {
116116
ty::TyAdt(ref adt_def, _) => {
117117
assert!(adt_def.is_enum(), "Unexpected return type for {}", item_path);
118-
let none_variant_index = adt_def.variants.iter().enumerate().find(|&(_i, ref def)| {
118+
let none_variant_index = adt_def.variants.iter().position(|def| {
119119
def.name.as_str() == "None"
120-
}).expect("No None variant").0;
120+
}).expect("No None variant");
121121
let (return_place, return_to_block) = destination.unwrap();
122122
write_discriminant_value(self, ret_ty, return_place, none_variant_index)?;
123123
self.goto_block(return_to_block);
@@ -126,12 +126,6 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx> for EvalContext<'a, 'mir, '
126126
_ => panic!("Unexpected return type for {}", item_path)
127127
}
128128
}
129-
"std::sys_common::thread_info::set" | "std::sys_common::cleanup" => {
130-
// TODO rustc creates invalid mir inside std::cell::BorrowRef::new which is used by this function
131-
let (_return_place, return_to_block) = destination.unwrap();
132-
self.goto_block(return_to_block);
133-
return Ok(true);
134-
}
135129
"std::sys::unix::fast_thread_local::register_dtor" => {
136130
// TODO: register the dtor
137131
let (_return_place, return_to_block) = destination.unwrap();
@@ -465,17 +459,8 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx> for EvalContext<'a, 'mir, '
465459
instance,
466460
promoted: None,
467461
};
468-
// compute global if not cached
469-
let value: Value = match self.tcx.interpret_interner.get_cached(instance.def_id()) {
470-
Some(ptr) => {
471-
Value::ByRef(MemoryPointer::new(ptr, 0).into(), name_align)
472-
}
473-
None => {
474-
let res: Option<(Value, Pointer, Ty)> = eval_body(self.tcx.tcx, cid, ty::ParamEnv::reveal_all());
475-
res.ok_or_else(||EvalErrorKind::MachineError("<already reported>".to_string()))?.0
476-
},
477-
};
478-
let value = self.value_to_primval(ValTy { value, ty: args[0].ty })?.to_u64()?;
462+
let const_val = self.const_eval(cid)?;
463+
let value = const_val.val.unwrap_u64();
479464
if value == name {
480465
result = Some(path_value);
481466
break;

miri/intrinsic.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -217,16 +217,7 @@ impl<'a, 'mir, 'tcx> EvalContextExt<'tcx> for EvalContext<'a, 'mir, 'tcx, super:
217217
// Also see the write_bytes intrinsic.
218218
let elem_align = elem_layout.align;
219219
let src = self.into_ptr(args[0].value)?;
220-
//let src_align = self.layout_of(args[0].ty)?.align;
221-
//let src_align = ty::layout::Align::from_bytes(1, 1).unwrap();
222220
let dest = self.into_ptr(args[1].value)?;
223-
/*self.tcx.sess.warn(&format!("src_ty: {:?} src_align: {} elem_align: {} src_aligned: {:?} dst_aligned: {:?}",
224-
args[0].ty,
225-
src_align.abi(),
226-
elem_align.abi(),
227-
self.memory.check_align(src, src_align),
228-
self.memory.check_align(dest, elem_align)
229-
));*/
230221
self.memory.copy(
231222
src,
232223
elem_align,
@@ -360,7 +351,9 @@ impl<'a, 'mir, 'tcx> EvalContextExt<'tcx> for EvalContext<'a, 'mir, 'tcx, super:
360351
align: _align,
361352
extra: PlaceExtra::None,
362353
} => self.memory.write_repeat(ptr, 0, size)?,
363-
_ => bug!("TODO"),
354+
Place::Ptr { .. } => {
355+
bug!("init intrinsic tried to write to fat or unaligned ptr target")
356+
}
364357
}
365358
}
366359

@@ -637,7 +630,9 @@ impl<'a, 'mir, 'tcx> EvalContextExt<'tcx> for EvalContext<'a, 'mir, 'tcx, super:
637630
align: _align,
638631
extra: PlaceExtra::None,
639632
} => self.memory.mark_definedness(ptr, size, false)?,
640-
_ => bug!("todo"),
633+
Place::Ptr { .. } => {
634+
bug!("uninit intrinsic tried to write to fat or unaligned ptr target")
635+
}
641636
}
642637
}
643638

miri/lib.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ pub struct MemoryData<'tcx> {
210210
/// The entry is created when allocating the memory and deleted after deallocation.
211211
locks: HashMap<AllocId, RangeMap<LockInfo<'tcx>>>,
212212

213-
mut_statics: HashMap<GlobalId<'tcx>, AllocId>,
213+
statics: HashMap<GlobalId<'tcx>, AllocId>,
214214
}
215215

216216
impl<'mir, 'tcx: 'mir> Machine<'mir, 'tcx> for Evaluator<'tcx> {
@@ -252,25 +252,24 @@ impl<'mir, 'tcx: 'mir> Machine<'mir, 'tcx> for Evaluator<'tcx> {
252252
}
253253

254254
fn mark_static_initialized<'a>(
255-
_mem: &mut Memory<'a, 'mir, 'tcx, Self>,
256-
_id: AllocId,
255+
mem: &mut Memory<'a, 'mir, 'tcx, Self>,
256+
id: AllocId,
257257
_mutability: Mutability,
258258
) -> EvalResult<'tcx, bool> {
259-
/*use memory::MemoryKind::*;
260-
match m {
259+
use memory::MemoryKind::*;
260+
match mem.get_alloc_kind(id) {
261261
// FIXME: This could be allowed, but not for env vars set during miri execution
262-
Env => err!(Unimplemented("statics can't refer to env vars".to_owned())),
262+
Some(MemoryKind::Machine(Env)) => err!(Unimplemented("statics can't refer to env vars".to_owned())),
263263
_ => Ok(false), // TODO: What does the bool mean?
264-
}*/
265-
Ok(false)
264+
}
266265
}
267266

268267
fn init_static<'a>(
269268
ecx: &mut EvalContext<'a, 'mir, 'tcx, Self>,
270269
cid: GlobalId<'tcx>,
271270
) -> EvalResult<'tcx, AllocId> {
272271
// Step 1: If the static has already been evaluated return the cached version
273-
if let Some(alloc_id) = ecx.memory.data.mut_statics.get(&cid) {
272+
if let Some(alloc_id) = ecx.memory.data.statics.get(&cid) {
274273
return Ok(*alloc_id);
275274
}
276275

@@ -293,7 +292,7 @@ impl<'mir, 'tcx: 'mir> Machine<'mir, 'tcx> for Evaluator<'tcx> {
293292
)?;
294293

295294
// Step 4: Cache allocation id for recursive statics
296-
assert!(ecx.memory.data.mut_statics.insert(cid, ptr.alloc_id).is_none());
295+
assert!(ecx.memory.data.statics.insert(cid, ptr.alloc_id).is_none());
297296

298297
// Step 5: Push stackframe to evaluate static
299298
let cleanup = StackPopCleanup::None;
@@ -325,6 +324,8 @@ impl<'mir, 'tcx: 'mir> Machine<'mir, 'tcx> for Evaluator<'tcx> {
325324
}
326325
}
327326

327+
// TODO: Freeze immutable statics without copying them to the global static cache
328+
328329
// Step 7: Return the alloc
329330
Ok(ptr.alloc_id)
330331
}

miri/validation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ impl<'a, 'mir, 'tcx: 'mir + 'a> EvalContextExt<'tcx> for EvalContext<'a, 'mir, '
560560
TyAdt(adt, _) if adt.is_box() => true,
561561
TySlice(_) | TyAdt(_, _) | TyTuple(..) | TyClosure(..) | TyArray(..) |
562562
TyDynamic(..) | TyGenerator(..) | TyForeign(_) => false,
563-
TyGeneratorWitness(..) => bug!("I'm not sure what to return here"),
563+
TyGeneratorWitness(..) => unreachable!("TyGeneratorWitness in validate"),
564564
TyParam(_) | TyInfer(_) | TyProjection(_) | TyAnon(..) | TyError => {
565565
bug!("I got an incomplete/unnormalized type for validation")
566566
}

rust-toolchain

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nightly

0 commit comments

Comments
 (0)