about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2020-01-04 13:17:27 +0100
committerGitHub <noreply@github.com>2020-01-04 13:17:27 +0100
commita469b1785d7ff414abe718cf013440ca0ead7e42 (patch)
tree10c9e823e6be2be9e662acc3b604666c672f23e9
parentabf2e00e38ad404d563f03acbcf06b08813fd086 (diff)
parent8e4886dd55266aca41943f7ce48863f61bcd250e (diff)
downloadrust-a469b1785d7ff414abe718cf013440ca0ead7e42.tar.gz
rust-a469b1785d7ff414abe718cf013440ca0ead7e42.zip
Rollup merge of #67822 - wesleywiser:revert_67676, r=oli-obk
Revert `const_err` lint checking of casts

Reverts part of #67676

r? @oli-obk

cc @SimonSapin
-rw-r--r--src/librustc_mir/transform/const_prop.rs70
-rw-r--r--src/test/ui/consts/const-prop-overflowing-casts.rs16
-rw-r--r--src/test/ui/consts/const-prop-overflowing-casts.stderr22
-rw-r--r--src/test/ui/simd/simd-intrinsic-generic-cast.rs1
4 files changed, 18 insertions, 91 deletions
diff --git a/src/librustc_mir/transform/const_prop.rs b/src/librustc_mir/transform/const_prop.rs
index 3895cdf71a6..958239b76e4 100644
--- a/src/librustc_mir/transform/const_prop.rs
+++ b/src/librustc_mir/transform/const_prop.rs
@@ -12,10 +12,10 @@ use rustc::mir::visit::{
     MutVisitor, MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor,
 };
 use rustc::mir::{
-    read_only, AggregateKind, BasicBlock, BinOp, Body, BodyAndCache, CastKind, ClearCrossCrate,
-    Constant, Local, LocalDecl, LocalKind, Location, Operand, Place, PlaceBase,
-    ReadOnlyBodyAndCache, Rvalue, SourceInfo, SourceScope, SourceScopeData, Statement,
-    StatementKind, Terminator, TerminatorKind, UnOp, RETURN_PLACE,
+    read_only, AggregateKind, BasicBlock, BinOp, Body, BodyAndCache, ClearCrossCrate, Constant,
+    Local, LocalDecl, LocalKind, Location, Operand, Place, PlaceBase, ReadOnlyBodyAndCache, Rvalue,
+    SourceInfo, SourceScope, SourceScopeData, Statement, StatementKind, Terminator, TerminatorKind,
+    UnOp, RETURN_PLACE,
 };
 use rustc::ty::layout::{
     HasDataLayout, HasTyCtxt, LayoutError, LayoutOf, Size, TargetDataLayout, TyLayout,
@@ -29,9 +29,9 @@ use syntax::ast::Mutability;
 
 use crate::const_eval::error_to_const_error;
 use crate::interpret::{
-    self, intern_const_alloc_recursive, truncate, AllocId, Allocation, Frame, ImmTy, Immediate,
-    InterpCx, LocalState, LocalValue, Memory, MemoryKind, OpTy, Operand as InterpOperand, PlaceTy,
-    Pointer, ScalarMaybeUndef, StackPopCleanup,
+    self, intern_const_alloc_recursive, AllocId, Allocation, Frame, ImmTy, Immediate, InterpCx,
+    LocalState, LocalValue, Memory, MemoryKind, OpTy, Operand as InterpOperand, PlaceTy, Pointer,
+    ScalarMaybeUndef, StackPopCleanup,
 };
 use crate::rustc::ty::subst::Subst;
 use crate::transform::{MirPass, MirSource};
@@ -539,57 +539,6 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
         Some(())
     }
 
