From 65544e51098c01c7fdd53a541a387382bacfc537 Mon Sep 17 00:00:00 2001 From: Bernardo Meurer Date: Wed, 22 Aug 2018 09:15:30 -0700 Subject: [PATCH 1/9] Removed librustc/mir/interpret and librustc_mir/interpreter from tidy skip list --- src/tools/tidy/src/lib.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/tools/tidy/src/lib.rs b/src/tools/tidy/src/lib.rs index f2107f92debd2..c4bd0bbd03ca3 100644 --- a/src/tools/tidy/src/lib.rs +++ b/src/tools/tidy/src/lib.rs @@ -76,8 +76,6 @@ fn filter_dirs(path: &Path) -> bool { "src/tools/miri", "src/tools/lld", "src/tools/lldb", - "src/librustc/mir/interpret", - "src/librustc_mir/interpret", "src/target", "src/stdsimd", ]; From 22beeb4cab4943b055625b71a5b7b8339ee38b3d Mon Sep 17 00:00:00 2001 From: Bernardo Meurer Date: Wed, 22 Aug 2018 16:52:01 -0300 Subject: [PATCH 2/9] Add license header to CTFE/MIRI --- src/librustc/mir/interpret/error.rs | 10 ++++++++++ src/librustc/mir/interpret/mod.rs | 10 ++++++++++ src/librustc/mir/interpret/value.rs | 10 ++++++++++ src/librustc_mir/interpret/cast.rs | 10 ++++++++++ src/librustc_mir/interpret/const_eval.rs | 10 ++++++++++ src/librustc_mir/interpret/eval_context.rs | 10 ++++++++++ src/librustc_mir/interpret/machine.rs | 10 ++++++++++ src/librustc_mir/interpret/memory.rs | 10 ++++++++++ src/librustc_mir/interpret/mod.rs | 10 ++++++++++ src/librustc_mir/interpret/operand.rs | 10 ++++++++++ src/librustc_mir/interpret/operator.rs | 10 ++++++++++ src/librustc_mir/interpret/place.rs | 10 ++++++++++ src/librustc_mir/interpret/step.rs | 10 ++++++++++ src/librustc_mir/interpret/terminator/drop.rs | 10 ++++++++++ src/librustc_mir/interpret/terminator/mod.rs | 10 ++++++++++ src/librustc_mir/interpret/traits.rs | 10 ++++++++++ src/librustc_mir/interpret/validity.rs | 10 ++++++++++ 17 files changed, 170 insertions(+) diff --git a/src/librustc/mir/interpret/error.rs b/src/librustc/mir/interpret/error.rs index 436478b84162a..ac114e0c725b1 100644 --- a/src/librustc/mir/interpret/error.rs +++ b/src/librustc/mir/interpret/error.rs @@ -1,3 +1,13 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + use std::{fmt, env}; use mir; diff --git a/src/librustc/mir/interpret/mod.rs b/src/librustc/mir/interpret/mod.rs index ca664c6e18b4d..93cc5fe492fe2 100644 --- a/src/librustc/mir/interpret/mod.rs +++ b/src/librustc/mir/interpret/mod.rs @@ -1,3 +1,13 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + //! An interpreter for MIR used in CTFE and by miri #[macro_export] diff --git a/src/librustc/mir/interpret/value.rs b/src/librustc/mir/interpret/value.rs index b142de81c1e91..6b34e3f47cc5d 100644 --- a/src/librustc/mir/interpret/value.rs +++ b/src/librustc/mir/interpret/value.rs @@ -1,3 +1,13 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + #![allow(unknown_lints)] use ty::layout::{HasDataLayout, Size}; diff --git a/src/librustc_mir/interpret/cast.rs b/src/librustc_mir/interpret/cast.rs index 0c5dbf9a0f63e..8b1144fab6718 100644 --- a/src/librustc_mir/interpret/cast.rs +++ b/src/librustc_mir/interpret/cast.rs @@ -1,3 +1,13 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + use rustc::ty::{self, Ty, TypeAndMut}; use rustc::ty::layout::{self, TyLayout, Size}; use syntax::ast::{FloatTy, IntTy, UintTy}; diff --git a/src/librustc_mir/interpret/const_eval.rs b/src/librustc_mir/interpret/const_eval.rs index a306a4d7c1e1e..83c535e962fe8 100644 --- a/src/librustc_mir/interpret/const_eval.rs +++ b/src/librustc_mir/interpret/const_eval.rs @@ -1,3 +1,13 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + use std::fmt; use std::error::Error; diff --git a/src/librustc_mir/interpret/eval_context.rs b/src/librustc_mir/interpret/eval_context.rs index 9a6f1199421d6..9c31f4140bd93 100644 --- a/src/librustc_mir/interpret/eval_context.rs +++ b/src/librustc_mir/interpret/eval_context.rs @@ -1,3 +1,13 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + use std::fmt::Write; use std::hash::{Hash, Hasher}; use std::mem; diff --git a/src/librustc_mir/interpret/machine.rs b/src/librustc_mir/interpret/machine.rs index 84556c7249dbd..1fcdab2be62b8 100644 --- a/src/librustc_mir/interpret/machine.rs +++ b/src/librustc_mir/interpret/machine.rs @@ -1,3 +1,13 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + //! This module contains everything needed to instantiate an interpreter. //! This separation exists to ensure that no fancy miri features like //! interpreting common C functions leak into CTFE. diff --git a/src/librustc_mir/interpret/memory.rs b/src/librustc_mir/interpret/memory.rs index 89c308d87ef28..a0bc047891834 100644 --- a/src/librustc_mir/interpret/memory.rs +++ b/src/librustc_mir/interpret/memory.rs @@ -1,3 +1,13 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + //! The memory subsystem. //! //! Generally, we use `Pointer` to denote memory addresses. However, some operations diff --git a/src/librustc_mir/interpret/mod.rs b/src/librustc_mir/interpret/mod.rs index b769dae0a7abc..8192ef3d0f31d 100644 --- a/src/librustc_mir/interpret/mod.rs +++ b/src/librustc_mir/interpret/mod.rs @@ -1,3 +1,13 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + //! An interpreter for MIR used in CTFE and by miri mod cast; diff --git a/src/librustc_mir/interpret/operand.rs b/src/librustc_mir/interpret/operand.rs index d9e7f33e836f5..aa4585fb89242 100644 --- a/src/librustc_mir/interpret/operand.rs +++ b/src/librustc_mir/interpret/operand.rs @@ -1,3 +1,13 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + //! Functions concerning immediate values and operands, and reading from operands. //! All high-level functions to read from memory work on operands as sources. diff --git a/src/librustc_mir/interpret/operator.rs b/src/librustc_mir/interpret/operator.rs index a8e5525b2e1c0..69babac968bc8 100644 --- a/src/librustc_mir/interpret/operator.rs +++ b/src/librustc_mir/interpret/operator.rs @@ -1,3 +1,13 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + use rustc::mir; use rustc::ty::{self, layout::{self, TyLayout}}; use syntax::ast::FloatTy; diff --git a/src/librustc_mir/interpret/place.rs b/src/librustc_mir/interpret/place.rs index 1795548d81c77..62c95d367195e 100644 --- a/src/librustc_mir/interpret/place.rs +++ b/src/librustc_mir/interpret/place.rs @@ -1,3 +1,13 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + //! Computations on places -- field projections, going from mir::Place, and writing //! into a place. //! All high-level functions to write to memory work on places as destinations. diff --git a/src/librustc_mir/interpret/step.rs b/src/librustc_mir/interpret/step.rs index f39a5ee3e4ef2..0771fe882445b 100644 --- a/src/librustc_mir/interpret/step.rs +++ b/src/librustc_mir/interpret/step.rs @@ -1,3 +1,13 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + //! This module contains the `EvalContext` methods for executing a single step of the interpreter. //! //! The main entry point is the `step` method. diff --git a/src/librustc_mir/interpret/terminator/drop.rs b/src/librustc_mir/interpret/terminator/drop.rs index 0bddd7147b998..25835c7184a97 100644 --- a/src/librustc_mir/interpret/terminator/drop.rs +++ b/src/librustc_mir/interpret/terminator/drop.rs @@ -1,3 +1,13 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + use rustc::mir::BasicBlock; use rustc::ty::{self, layout::LayoutOf}; use syntax::source_map::Span; diff --git a/src/librustc_mir/interpret/terminator/mod.rs b/src/librustc_mir/interpret/terminator/mod.rs index 913b88fc8eb48..50f67fca921cc 100644 --- a/src/librustc_mir/interpret/terminator/mod.rs +++ b/src/librustc_mir/interpret/terminator/mod.rs @@ -1,3 +1,13 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + use rustc::mir; use rustc::ty::{self, Ty}; use rustc::ty::layout::LayoutOf; diff --git a/src/librustc_mir/interpret/traits.rs b/src/librustc_mir/interpret/traits.rs index 18718cc3dcd6e..0aada9c0d3c24 100644 --- a/src/librustc_mir/interpret/traits.rs +++ b/src/librustc_mir/interpret/traits.rs @@ -1,3 +1,13 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + use rustc::ty::{self, Ty}; use rustc::ty::layout::{Size, Align, LayoutOf}; use rustc::mir::interpret::{Scalar, Pointer, EvalResult}; diff --git a/src/librustc_mir/interpret/validity.rs b/src/librustc_mir/interpret/validity.rs index a446a534624ba..68558e64ec7e5 100644 --- a/src/librustc_mir/interpret/validity.rs +++ b/src/librustc_mir/interpret/validity.rs @@ -1,3 +1,13 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + use std::fmt::Write; use syntax_pos::symbol::Symbol; From 37db3db53dd1cd34b22efb52e2cd79b34aeb7e11 Mon Sep 17 00:00:00 2001 From: Bernardo Meurer Date: Wed, 22 Aug 2018 16:54:05 -0300 Subject: [PATCH 3/9] Fixed long strings --- src/librustc/mir/interpret/error.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/librustc/mir/interpret/error.rs b/src/librustc/mir/interpret/error.rs index ac114e0c725b1..3590cbdf2c767 100644 --- a/src/librustc/mir/interpret/error.rs +++ b/src/librustc/mir/interpret/error.rs @@ -325,7 +325,8 @@ impl<'tcx, O> EvalErrorKind<'tcx, O> { ReadForeignStatic => "tried to read from foreign (extern) static", InvalidPointerMath => - "attempted to do invalid arithmetic on pointers that would leak base addresses, e.g. comparing pointers into different allocations", + "attempted to do invalid arithmetic on pointers that would leak base addresses, \ + e.g. comparing pointers into different allocations", ReadUndefBytes => "attempted to read undefined bytes", DeadLocal => @@ -379,11 +380,13 @@ impl<'tcx, O> EvalErrorKind<'tcx, O> { Layout(_) => "rustc layout computation failed", UnterminatedCString(_) => - "attempted to get length of a null terminated string, but no null found before end of allocation", + "attempted to get length of a null terminated string, but no null found before end + of allocation", HeapAllocZeroBytes => "tried to re-, de- or allocate zero bytes on the heap", HeapAllocNonPowerOfTwoAlignment(_) => - "tried to re-, de-, or allocate heap memory with alignment that is not a power of two", + "tried to re-, de-, or allocate heap memory with alignment that is not a power of + two", Unreachable => "entered unreachable code", Panic { .. } => @@ -445,8 +448,8 @@ impl<'tcx, O: fmt::Debug> fmt::Debug for EvalErrorKind<'tcx, O> { kind, ptr, len, lock) } InvalidMemoryLockRelease { ptr, len, frame, ref lock } => { - write!(f, "frame {} tried to release memory write lock at {:?}, size {}, but cannot release lock {:?}", - frame, ptr, len, lock) + write!(f, "frame {} tried to release memory write lock at {:?}, size {}, but \ + cannot release lock {:?}", frame, ptr, len, lock) } DeallocatedLockedMemory { ptr, ref lock } => { write!(f, "tried to deallocate memory at {:?} in conflict with lock {:?}", @@ -457,7 +460,8 @@ impl<'tcx, O: fmt::Debug> fmt::Debug for EvalErrorKind<'tcx, O> { } NoMirFor(ref func) => write!(f, "no mir for `{}`", func), FunctionPointerTyMismatch(sig, got) => - write!(f, "tried to call a function with sig {} through a function pointer of type {}", sig, got), + write!(f, "tried to call a function with sig {} through a \ + function pointer of type {}", sig, got), BoundsCheck { ref len, ref index } => write!(f, "index out of bounds: the len is {:?} but the index is {:?}", len, index), ReallocatedWrongMemoryKind(ref old, ref new) => @@ -480,7 +484,8 @@ impl<'tcx, O: fmt::Debug> fmt::Debug for EvalErrorKind<'tcx, O> { MachineError(ref inner) => write!(f, "{}", inner), IncorrectAllocationInformation(size, size2, align, align2) => - write!(f, "incorrect alloc info: expected size {} and align {}, got size {} and align {}", size.bytes(), align.abi(), size2.bytes(), align2.abi()), + write!(f, "incorrect alloc info: expected size {} and align {}, got size {} and \ + align {}", size.bytes(), align.abi(), size2.bytes(), align2.abi()), Panic { ref msg, line, col, ref file } => write!(f, "the evaluated program panicked at '{}', {}:{}:{}", msg, file, line, col), _ => write!(f, "{}", self.description()), From 35f25bfef3012c634a8c255033c9dbdcf268fb41 Mon Sep 17 00:00:00 2001 From: Bernardo Meurer Date: Wed, 22 Aug 2018 16:58:39 -0300 Subject: [PATCH 4/9] Reflow and fixup comments --- src/librustc/mir/interpret/mod.rs | 3 +- src/librustc_mir/interpret/cast.rs | 6 ++-- src/librustc_mir/interpret/eval_context.rs | 6 ++-- src/librustc_mir/interpret/memory.rs | 29 ++++++++++++------- src/librustc_mir/interpret/place.rs | 12 ++++---- src/librustc_mir/interpret/step.rs | 4 +-- src/librustc_mir/interpret/terminator/drop.rs | 6 ++-- src/librustc_mir/interpret/terminator/mod.rs | 11 ++++--- src/librustc_mir/interpret/validity.rs | 3 +- 9 files changed, 49 insertions(+), 31 deletions(-) diff --git a/src/librustc/mir/interpret/mod.rs b/src/librustc/mir/interpret/mod.rs index 93cc5fe492fe2..6458c211ab537 100644 --- a/src/librustc/mir/interpret/mod.rs +++ b/src/librustc/mir/interpret/mod.rs @@ -50,7 +50,8 @@ use std::num::NonZeroU32; pub enum Lock { NoLock, WriteLock(DynamicLifetime), - /// This should never be empty -- that would be a read lock held and nobody there to release it... + /// This should never be empty -- that would be a read lock held and nobody + /// there to release it... ReadLock(Vec), } diff --git a/src/librustc_mir/interpret/cast.rs b/src/librustc_mir/interpret/cast.rs index 8b1144fab6718..aa172c7603ff5 100644 --- a/src/librustc_mir/interpret/cast.rs +++ b/src/librustc_mir/interpret/cast.rs @@ -226,7 +226,8 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> { Ok(Scalar::Bits { bits: v, size: 4 }) }, - // No alignment check needed for raw pointers. But we have to truncate to target ptr size. + // No alignment check needed for raw pointers. + // But we have to truncate to target ptr size. RawPtr(_) => { Ok(Scalar::Bits { bits: self.memory.truncate_to_ptr(v).0 as u128, @@ -302,7 +303,8 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> { fn cast_from_ptr(&self, ptr: Pointer, ty: Ty<'tcx>) -> EvalResult<'tcx, Scalar> { use rustc::ty::TyKind::*; match ty.sty { - // Casting to a reference or fn pointer is not permitted by rustc, no need to support it here. + // Casting to a reference or fn pointer is not permitted by rustc, + // no need to support it here. RawPtr(_) | Int(IntTy::Isize) | Uint(UintTy::Usize) => Ok(ptr.into()), diff --git a/src/librustc_mir/interpret/eval_context.rs b/src/librustc_mir/interpret/eval_context.rs index 9c31f4140bd93..3ea5fe89163f6 100644 --- a/src/librustc_mir/interpret/eval_context.rs +++ b/src/librustc_mir/interpret/eval_context.rs @@ -92,7 +92,8 @@ pub struct Frame<'mir, 'tcx: 'mir> { pub return_place: Place, /// The list of locals for this stack frame, stored in order as - /// `[return_ptr, arguments..., variables..., temporaries...]`. The locals are stored as `Option`s. + /// `[return_ptr, arguments..., variables..., temporaries...]`. + /// The locals are stored as `Option`s. /// `None` represents a local that is currently dead, while a live local /// can either directly contain `Scalar` or refer to some part of an `Allocation`. pub locals: IndexVec, @@ -624,7 +625,8 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M match frame.return_to_block { StackPopCleanup::MarkStatic(mutable) => { if let Place::Ptr(MemPlace { ptr, .. }) = frame.return_place { - // FIXME: to_ptr()? might be too extreme here, static zsts might reach this under certain conditions + // FIXME: to_ptr()? might be too extreme here, + // static zsts might reach this under certain conditions self.memory.mark_static_initialized( ptr.to_ptr()?.alloc_id, mutable, diff --git a/src/librustc_mir/interpret/memory.rs b/src/librustc_mir/interpret/memory.rs index a0bc047891834..3ed5d3cae2345 100644 --- a/src/librustc_mir/interpret/memory.rs +++ b/src/librustc_mir/interpret/memory.rs @@ -243,11 +243,12 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> { let alloc_kind = self.alloc_kind.remove(&ptr.alloc_id).expect("alloc_map out of sync with alloc_kind"); - // It is okay for us to still holds locks on deallocation -- for example, we could store data we own - // in a local, and the local could be deallocated (from StorageDead) before the function returns. - // However, we should check *something*. For now, we make sure that there is no conflicting write - // lock by another frame. We *have* to permit deallocation if we hold a read lock. - // TODO: Figure out the exact rules here. + // It is okay for us to still holds locks on deallocation -- for example, we could store + // data we own in a local, and the local could be deallocated (from StorageDead) before the + // function returns. However, we should check *something*. For now, we make sure that there + // is no conflicting write lock by another frame. We *have* to permit deallocation if we + // hold a read lock. + // FIXME: Figure out the exact rules here. M::free_lock(self, ptr.alloc_id, alloc.bytes.len() as u64)?; if alloc_kind != kind { @@ -521,13 +522,15 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> { size: Size, align: Align, ) -> EvalResult<'tcx, &[u8]> { - // Zero-sized accesses can use dangling pointers, but they still have to be aligned and non-NULL + // Zero-sized accesses can use dangling pointers, + // but they still have to be aligned and non-NULL self.check_align(ptr.into(), align)?; if size.bytes() == 0 { return Ok(&[]); } M::check_locks(self, ptr, size, AccessKind::Read)?; - self.check_bounds(ptr.offset(size, self)?, true)?; // if ptr.offset is in bounds, then so is ptr (because offset checks for overflow) + // if ptr.offset is in bounds, then so is ptr (because offset checks for overflow) + self.check_bounds(ptr.offset(size, self)?, true)?; let alloc = self.get(ptr.alloc_id)?; assert_eq!(ptr.offset.bytes() as usize as u64, ptr.offset.bytes()); assert_eq!(size.bytes() as usize as u64, size.bytes()); @@ -542,13 +545,15 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> { size: Size, align: Align, ) -> EvalResult<'tcx, &mut [u8]> { - // Zero-sized accesses can use dangling pointers, but they still have to be aligned and non-NULL + // Zero-sized accesses can use dangling pointers, + // but they still have to be aligned and non-NULL self.check_align(ptr.into(), align)?; if size.bytes() == 0 { return Ok(&mut []); } M::check_locks(self, ptr, size, AccessKind::Write)?; - self.check_bounds(ptr.offset(size, &*self)?, true)?; // if ptr.offset is in bounds, then so is ptr (because offset checks for overflow) + // if ptr.offset is in bounds, then so is ptr (because offset checks for overflow) + self.check_bounds(ptr.offset(size, &*self)?, true)?; let alloc = self.get_mut(ptr.alloc_id)?; assert_eq!(ptr.offset.bytes() as usize as u64, ptr.offset.bytes()); assert_eq!(size.bytes() as usize as u64, size.bytes()); @@ -774,14 +779,16 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> { /// Read a *non-ZST* scalar pub fn read_scalar(&self, ptr: Pointer, ptr_align: Align, size: Size) -> EvalResult<'tcx, ScalarMaybeUndef> { - self.check_relocation_edges(ptr, size)?; // Make sure we don't read part of a pointer as a pointer + // Make sure we don't read part of a pointer as a pointer + self.check_relocation_edges(ptr, size)?; let endianness = self.endianness(); // get_bytes_unchecked tests alignment let bytes = self.get_bytes_unchecked(ptr, size, ptr_align.min(self.int_align(size)))?; // Undef check happens *after* we established that the alignment is correct. // We must not return Ok() for unaligned pointers! if self.check_defined(ptr, size).is_err() { - // this inflates undefined bytes to the entire scalar, even if only a few bytes are undefined + // this inflates undefined bytes to the entire scalar, + // even if only a few bytes are undefined return Ok(ScalarMaybeUndef::Undef); } // Now we do the actual reading diff --git a/src/librustc_mir/interpret/place.rs b/src/librustc_mir/interpret/place.rs index 62c95d367195e..cf21e0238324f 100644 --- a/src/librustc_mir/interpret/place.rs +++ b/src/librustc_mir/interpret/place.rs @@ -119,8 +119,9 @@ impl MemPlace { /// Extract the ptr part of the mplace #[inline(always)] pub fn to_ptr(self) -> EvalResult<'tcx, Pointer> { - // At this point, we forget about the alignment information -- the place has been turned into a reference, - // and no matter where it came from, it now must be aligned. + // At this point, we forget about the alignment information -- + // the place has been turned into a reference, and no matter where it came from, + // it now must be aligned. self.to_scalar_ptr_align().0.to_ptr() } @@ -582,9 +583,10 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> { dest: MPlaceTy<'tcx>, ) -> EvalResult<'tcx> { let (ptr, ptr_align) = dest.to_scalar_ptr_align(); - // Note that it is really important that the type here is the right one, and matches the type things are read at. - // In case `src_val` is a `ScalarPair`, we don't do any magic here to handle padding properly, which is only - // correct if we never look at this data with the wrong type. + // Note that it is really important that the type here is the right one, and matches the + // type things are read at. In case `src_val` is a `ScalarPair`, we don't do any magic here + // to handle padding properly, which is only correct if we never look at this data with the + // wrong type. // Nothing to do for ZSTs, other than checking alignment if dest.layout.size.bytes() == 0 { diff --git a/src/librustc_mir/interpret/step.rs b/src/librustc_mir/interpret/step.rs index 0771fe882445b..1648bd2f9db54 100644 --- a/src/librustc_mir/interpret/step.rs +++ b/src/librustc_mir/interpret/step.rs @@ -108,8 +108,8 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> { use rustc::mir::StatementKind::*; - // Some statements (e.g. box) push new stack frames. We have to record the stack frame number - // *before* executing the statement. + // Some statements (e.g. box) push new stack frames. + // We have to record the stack frame number *before* executing the statement. let frame_idx = self.cur_frame(); self.tcx.span = stmt.source_info.span; self.memory.tcx.span = stmt.source_info.span; diff --git a/src/librustc_mir/interpret/terminator/drop.rs b/src/librustc_mir/interpret/terminator/drop.rs index 25835c7184a97..8e413aa8284fb 100644 --- a/src/librustc_mir/interpret/terminator/drop.rs +++ b/src/librustc_mir/interpret/terminator/drop.rs @@ -24,9 +24,9 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> { target: BasicBlock, ) -> EvalResult<'tcx> { trace!("drop_in_place: {:?},\n {:?}, {:?}", *place, place.layout.ty, instance); - // We take the address of the object. This may well be unaligned, which is fine for us here. - // However, unaligned accesses will probably make the actual drop implementation fail -- a problem shared - // by rustc. + // We take the address of the object. This may well be unaligned, which is fine for us + // here. However, unaligned accesses will probably make the actual drop implementation fail + // -- a problem shared by rustc. let place = self.force_allocation(place)?; let (instance, place) = match place.layout.ty.sty { diff --git a/src/librustc_mir/interpret/terminator/mod.rs b/src/librustc_mir/interpret/terminator/mod.rs index 50f67fca921cc..10681e28e020d 100644 --- a/src/librustc_mir/interpret/terminator/mod.rs +++ b/src/librustc_mir/interpret/terminator/mod.rs @@ -185,15 +185,18 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> { DropAndReplace { .. } => unimplemented!(), Resume => unimplemented!(), Abort => unimplemented!(), - FalseEdges { .. } => bug!("should have been eliminated by `simplify_branches` mir pass"), - FalseUnwind { .. } => bug!("should have been eliminated by `simplify_branches` mir pass"), + FalseEdges { .. } => bug!("should have been eliminated by\ + `simplify_branches` mir pass"), + FalseUnwind { .. } => bug!("should have been eliminated by\ + `simplify_branches` mir pass"), Unreachable => return err!(Unreachable), } Ok(()) } - /// Decides whether it is okay to call the method with signature `real_sig` using signature `sig`. + /// Decides whether it is okay to call the method with signature `real_sig` + /// using signature `sig`. /// FIXME: This should take into account the platform-dependent ABI description. fn check_sig_compat( &mut self, @@ -207,7 +210,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> { return match (&ty.sty, &real_ty.sty) { // Permit changing the pointer type of raw pointers and references as well as // mutability of raw pointers. - // TODO: Should not be allowed when fat pointers are involved. + // FIXME: Should not be allowed when fat pointers are involved. (&ty::RawPtr(_), &ty::RawPtr(_)) => true, (&ty::Ref(_, _, _), &ty::Ref(_, _, _)) => { ty.is_mutable_pointer() == real_ty.is_mutable_pointer() diff --git a/src/librustc_mir/interpret/validity.rs b/src/librustc_mir/interpret/validity.rs index 68558e64ec7e5..00fca7c586a56 100644 --- a/src/librustc_mir/interpret/validity.rs +++ b/src/librustc_mir/interpret/validity.rs @@ -265,7 +265,8 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> { if value.layout.ty.builtin_deref(false).is_some() { trace!("Recursing below ptr {:#?}", value); let ptr_place = self.ref_to_mplace(value)?; - // we have not encountered this pointer+layout combination before + // we have not encountered this pointer+layout + // combination before if seen.insert(ptr_place) { todo.push((ptr_place, path_clone_and_deref(path))); } From 22d3f669452ed95882e0dd44fab956afa78776be Mon Sep 17 00:00:00 2001 From: Bernardo Meurer Date: Wed, 22 Aug 2018 16:59:14 -0300 Subject: [PATCH 5/9] Fixup long code lines --- src/librustc_mir/interpret/cast.rs | 7 ++- src/librustc_mir/interpret/eval_context.rs | 20 ++++++--- src/librustc_mir/interpret/memory.rs | 47 +++++++++++++++----- src/librustc_mir/interpret/operator.rs | 3 +- src/librustc_mir/interpret/place.rs | 12 +++-- src/librustc_mir/interpret/terminator/mod.rs | 25 ++++++++--- src/librustc_mir/interpret/traits.rs | 3 +- src/librustc_mir/interpret/validity.rs | 5 ++- 8 files changed, 91 insertions(+), 31 deletions(-) diff --git a/src/librustc_mir/interpret/cast.rs b/src/librustc_mir/interpret/cast.rs index aa172c7603ff5..373da1d04ff9d 100644 --- a/src/librustc_mir/interpret/cast.rs +++ b/src/librustc_mir/interpret/cast.rs @@ -240,7 +240,12 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> { } } - fn cast_from_float(&self, bits: u128, fty: FloatTy, dest_ty: Ty<'tcx>) -> EvalResult<'tcx, Scalar> { + fn cast_from_float( + &self, + bits: u128, + fty: FloatTy, + dest_ty: Ty<'tcx> + ) -> EvalResult<'tcx, Scalar> { use rustc::ty::TyKind::*; use rustc_apfloat::FloatConvert; match dest_ty.sty { diff --git a/src/librustc_mir/interpret/eval_context.rs b/src/librustc_mir/interpret/eval_context.rs index 3ea5fe89163f6..fdb1a1596c4de 100644 --- a/src/librustc_mir/interpret/eval_context.rs +++ b/src/librustc_mir/interpret/eval_context.rs @@ -280,7 +280,9 @@ impl<'c, 'b, 'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> HasDataLayout } } -impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> layout::HasTyCtxt<'tcx> for &'a EvalContext<'a, 'mir, 'tcx, M> { +impl<'a, 'mir, 'tcx, M> layout::HasTyCtxt<'tcx> for &'a EvalContext<'a, 'mir, 'tcx, M> +where M: Machine<'mir, 'tcx> +{ #[inline] fn tcx<'b>(&'b self) -> TyCtxt<'b, 'tcx, 'tcx> { *self.tcx @@ -340,7 +342,8 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M pub(crate) fn with_fresh_body R, R>(&mut self, f: F) -> R { let stack = mem::replace(&mut self.stack, Vec::new()); - let steps = mem::replace(&mut self.steps_since_detector_enabled, -STEPS_UNTIL_DETECTOR_ENABLED); + let steps = mem::replace(&mut self.steps_since_detector_enabled, + -STEPS_UNTIL_DETECTOR_ENABLED); let r = f(self); self.stack = stack; self.steps_since_detector_enabled = steps; @@ -389,7 +392,8 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M Ok(Value::new_slice(Scalar::Ptr(ptr), s.len() as u64, self.tcx.tcx)) } - pub(super) fn resolve(&self, def_id: DefId, substs: &'tcx Substs<'tcx>) -> EvalResult<'tcx, ty::Instance<'tcx>> { + pub(super) fn resolve(&self, def_id: DefId, substs: &'tcx Substs<'tcx>) + -> EvalResult<'tcx, ty::Instance<'tcx>> { trace!("resolve: {:?}, {:#?}", def_id, substs); trace!("substs: {:#?}", self.substs()); trace!("param_env: {:#?}", self.param_env); @@ -416,7 +420,9 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M ) -> EvalResult<'tcx, &'tcx mir::Mir<'tcx>> { // do not continue if typeck errors occurred (can only occur in local crate) let did = instance.def_id(); - if did.is_local() && self.tcx.has_typeck_tables(did) && self.tcx.typeck_tables_of(did).tainted_by_errors { + if did.is_local() + && self.tcx.has_typeck_tables(did) + && self.tcx.typeck_tables_of(did).tainted_by_errors { return err!(TypeckError); } trace!("load mir {:?}", instance); @@ -663,7 +669,8 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M } else { self.param_env }; - self.tcx.const_eval(param_env.and(gid)).map_err(|err| EvalErrorKind::ReferencedConstant(err).into()) + self.tcx.const_eval(param_env.and(gid)) + .map_err(|err| EvalErrorKind::ReferencedConstant(err).into()) } #[inline(always)] @@ -769,7 +776,8 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M } else { last_span = Some(span); } - let location = if self.tcx.def_key(instance.def_id()).disambiguated_data.data == DefPathData::ClosureExpr { + let location = if self.tcx.def_key(instance.def_id()).disambiguated_data.data + == DefPathData::ClosureExpr { "closure".to_owned() } else { instance.to_string() diff --git a/src/librustc_mir/interpret/memory.rs b/src/librustc_mir/interpret/memory.rs index 3ed5d3cae2345..557b1509c69d5 100644 --- a/src/librustc_mir/interpret/memory.rs +++ b/src/librustc_mir/interpret/memory.rs @@ -241,7 +241,9 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> { } }; - let alloc_kind = self.alloc_kind.remove(&ptr.alloc_id).expect("alloc_map out of sync with alloc_kind"); + let alloc_kind = self.alloc_kind + .remove(&ptr.alloc_id) + .expect("alloc_map out of sync with alloc_kind"); // It is okay for us to still holds locks on deallocation -- for example, we could store // data we own in a local, and the local could be deallocated (from StorageDead) before the @@ -259,7 +261,11 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> { } if let Some((size, align)) = size_and_align { if size.bytes() != alloc.bytes.len() as u64 || align != alloc.align { - return err!(IncorrectAllocationInformation(size, Size::from_bytes(alloc.bytes.len() as u64), align, alloc.align)); + let bytes = Size::from_bytes(alloc.bytes.len() as u64); + return err!(IncorrectAllocationInformation(size, + bytes, + align, + alloc.align)); } } @@ -678,7 +684,8 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> { relocations .iter() .map(|&(offset, alloc_id)| { - (offset + dest.offset - src.offset + (i * size * relocations.len() as u64), alloc_id) + (offset + dest.offset - src.offset + (i * size * relocations.len() as u64), + alloc_id) }) ); } @@ -707,11 +714,15 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> { } for i in 0..length { - ptr::copy(src_bytes, dest_bytes.offset((size.bytes() * i) as isize), size.bytes() as usize); + ptr::copy(src_bytes, + dest_bytes.offset((size.bytes() * i) as isize), + size.bytes() as usize); } } else { for i in 0..length { - ptr::copy_nonoverlapping(src_bytes, dest_bytes.offset((size.bytes() * i) as isize), size.bytes() as usize); + ptr::copy_nonoverlapping(src_bytes, + dest_bytes.offset((size.bytes() * i) as isize), + size.bytes() as usize); } } } @@ -778,7 +789,8 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> { } /// Read a *non-ZST* scalar - pub fn read_scalar(&self, ptr: Pointer, ptr_align: Align, size: Size) -> EvalResult<'tcx, ScalarMaybeUndef> { + pub fn read_scalar(&self, ptr: Pointer, ptr_align: Align, size: Size) + -> EvalResult<'tcx, ScalarMaybeUndef> { // Make sure we don't read part of a pointer as a pointer self.check_relocation_edges(ptr, size)?; let endianness = self.endianness(); @@ -801,7 +813,10 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> { } else { let alloc = self.get(ptr.alloc_id)?; match alloc.relocations.get(&ptr.offset) { - Some(&alloc_id) => return Ok(ScalarMaybeUndef::Scalar(Pointer::new(alloc_id, Size::from_bytes(bits as u64)).into())), + Some(&alloc_id) => { + let ptr = Pointer::new(alloc_id, Size::from_bytes(bits as u64)); + return Ok(ScalarMaybeUndef::Scalar(ptr.into())) + } None => {}, } } @@ -812,7 +827,8 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> { })) } - pub fn read_ptr_sized(&self, ptr: Pointer, ptr_align: Align) -> EvalResult<'tcx, ScalarMaybeUndef> { + pub fn read_ptr_sized(&self, ptr: Pointer, ptr_align: Align) + -> EvalResult<'tcx, ScalarMaybeUndef> { self.read_scalar(ptr, ptr_align, self.pointer_size()) } @@ -865,7 +881,8 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> { Ok(()) } - pub fn write_ptr_sized(&mut self, ptr: Pointer, ptr_align: Align, val: ScalarMaybeUndef) -> EvalResult<'tcx> { + pub fn write_ptr_sized(&mut self, ptr: Pointer, ptr_align: Align, val: ScalarMaybeUndef) + -> EvalResult<'tcx> { let ptr_size = self.pointer_size(); self.write_scalar(ptr.into(), ptr_align, val, ptr_size) } @@ -1009,7 +1026,9 @@ pub trait HasMemory<'a, 'mir, 'tcx: 'a + 'mir, M: Machine<'mir, 'tcx>> { fn memory(&self) -> &Memory<'a, 'mir, 'tcx, M>; } -impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> HasMemory<'a, 'mir, 'tcx, M> for Memory<'a, 'mir, 'tcx, M> { +impl<'a, 'mir, 'tcx, M> HasMemory<'a, 'mir, 'tcx, M> for Memory<'a, 'mir, 'tcx, M> +where M: Machine<'mir, 'tcx> +{ #[inline] fn memory_mut(&mut self) -> &mut Memory<'a, 'mir, 'tcx, M> { self @@ -1021,7 +1040,9 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> HasMemory<'a, 'mir, 'tcx, M> for Me } } -impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> HasMemory<'a, 'mir, 'tcx, M> for EvalContext<'a, 'mir, 'tcx, M> { +impl<'a, 'mir, 'tcx, M> HasMemory<'a, 'mir, 'tcx, M> for EvalContext<'a, 'mir, 'tcx, M> +where M: Machine<'mir, 'tcx> +{ #[inline] fn memory_mut(&mut self) -> &mut Memory<'a, 'mir, 'tcx, M> { &mut self.memory @@ -1033,7 +1054,9 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> HasMemory<'a, 'mir, 'tcx, M> for Ev } } -impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> layout::HasDataLayout for &'a Memory<'a, 'mir, 'tcx, M> { +impl<'a, 'mir, 'tcx, M> layout::HasDataLayout for &'a Memory<'a, 'mir, 'tcx, M> +where M: Machine<'mir, 'tcx> +{ #[inline] fn data_layout(&self) -> &TargetDataLayout { &self.tcx.data_layout diff --git a/src/librustc_mir/interpret/operator.rs b/src/librustc_mir/interpret/operator.rs index 69babac968bc8..89293dc101232 100644 --- a/src/librustc_mir/interpret/operator.rs +++ b/src/librustc_mir/interpret/operator.rs @@ -68,7 +68,8 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> { layout::Abi::Scalar(ref scalar) => scalar.value, _ => return err!(TypeNotPrimitive(right_layout.ty)), }; - trace!("Running binary op {:?}: {:?} ({:?}), {:?} ({:?})", bin_op, left, left_kind, right, right_kind); + trace!("Running binary op {:?}: {:?} ({:?}), {:?} ({:?})", + bin_op, left, left_kind, right, right_kind); // I: Handle operations that support pointers if !left_kind.is_float() && !right_kind.is_float() { diff --git a/src/librustc_mir/interpret/place.rs b/src/librustc_mir/interpret/place.rs index cf21e0238324f..17ace26ed8a53 100644 --- a/src/librustc_mir/interpret/place.rs +++ b/src/librustc_mir/interpret/place.rs @@ -287,11 +287,13 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> { offsets[usize::try_from(field).unwrap()], layout::FieldPlacement::Array { stride, .. } => { let len = base.len(); - assert!(field < len, "Tried to access element {} of array/slice with length {}", field, len); + assert!(field < len, + "Tried to access element {} of array/slice with length {}", field, len); stride * field } layout::FieldPlacement::Union(count) => { - assert!(field < count as u64, "Tried to access field {} of union with {} fields", field, count); + assert!(field < count as u64, + "Tried to access field {} of union with {} fields", field, count); // Offset is always 0 Size::from_bytes(0) } @@ -604,7 +606,8 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> { Value::ScalarPair(a_val, b_val) => { let (a, b) = match dest.layout.abi { layout::Abi::ScalarPair(ref a, ref b) => (&a.value, &b.value), - _ => bug!("write_value_to_mplace: invalid ScalarPair layout: {:#?}", dest.layout) + _ => bug!("write_value_to_mplace: invalid ScalarPair layout: {:#?}", + dest.layout) }; let (a_size, b_size) = (a.size(&self), b.size(&self)); let (a_align, b_align) = (a.align(&self), b.align(&self)); @@ -770,7 +773,8 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> { /// Turn a place that is a dyn trait (i.e., PlaceExtra::Vtable and the appropriate layout) /// or a slice into the specific fixed-size place and layout that is given by the vtable/len. /// This "unpacks" the existential quantifier, so to speak. - pub fn unpack_unsized_mplace(&self, mplace: MPlaceTy<'tcx>) -> EvalResult<'tcx, MPlaceTy<'tcx>> { + pub fn unpack_unsized_mplace(&self, mplace: MPlaceTy<'tcx>) + -> EvalResult<'tcx, MPlaceTy<'tcx>> { trace!("Unpacking {:?} ({:?})", *mplace, mplace.layout.ty); let layout = match mplace.extra { PlaceExtra::Vtable(vtable) => { diff --git a/src/librustc_mir/interpret/terminator/mod.rs b/src/librustc_mir/interpret/terminator/mod.rs index 10681e28e020d..782c033e2df06 100644 --- a/src/librustc_mir/interpret/terminator/mod.rs +++ b/src/librustc_mir/interpret/terminator/mod.rs @@ -55,7 +55,10 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> { for (index, &const_int) in values.iter().enumerate() { // Compare using binary_op - let const_int = Scalar::Bits { bits: const_int, size: discr.layout.size.bytes() as u8 }; + let const_int = Scalar::Bits { + bits: const_int, + size: discr.layout.size.bytes() as u8 + }; let (res, _) = self.binary_op(mir::BinOp::Eq, discr, ValTy { value: Value::Scalar(const_int.into()), layout: discr.layout } @@ -154,7 +157,9 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> { target, .. } => { - let cond_val = self.eval_operand_and_read_value(cond, None)?.to_scalar()?.to_bool()?; + let cond_val = self.eval_operand_and_read_value(cond, None)? + .to_scalar()? + .to_bool()?; if expected == cond_val { self.goto_block(target); } else { @@ -239,15 +244,24 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> { // We need to allow what comes up when a non-capturing closure is cast to a fn(). match (sig.abi, real_sig.abi) { (Abi::Rust, Abi::RustCall) // check the ABIs. This makes the test here non-symmetric. - if check_ty_compat(sig.output(), real_sig.output()) && real_sig.inputs_and_output.len() == 3 => { + if check_ty_compat(sig.output(), real_sig.output()) + && real_sig.inputs_and_output.len() == 3 => { // First argument of real_sig must be a ZST let fst_ty = real_sig.inputs_and_output[0]; if self.layout_of(fst_ty)?.is_zst() { // Second argument must be a tuple matching the argument list of sig let snd_ty = real_sig.inputs_and_output[1]; match snd_ty.sty { +<<<<<<< HEAD ty::Tuple(tys) if sig.inputs().len() == tys.len() => if sig.inputs().iter().zip(tys).all(|(ty, real_ty)| check_ty_compat(ty, real_ty)) { +======= + ty::TyTuple(tys) if sig.inputs().len() == tys.len() => + if sig.inputs() + .iter() + .zip(tys) + .all(|(ty, real_ty)| check_ty_compat(ty, real_ty)) { +>>>>>>> 7d30ba9... Fixup long code lines return Ok(true) }, _ => {} @@ -304,7 +318,8 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> { trace!( "args: {:#?}", self.frame().mir.args_iter().zip(args.iter()) - .map(|(local, arg)| (local, **arg, arg.layout.ty)).collect::>() + .map(|(local, arg)| (local, **arg, arg.layout.ty)) + .collect::>() ); let local = arg_locals.nth(1).unwrap(); for (i, &op) in args.into_iter().enumerate() { @@ -325,7 +340,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> { ty::InstanceDef::Item(_) => { // Push the stack frame, and potentially be entirely done if the call got hooked if M::eval_fn_call(self, instance, destination, args, span)? { - // TODO: Can we make it return the frame to push, instead + // FIXME: Can we make it return the frame to push, instead // of the hook doing half of the work and us doing the argument // initialization? return Ok(()); diff --git a/src/librustc_mir/interpret/traits.rs b/src/librustc_mir/interpret/traits.rs index 0aada9c0d3c24..4ce0563749ac6 100644 --- a/src/librustc_mir/interpret/traits.rs +++ b/src/librustc_mir/interpret/traits.rs @@ -92,7 +92,8 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> { ) -> EvalResult<'tcx, (Size, Align)> { let pointer_size = self.memory.pointer_size(); let pointer_align = self.tcx.data_layout.pointer_align; - let size = self.memory.read_ptr_sized(vtable.offset(pointer_size, self)?, pointer_align)?.to_bits(pointer_size)? as u64; + let size = self.memory.read_ptr_sized(vtable.offset(pointer_size, self)?,pointer_align)? + .to_bits(pointer_size)? as u64; let align = self.memory.read_ptr_sized( vtable.offset(pointer_size * 2, self)?, pointer_align diff --git a/src/librustc_mir/interpret/validity.rs b/src/librustc_mir/interpret/validity.rs index 00fca7c586a56..b0dfceb259746 100644 --- a/src/librustc_mir/interpret/validity.rs +++ b/src/librustc_mir/interpret/validity.rs @@ -221,7 +221,10 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> { let variant = self.read_discriminant_as_variant_index(dest.into())?; let inner_dest = self.mplace_downcast(dest, variant)?; // Put the variant projection onto the path, as a field - path.push(PathElem::Field(dest.layout.ty.ty_adt_def().unwrap().variants[variant].name)); + path.push(PathElem::Field(dest.layout.ty + .ty_adt_def() + .unwrap() + .variants[variant].name)); trace!("variant layout: {:#?}", dest.layout); (variant, inner_dest) }, From 46b885f62b03957b5a89786f87c66b82fbde7253 Mon Sep 17 00:00:00 2001 From: Bernardo Meurer Date: Thu, 23 Aug 2018 08:45:59 -0700 Subject: [PATCH 6/9] Fix stylistic mistakes --- src/librustc/mir/interpret/error.rs | 4 ++-- src/librustc_mir/interpret/eval_context.rs | 13 +++++++++---- src/librustc_mir/interpret/place.rs | 6 ++++-- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/librustc/mir/interpret/error.rs b/src/librustc/mir/interpret/error.rs index 3590cbdf2c767..dc6d17d34535b 100644 --- a/src/librustc/mir/interpret/error.rs +++ b/src/librustc/mir/interpret/error.rs @@ -380,12 +380,12 @@ impl<'tcx, O> EvalErrorKind<'tcx, O> { Layout(_) => "rustc layout computation failed", UnterminatedCString(_) => - "attempted to get length of a null terminated string, but no null found before end + "attempted to get length of a null terminated string, but no null found before end \ of allocation", HeapAllocZeroBytes => "tried to re-, de- or allocate zero bytes on the heap", HeapAllocNonPowerOfTwoAlignment(_) => - "tried to re-, de-, or allocate heap memory with alignment that is not a power of + "tried to re-, de-, or allocate heap memory with alignment that is not a power of \ two", Unreachable => "entered unreachable code", diff --git a/src/librustc_mir/interpret/eval_context.rs b/src/librustc_mir/interpret/eval_context.rs index fdb1a1596c4de..29c1e32c479ff 100644 --- a/src/librustc_mir/interpret/eval_context.rs +++ b/src/librustc_mir/interpret/eval_context.rs @@ -392,8 +392,11 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M Ok(Value::new_slice(Scalar::Ptr(ptr), s.len() as u64, self.tcx.tcx)) } - pub(super) fn resolve(&self, def_id: DefId, substs: &'tcx Substs<'tcx>) - -> EvalResult<'tcx, ty::Instance<'tcx>> { + pub(super) fn resolve( + &self, + def_id: DefId, + substs: &'tcx Substs<'tcx> + ) -> EvalResult<'tcx, ty::Instance<'tcx>> { trace!("resolve: {:?}, {:#?}", def_id, substs); trace!("substs: {:#?}", self.substs()); trace!("param_env: {:#?}", self.param_env); @@ -422,7 +425,8 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M let did = instance.def_id(); if did.is_local() && self.tcx.has_typeck_tables(did) - && self.tcx.typeck_tables_of(did).tainted_by_errors { + && self.tcx.typeck_tables_of(did).tainted_by_errors + { return err!(TypeckError); } trace!("load mir {:?}", instance); @@ -777,7 +781,8 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M last_span = Some(span); } let location = if self.tcx.def_key(instance.def_id()).disambiguated_data.data - == DefPathData::ClosureExpr { + == DefPathData::ClosureExpr + { "closure".to_owned() } else { instance.to_string() diff --git a/src/librustc_mir/interpret/place.rs b/src/librustc_mir/interpret/place.rs index 17ace26ed8a53..f46d6a1ea7df3 100644 --- a/src/librustc_mir/interpret/place.rs +++ b/src/librustc_mir/interpret/place.rs @@ -773,8 +773,10 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> { /// Turn a place that is a dyn trait (i.e., PlaceExtra::Vtable and the appropriate layout) /// or a slice into the specific fixed-size place and layout that is given by the vtable/len. /// This "unpacks" the existential quantifier, so to speak. - pub fn unpack_unsized_mplace(&self, mplace: MPlaceTy<'tcx>) - -> EvalResult<'tcx, MPlaceTy<'tcx>> { + pub fn unpack_unsized_mplace( + &self, + mplace: MPlaceTy<'tcx> + ) -> EvalResult<'tcx, MPlaceTy<'tcx>> { trace!("Unpacking {:?} ({:?})", *mplace, mplace.layout.ty); let layout = match mplace.extra { PlaceExtra::Vtable(vtable) => { From 67a8666ac8e053981f9de854a96841003f36ced3 Mon Sep 17 00:00:00 2001 From: Bernardo Meurer Date: Thu, 23 Aug 2018 09:21:40 -0700 Subject: [PATCH 7/9] More tidy fixes to CTFE --- src/librustc_mir/interpret/const_eval.rs | 3 ++- src/librustc_mir/interpret/terminator/mod.rs | 11 +++-------- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/librustc_mir/interpret/const_eval.rs b/src/librustc_mir/interpret/const_eval.rs index 83c535e962fe8..8e77af7526e35 100644 --- a/src/librustc_mir/interpret/const_eval.rs +++ b/src/librustc_mir/interpret/const_eval.rs @@ -472,7 +472,8 @@ fn to_str<'a, 'tcx, 'mir>( if let Value::ScalarPair(ptr, len) = val { let len = len.not_undef()?.to_bits(ecx.memory.pointer_size())?; let bytes = ecx.memory.read_bytes(ptr.not_undef()?, Size::from_bytes(len as u64))?; - let str = ::std::str::from_utf8(bytes).map_err(|err| EvalErrorKind::ValidationFailure(err.to_string()))?; + let str = ::std::str::from_utf8(bytes) + .map_err(|err| EvalErrorKind::ValidationFailure(err.to_string()))?; Ok(Symbol::intern(str)) } else { bug!("panic arg is not a str") diff --git a/src/librustc_mir/interpret/terminator/mod.rs b/src/librustc_mir/interpret/terminator/mod.rs index 782c033e2df06..aec7bb0c0d606 100644 --- a/src/librustc_mir/interpret/terminator/mod.rs +++ b/src/librustc_mir/interpret/terminator/mod.rs @@ -252,16 +252,11 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> { // Second argument must be a tuple matching the argument list of sig let snd_ty = real_sig.inputs_and_output[1]; match snd_ty.sty { -<<<<<<< HEAD ty::Tuple(tys) if sig.inputs().len() == tys.len() => - if sig.inputs().iter().zip(tys).all(|(ty, real_ty)| check_ty_compat(ty, real_ty)) { -======= - ty::TyTuple(tys) if sig.inputs().len() == tys.len() => if sig.inputs() - .iter() - .zip(tys) - .all(|(ty, real_ty)| check_ty_compat(ty, real_ty)) { ->>>>>>> 7d30ba9... Fixup long code lines + .iter() + .zip(tys) + .all(|(ty, real_ty)| check_ty_compat(ty, real_ty)) { return Ok(true) }, _ => {} From 6ca038475537d8a58d793e4312274332065e3c9a Mon Sep 17 00:00:00 2001 From: Bernardo Meurer Date: Thu, 23 Aug 2018 09:26:37 -0700 Subject: [PATCH 8/9] Small style fixes --- src/librustc_mir/interpret/cast.rs | 2 +- src/librustc_mir/interpret/eval_context.rs | 2 +- src/librustc_mir/interpret/memory.rs | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/librustc_mir/interpret/cast.rs b/src/librustc_mir/interpret/cast.rs index 373da1d04ff9d..4522f477959b3 100644 --- a/src/librustc_mir/interpret/cast.rs +++ b/src/librustc_mir/interpret/cast.rs @@ -245,7 +245,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> { bits: u128, fty: FloatTy, dest_ty: Ty<'tcx> - ) -> EvalResult<'tcx, Scalar> { + ) -> EvalResult<'tcx, Scalar> { use rustc::ty::TyKind::*; use rustc_apfloat::FloatConvert; match dest_ty.sty { diff --git a/src/librustc_mir/interpret/eval_context.rs b/src/librustc_mir/interpret/eval_context.rs index 29c1e32c479ff..85942956d2e49 100644 --- a/src/librustc_mir/interpret/eval_context.rs +++ b/src/librustc_mir/interpret/eval_context.rs @@ -281,7 +281,7 @@ impl<'c, 'b, 'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> HasDataLayout } impl<'a, 'mir, 'tcx, M> layout::HasTyCtxt<'tcx> for &'a EvalContext<'a, 'mir, 'tcx, M> -where M: Machine<'mir, 'tcx> + where M: Machine<'mir, 'tcx> { #[inline] fn tcx<'b>(&'b self) -> TyCtxt<'b, 'tcx, 'tcx> { diff --git a/src/librustc_mir/interpret/memory.rs b/src/librustc_mir/interpret/memory.rs index 557b1509c69d5..0d0b7bac87b30 100644 --- a/src/librustc_mir/interpret/memory.rs +++ b/src/librustc_mir/interpret/memory.rs @@ -1027,7 +1027,7 @@ pub trait HasMemory<'a, 'mir, 'tcx: 'a + 'mir, M: Machine<'mir, 'tcx>> { } impl<'a, 'mir, 'tcx, M> HasMemory<'a, 'mir, 'tcx, M> for Memory<'a, 'mir, 'tcx, M> -where M: Machine<'mir, 'tcx> + where M: Machine<'mir, 'tcx> { #[inline] fn memory_mut(&mut self) -> &mut Memory<'a, 'mir, 'tcx, M> { @@ -1041,7 +1041,7 @@ where M: Machine<'mir, 'tcx> } impl<'a, 'mir, 'tcx, M> HasMemory<'a, 'mir, 'tcx, M> for EvalContext<'a, 'mir, 'tcx, M> -where M: Machine<'mir, 'tcx> + where M: Machine<'mir, 'tcx> { #[inline] fn memory_mut(&mut self) -> &mut Memory<'a, 'mir, 'tcx, M> { @@ -1055,7 +1055,7 @@ where M: Machine<'mir, 'tcx> } impl<'a, 'mir, 'tcx, M> layout::HasDataLayout for &'a Memory<'a, 'mir, 'tcx, M> -where M: Machine<'mir, 'tcx> + where M: Machine<'mir, 'tcx> { #[inline] fn data_layout(&self) -> &TargetDataLayout { From e07c1542ac4ab8bfd67748a604ddbe5fa766e408 Mon Sep 17 00:00:00 2001 From: Bernardo Meurer Date: Thu, 23 Aug 2018 09:34:21 -0700 Subject: [PATCH 9/9] Fixed multi-line function signatures --- src/librustc_mir/interpret/eval_context.rs | 2 +- src/librustc_mir/interpret/memory.rs | 8 ++++++-- src/librustc_mir/interpret/place.rs | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/librustc_mir/interpret/eval_context.rs b/src/librustc_mir/interpret/eval_context.rs index 85942956d2e49..fa70a1500d060 100644 --- a/src/librustc_mir/interpret/eval_context.rs +++ b/src/librustc_mir/interpret/eval_context.rs @@ -396,7 +396,7 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M &self, def_id: DefId, substs: &'tcx Substs<'tcx> - ) -> EvalResult<'tcx, ty::Instance<'tcx>> { + ) -> EvalResult<'tcx, ty::Instance<'tcx>> { trace!("resolve: {:?}, {:#?}", def_id, substs); trace!("substs: {:#?}", self.substs()); trace!("param_env: {:#?}", self.param_env); diff --git a/src/librustc_mir/interpret/memory.rs b/src/librustc_mir/interpret/memory.rs index 0d0b7bac87b30..6f1a126534ce8 100644 --- a/src/librustc_mir/interpret/memory.rs +++ b/src/librustc_mir/interpret/memory.rs @@ -789,8 +789,12 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> { } /// Read a *non-ZST* scalar - pub fn read_scalar(&self, ptr: Pointer, ptr_align: Align, size: Size) - -> EvalResult<'tcx, ScalarMaybeUndef> { + pub fn read_scalar( + &self, + ptr: Pointer, + ptr_align: Align, + size: Size + ) -> EvalResult<'tcx, ScalarMaybeUndef> { // Make sure we don't read part of a pointer as a pointer self.check_relocation_edges(ptr, size)?; let endianness = self.endianness(); diff --git a/src/librustc_mir/interpret/place.rs b/src/librustc_mir/interpret/place.rs index f46d6a1ea7df3..f1c2b6b34fb15 100644 --- a/src/librustc_mir/interpret/place.rs +++ b/src/librustc_mir/interpret/place.rs @@ -776,7 +776,7 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> { pub fn unpack_unsized_mplace( &self, mplace: MPlaceTy<'tcx> - ) -> EvalResult<'tcx, MPlaceTy<'tcx>> { + ) -> EvalResult<'tcx, MPlaceTy<'tcx>> { trace!("Unpacking {:?} ({:?})", *mplace, mplace.layout.ty); let layout = match mplace.extra { PlaceExtra::Vtable(vtable) => {