Skip to content

Commit e41f411

Browse files
committed
Auto merge of #53609 - bemeurer:tidy-ctfe, r=RalfJung
Tidy CFTE/MIRI Fixes #53596
2 parents f87d913 + e07c154 commit e41f411

File tree

18 files changed

+331
-73
lines changed

18 files changed

+331
-73
lines changed

Diff for: src/librustc/mir/interpret/error.rs

+22-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
111
use std::{fmt, env};
212

313
use mir;
@@ -315,7 +325,8 @@ impl<'tcx, O> EvalErrorKind<'tcx, O> {
315325
ReadForeignStatic =>
316326
"tried to read from foreign (extern) static",
317327
InvalidPointerMath =>
318-
"attempted to do invalid arithmetic on pointers that would leak base addresses, e.g. comparing pointers into different allocations",
328+
"attempted to do invalid arithmetic on pointers that would leak base addresses, \
329+
e.g. comparing pointers into different allocations",
319330
ReadUndefBytes =>
320331
"attempted to read undefined bytes",
321332
DeadLocal =>
@@ -369,11 +380,13 @@ impl<'tcx, O> EvalErrorKind<'tcx, O> {
369380
Layout(_) =>
370381
"rustc layout computation failed",
371382
UnterminatedCString(_) =>
372-
"attempted to get length of a null terminated string, but no null found before end of allocation",
383+
"attempted to get length of a null terminated string, but no null found before end \
384+
of allocation",
373385
HeapAllocZeroBytes =>
374386
"tried to re-, de- or allocate zero bytes on the heap",
375387
HeapAllocNonPowerOfTwoAlignment(_) =>
376-
"tried to re-, de-, or allocate heap memory with alignment that is not a power of two",
388+
"tried to re-, de-, or allocate heap memory with alignment that is not a power of \
389+
two",
377390
Unreachable =>
378391
"entered unreachable code",
379392
Panic { .. } =>
@@ -435,8 +448,8 @@ impl<'tcx, O: fmt::Debug> fmt::Debug for EvalErrorKind<'tcx, O> {
435448
kind, ptr, len, lock)
436449
}
437450
InvalidMemoryLockRelease { ptr, len, frame, ref lock } => {
438-
write!(f, "frame {} tried to release memory write lock at {:?}, size {}, but cannot release lock {:?}",
439-
frame, ptr, len, lock)
451+
write!(f, "frame {} tried to release memory write lock at {:?}, size {}, but \
452+
cannot release lock {:?}", frame, ptr, len, lock)
440453
}
441454
DeallocatedLockedMemory { ptr, ref lock } => {
442455
write!(f, "tried to deallocate memory at {:?} in conflict with lock {:?}",
@@ -447,7 +460,8 @@ impl<'tcx, O: fmt::Debug> fmt::Debug for EvalErrorKind<'tcx, O> {
447460
}
448461
NoMirFor(ref func) => write!(f, "no mir for `{}`", func),
449462
FunctionPointerTyMismatch(sig, got) =>
450-
write!(f, "tried to call a function with sig {} through a function pointer of type {}", sig, got),
463+
write!(f, "tried to call a function with sig {} through a \
464+
function pointer of type {}", sig, got),
451465
BoundsCheck { ref len, ref index } =>
452466
write!(f, "index out of bounds: the len is {:?} but the index is {:?}", len, index),
453467
ReallocatedWrongMemoryKind(ref old, ref new) =>
@@ -470,7 +484,8 @@ impl<'tcx, O: fmt::Debug> fmt::Debug for EvalErrorKind<'tcx, O> {
470484
MachineError(ref inner) =>
471485
write!(f, "{}", inner),
472486
IncorrectAllocationInformation(size, size2, align, align2) =>
473-
write!(f, "incorrect alloc info: expected size {} and align {}, got size {} and align {}", size.bytes(), align.abi(), size2.bytes(), align2.abi()),
487+
write!(f, "incorrect alloc info: expected size {} and align {}, got size {} and \
488+
align {}", size.bytes(), align.abi(), size2.bytes(), align2.abi()),
474489
Panic { ref msg, line, col, ref file } =>
475490
write!(f, "the evaluated program panicked at '{}', {}:{}:{}", msg, file, line, col),
476491
_ => write!(f, "{}", self.description()),

Diff for: src/librustc/mir/interpret/mod.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
111
//! An interpreter for MIR used in CTFE and by miri
212
313
#[macro_export]
@@ -40,7 +50,8 @@ use std::num::NonZeroU32;
4050
pub enum Lock {
4151
NoLock,
4252
WriteLock(DynamicLifetime),
43-
/// This should never be empty -- that would be a read lock held and nobody there to release it...
53+
/// This should never be empty -- that would be a read lock held and nobody
54+
/// there to release it...
4455
ReadLock(Vec<DynamicLifetime>),
4556
}
4657

Diff for: src/librustc/mir/interpret/value.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
111
#![allow(unknown_lints)]
212

313
use ty::layout::{HasDataLayout, Size};

Diff for: src/librustc_mir/interpret/cast.rs

+20-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
111
use rustc::ty::{self, Ty, TypeAndMut};
212
use rustc::ty::layout::{self, TyLayout, Size};
313
use syntax::ast::{FloatTy, IntTy, UintTy};
@@ -216,7 +226,8 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> {
216226
Ok(Scalar::Bits { bits: v, size: 4 })
217227
},
218228

219-
// No alignment check needed for raw pointers. But we have to truncate to target ptr size.
229+
// No alignment check needed for raw pointers.
230+
// But we have to truncate to target ptr size.
220231
RawPtr(_) => {
221232
Ok(Scalar::Bits {
222233
bits: self.memory.truncate_to_ptr(v).0 as u128,
@@ -229,7 +240,12 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> {
229240
}
230241
}
231242

232-
fn cast_from_float(&self, bits: u128, fty: FloatTy, dest_ty: Ty<'tcx>) -> EvalResult<'tcx, Scalar> {
243+
fn cast_from_float(
244+
&self,
245+
bits: u128,
246+
fty: FloatTy,
247+
dest_ty: Ty<'tcx>
248+
) -> EvalResult<'tcx, Scalar> {
233249
use rustc::ty::TyKind::*;
234250
use rustc_apfloat::FloatConvert;
235251
match dest_ty.sty {
@@ -292,7 +308,8 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> {
292308
fn cast_from_ptr(&self, ptr: Pointer, ty: Ty<'tcx>) -> EvalResult<'tcx, Scalar> {
293309
use rustc::ty::TyKind::*;
294310
match ty.sty {
295-
// Casting to a reference or fn pointer is not permitted by rustc, no need to support it here.
311+
// Casting to a reference or fn pointer is not permitted by rustc,
312+
// no need to support it here.
296313
RawPtr(_) |
297314
Int(IntTy::Isize) |
298315
Uint(UintTy::Usize) => Ok(ptr.into()),

Diff for: src/librustc_mir/interpret/const_eval.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
111
use std::fmt;
212
use std::error::Error;
313

@@ -462,7 +472,8 @@ fn to_str<'a, 'tcx, 'mir>(
462472
if let Value::ScalarPair(ptr, len) = val {
463473
let len = len.not_undef()?.to_bits(ecx.memory.pointer_size())?;
464474
let bytes = ecx.memory.read_bytes(ptr.not_undef()?, Size::from_bytes(len as u64))?;
465-
let str = ::std::str::from_utf8(bytes).map_err(|err| EvalErrorKind::ValidationFailure(err.to_string()))?;
475+
let str = ::std::str::from_utf8(bytes)
476+
.map_err(|err| EvalErrorKind::ValidationFailure(err.to_string()))?;
466477
Ok(Symbol::intern(str))
467478
} else {
468479
bug!("panic arg is not a str")

Diff for: src/librustc_mir/interpret/eval_context.rs

+33-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
111
use std::fmt::Write;
212
use std::hash::{Hash, Hasher};
313
use std::mem;
@@ -82,7 +92,8 @@ pub struct Frame<'mir, 'tcx: 'mir> {
8292
pub return_place: Place,
8393

8494
/// The list of locals for this stack frame, stored in order as
85-
/// `[return_ptr, arguments..., variables..., temporaries...]`. The locals are stored as `Option<Value>`s.
95+
/// `[return_ptr, arguments..., variables..., temporaries...]`.
96+
/// The locals are stored as `Option<Value>`s.
8697
/// `None` represents a local that is currently dead, while a live local
8798
/// can either directly contain `Scalar` or refer to some part of an `Allocation`.
8899
pub locals: IndexVec<mir::Local, LocalValue>,
@@ -269,7 +280,9 @@ impl<'c, 'b, 'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> HasDataLayout
269280
}
270281
}
271282

272-
impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> layout::HasTyCtxt<'tcx> for &'a EvalContext<'a, 'mir, 'tcx, M> {
283+
impl<'a, 'mir, 'tcx, M> layout::HasTyCtxt<'tcx> for &'a EvalContext<'a, 'mir, 'tcx, M>
284+
where M: Machine<'mir, 'tcx>
285+
{
273286
#[inline]
274287
fn tcx<'b>(&'b self) -> TyCtxt<'b, 'tcx, 'tcx> {
275288
*self.tcx
@@ -329,7 +342,8 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M
329342

330343
pub(crate) fn with_fresh_body<F: FnOnce(&mut Self) -> R, R>(&mut self, f: F) -> R {
331344
let stack = mem::replace(&mut self.stack, Vec::new());
332-
let steps = mem::replace(&mut self.steps_since_detector_enabled, -STEPS_UNTIL_DETECTOR_ENABLED);
345+
let steps = mem::replace(&mut self.steps_since_detector_enabled,
346+
-STEPS_UNTIL_DETECTOR_ENABLED);
333347
let r = f(self);
334348
self.stack = stack;
335349
self.steps_since_detector_enabled = steps;
@@ -378,7 +392,11 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M
378392
Ok(Value::new_slice(Scalar::Ptr(ptr), s.len() as u64, self.tcx.tcx))
379393
}
380394

381-
pub(super) fn resolve(&self, def_id: DefId, substs: &'tcx Substs<'tcx>) -> EvalResult<'tcx, ty::Instance<'tcx>> {
395+
pub(super) fn resolve(
396+
&self,
397+
def_id: DefId,
398+
substs: &'tcx Substs<'tcx>
399+
) -> EvalResult<'tcx, ty::Instance<'tcx>> {
382400
trace!("resolve: {:?}, {:#?}", def_id, substs);
383401
trace!("substs: {:#?}", self.substs());
384402
trace!("param_env: {:#?}", self.param_env);
@@ -405,7 +423,10 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M
405423
) -> EvalResult<'tcx, &'tcx mir::Mir<'tcx>> {
406424
// do not continue if typeck errors occurred (can only occur in local crate)
407425
let did = instance.def_id();
408-
if did.is_local() && self.tcx.has_typeck_tables(did) && self.tcx.typeck_tables_of(did).tainted_by_errors {
426+
if did.is_local()
427+
&& self.tcx.has_typeck_tables(did)
428+
&& self.tcx.typeck_tables_of(did).tainted_by_errors
429+
{
409430
return err!(TypeckError);
410431
}
411432
trace!("load mir {:?}", instance);
@@ -614,7 +635,8 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M
614635
match frame.return_to_block {
615636
StackPopCleanup::MarkStatic(mutable) => {
616637
if let Place::Ptr(MemPlace { ptr, .. }) = frame.return_place {
617-
// FIXME: to_ptr()? might be too extreme here, static zsts might reach this under certain conditions
638+
// FIXME: to_ptr()? might be too extreme here,
639+
// static zsts might reach this under certain conditions
618640
self.memory.mark_static_initialized(
619641
ptr.to_ptr()?.alloc_id,
620642
mutable,
@@ -651,7 +673,8 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M
651673
} else {
652674
self.param_env
653675
};
654-
self.tcx.const_eval(param_env.and(gid)).map_err(|err| EvalErrorKind::ReferencedConstant(err).into())
676+
self.tcx.const_eval(param_env.and(gid))
677+
.map_err(|err| EvalErrorKind::ReferencedConstant(err).into())
655678
}
656679

657680
#[inline(always)]
@@ -757,7 +780,9 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M
757780
} else {
758781
last_span = Some(span);
759782
}
760-
let location = if self.tcx.def_key(instance.def_id()).disambiguated_data.data == DefPathData::ClosureExpr {
783+
let location = if self.tcx.def_key(instance.def_id()).disambiguated_data.data
784+
== DefPathData::ClosureExpr
785+
{
761786
"closure".to_owned()
762787
} else {
763788
instance.to_string()

Diff for: src/librustc_mir/interpret/machine.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
111
//! This module contains everything needed to instantiate an interpreter.
212
//! This separation exists to ensure that no fancy miri features like
313
//! interpreting common C functions leak into CTFE.

0 commit comments

Comments
 (0)