-    fn check_cast(
-        &mut self,
-        op: &Operand<'tcx>,
-        ty: Ty<'tcx>,
-        source_info: SourceInfo,
-        place_layout: TyLayout<'tcx>,
-    ) -> Option<()> {
-        if !ty.is_integral() || !op.ty(&self.local_decls, self.tcx).is_integral() {
-            return Some(());
-        }
-
-        let value = self.use_ecx(source_info, |this| {
-            this.ecx.read_immediate(this.ecx.eval_operand(op, None)?)
-        })?;
-
-        // Do not try to read bits for ZSTs. This can occur when casting an enum with one variant
-        // to an integer. Such enums are represented as ZSTs but still have a discriminant value
-        // which can be casted.
-        if value.layout.is_zst() {
-            return Some(());
-        }
-
-        let value_size = value.layout.size;
-        let value_bits = value.to_scalar().and_then(|r| r.to_bits(value_size));
-        if let Ok(value_bits) = value_bits {
-            let truncated = truncate(value_bits, place_layout.size);
-            if truncated != value_bits {
-                let scope = source_info.scope;
-                let lint_root = match &self.source_scopes[scope].local_data {
-                    ClearCrossCrate::Set(data) => data.lint_root,
-                    ClearCrossCrate::Clear => return None,
-                };
-                self.tcx.lint_hir(
-                    ::rustc::lint::builtin::CONST_ERR,
-                    lint_root,
-                    source_info.span,
-                    &format!(
-                        "truncating cast: the value {} requires {} bits but the target type is \
-                                          only {} bits",
-                        value_bits,
-                        value_size.bits(),
-                        place_layout.size.bits()
-                    ),
-                );
-                return None;
-            }
-        }
-
-        Some(())
-    }
-
     fn const_prop(
         &mut self,
         rvalue: &Rvalue<'tcx>,
@@ -651,11 +600,6 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
                 }
             }
 
-            Rvalue::Cast(CastKind::Misc, op, ty) => {
-                trace!("checking Cast(Misc, {:?}, {:?})", op, ty);
-                self.check_cast(op, ty, source_info, place_layout)?;
-            }
-
             _ => {}
         }
 
diff --git a/src/test/ui/consts/const-prop-overflowing-casts.rs b/src/test/ui/consts/const-prop-overflowing-casts.rs
index 11a04611487..8cc5b98250b 100644
--- a/src/test/ui/consts/const-prop-overflowing-casts.rs
+++ b/src/test/ui/consts/const-prop-overflowing-casts.rs
@@ -1,9 +1,15 @@
-// build-fail
-// ignore-tidy-linelength
+// check-pass
+
+enum Foo {
+    Bar = -42,
+    Baz = 42,
+}
 
 fn main() {
     let _ = 0u8 as u32;
-    let _ = (1u32 << 31) as u16; //~ ERROR truncating cast: the value 2147483648 requires 32 bits but the target type is only 16 bits
-    let _ = (1u16 << 15) as u8; //~ ERROR truncating cast: the value 32768 requires 16 bits but the target type is only 8 bits
-    let _ = (!0u16) as u8; //~ ERROR truncating cast: the value 65535 requires 16 bits but the target type is only 8 bits
+    let _ = (1u32 << 31) as u16;
+    let _ = (1u16 << 15) as u8;
+    let _ = (!0u16) as u8;
+    let _ = (-1i16) as i8;
+    let _ = (Foo::Bar) as i8;
 }
diff --git a/src/test/ui/consts/const-prop-overflowing-casts.stderr b/src/test/ui/consts/const-prop-overflowing-casts.stderr
deleted file mode 100644
index af4e2c7005a..00000000000
--- a/src/test/ui/consts/const-prop-overflowing-casts.stderr
+++ /dev/null
@@ -1,22 +0,0 @@
-error: truncating cast: the value 2147483648 requires 32 bits but the target type is only 16 bits
-  --> $DIR/const-prop-overflowing-casts.rs:6:13
-   |
-LL |     let _ = (1u32 << 31) as u16;
-   |             ^^^^^^^^^^^^^^^^^^^
-   |
-   = note: `#[deny(const_err)]` on by default
-
-error: truncating cast: the value 32768 requires 16 bits but the target type is only 8 bits
-  --> $DIR/const-prop-overflowing-casts.rs:7:13
-   |
-LL |     let _ = (1u16 << 15) as u8;
-   |             ^^^^^^^^^^^^^^^^^^
-
-error: truncating cast: the value 65535 requires 16 bits but the target type is only 8 bits
-  --> $DIR/const-prop-overflowing-casts.rs:8:13
-   |
-LL |     let _ = (!0u16) as u8;
-   |             ^^^^^^^^^^^^^
-
-error: aborting due to 3 previous errors
-
diff --git a/src/test/ui/simd/simd-intrinsic-generic-cast.rs b/src/test/ui/simd/simd-intrinsic-generic-cast.rs
index b81a76851d3..15f232e2c0f 100644
--- a/src/test/ui/simd/simd-intrinsic-generic-cast.rs
+++ b/src/test/ui/simd/simd-intrinsic-generic-cast.rs
@@ -4,7 +4,6 @@
 
 #![feature(repr_simd, platform_intrinsics, concat_idents, test)]
 #![allow(non_camel_case_types)]
-#![allow(const_err)] // the test macro casts i32s to i8 and u8 which causes lots of warnings
 
 extern crate test;