]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc/mir/interpret/mod.rs
fix rustfmt fallout
[rust.git] / src / librustc / mir / interpret / mod.rs
index fff876752db55ab971a45126f726f5a81742f092..e554b280ef78cbc8723d559869c455a66a847fb4 100644 (file)
@@ -97,38 +97,41 @@ macro_rules! throw_machine_stop {
     };
 }
 
-mod error;
-mod value;
 mod allocation;
+mod error;
 mod pointer;
+mod queries;
+mod value;
 
 pub use self::error::{
-    InterpErrorInfo, InterpResult, InterpError, AssertMessage, ConstEvalErr, struct_error,
-    FrameInfo, ConstEvalRawResult, ConstEvalResult, ErrorHandled, PanicInfo, UnsupportedOpInfo,
-    InvalidProgramInfo, ResourceExhaustionInfo, UndefinedBehaviorInfo,
+    struct_error, AssertMessage, ConstEvalErr, ConstEvalRawResult, ConstEvalResult, ErrorHandled,
+    FrameInfo, InterpError, InterpErrorInfo, InterpResult, InvalidProgramInfo, PanicInfo,
+    ResourceExhaustionInfo, UndefinedBehaviorInfo, UnsupportedOpInfo,
 };
 
-pub use self::value::{Scalar, ScalarMaybeUndef, RawConst, ConstValue, get_slice_bytes};
+pub use self::value::{get_slice_bytes, ConstValue, RawConst, Scalar, ScalarMaybeUndef};
 
 pub use self::allocation::{Allocation, AllocationExtra, Relocations, UndefMask};
 
-pub use self::pointer::{Pointer, PointerArithmetic, CheckInAllocMsg};
+pub use self::pointer::{CheckInAllocMsg, Pointer, PointerArithmetic};
 
 use crate::mir;
-use crate::hir::def_id::DefId;
-use crate::ty::{self, TyCtxt, Instance, subst::GenericArgKind};
 use crate::ty::codec::TyDecoder;
 use crate::ty::layout::{self, Size};
-use std::io;
-use std::fmt;
-use std::num::NonZeroU32;
-use std::sync::atomic::{AtomicU32, Ordering};
-use rustc_serialize::{Encoder, Decodable, Encodable};
+use crate::ty::subst::GenericArgKind;
+use crate::ty::{self, Instance, Ty, TyCtxt};
+use byteorder::{BigEndian, LittleEndian, ReadBytesExt, WriteBytesExt};
 use rustc_data_structures::fx::FxHashMap;
-use rustc_data_structures::sync::{Lock, HashMapExt};
+use rustc_data_structures::sync::{HashMapExt, Lock};
 use rustc_data_structures::tiny_list::TinyList;
+use rustc_hir::def_id::DefId;
 use rustc_macros::HashStable;
-use byteorder::{WriteBytesExt, ReadBytesExt, LittleEndian, BigEndian};
+use rustc_serialize::{Decodable, Encodable, Encoder};
+use std::fmt;
+use std::io;
+use std::num::NonZeroU32;
+use std::sync::atomic::{AtomicU32, Ordering};
+use syntax::ast::LitKind;
 
 /// Uniquely identifies one of the following:
 /// - A constant
@@ -145,6 +148,24 @@ pub struct GlobalId<'tcx> {
     pub promoted: Option<mir::Promoted>,
 }
 
+/// Input argument for `tcx.lit_to_const`.
+#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, HashStable)]
+pub struct LitToConstInput<'tcx> {
+    /// The absolute value of the resultant constant.
+    pub lit: &'tcx LitKind,
+    /// The type of the constant.
+    pub ty: Ty<'tcx>,
+    /// If the constant is negative.
+    pub neg: bool,
+}
+
+/// Error type for `tcx.lit_to_const`.
+#[derive(Copy, Clone, Debug, Eq, PartialEq, HashStable)]
+pub enum LitToConstError {
+    UnparseableFloat,
+    Reported,
+}
+
 #[derive(Copy, Clone, Eq, Hash, Ord, PartialEq, PartialOrd, Debug)]
 pub struct AllocId(pub u64);
 
@@ -163,8 +184,8 @@ pub fn specialized_encode_alloc_id<'tcx, E: Encoder>(
     tcx: TyCtxt<'tcx>,
     alloc_id: AllocId,
 ) -> Result<(), E::Error> {
-    let alloc: GlobalAlloc<'tcx> = tcx.alloc_map.lock().get(alloc_id)
-        .expect("no value for given alloc ID");
+    let alloc: GlobalAlloc<'tcx> =
+        tcx.alloc_map.lock().get(alloc_id).expect("no value for given alloc ID");
     match alloc {
         GlobalAlloc::Memory(alloc) => {
             trace!("encoding {:?} with {:#?}", alloc_id, alloc);
@@ -212,19 +233,13 @@ pub fn new_decoding_session(&self) -> AllocDecodingSession<'_> {
         // Make sure this is never zero.
         let session_id = DecodingSessionId::new((counter & 0x7FFFFFFF) + 1).unwrap();
 
-        AllocDecodingSession {
-            state: self,
-            session_id,
-        }
+        AllocDecodingSession { state: self, session_id }
     }
 
     pub fn new(data_offsets: Vec<u32>) -> Self {
         let decoding_state = vec![Lock::new(State::Empty); data_offsets.len()];
 
-        Self {
-            decoding_state,
-            data_offsets,
-        }
+        Self { decoding_state, data_offsets }
     }
 }
 
@@ -267,16 +282,15 @@ pub fn decode_alloc_id<D>(&self, decoder: &mut D) -> Result<AllocId, D::Error>
                             // If this is an allocation, we need to reserve an
                             // `AllocId` so we can decode cyclic graphs.
                             let alloc_id = decoder.tcx().alloc_map.lock().reserve();
-                            *entry = State::InProgress(
-                                TinyList::new_single(self.session_id),
-                                alloc_id);
+                            *entry =
+                                State::InProgress(TinyList::new_single(self.session_id), alloc_id);
                             Some(alloc_id)
-                        },
+                        }
                         AllocDiscriminant::Fn | AllocDiscriminant::Static => {
                             // Fns and statics cannot be cyclic, and their `AllocId`
                             // is determined later by interning.
-                            *entry = State::InProgressNonAlloc(
-                                TinyList::new_single(self.session_id));
+                            *entry =
+                                State::InProgressNonAlloc(TinyList::new_single(self.session_id));
                             None
                         }
                     }
