From: Bastian Kauschke Date: Sat, 16 Jan 2021 23:25:18 +0000 (+0100) Subject: prevent potential bug in `encode_with_shorthand`. X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=84b056d5970b3f91073de0414a03d613ecc1009f;p=rust.git prevent potential bug in `encode_with_shorthand`. --- diff --git a/compiler/rustc_middle/src/ty/codec.rs b/compiler/rustc_middle/src/ty/codec.rs index df594690215..7ad80e748aa 100644 --- a/compiler/rustc_middle/src/ty/codec.rs +++ b/compiler/rustc_middle/src/ty/codec.rs @@ -18,7 +18,6 @@ use rustc_hir::def_id::{CrateNum, DefId}; use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; use rustc_span::Span; -use std::convert::{TryFrom, TryInto}; use std::hash::Hash; use std::intrinsics; use std::marker::DiscriminantKind; @@ -95,7 +94,8 @@ pub fn encode_with_shorthand(encoder: &mut E, value: &T, cache: M) -> R E: TyEncoder<'tcx>, M: for<'b> Fn(&'b mut E) -> &'b mut FxHashMap, T: EncodableWithShorthand<'tcx, E>, - ::Discriminant: Ord + TryFrom, + // The discriminant and shorthand must have the same size. + T::Variant: DiscriminantKind, { let existing_shorthand = cache(encoder).get(value).copied(); if let Some(shorthand) = existing_shorthand { @@ -111,7 +111,7 @@ pub fn encode_with_shorthand(encoder: &mut E, value: &T, cache: M) -> R // The shorthand encoding uses the same usize as the // discriminant, with an offset so they can't conflict. let discriminant = intrinsics::discriminant_value(variant); - assert!(discriminant < SHORTHAND_OFFSET.try_into().ok().unwrap()); + assert!(SHORTHAND_OFFSET > discriminant as usize); let shorthand = start + SHORTHAND_OFFSET;