Skip to content

Commit a469b17

Browse files
Rollup merge of rust-lang#67822 - wesleywiser:revert_67676, r=oli-obk
Revert `const_err` lint checking of casts Reverts part of rust-lang#67676 r? @oli-obk cc @SimonSapin
2 parents abf2e00 + 8e4886d commit a469b17

File tree

4 files changed

+18
-91
lines changed

4 files changed

+18
-91
lines changed

src/librustc_mir/transform/const_prop.rs

+7-63
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ use rustc::mir::visit::{
1212
MutVisitor, MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor,
1313
};
1414
use rustc::mir::{
15-
read_only, AggregateKind, BasicBlock, BinOp, Body, BodyAndCache, CastKind, ClearCrossCrate,
16-
Constant, Local, LocalDecl, LocalKind, Location, Operand, Place, PlaceBase,
17-
ReadOnlyBodyAndCache, Rvalue, SourceInfo, SourceScope, SourceScopeData, Statement,
18-
StatementKind, Terminator, TerminatorKind, UnOp, RETURN_PLACE,
15+
read_only, AggregateKind, BasicBlock, BinOp, Body, BodyAndCache, ClearCrossCrate, Constant,
16+
Local, LocalDecl, LocalKind, Location, Operand, Place, PlaceBase, ReadOnlyBodyAndCache, Rvalue,
17+
SourceInfo, SourceScope, SourceScopeData, Statement, StatementKind, Terminator, TerminatorKind,
18+
UnOp, RETURN_PLACE,
1919
};
2020
use rustc::ty::layout::{
2121
HasDataLayout, HasTyCtxt, LayoutError, LayoutOf, Size, TargetDataLayout, TyLayout,
@@ -29,9 +29,9 @@ use syntax::ast::Mutability;
2929

3030
use crate::const_eval::error_to_const_error;
3131
use crate::interpret::{
32-
self, intern_const_alloc_recursive, truncate, AllocId, Allocation, Frame, ImmTy, Immediate,
33-
InterpCx, LocalState, LocalValue, Memory, MemoryKind, OpTy, Operand as InterpOperand, PlaceTy,
34-
Pointer, ScalarMaybeUndef, StackPopCleanup,
32+
self, intern_const_alloc_recursive, AllocId, Allocation, Frame, ImmTy, Immediate, InterpCx,
33+
LocalState, LocalValue, Memory, MemoryKind, OpTy, Operand as InterpOperand, PlaceTy, Pointer,
34+
ScalarMaybeUndef, StackPopCleanup,
3535
};
3636
use crate::rustc::ty::subst::Subst;
3737
use crate::transform::{MirPass, MirSource};
@@ -539,57 +539,6 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
539539
Some(())
540540
}
541541

542-
fn check_cast(
543-
&mut self,
544-
op: &Operand<'tcx>,
545-
ty: Ty<'tcx>,
546-
source_info: SourceInfo,
547-
place_layout: TyLayout<'tcx>,
548-
) -> Option<()> {
549-
if !ty.is_integral() || !op.ty(&self.local_decls, self.tcx).is_integral() {
550-
return Some(());
551-
}
552-
553-
let value = self.use_ecx(source_info, |this| {
554-
this.ecx.read_immediate(this.ecx.eval_operand(op, None)?)
555-
})?;
556-
557-
// Do not try to read bits for ZSTs. This can occur when casting an enum with one variant
558-
// to an integer. Such enums are represented as ZSTs but still have a discriminant value
559-
// which can be casted.
560-
if value.layout.is_zst() {
561-
return Some(());
562-
}
563-
564-
let value_size = value.layout.size;
565-
let value_bits = value.to_scalar().and_then(|r| r.to_bits(value_size));
566-
if let Ok(value_bits) = value_bits {
567-
let truncated = truncate(value_bits, place_layout.size);
568-
if truncated != value_bits {
569-
let scope = source_info.scope;
570-
let lint_root = match &self.source_scopes[scope].local_data {
571-
ClearCrossCrate::Set(data) => data.lint_root,
572-
ClearCrossCrate::Clear => return None,
573-
};
574-
self.tcx.lint_hir(
575-
::rustc::lint::builtin::CONST_ERR,
576-
lint_root,
577-
source_info.span,
578-
&format!(
579-
"truncating cast: the value {} requires {} bits but the target type is \
580-
only {} bits",
581-
value_bits,
582-
value_size.bits(),
583-
place_layout.size.bits()
584-
),
585-
);
586-
return None;
587-
}
588-
}
589-
590-
Some(())
591-
}
592-
593542
fn const_prop(
594543
&mut self,
595544
rvalue: &Rvalue<'tcx>,
@@ -651,11 +600,6 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
651600
}
652601
}
653602

654-
Rvalue::Cast(CastKind::Misc, op, ty) => {
655-
trace!("checking Cast(Misc, {:?}, {:?})", op, ty);
656-
self.check_cast(op, ty, source_info, place_layout)?;
657-
}
658-
659603
_ => {}
660604
}
661605

Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
// build-fail
2-
// ignore-tidy-linelength
1+
// check-pass
2+
3+
enum Foo {
4+
Bar = -42,
5+
Baz = 42,
6+
}
37

48
fn main() {
59
let _ = 0u8 as u32;
6-
let _ = (1u32 << 31) as u16; //~ ERROR truncating cast: the value 2147483648 requires 32 bits but the target type is only 16 bits
7-
let _ = (1u16 << 15) as u8; //~ ERROR truncating cast: the value 32768 requires 16 bits but the target type is only 8 bits
8-
let _ = (!0u16) as u8; //~ ERROR truncating cast: the value 65535 requires 16 bits but the target type is only 8 bits
10+
let _ = (1u32 << 31) as u16;
11+
let _ = (1u16 << 15) as u8;
12+
let _ = (!0u16) as u8;
13+
let _ = (-1i16) as i8;
14+
let _ = (Foo::Bar) as i8;
915
}

src/test/ui/consts/const-prop-overflowing-casts.stderr

-22
This file was deleted.

src/test/ui/simd/simd-intrinsic-generic-cast.rs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
#![feature(repr_simd, platform_intrinsics, concat_idents, test)]
66
#![allow(non_camel_case_types)]
7-
#![allow(const_err)] // the test macro casts i32s to i8 and u8 which causes lots of warnings
87

98
extern crate test;
109

0 commit comments

Comments
 (0)