@@ -293,7 +307,7 @@ pub fn decode_alloc_id<D>(&self, decoder: &mut D) -> Result<AllocId, D::Error>
                 State::InProgress(ref mut sessions, alloc_id) => {
                     if sessions.contains(&self.session_id) {
                         // Don't recurse.
-                        return Ok(alloc_id)
+                        return Ok(alloc_id);
                     } else {
                         // Start decoding concurrently.
                         sessions.insert(self.session_id);
@@ -313,7 +327,7 @@ pub fn decode_alloc_id<D>(&self, decoder: &mut D) -> Result<AllocId, D::Error>
                     trace!("decoded alloc {:?}: {:#?}", alloc_id, alloc);
                     decoder.tcx().alloc_map.lock().set_alloc_id_same_memory(alloc_id, alloc);
                     Ok(alloc_id)
-                },
+                }
                 AllocDiscriminant::Fn => {
                     assert!(alloc_id.is_none());
                     trace!("creating fn alloc ID");
@@ -321,7 +335,7 @@ pub fn decode_alloc_id<D>(&self, decoder: &mut D) -> Result<AllocId, D::Error>
                     trace!("decoded fn alloc instance: {:?}", instance);
                     let alloc_id = decoder.tcx().alloc_map.lock().create_fn_alloc(instance);
                     Ok(alloc_id)
-                },
+                }
                 AllocDiscriminant::Static => {
                     assert!(alloc_id.is_none());
                     trace!("creating extern static alloc ID");
@@ -377,11 +391,7 @@ pub struct AllocMap<'tcx> {
 
 impl<'tcx> AllocMap<'tcx> {
     pub fn new() -> Self {
-        AllocMap {
-            alloc_map: Default::default(),
-            dedup: Default::default(),
-            next_id: AllocId(0),
-        }
+        AllocMap { alloc_map: Default::default(), dedup: Default::default(), next_id: AllocId(0) }
     }
 
     /// Obtains a new allocation ID that can be referenced but does not
@@ -389,15 +399,13 @@ pub fn new() -> Self {
     ///
     /// Make sure to call `set_alloc_id_memory` or `set_alloc_id_same_memory` before returning such
     /// an `AllocId` from a query.
-    pub fn reserve(
-        &mut self,
-    ) -> AllocId {
+    pub fn reserve(&mut self) -> AllocId {
         let next = self.next_id;
-        self.next_id.0 = self.next_id.0
-            .checked_add(1)
-            .expect("You overflowed a u64 by incrementing by 1... \
-                     You've just earned yourself a free drink if we ever meet. \
-                     Seriously, how did you do that?!");
+        self.next_id.0 = self.next_id.0.checked_add(1).expect(
+            "You overflowed a u64 by incrementing by 1... \
+             You've just earned yourself a free drink if we ever meet. \
+             Seriously, how did you do that?!",
+        );
         next
     }
 
@@ -406,7 +414,7 @@ pub fn reserve(
     /// to dedup IDs for "real" memory!
     fn reserve_and_set_dedup(&mut self, alloc: GlobalAlloc<'tcx>) -> AllocId {
         match alloc {
-            GlobalAlloc::Function(..) | GlobalAlloc::Static(..) => {},
+            GlobalAlloc::Function(..) | GlobalAlloc::Static(..) => {}
             GlobalAlloc::Memory(..) => bug!("Trying to dedup-reserve memory with real data!"),
         }
         if let Some(&alloc_id) = self.dedup.get(&alloc) {
@@ -435,11 +443,9 @@ pub fn create_fn_alloc(&mut self, instance: Instance<'tcx>) -> AllocId {
         // `main as fn() == main as fn()` is false, while `let x = main as fn(); x == x` is true.
         // However, formatting code relies on function identity (see #58320), so we only do
         // this for generic functions.  Lifetime parameters are ignored.
-        let is_generic = instance.substs.into_iter().any(|kind| {
-            match kind.unpack() {
-                GenericArgKind::Lifetime(_) => false,
-                _ => true,
-            }
+        let is_generic = instance.substs.into_iter().any(|kind| match kind.unpack() {
+            GenericArgKind::Lifetime(_) => false,
+            _ => true,
         });
         if is_generic {
             // Get a fresh ID.