1
Fork 0

Rollup merge of #110927 - nnethercote:Encoder-Decoder-cleanups, r=scottmcm

Encoder/decoder cleanups

Best reviewed one commit at a time.

r? ``@scottmcm``
This commit is contained in:
Matthias Krüger 2023-04-28 22:56:45 +02:00 committed by GitHub
commit 37076ebbe5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 130 additions and 175 deletions

View file

@ -26,7 +26,7 @@ use rustc_target::spec::abi::Abi;
use smallvec::SmallVec; use smallvec::SmallVec;
use std::fmt; use std::fmt;
#[derive(Debug, Copy, Clone, Encodable, HashStable_Generic)] #[derive(Debug, Copy, Clone, HashStable_Generic)]
pub struct Lifetime { pub struct Lifetime {
pub hir_id: HirId, pub hir_id: HirId,
@ -41,8 +41,7 @@ pub struct Lifetime {
pub res: LifetimeName, pub res: LifetimeName,
} }
#[derive(Debug, Clone, PartialEq, Eq, Encodable, Hash, Copy)] #[derive(Debug, Copy, Clone, HashStable_Generic)]
#[derive(HashStable_Generic)]
pub enum ParamName { pub enum ParamName {
/// Some user-given name like `T` or `'x`. /// Some user-given name like `T` or `'x`.
Plain(Ident), Plain(Ident),
@ -85,8 +84,7 @@ impl ParamName {
} }
} }
#[derive(Debug, Clone, PartialEq, Eq, Encodable, Hash, Copy)] #[derive(Debug, Copy, Clone, PartialEq, Eq, HashStable_Generic)]
#[derive(HashStable_Generic)]
pub enum LifetimeName { pub enum LifetimeName {
/// User-given names or fresh (synthetic) names. /// User-given names or fresh (synthetic) names.
Param(LocalDefId), Param(LocalDefId),
@ -243,13 +241,13 @@ impl<'hir> PathSegment<'hir> {
} }
} }
#[derive(Encodable, Clone, Copy, Debug, HashStable_Generic)] #[derive(Clone, Copy, Debug, HashStable_Generic)]
pub struct ConstArg { pub struct ConstArg {
pub value: AnonConst, pub value: AnonConst,
pub span: Span, pub span: Span,
} }
#[derive(Encodable, Clone, Copy, Debug, HashStable_Generic)] #[derive(Clone, Copy, Debug, HashStable_Generic)]
pub struct InferArg { pub struct InferArg {
pub hir_id: HirId, pub hir_id: HirId,
pub span: Span, pub span: Span,
@ -422,8 +420,7 @@ impl<'hir> GenericArgs<'hir> {
} }
} }
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Hash, Debug)] #[derive(Copy, Clone, PartialEq, Eq, Debug, HashStable_Generic)]
#[derive(HashStable_Generic)]
pub enum GenericArgsParentheses { pub enum GenericArgsParentheses {
No, No,
/// Bounds for `feature(return_type_notation)`, like `T: Trait<method(..): Send>`, /// Bounds for `feature(return_type_notation)`, like `T: Trait<method(..): Send>`,
@ -435,8 +432,7 @@ pub enum GenericArgsParentheses {
/// A modifier on a bound, currently this is only used for `?Sized`, where the /// A modifier on a bound, currently this is only used for `?Sized`, where the
/// modifier is `Maybe`. Negative bounds should also be handled here. /// modifier is `Maybe`. Negative bounds should also be handled here.
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Hash, Debug)] #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, HashStable_Generic)]
#[derive(HashStable_Generic)]
pub enum TraitBoundModifier { pub enum TraitBoundModifier {
None, None,
Maybe, Maybe,
@ -474,7 +470,7 @@ impl GenericBound<'_> {
pub type GenericBounds<'hir> = &'hir [GenericBound<'hir>]; pub type GenericBounds<'hir> = &'hir [GenericBound<'hir>];
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Debug, HashStable_Generic)] #[derive(Copy, Clone, Debug, HashStable_Generic)]
pub enum LifetimeParamKind { pub enum LifetimeParamKind {
// Indicates that the lifetime definition was explicitly declared (e.g., in // Indicates that the lifetime definition was explicitly declared (e.g., in
// `fn foo<'a>(x: &'a u8) -> &'a u8 { x }`). // `fn foo<'a>(x: &'a u8) -> &'a u8 { x }`).
@ -539,7 +535,7 @@ impl<'hir> GenericParam<'hir> {
/// early-bound (but can be a late-bound lifetime in functions, for example), /// early-bound (but can be a late-bound lifetime in functions, for example),
/// or from a `for<...>` binder, in which case it's late-bound (and notably, /// or from a `for<...>` binder, in which case it's late-bound (and notably,
/// does not show up in the parent item's generics). /// does not show up in the parent item's generics).
#[derive(Debug, HashStable_Generic, PartialEq, Eq, Copy, Clone)] #[derive(Debug, Clone, Copy, HashStable_Generic)]
pub enum GenericParamSource { pub enum GenericParamSource {
// Early or late-bound parameters defined on an item // Early or late-bound parameters defined on an item
Generics, Generics,
@ -1097,7 +1093,7 @@ pub struct PatField<'hir> {
pub span: Span, pub span: Span,
} }
#[derive(Copy, Clone, PartialEq, Encodable, Debug, HashStable_Generic)] #[derive(Copy, Clone, PartialEq, Debug, HashStable_Generic)]
pub enum RangeEnd { pub enum RangeEnd {
Included, Included,
Excluded, Excluded,
@ -1197,7 +1193,7 @@ pub enum PatKind<'hir> {
Slice(&'hir [Pat<'hir>], Option<&'hir Pat<'hir>>, &'hir [Pat<'hir>]), Slice(&'hir [Pat<'hir>], Option<&'hir Pat<'hir>>, &'hir [Pat<'hir>]),
} }
#[derive(Copy, Clone, PartialEq, Encodable, Debug, HashStable_Generic)] #[derive(Copy, Clone, PartialEq, Debug, HashStable_Generic)]
pub enum BinOpKind { pub enum BinOpKind {
/// The `+` operator (addition). /// The `+` operator (addition).
Add, Add,
@ -1325,7 +1321,7 @@ impl Into<ast::BinOpKind> for BinOpKind {
pub type BinOp = Spanned<BinOpKind>; pub type BinOp = Spanned<BinOpKind>;
#[derive(Copy, Clone, PartialEq, Encodable, Debug, HashStable_Generic)] #[derive(Copy, Clone, PartialEq, Debug, HashStable_Generic)]
pub enum UnOp { pub enum UnOp {
/// The `*` operator (dereferencing). /// The `*` operator (dereferencing).
Deref, Deref,
@ -1450,19 +1446,19 @@ pub struct ExprField<'hir> {
pub is_shorthand: bool, pub is_shorthand: bool,
} }
#[derive(Copy, Clone, PartialEq, Encodable, Debug, HashStable_Generic)] #[derive(Copy, Clone, PartialEq, Debug, HashStable_Generic)]
pub enum BlockCheckMode { pub enum BlockCheckMode {
DefaultBlock, DefaultBlock,
UnsafeBlock(UnsafeSource), UnsafeBlock(UnsafeSource),
} }
#[derive(Copy, Clone, PartialEq, Encodable, Debug, HashStable_Generic)] #[derive(Copy, Clone, PartialEq, Debug, HashStable_Generic)]
pub enum UnsafeSource { pub enum UnsafeSource {
CompilerGenerated, CompilerGenerated,
UserProvided, UserProvided,
} }
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Decodable, Hash, Debug)] #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
pub struct BodyId { pub struct BodyId {
pub hir_id: HirId, pub hir_id: HirId,
} }
@ -1506,7 +1502,7 @@ impl<'hir> Body<'hir> {
} }
/// The type of source expression that caused this generator to be created. /// The type of source expression that caused this generator to be created.
#[derive(Clone, PartialEq, PartialOrd, Eq, Hash, Debug, Copy)] #[derive(Clone, PartialEq, Eq, Debug, Copy, Hash)]
#[derive(HashStable_Generic, Encodable, Decodable)] #[derive(HashStable_Generic, Encodable, Decodable)]
pub enum GeneratorKind { pub enum GeneratorKind {
/// An explicit `async` block or the body of an async function. /// An explicit `async` block or the body of an async function.
@ -1539,7 +1535,7 @@ impl GeneratorKind {
/// ///
/// This helps error messages but is also used to drive coercions in /// This helps error messages but is also used to drive coercions in
/// type-checking (see #60424). /// type-checking (see #60424).
#[derive(Clone, PartialEq, PartialOrd, Eq, Hash, Debug, Copy)] #[derive(Clone, PartialEq, Eq, Hash, Debug, Copy)]
#[derive(HashStable_Generic, Encodable, Decodable)] #[derive(HashStable_Generic, Encodable, Decodable)]
pub enum AsyncGeneratorKind { pub enum AsyncGeneratorKind {
/// An explicit `async` block written by the user. /// An explicit `async` block written by the user.
@ -1649,7 +1645,7 @@ impl fmt::Display for ConstContext {
/// A literal. /// A literal.
pub type Lit = Spanned<LitKind>; pub type Lit = Spanned<LitKind>;
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Debug, HashStable_Generic)] #[derive(Copy, Clone, Debug, HashStable_Generic)]
pub enum ArrayLen { pub enum ArrayLen {
Infer(HirId, Span), Infer(HirId, Span),
Body(AnonConst), Body(AnonConst),
@ -1671,7 +1667,7 @@ impl ArrayLen {
/// ///
/// You can check if this anon const is a default in a const param /// You can check if this anon const is a default in a const param
/// `const N: usize = { ... }` with `tcx.hir().opt_const_param_default_param_def_id(..)` /// `const N: usize = { ... }` with `tcx.hir().opt_const_param_default_param_def_id(..)`
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Debug, HashStable_Generic)] #[derive(Copy, Clone, Debug, HashStable_Generic)]
pub struct AnonConst { pub struct AnonConst {
pub hir_id: HirId, pub hir_id: HirId,
pub def_id: LocalDefId, pub def_id: LocalDefId,
@ -2105,7 +2101,7 @@ impl<'hir> QPath<'hir> {
} }
/// Hints at the original code for a let statement. /// Hints at the original code for a let statement.
#[derive(Copy, Clone, Encodable, Debug, HashStable_Generic)] #[derive(Copy, Clone, Debug, HashStable_Generic)]
pub enum LocalSource { pub enum LocalSource {
/// A `match _ { .. }`. /// A `match _ { .. }`.
Normal, Normal,
@ -2158,7 +2154,7 @@ impl MatchSource {
} }
/// The loop type that yielded an `ExprKind::Loop`. /// The loop type that yielded an `ExprKind::Loop`.
#[derive(Copy, Clone, PartialEq, Encodable, Debug, HashStable_Generic)] #[derive(Copy, Clone, PartialEq, Debug, HashStable_Generic)]
pub enum LoopSource { pub enum LoopSource {
/// A `loop { .. }` loop. /// A `loop { .. }` loop.
Loop, Loop,
@ -2178,7 +2174,7 @@ impl LoopSource {
} }
} }
#[derive(Copy, Clone, Encodable, Debug, HashStable_Generic)] #[derive(Copy, Clone, Debug, HashStable_Generic)]
pub enum LoopIdError { pub enum LoopIdError {
OutsideLoopScope, OutsideLoopScope,
UnlabeledCfInWhileCondition, UnlabeledCfInWhileCondition,
@ -2197,7 +2193,7 @@ impl fmt::Display for LoopIdError {
} }
} }
#[derive(Copy, Clone, Encodable, Debug, HashStable_Generic)] #[derive(Copy, Clone, Debug, HashStable_Generic)]
pub struct Destination { pub struct Destination {
/// This is `Some(_)` iff there is an explicit user-specified 'label /// This is `Some(_)` iff there is an explicit user-specified 'label
pub label: Option<Label>, pub label: Option<Label>,
@ -2208,7 +2204,7 @@ pub struct Destination {
} }
/// The yield kind that caused an `ExprKind::Yield`. /// The yield kind that caused an `ExprKind::Yield`.
#[derive(Copy, Clone, PartialEq, Eq, Debug, Encodable, Decodable, HashStable_Generic)] #[derive(Copy, Clone, Debug, HashStable_Generic)]
pub enum YieldSource { pub enum YieldSource {
/// An `<expr>.await`. /// An `<expr>.await`.
Await { expr: Option<HirId> }, Await { expr: Option<HirId> },
@ -2327,7 +2323,7 @@ impl<'hir> TraitItem<'hir> {
} }
/// Represents a trait method's body (or just argument names). /// Represents a trait method's body (or just argument names).
#[derive(Encodable, Debug, Clone, Copy, HashStable_Generic)] #[derive(Debug, Clone, Copy, HashStable_Generic)]
pub enum TraitFn<'hir> { pub enum TraitFn<'hir> {
/// No default body in the trait, just a signature. /// No default body in the trait, just a signature.
Required(&'hir [Ident]), Required(&'hir [Ident]),
@ -2658,7 +2654,7 @@ pub struct OpaqueTy<'hir> {
} }
/// From whence the opaque type came. /// From whence the opaque type came.
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Decodable, Debug, HashStable_Generic)] #[derive(Copy, Clone, PartialEq, Eq, Debug, HashStable_Generic)]
pub enum OpaqueTyOrigin { pub enum OpaqueTyOrigin {
/// `-> impl Trait` /// `-> impl Trait`
FnReturn(LocalDefId), FnReturn(LocalDefId),
@ -2818,7 +2814,7 @@ impl ImplicitSelfKind {
} }
} }
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Encodable, Decodable, Debug)] #[derive(Copy, Clone, PartialEq, Eq, Encodable, Decodable, Debug)]
#[derive(HashStable_Generic)] #[derive(HashStable_Generic)]
pub enum IsAsync { pub enum IsAsync {
Async, Async,
@ -2831,7 +2827,7 @@ impl IsAsync {
} }
} }
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Encodable, Decodable, HashStable_Generic)] #[derive(Copy, Clone, PartialEq, Eq, Debug, Encodable, Decodable, HashStable_Generic)]
pub enum Defaultness { pub enum Defaultness {
Default { has_value: bool }, Default { has_value: bool },
Final, Final,
@ -2887,13 +2883,13 @@ pub enum ClosureBinder {
For { span: Span }, For { span: Span },
} }
#[derive(Encodable, Debug, Clone, Copy, HashStable_Generic)] #[derive(Debug, Clone, Copy, HashStable_Generic)]
pub struct Mod<'hir> { pub struct Mod<'hir> {
pub spans: ModSpans, pub spans: ModSpans,
pub item_ids: &'hir [ItemId], pub item_ids: &'hir [ItemId],
} }
#[derive(Copy, Clone, Debug, HashStable_Generic, Encodable)] #[derive(Copy, Clone, Debug, HashStable_Generic)]
pub struct ModSpans { pub struct ModSpans {
/// A span from the first token past `{` to the last token until `}`. /// A span from the first token past `{` to the last token until `}`.
/// For `mod foo;`, the inner span ranges from the first token /// For `mod foo;`, the inner span ranges from the first token
@ -2922,7 +2918,7 @@ pub struct Variant<'hir> {
pub span: Span, pub span: Span,
} }
#[derive(Copy, Clone, PartialEq, Encodable, Debug, HashStable_Generic)] #[derive(Copy, Clone, PartialEq, Debug, HashStable_Generic)]
pub enum UseKind { pub enum UseKind {
/// One import, e.g., `use foo::bar` or `use foo::bar as baz`. /// One import, e.g., `use foo::bar` or `use foo::bar as baz`.
/// Also produced for each element of a list `use`, e.g. /// Also produced for each element of a list `use`, e.g.
@ -3233,7 +3229,7 @@ impl fmt::Display for Unsafety {
} }
} }
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] #[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[derive(Encodable, Decodable, HashStable_Generic)] #[derive(Encodable, Decodable, HashStable_Generic)]
pub enum Constness { pub enum Constness {
Const, Const,
@ -3249,7 +3245,7 @@ impl fmt::Display for Constness {
} }
} }
#[derive(Copy, Clone, Encodable, Debug, HashStable_Generic)] #[derive(Copy, Clone, Debug, HashStable_Generic)]
pub struct FnHeader { pub struct FnHeader {
pub unsafety: Unsafety, pub unsafety: Unsafety,
pub constness: Constness, pub constness: Constness,
@ -3381,7 +3377,7 @@ impl ItemKind<'_> {
/// type or method, and whether it is public). This allows other /// type or method, and whether it is public). This allows other
/// passes to find the impl they want without loading the ID (which /// passes to find the impl they want without loading the ID (which
/// means fewer edges in the incremental compilation graph). /// means fewer edges in the incremental compilation graph).
#[derive(Encodable, Debug, Clone, Copy, HashStable_Generic)] #[derive(Debug, Clone, Copy, HashStable_Generic)]
pub struct TraitItemRef { pub struct TraitItemRef {
pub id: TraitItemId, pub id: TraitItemId,
pub ident: Ident, pub ident: Ident,
@ -3405,7 +3401,7 @@ pub struct ImplItemRef {
pub trait_item_def_id: Option<DefId>, pub trait_item_def_id: Option<DefId>,
} }
#[derive(Copy, Clone, PartialEq, Encodable, Debug, HashStable_Generic)] #[derive(Copy, Clone, PartialEq, Debug, HashStable_Generic)]
pub enum AssocItemKind { pub enum AssocItemKind {
Const, Const,
Fn { has_self: bool }, Fn { has_self: bool },
@ -3474,7 +3470,7 @@ pub enum ForeignItemKind<'hir> {
} }
/// A variable captured by a closure. /// A variable captured by a closure.
#[derive(Debug, Copy, Clone, Encodable, HashStable_Generic)] #[derive(Debug, Copy, Clone, HashStable_Generic)]
pub struct Upvar { pub struct Upvar {
/// First span where it is accessed (there can be multiple). /// First span where it is accessed (there can be multiple).
pub span: Span, pub span: Span,
@ -3483,7 +3479,7 @@ pub struct Upvar {
// The TraitCandidate's import_ids is empty if the trait is defined in the same module, and // The TraitCandidate's import_ids is empty if the trait is defined in the same module, and
// has length > 0 if the trait is found through an chain of imports, starting with the // has length > 0 if the trait is found through an chain of imports, starting with the
// import/use statement in the scope where the trait is used. // import/use statement in the scope where the trait is used.
#[derive(Encodable, Decodable, Debug, Clone, HashStable_Generic)] #[derive(Debug, Clone, HashStable_Generic)]
pub struct TraitCandidate { pub struct TraitCandidate {
pub def_id: DefId, pub def_id: DefId,
pub import_ids: SmallVec<[LocalDefId; 1]>, pub import_ids: SmallVec<[LocalDefId; 1]>,

View file

@ -203,7 +203,7 @@ impl Scope {
pub type ScopeDepth = u32; pub type ScopeDepth = u32;
/// The region scope tree encodes information about region relationships. /// The region scope tree encodes information about region relationships.
#[derive(TyEncodable, TyDecodable, Default, Debug)] #[derive(Default, Debug)]
pub struct ScopeTree { pub struct ScopeTree {
/// If not empty, this body is the root of this region hierarchy. /// If not empty, this body is the root of this region hierarchy.
pub root_body: Option<hir::HirId>, pub root_body: Option<hir::HirId>,
@ -317,13 +317,13 @@ pub struct ScopeTree {
/// candidates in general). In constants, the `lifetime` field is None /// candidates in general). In constants, the `lifetime` field is None
/// to indicate that certain expressions escape into 'static and /// to indicate that certain expressions escape into 'static and
/// should have no local cleanup scope. /// should have no local cleanup scope.
#[derive(Debug, Copy, Clone, TyEncodable, TyDecodable, HashStable)] #[derive(Debug, Copy, Clone, HashStable)]
pub enum RvalueCandidateType { pub enum RvalueCandidateType {
Borrow { target: hir::ItemLocalId, lifetime: Option<Scope> }, Borrow { target: hir::ItemLocalId, lifetime: Option<Scope> },
Pattern { target: hir::ItemLocalId, lifetime: Option<Scope> }, Pattern { target: hir::ItemLocalId, lifetime: Option<Scope> },
} }
#[derive(Debug, Copy, Clone, TyEncodable, TyDecodable, HashStable)] #[derive(Debug, Copy, Clone, HashStable)]
pub struct YieldData { pub struct YieldData {
/// The `Span` of the yield. /// The `Span` of the yield.
pub span: Span, pub span: Span,

View file

@ -199,7 +199,7 @@ impl<'tcx> ObligationCause<'tcx> {
} }
} }
#[derive(Clone, Debug, PartialEq, Eq, Hash, Lift, HashStable, TyEncodable, TyDecodable)] #[derive(Clone, Debug, PartialEq, Eq, Lift, HashStable, TyEncodable, TyDecodable)]
#[derive(TypeVisitable, TypeFoldable)] #[derive(TypeVisitable, TypeFoldable)]
pub struct UnifyReceiverContext<'tcx> { pub struct UnifyReceiverContext<'tcx> {
pub assoc_item: ty::AssocItem, pub assoc_item: ty::AssocItem,
@ -207,7 +207,7 @@ pub struct UnifyReceiverContext<'tcx> {
pub substs: SubstsRef<'tcx>, pub substs: SubstsRef<'tcx>,
} }
#[derive(Clone, PartialEq, Eq, Hash, Lift, Default, HashStable)] #[derive(Clone, PartialEq, Eq, Lift, Default, HashStable)]
#[derive(TypeVisitable, TypeFoldable, TyEncodable, TyDecodable)] #[derive(TypeVisitable, TypeFoldable, TyEncodable, TyDecodable)]
pub struct InternedObligationCauseCode<'tcx> { pub struct InternedObligationCauseCode<'tcx> {
/// `None` for `ObligationCauseCode::MiscObligation` (a common case, occurs ~60% of /// `None` for `ObligationCauseCode::MiscObligation` (a common case, occurs ~60% of
@ -243,7 +243,7 @@ impl<'tcx> std::ops::Deref for InternedObligationCauseCode<'tcx> {
} }
} }
#[derive(Clone, Debug, PartialEq, Eq, Hash, Lift, HashStable, TyEncodable, TyDecodable)] #[derive(Clone, Debug, PartialEq, Eq, Lift, HashStable, TyEncodable, TyDecodable)]
#[derive(TypeVisitable, TypeFoldable)] #[derive(TypeVisitable, TypeFoldable)]
pub enum ObligationCauseCode<'tcx> { pub enum ObligationCauseCode<'tcx> {
/// Not well classified or should be obvious from the span. /// Not well classified or should be obvious from the span.
@ -468,7 +468,7 @@ pub enum WellFormedLoc {
}, },
} }
#[derive(Clone, Debug, PartialEq, Eq, Hash, Lift, HashStable, TyEncodable, TyDecodable)] #[derive(Clone, Debug, PartialEq, Eq, Lift, HashStable, TyEncodable, TyDecodable)]
#[derive(TypeVisitable, TypeFoldable)] #[derive(TypeVisitable, TypeFoldable)]
pub struct ImplDerivedObligationCause<'tcx> { pub struct ImplDerivedObligationCause<'tcx> {
pub derived: DerivedObligationCause<'tcx>, pub derived: DerivedObligationCause<'tcx>,
@ -529,7 +529,7 @@ impl<'tcx> ty::Lift<'tcx> for StatementAsExpression {
} }
} }
#[derive(Clone, Debug, PartialEq, Eq, Hash, Lift, HashStable, TyEncodable, TyDecodable)] #[derive(Clone, Debug, PartialEq, Eq, Lift, HashStable, TyEncodable, TyDecodable)]
#[derive(TypeVisitable, TypeFoldable)] #[derive(TypeVisitable, TypeFoldable)]
pub struct MatchExpressionArmCause<'tcx> { pub struct MatchExpressionArmCause<'tcx> {
pub arm_block_id: Option<hir::HirId>, pub arm_block_id: Option<hir::HirId>,
@ -545,7 +545,7 @@ pub struct MatchExpressionArmCause<'tcx> {
pub opt_suggest_box_span: Option<Span>, pub opt_suggest_box_span: Option<Span>,
} }
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] #[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[derive(Lift, TypeFoldable, TypeVisitable, HashStable, TyEncodable, TyDecodable)] #[derive(Lift, TypeFoldable, TypeVisitable, HashStable, TyEncodable, TyDecodable)]
pub struct IfExpressionCause<'tcx> { pub struct IfExpressionCause<'tcx> {
pub then_id: hir::HirId, pub then_id: hir::HirId,
@ -556,7 +556,7 @@ pub struct IfExpressionCause<'tcx> {
pub opt_suggest_box_span: Option<Span>, pub opt_suggest_box_span: Option<Span>,
} }
#[derive(Clone, Debug, PartialEq, Eq, Hash, Lift, HashStable, TyEncodable, TyDecodable)] #[derive(Clone, Debug, PartialEq, Eq, Lift, HashStable, TyEncodable, TyDecodable)]
#[derive(TypeVisitable, TypeFoldable)] #[derive(TypeVisitable, TypeFoldable)]
pub struct DerivedObligationCause<'tcx> { pub struct DerivedObligationCause<'tcx> {
/// The trait predicate of the parent obligation that led to the /// The trait predicate of the parent obligation that led to the

View file

@ -188,7 +188,7 @@ impl<'tcx> AdtDef<'tcx> {
} }
} }
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, HashStable, TyEncodable, TyDecodable)] #[derive(Copy, Clone, Debug, Eq, PartialEq, HashStable, TyEncodable, TyDecodable)]
pub enum AdtKind { pub enum AdtKind {
Struct, Struct,
Union, Union,

View file

@ -51,13 +51,6 @@ macro_rules! write_leb128 {
}}; }};
} }
/// A byte that [cannot occur in UTF8 sequences][utf8]. Used to mark the end of a string.
/// This way we can skip validation and still be relatively sure that deserialization
/// did not desynchronize.
///
/// [utf8]: https://en.wikipedia.org/w/index.php?title=UTF-8&oldid=1058865525#Codepage_layout
const STR_SENTINEL: u8 = 0xC1;
impl Encoder for MemEncoder { impl Encoder for MemEncoder {
#[inline] #[inline]
fn emit_usize(&mut self, v: usize) { fn emit_usize(&mut self, v: usize) {
@ -114,28 +107,6 @@ impl Encoder for MemEncoder {
self.data.extend_from_slice(&v.to_le_bytes()); self.data.extend_from_slice(&v.to_le_bytes());
} }
#[inline]
fn emit_i8(&mut self, v: i8) {
self.emit_u8(v as u8);
}
#[inline]
fn emit_bool(&mut self, v: bool) {
self.emit_u8(if v { 1 } else { 0 });
}
#[inline]
fn emit_char(&mut self, v: char) {
self.emit_u32(v as u32);
}
#[inline]
fn emit_str(&mut self, v: &str) {
self.emit_usize(v.len());
self.emit_raw_bytes(v.as_bytes());
self.emit_u8(STR_SENTINEL);
}
#[inline] #[inline]
fn emit_raw_bytes(&mut self, s: &[u8]) { fn emit_raw_bytes(&mut self, s: &[u8]) {
self.data.extend_from_slice(s); self.data.extend_from_slice(s);
@ -480,28 +451,6 @@ impl Encoder for FileEncoder {
self.write_all(&v.to_le_bytes()); self.write_all(&v.to_le_bytes());
} }
#[inline]
fn emit_i8(&mut self, v: i8) {
self.emit_u8(v as u8);
}
#[inline]
fn emit_bool(&mut self, v: bool) {
self.emit_u8(if v { 1 } else { 0 });
}
#[inline]
fn emit_char(&mut self, v: char) {
self.emit_u32(v as u32);
}
#[inline]
fn emit_str(&mut self, v: &str) {
self.emit_usize(v.len());
self.emit_raw_bytes(v.as_bytes());
self.emit_u8(STR_SENTINEL);
}
#[inline] #[inline]
fn emit_raw_bytes(&mut self, s: &[u8]) { fn emit_raw_bytes(&mut self, s: &[u8]) {
self.write_all(s); self.write_all(s);
@ -555,40 +504,11 @@ impl<'a> MemDecoder<'a> {
panic!("MemDecoder exhausted") panic!("MemDecoder exhausted")
} }
#[inline]
fn read_byte(&mut self) -> u8 {
if self.current == self.end {
Self::decoder_exhausted();
}
// SAFETY: This type guarantees current <= end, and we just checked current == end.
unsafe {
let byte = *self.current;
self.current = self.current.add(1);
byte
}
}
#[inline] #[inline]
fn read_array<const N: usize>(&mut self) -> [u8; N] { fn read_array<const N: usize>(&mut self) -> [u8; N] {
self.read_raw_bytes(N).try_into().unwrap() self.read_raw_bytes(N).try_into().unwrap()
} }
// The trait method doesn't have a lifetime parameter, and we need a version of this
// that definitely returns a slice based on the underlying storage as opposed to
// the Decoder itself in order to implement read_str efficiently.
#[inline]
fn read_raw_bytes_inherent(&mut self, bytes: usize) -> &'a [u8] {
if bytes > self.remaining() {
Self::decoder_exhausted();
}
// SAFETY: We just checked if this range is in-bounds above.
unsafe {
let slice = std::slice::from_raw_parts(self.current, bytes);
self.current = self.current.add(bytes);
slice
}
}
/// While we could manually expose manipulation of the decoder position, /// While we could manually expose manipulation of the decoder position,
/// all current users of that method would need to reset the position later, /// all current users of that method would need to reset the position later,
/// incurring the bounds check of set_position twice. /// incurring the bounds check of set_position twice.
@ -653,7 +573,15 @@ impl<'a> Decoder for MemDecoder<'a> {
#[inline] #[inline]
fn read_u8(&mut self) -> u8 { fn read_u8(&mut self) -> u8 {
self.read_byte() if self.current == self.end {
Self::decoder_exhausted();
}
// SAFETY: This type guarantees current <= end, and we just checked current == end.
unsafe {
let byte = *self.current;
self.current = self.current.add(1);
byte
}
} }
#[inline] #[inline]
@ -681,39 +609,22 @@ impl<'a> Decoder for MemDecoder<'a> {
i16::from_le_bytes(self.read_array()) i16::from_le_bytes(self.read_array())
} }
#[inline]
fn read_i8(&mut self) -> i8 {
self.read_byte() as i8
}
#[inline] #[inline]
fn read_isize(&mut self) -> isize { fn read_isize(&mut self) -> isize {
read_leb128!(self, read_isize_leb128) read_leb128!(self, read_isize_leb128)
} }
#[inline] #[inline]
fn read_bool(&mut self) -> bool { fn read_raw_bytes(&mut self, bytes: usize) -> &'a [u8] {
let value = self.read_u8(); if bytes > self.remaining() {
value != 0 Self::decoder_exhausted();
} }
// SAFETY: We just checked if this range is in-bounds above.
#[inline] unsafe {
fn read_char(&mut self) -> char { let slice = std::slice::from_raw_parts(self.current, bytes);
let bits = self.read_u32(); self.current = self.current.add(bytes);
std::char::from_u32(bits).unwrap() slice
} }
#[inline]
fn read_str(&mut self) -> &str {
let len = self.read_usize();
let bytes = self.read_raw_bytes_inherent(len + 1);
assert!(bytes[len] == STR_SENTINEL);
unsafe { std::str::from_utf8_unchecked(&bytes[..len]) }
}
#[inline]
fn read_raw_bytes(&mut self, bytes: usize) -> &[u8] {
self.read_raw_bytes_inherent(bytes)
} }
#[inline] #[inline]
@ -787,12 +698,7 @@ impl Encodable<FileEncoder> for IntEncodedWithFixedSize {
impl<'a> Decodable<MemDecoder<'a>> for IntEncodedWithFixedSize { impl<'a> Decodable<MemDecoder<'a>> for IntEncodedWithFixedSize {
#[inline] #[inline]
fn decode(decoder: &mut MemDecoder<'a>) -> IntEncodedWithFixedSize { fn decode(decoder: &mut MemDecoder<'a>) -> IntEncodedWithFixedSize {
let _start_pos = decoder.position(); let bytes = decoder.read_array::<{ IntEncodedWithFixedSize::ENCODED_SIZE }>();
let bytes = decoder.read_raw_bytes(IntEncodedWithFixedSize::ENCODED_SIZE); IntEncodedWithFixedSize(u64::from_le_bytes(bytes))
let value = u64::from_le_bytes(bytes.try_into().unwrap());
let _end_pos = decoder.position();
debug_assert_eq!((_end_pos - _start_pos), IntEncodedWithFixedSize::ENCODED_SIZE);
IntEncodedWithFixedSize(value)
} }
} }

View file

@ -12,6 +12,13 @@ use std::path;
use std::rc::Rc; use std::rc::Rc;
use std::sync::Arc; use std::sync::Arc;
/// A byte that [cannot occur in UTF8 sequences][utf8]. Used to mark the end of a string.
/// This way we can skip validation and still be relatively sure that deserialization
/// did not desynchronize.
///
/// [utf8]: https://en.wikipedia.org/w/index.php?title=UTF-8&oldid=1058865525#Codepage_layout
const STR_SENTINEL: u8 = 0xC1;
/// A note about error handling. /// A note about error handling.
/// ///
/// Encoders may be fallible, but in practice failure is rare and there are so /// Encoders may be fallible, but in practice failure is rare and there are so
@ -40,10 +47,29 @@ pub trait Encoder {
fn emit_i64(&mut self, v: i64); fn emit_i64(&mut self, v: i64);
fn emit_i32(&mut self, v: i32); fn emit_i32(&mut self, v: i32);
fn emit_i16(&mut self, v: i16); fn emit_i16(&mut self, v: i16);
fn emit_i8(&mut self, v: i8);
fn emit_bool(&mut self, v: bool); #[inline]
fn emit_char(&mut self, v: char); fn emit_i8(&mut self, v: i8) {
fn emit_str(&mut self, v: &str); self.emit_u8(v as u8);
}
#[inline]
fn emit_bool(&mut self, v: bool) {
self.emit_u8(if v { 1 } else { 0 });
}
#[inline]
fn emit_char(&mut self, v: char) {
self.emit_u32(v as u32);
}
#[inline]
fn emit_str(&mut self, v: &str) {
self.emit_usize(v.len());
self.emit_raw_bytes(v.as_bytes());
self.emit_u8(STR_SENTINEL);
}
fn emit_raw_bytes(&mut self, s: &[u8]); fn emit_raw_bytes(&mut self, s: &[u8]);
fn emit_enum_variant<F>(&mut self, v_id: usize, f: F) fn emit_enum_variant<F>(&mut self, v_id: usize, f: F)
@ -79,11 +105,38 @@ pub trait Decoder {
fn read_i64(&mut self) -> i64; fn read_i64(&mut self) -> i64;
fn read_i32(&mut self) -> i32; fn read_i32(&mut self) -> i32;
fn read_i16(&mut self) -> i16; fn read_i16(&mut self) -> i16;
fn read_i8(&mut self) -> i8;
fn read_bool(&mut self) -> bool; #[inline]
fn read_char(&mut self) -> char; fn read_i8(&mut self) -> i8 {
fn read_str(&mut self) -> &str; self.read_u8() as i8
}
#[inline]
fn read_bool(&mut self) -> bool {
let value = self.read_u8();
value != 0
}
#[inline]
fn read_char(&mut self) -> char {
let bits = self.read_u32();
std::char::from_u32(bits).unwrap()
}
#[inline]
fn read_str(&mut self) -> &str {
let len = self.read_usize();
let bytes = self.read_raw_bytes(len + 1);
assert!(bytes[len] == STR_SENTINEL);
unsafe { std::str::from_utf8_unchecked(&bytes[..len]) }
}
fn read_raw_bytes(&mut self, len: usize) -> &[u8]; fn read_raw_bytes(&mut self, len: usize) -> &[u8];
// Although there is an `emit_enum_variant` method in `Encoder`, the code
// patterns in decoding are different enough to encoding that there is no
// need for a corresponding `read_enum_variant` method here.
fn peek_byte(&self) -> u8; fn peek_byte(&self) -> u8;
fn position(&self) -> usize; fn position(&self) -> usize;
} }