Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test - simulate default discriminant start at 1 #88984

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_ssa/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub enum IntPredicate {
}

pub enum RealPredicate {
RealPredicateFalse,
RealPredicateFalse = 0, // FIXME(bonega)
RealOEQ,
RealOGT,
RealOGE,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2631,7 +2631,7 @@ impl Item<'_> {
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[derive(Encodable, Decodable, HashStable_Generic)]
pub enum Unsafety {
Unsafe,
Unsafe = 0, //FIXME(bonega)
Normal,
}

Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_hir/src/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use rustc_span::Span;
use std::lazy::SyncLazy;

pub enum LangItemGroup {
Op,
Op = 0, //FIXME(bonega)
}

const NUM_GROUPS: usize = 1;
Expand All @@ -44,6 +44,7 @@ macro_rules! language_item_table {
enum_from_u32! {
/// A representation of all the valid language items in Rust.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Encodable, Decodable)]
#[repr(u32)]
pub enum LangItem {
$(
#[doc = concat!("The `", stringify!($name), "` lang item.")]
Expand Down
37 changes: 35 additions & 2 deletions compiler/rustc_middle/src/ty/adt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,24 @@ impl<'tcx> AdtDef {
tcx: TyCtxt<'tcx>,
) -> impl Iterator<Item = (VariantIdx, Discr<'tcx>)> + Captures<'tcx> {
assert!(self.is_enum());
let no_explicit_discriminants = self
.variants
.iter_enumerated()
.all(|(i, v)| v.discr == ty::VariantDiscr::Relative(i.as_u32()));

let mut any_dataful_variants = false;
for fields in self.variants.iter() {
if fields.fields.len() > 0 {
any_dataful_variants = true;
break;
}
}
let initial_discr = no_explicit_discriminants
&& !self.repr.inhibit_enum_layout_opt()
&& !self.repr.inhibit_struct_field_reordering_opt()
&& !any_dataful_variants;
let repr_type = self.repr.discr_type();
let initial = repr_type.initial_discriminant(tcx);
let initial = Discr { val: initial_discr as u128, ty: repr_type.to_ty(tcx) };
let mut prev_discr = None::<Discr<'tcx>>;
self.variants.iter_enumerated().map(move |(i, v)| {
let mut discr = prev_discr.map_or(initial, |d| d.wrap_incr(tcx));
Expand Down Expand Up @@ -436,9 +452,26 @@ impl<'tcx> AdtDef {
) -> Discr<'tcx> {
assert!(self.is_enum());
let (val, offset) = self.discriminant_def_for_variant(variant_index);
let no_explicit_discriminants = self
.variants
.iter_enumerated()
.all(|(i, v)| v.discr == ty::VariantDiscr::Relative(i.as_u32()));

let mut any_dataful_variants = false;
for fields in self.variants.iter() {
if fields.fields.len() > 0 {
any_dataful_variants = true;
break;
}
}
let initial_discr = no_explicit_discriminants
&& !self.repr.inhibit_enum_layout_opt()
&& !self.repr.inhibit_struct_field_reordering_opt()
&& !any_dataful_variants;
let initial = Discr { val: initial_discr as u128, ty: self.repr.discr_type().to_ty(tcx) };
let explicit_value = val
.and_then(|expr_did| self.eval_explicit_discr(tcx, expr_did))
.unwrap_or_else(|| self.repr.discr_type().initial_discriminant(tcx));
.unwrap_or_else(|| initial);
explicit_value.checked_add(tcx, offset as u128).0
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_parse_format/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ pub enum Alignment {
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Flag {
/// A `+` will be used to denote positive numbers.
FlagSignPlus,
FlagSignPlus = 0, //FIXME(bonega)
/// A `-` will be used to denote negative numbers. This is the default.
FlagSignMinus,
/// An alternate form will be used for the value. In the case of numbers,
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_type_ir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ impl DebruijnIndex {
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[derive(Encodable, Decodable)]
pub enum IntTy {
Isize,
Isize = 0, //FIXME(bonega)
I8,
I16,
I32,
Expand Down Expand Up @@ -287,7 +287,7 @@ impl IntTy {
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, Debug)]
#[derive(Encodable, Decodable)]
pub enum UintTy {
Usize,
Usize = 0, //FIXME(bonega)
U8,
U16,
U32,
Expand Down Expand Up @@ -334,7 +334,7 @@ impl UintTy {
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[derive(Encodable, Decodable)]
pub enum FloatTy {
F32,
F32 = 0, //FIXME(bonega)
F64,
}

Expand Down
17 changes: 16 additions & 1 deletion compiler/rustc_typeck/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,22 @@ fn convert_variant_ctor(tcx: TyCtxt<'_>, ctor_id: hir::HirId) {
fn convert_enum_variant_types(tcx: TyCtxt<'_>, def_id: DefId, variants: &[hir::Variant<'_>]) {
let def = tcx.adt_def(def_id);
let repr_type = def.repr.discr_type();
let initial = repr_type.initial_discriminant(tcx);
let no_explicit_discriminants = def
.variants
.iter_enumerated()
.all(|(i, v)| v.discr == ty::VariantDiscr::Relative(i.as_u32()));
let mut any_dataful_variants = false;
for fields in def.variants.iter() {
if fields.fields.len() > 0 {
any_dataful_variants = true;
break;
}
}
let initial_discr = no_explicit_discriminants
&& !def.repr.inhibit_enum_layout_opt()
&& !def.repr.inhibit_struct_field_reordering_opt()
&& !any_dataful_variants;
let initial = Discr { val: initial_discr as u128, ty: repr_type.to_ty(tcx) };
let mut prev_discr = None::<Discr<'_>>;

// fill the discriminant values and field types
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ impl<'a> ArgumentV1<'a> {
// flags available in the v1 format of format_args
#[derive(Copy, Clone)]
enum FlagV1 {
SignPlus,
SignPlus = 0, //FIXME(bonega)
SignMinus,
Alternate,
SignAwareZeroPad,
Expand Down
9 changes: 6 additions & 3 deletions library/std/src/sync/mpsc/oneshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,23 @@ pub struct Packet<T> {
upgrade: UnsafeCell<MyUpgrade<T>>,
}

#[repr(usize)]
pub enum Failure<T> {
Empty,
Empty = 0, //FIXME(bonega) repr
Disconnected,
Upgraded(Receiver<T>),
}

#[repr(usize)]
pub enum UpgradeResult {
UpSuccess,
UpSuccess = 0, //FIXME(bonega) repr
UpDisconnected,
UpWoke(SignalToken),
}

#[repr(usize)]
enum MyUpgrade<T> {
NothingSent,
NothingSent = 0, //FIXME(bonega) repr
SendUsed,
GoUp(Receiver<T>),
}
Expand Down
5 changes: 3 additions & 2 deletions src/test/codegen/enum-bounds-check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
#![crate_type = "lib"]

pub enum Foo {
A, B
A = 0,
B,
}

// CHECK-LABEL: @lookup
Expand All @@ -15,7 +16,7 @@ pub fn lookup(buf: &[u8; 2], f: Foo) -> u8 {

pub enum Bar {
A = 2,
B = 3
B = 3,
}

// CHECK-LABEL: @lookup_unmodified
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

bb0: {
_2 = discriminant(_1); // scope 0 at $DIR/76803_regression.rs:11:11: 11:12
switchInt(move _2) -> [0_isize: bb2, otherwise: bb1]; // scope 0 at $DIR/76803_regression.rs:11:5: 11:12
switchInt(move _2) -> [1_isize: bb2, otherwise: bb1]; // scope 0 at $DIR/76803_regression.rs:11:5: 11:12
}

bb1: {
Expand Down
4 changes: 2 additions & 2 deletions src/test/mir-opt/const_goto.issue_77355_opt.ConstGoto.diff
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
bb0: {
- StorageLive(_2); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
- _3 = discriminant(_1); // scope 0 at $DIR/const_goto.rs:12:17: 12:20
- switchInt(move _3) -> [1_isize: bb2, 2_isize: bb2, otherwise: bb1]; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
- switchInt(move _3) -> [2_isize: bb2, 3_isize: bb2, otherwise: bb1]; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
+ _2 = discriminant(_1); // scope 0 at $DIR/const_goto.rs:12:17: 12:20
+ switchInt(move _2) -> [1_isize: bb2, 2_isize: bb2, otherwise: bb1]; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
+ switchInt(move _2) -> [2_isize: bb2, 3_isize: bb2, otherwise: bb1]; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
}

bb1: {
Expand Down
4 changes: 2 additions & 2 deletions src/test/mir-opt/issue_73223.main.PreCodegen.64bit.diff
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@
// + literal: Const { ty: for<'r, 's, 't0> fn(core::panicking::AssertKind, &'r i32, &'s i32, std::option::Option<std::fmt::Arguments<'t0>>) -> ! {core::panicking::assert_failed::<i32, i32>}, val: Value(Scalar(<ZST>)) }
// ty::Const
// + ty: core::panicking::AssertKind
// + val: Value(Scalar(0x00))
// + val: Value(Scalar(0x01))
// mir::Constant
// + span: $SRC_DIR/core/src/macros/mod.rs:LL:COL
// + literal: Const { ty: core::panicking::AssertKind, val: Value(Scalar(0x00)) }
// + literal: Const { ty: core::panicking::AssertKind, val: Value(Scalar(0x01)) }
}

bb2: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@
_22 = const core::panicking::AssertKind::Eq; // scope 5 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
// ty::Const
// + ty: core::panicking::AssertKind
// + val: Value(Scalar(0x00))
// + val: Value(Scalar(0x01))
// mir::Constant
// + span: $SRC_DIR/core/src/macros/mod.rs:LL:COL
// + literal: Const { ty: core::panicking::AssertKind, val: Value(Scalar(0x00)) }
// + literal: Const { ty: core::panicking::AssertKind, val: Value(Scalar(0x01)) }
StorageLive(_23); // scope 5 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
StorageLive(_24); // scope 5 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
_24 = _13; // scope 5 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
Expand All @@ -139,10 +139,10 @@
// + literal: Const { ty: for<'r, 's, 't0> fn(core::panicking::AssertKind, &'r i32, &'s i32, std::option::Option<std::fmt::Arguments<'t0>>) -> ! {core::panicking::assert_failed::<i32, i32>}, val: Value(Scalar(<ZST>)) }
// ty::Const
// + ty: core::panicking::AssertKind
// + val: Value(Scalar(0x00))
// + val: Value(Scalar(0x01))
// mir::Constant
// + span: $SRC_DIR/core/src/macros/mod.rs:LL:COL
// + literal: Const { ty: core::panicking::AssertKind, val: Value(Scalar(0x00)) }
// + literal: Const { ty: core::panicking::AssertKind, val: Value(Scalar(0x01)) }
}

bb4: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

bb0: {
_2 = discriminant(_1); // scope 0 at $DIR/matches_u8.rs:12:11: 12:12
switchInt(move _2) -> [0_isize: bb2, otherwise: bb1]; // scope 0 at $DIR/matches_u8.rs:12:5: 12:12
switchInt(move _2) -> [1_isize: bb2, otherwise: bb1]; // scope 0 at $DIR/matches_u8.rs:12:5: 12:12
}

bb1: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

bb0: {
_2 = discriminant(_1); // scope 0 at $DIR/matches_u8.rs:20:11: 20:12
switchInt(move _2) -> [0_isize: bb2, otherwise: bb1]; // scope 0 at $DIR/matches_u8.rs:20:5: 20:12
switchInt(move _2) -> [1_isize: bb2, otherwise: bb1]; // scope 0 at $DIR/matches_u8.rs:20:5: 20:12
}

bb1: {
Expand Down
8 changes: 4 additions & 4 deletions src/test/mir-opt/simplify_locals.d2.SimplifyLocals.diff
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@
- (_2.1: E) = const E::A; // scope 0 at $DIR/simplify-locals.rs:28:6: 28:16
- // ty::Const
- // + ty: E
- // + val: Value(Scalar(0x00))
- // + val: Value(Scalar(0x01))
- // mir::Constant
- // + span: $DIR/simplify-locals.rs:28:6: 28:16
- // + literal: Const { ty: E, val: Value(Scalar(0x00)) }
- // + literal: Const { ty: E, val: Value(Scalar(0x01)) }
- StorageDead(_3); // scope 0 at $DIR/simplify-locals.rs:28:15: 28:16
- (_2.1: E) = const E::B; // scope 0 at $DIR/simplify-locals.rs:28:5: 28:26
- // ty::Const
- // + ty: E
- // + val: Value(Scalar(0x01))
- // + val: Value(Scalar(0x02))
- // mir::Constant
- // + span: $DIR/simplify-locals.rs:28:5: 28:26
- // + literal: Const { ty: E, val: Value(Scalar(0x01)) }
- // + literal: Const { ty: E, val: Value(Scalar(0x02)) }
- StorageDead(_1); // scope 0 at $DIR/simplify-locals.rs:28:25: 28:26
- StorageDead(_2); // scope 0 at $DIR/simplify-locals.rs:28:26: 28:27
return; // scope 0 at $DIR/simplify-locals.rs:29:2: 29:2
Expand Down
36 changes: 27 additions & 9 deletions src/test/ui/consts/const-enum-cast.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
// run-pass
#![allow(non_upper_case_globals)]

enum A { A1, A2 }
enum B { B1=4, B2=2 }
enum A {
A1,
A2,
}
enum B {
B1 = 4,
B2 = 2,
}

pub fn main () {
pub fn main() {
static c1: isize = A::A2 as isize;
static c2: isize = B::B2 as isize;
let a1 = A::A2 as isize;
Expand All @@ -15,12 +21,24 @@ pub fn main () {
assert_eq!(a2, 2);

// Turns out that adding a let-binding generates totally different MIR.
static c1_2: isize = { let v = A::A1; v as isize };
static c2_2: isize = { let v = B::B1; v as isize };
let a1_2 = { let v = A::A1; v as isize };
let a2_2 = { let v = B::B1; v as isize };
assert_eq!(c1_2, 0);
static c1_2: isize = {
let v = A::A1;
v as isize
};
static c2_2: isize = {
let v = B::B1;
v as isize
};
let a1_2 = {
let v = A::A1;
v as isize
};
let a2_2 = {
let v = B::B1;
v as isize
};
assert_eq!(c1_2, 1);
assert_eq!(c2_2, 4);
assert_eq!(a1_2, 0);
assert_eq!(a1_2, 1);
assert_eq!(a2_2, 4);
}
4 changes: 2 additions & 2 deletions src/test/ui/enum-discriminant/discriminant_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ enum E4 {
fn main() {
let mut target: [isize; 3] = [0, 0, 0];
target[1] = discriminant_value(&E1::A);
assert_eq!(target, [0, 0, 0]);
target[1] = discriminant_value(&E1::B);
assert_eq!(target, [0, 1, 0]);
target[1] = discriminant_value(&E1::B);
assert_eq!(target, [0, 2, 0]);

let mut target: [i8; 3] = [0, 0, 0];
target[1] = discriminant_value(&E2::A);
Expand Down
Loading