Skip to content

Commit 84b056d

Browse files
committed
prevent potential bug in encode_with_shorthand.
1 parent ef58949 commit 84b056d

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

compiler/rustc_middle/src/ty/codec.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use rustc_data_structures::fx::FxHashMap;
1818
use rustc_hir::def_id::{CrateNum, DefId};
1919
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
2020
use rustc_span::Span;
21-
use std::convert::{TryFrom, TryInto};
2221
use std::hash::Hash;
2322
use std::intrinsics;
2423
use std::marker::DiscriminantKind;
@@ -95,7 +94,8 @@ where
9594
E: TyEncoder<'tcx>,
9695
M: for<'b> Fn(&'b mut E) -> &'b mut FxHashMap<T, usize>,
9796
T: EncodableWithShorthand<'tcx, E>,
98-
<T::Variant as DiscriminantKind>::Discriminant: Ord + TryFrom<usize>,
97+
// The discriminant and shorthand must have the same size.
98+
T::Variant: DiscriminantKind<Discriminant = isize>,
9999
{
100100
let existing_shorthand = cache(encoder).get(value).copied();
101101
if let Some(shorthand) = existing_shorthand {
@@ -111,7 +111,7 @@ where
111111
// The shorthand encoding uses the same usize as the
112112
// discriminant, with an offset so they can't conflict.
113113
let discriminant = intrinsics::discriminant_value(variant);
114-
assert!(discriminant < SHORTHAND_OFFSET.try_into().ok().unwrap());
114+
assert!(SHORTHAND_OFFSET > discriminant as usize);
115115

116116
let shorthand = start + SHORTHAND_OFFSET;
117117

0 commit comments

Comments
 (0)