Skip to content

Commit 3188ca7

Browse files
committed
update discriminant_value usage in the compiler
1 parent 25930e4 commit 3188ca7

File tree

3 files changed

+16
-71
lines changed

3 files changed

+16
-71
lines changed

src/librustc_builtin_macros/deriving/generic/mod.rs

+10-70
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,6 @@ use rustc_ast::ptr::P;
187187
use rustc_attr as attr;
188188
use rustc_data_structures::map_in_place::MapInPlace;
189189
use rustc_expand::base::{Annotatable, ExtCtxt};
190-
use rustc_session::parse::ParseSess;
191190
use rustc_span::source_map::respan;
192191
use rustc_span::symbol::{kw, sym, Ident, Symbol};
193192
use rustc_span::Span;
@@ -437,14 +436,7 @@ impl<'a> TraitDef<'a> {
437436
// This can only cause further compilation errors
438437
// downstream in blatantly illegal code, so it
439438
// is fine.
440-
self.expand_enum_def(
441-
cx,
442-
enum_def,
443-
&item.attrs,
444-
item.ident,
445-
generics,
446-
from_scratch,
447-
)
439+
self.expand_enum_def(cx, enum_def, item.ident, generics, from_scratch)
448440
}
449441
ast::ItemKind::Union(ref struct_def, ref generics) => {
450442
if self.supports_unions {
@@ -769,7 +761,6 @@ impl<'a> TraitDef<'a> {
769761
&self,
770762
cx: &mut ExtCtxt<'_>,
771763
enum_def: &'a EnumDef,
772-
type_attrs: &[ast::Attribute],
773764
type_ident: Ident,
774765
generics: &Generics,
775766
from_scratch: bool,
@@ -801,7 +792,6 @@ impl<'a> TraitDef<'a> {
801792
cx,
802793
self,
803794
enum_def,
804-
type_attrs,
805795
type_ident,
806796
self_args,
807797
&nonself_args[..],
@@ -816,38 +806,6 @@ impl<'a> TraitDef<'a> {
816806
}
817807
}
818808

819-
fn find_repr_type_name(sess: &ParseSess, type_attrs: &[ast::Attribute]) -> &'static str {
820-
let mut repr_type_name = "isize";
821-
for a in type_attrs {
822-
for r in &attr::find_repr_attrs(sess, a) {
823-
repr_type_name = match *r {
824-
attr::ReprPacked(_)
825-
| attr::ReprSimd
826-
| attr::ReprAlign(_)
827-
| attr::ReprTransparent
828-
| attr::ReprNoNiche => continue,
829-
830-
attr::ReprC => "i32",
831-
832-
attr::ReprInt(attr::SignedInt(ast::IntTy::Isize)) => "isize",
833-
attr::ReprInt(attr::SignedInt(ast::IntTy::I8)) => "i8",
834-
attr::ReprInt(attr::SignedInt(ast::IntTy::I16)) => "i16",
835-
attr::ReprInt(attr::SignedInt(ast::IntTy::I32)) => "i32",
836-
attr::ReprInt(attr::SignedInt(ast::IntTy::I64)) => "i64",
837-
attr::ReprInt(attr::SignedInt(ast::IntTy::I128)) => "i128",
838-
839-
attr::ReprInt(attr::UnsignedInt(ast::UintTy::Usize)) => "usize",
840-
attr::ReprInt(attr::UnsignedInt(ast::UintTy::U8)) => "u8",
841-
attr::ReprInt(attr::UnsignedInt(ast::UintTy::U16)) => "u16",
842-
attr::ReprInt(attr::UnsignedInt(ast::UintTy::U32)) => "u32",
843-
attr::ReprInt(attr::UnsignedInt(ast::UintTy::U64)) => "u64",
844-
attr::ReprInt(attr::UnsignedInt(ast::UintTy::U128)) => "u128",
845-
}
846-
}
847-
}
848-
repr_type_name
849-
}
850-
851809
impl<'a> MethodDef<'a> {
852810
fn call_substructure_method(
853811
&self,
@@ -1148,20 +1106,11 @@ impl<'a> MethodDef<'a> {
11481106
cx: &mut ExtCtxt<'_>,
11491107
trait_: &TraitDef<'b>,
11501108
enum_def: &'b EnumDef,
1151-
type_attrs: &[ast::Attribute],
11521109
type_ident: Ident,
11531110
self_args: Vec<P<Expr>>,
11541111
nonself_args: &[P<Expr>],
11551112
) -> P<Expr> {
1156-
self.build_enum_match_tuple(
1157-
cx,
1158-
trait_,
1159-
enum_def,
1160-
type_attrs,
1161-
type_ident,
1162-
self_args,
1163-
nonself_args,
1164-
)
1113+
self.build_enum_match_tuple(cx, trait_, enum_def, type_ident, self_args, nonself_args)
11651114
}
11661115

11671116
/// Creates a match for a tuple of all `self_args`, where either all
@@ -1181,11 +1130,11 @@ impl<'a> MethodDef<'a> {
11811130
11821131
/// ```{.text}
11831132
/// let __self0_vi = unsafe {
1184-
/// std::intrinsics::discriminant_value(&self) } as i32;
1133+
/// std::intrinsics::discriminant_value(&self) };
11851134
/// let __self1_vi = unsafe {
1186-
/// std::intrinsics::discriminant_value(&arg1) } as i32;
1135+
/// std::intrinsics::discriminant_value(&arg1) };
11871136
/// let __self2_vi = unsafe {
1188-
/// std::intrinsics::discriminant_value(&arg2) } as i32;
1137+
/// std::intrinsics::discriminant_value(&arg2) };
11891138
///
11901139
/// if __self0_vi == __self1_vi && __self0_vi == __self2_vi && ... {
11911140
/// match (...) {
@@ -1204,7 +1153,6 @@ impl<'a> MethodDef<'a> {
12041153
cx: &mut ExtCtxt<'_>,
12051154
trait_: &TraitDef<'b>,
12061155
enum_def: &'b EnumDef,
1207-
type_attrs: &[ast::Attribute],
12081156
type_ident: Ident,
12091157
mut self_args: Vec<P<Expr>>,
12101158
nonself_args: &[P<Expr>],
@@ -1392,39 +1340,31 @@ impl<'a> MethodDef<'a> {
13921340
//
13931341
if variants.len() > 1 && self_args.len() > 1 {
13941342
// Build a series of let statements mapping each self_arg
1395-
// to its discriminant value. If this is a C-style enum
1396-
// with a specific repr type, then casts the values to
1397-
// that type. Otherwise casts to `i32` (the default repr
1398-
// type).
1343+
// to its discriminant value.
13991344
//
14001345
// i.e., for `enum E<T> { A, B(1), C(T, T) }`, and a deriving
14011346
// with three Self args, builds three statements:
14021347
//
14031348
// ```
14041349
// let __self0_vi = unsafe {
1405-
// std::intrinsics::discriminant_value(&self) } as i32;
1350+
// std::intrinsics::discriminant_value(&self) };
14061351
// let __self1_vi = unsafe {
1407-
// std::intrinsics::discriminant_value(&arg1) } as i32;
1352+
// std::intrinsics::discriminant_value(&arg1) };
14081353
// let __self2_vi = unsafe {
1409-
// std::intrinsics::discriminant_value(&arg2) } as i32;
1354+
// std::intrinsics::discriminant_value(&arg2) };
14101355
// ```
14111356
let mut index_let_stmts: Vec<ast::Stmt> = Vec::with_capacity(vi_idents.len() + 1);
14121357

14131358
// We also build an expression which checks whether all discriminants are equal
14141359
// discriminant_test = __self0_vi == __self1_vi && __self0_vi == __self2_vi && ...
14151360
let mut discriminant_test = cx.expr_bool(sp, true);
14161361

1417-
let target_type_name = find_repr_type_name(&cx.parse_sess, type_attrs);
1418-
14191362
let mut first_ident = None;
14201363
for (&ident, self_arg) in vi_idents.iter().zip(&self_args) {
14211364
let self_addr = cx.expr_addr_of(sp, self_arg.clone());
14221365
let variant_value =
14231366
deriving::call_intrinsic(cx, sp, "discriminant_value", vec![self_addr]);
1424-
1425-
let target_ty = cx.ty_ident(sp, cx.ident_of(target_type_name, sp));
1426-
let variant_disr = cx.expr_cast(sp, variant_value, target_ty);
1427-
let let_stmt = cx.stmt_let(sp, false, ident, variant_disr);
1367+
let let_stmt = cx.stmt_let(sp, false, ident, variant_value);
14281368
index_let_stmts.push(let_stmt);
14291369

14301370
match first_ident {

src/librustc_middle/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#![feature(const_panic)]
3333
#![feature(const_transmute)]
3434
#![feature(core_intrinsics)]
35+
#![feature(discriminant_kind)]
3536
#![feature(drain_filter)]
3637
#![feature(never_type)]
3738
#![feature(exhaustive_patterns)]

src/librustc_middle/ty/codec.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ use rustc_data_structures::fx::FxHashMap;
1515
use rustc_hir::def_id::{CrateNum, DefId};
1616
use rustc_serialize::{opaque, Decodable, Decoder, Encodable, Encoder};
1717
use rustc_span::Span;
18+
use std::convert::{TryFrom, TryInto};
1819
use std::hash::Hash;
1920
use std::intrinsics;
21+
use std::marker::DiscriminantKind;
2022

2123
/// The shorthand encoding uses an enum's variant index `usize`
2224
/// and is offset by this value so it never matches a real variant.
@@ -60,6 +62,7 @@ where
6062
E: TyEncoder,
6163
M: for<'b> Fn(&'b mut E) -> &'b mut FxHashMap<T, usize>,
6264
T: EncodableWithShorthand,
65+
<T::Variant as DiscriminantKind>::Discriminant: Ord + TryFrom<usize>,
6366
{
6467
let existing_shorthand = cache(encoder).get(value).cloned();
6568
if let Some(shorthand) = existing_shorthand {
@@ -75,7 +78,8 @@ where
7578
// The shorthand encoding uses the same usize as the
7679
// discriminant, with an offset so they can't conflict.
7780
let discriminant = intrinsics::discriminant_value(variant);
78-
assert!(discriminant < SHORTHAND_OFFSET as u64);
81+
assert!(discriminant < SHORTHAND_OFFSET.try_into().ok().unwrap());
82+
7983
let shorthand = start + SHORTHAND_OFFSET;
8084

8185
// Get the number of bits that leb128 could fit

0 commit comments

Comments
 (0)