]> git.lizzy.rs Git - rust.git/commitdiff
Rollup merge of #74399 - mark-i-m:ty-err-4, r=eddyb
authorTyler Mandry <tmandry@gmail.com>
Sun, 16 Aug 2020 21:59:25 +0000 (14:59 -0700)
committerGitHub <noreply@github.com>
Sun, 16 Aug 2020 21:59:25 +0000 (14:59 -0700)
Move DelaySpanBugEmitted to ty::context

This makes it even hard to abuse.

r? @eddyb

cc @LeSeulArtichaut as this will probably conflict with your PR :/

1  2 
src/librustc_middle/ty/context.rs
src/librustc_middle/ty/mod.rs
src/librustc_middle/ty/sty.rs

index 784815f27201f29ad3f0b66dd125023819429553,206f6a270e4c36555391310ed3f38ebb0b007d6b..d7eeaafbf46bfb467ce4aad3f8992b897ab15265
@@@ -13,14 -13,13 +13,14 @@@ use crate::middle::stability
  use crate::mir::interpret::{self, Allocation, ConstValue, Scalar};
  use crate::mir::{Body, Field, Local, Place, PlaceElem, ProjectionKind, Promoted};
  use crate::traits;
 +use crate::ty::query::{self, TyCtxtAt};
  use crate::ty::steal::Steal;
  use crate::ty::subst::{GenericArg, GenericArgKind, InternalSubsts, Subst, SubstsRef, UserSubsts};
  use crate::ty::TyKind::*;
  use crate::ty::{
 -    self, query, AdtDef, AdtKind, BindingMode, BoundVar, CanonicalPolyFnSig, Const, ConstVid,
 -    DefIdTree, ExistentialPredicate, FloatVar, FloatVid, GenericParamDefKind, InferConst, InferTy,
 -    IntVar, IntVid, List, ParamConst, ParamTy, PolyFnSig, Predicate, PredicateInner, PredicateKind,
 +    self, AdtDef, AdtKind, BindingMode, BoundVar, CanonicalPolyFnSig, Const, ConstVid, DefIdTree,
 +    ExistentialPredicate, FloatVar, FloatVid, GenericParamDefKind, InferConst, InferTy, IntVar,
 +    IntVid, List, ParamConst, ParamTy, PolyFnSig, Predicate, PredicateInner, PredicateKind,
      ProjectionTy, Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind, TyS, TyVar,
      TyVid, TypeAndMut,
  };
@@@ -65,6 -64,12 +65,12 @@@ use std::mem
  use std::ops::{Bound, Deref};
  use std::sync::Arc;
  
+ /// A type that is not publicly constructable. This prevents people from making `TyKind::Error`
+ /// except through `tcx.err*()`, which are in this module.
+ #[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
+ #[derive(TyEncodable, TyDecodable, HashStable)]
+ pub struct DelaySpanBugEmitted(());
  type InternedSet<'tcx, T> = ShardedHashMap<Interned<'tcx, T>, ()>;
  
  pub struct CtxtInterners<'tcx> {
@@@ -1171,7 -1176,7 +1177,7 @@@ impl<'tcx> TyCtxt<'tcx> 
      #[track_caller]
      pub fn ty_error_with_message<S: Into<MultiSpan>>(self, span: S, msg: &str) -> Ty<'tcx> {
          self.sess.delay_span_bug(span, msg);
-         self.mk_ty(Error(super::sty::DelaySpanBugEmitted(())))
+         self.mk_ty(Error(DelaySpanBugEmitted(())))
      }
  
      /// Like `err` but for constants.
      pub fn const_error(self, ty: Ty<'tcx>) -> &'tcx Const<'tcx> {
          self.sess
              .delay_span_bug(DUMMY_SP, "ty::ConstKind::Error constructed but no error reported.");
-         self.mk_const(ty::Const {
-             val: ty::ConstKind::Error(super::sty::DelaySpanBugEmitted(())),
-             ty,
-         })
+         self.mk_const(ty::Const { val: ty::ConstKind::Error(DelaySpanBugEmitted(())), ty })
      }
  
      pub fn consider_optimizing<T: Fn() -> String>(&self, msg: T) -> bool {
@@@ -2616,21 -2618,6 +2619,21 @@@ impl<'tcx> TyCtxt<'tcx> 
      }
  }
  
 +impl TyCtxtAt<'tcx> {
 +    /// Constructs a `TyKind::Error` type and registers a `delay_span_bug` to ensure it gets used.
 +    #[track_caller]
 +    pub fn ty_error(self) -> Ty<'tcx> {
 +        self.tcx.ty_error_with_message(self.span, "TyKind::Error constructed but no error reported")
 +    }
 +
 +    /// Constructs a `TyKind::Error` type and registers a `delay_span_bug` with the given `msg to
 +    /// ensure it gets used.
 +    #[track_caller]
 +    pub fn ty_error_with_message(self, msg: &str) -> Ty<'tcx> {
 +        self.tcx.ty_error_with_message(self.span, msg)
 +    }
 +}
 +
  pub trait InternAs<T: ?Sized, R> {
      type Output;
      fn intern_with<F>(self, f: F) -> Self::Output
index dacf242118122c2f835defd9c7ce125b24c88c29,142978c1687fef6bb0ab4c904ace6d4d3366ce81..3c79fe12255799fc581a5ebdfb78ca9eb3482aab
@@@ -60,7 -60,6 +60,7 @@@ pub use self::sty::{Binder, BoundTy, Bo
  pub use self::sty::{BoundRegion, EarlyBoundRegion, FreeRegion, Region};
  pub use self::sty::{CanonicalPolyFnSig, FnSig, GenSig, PolyFnSig, PolyGenSig};
  pub use self::sty::{ClosureSubsts, GeneratorSubsts, TypeAndMut, UpvarSubsts};
 +pub use self::sty::{ClosureSubstsParts, GeneratorSubstsParts};
  pub use self::sty::{ConstVid, FloatVid, IntVid, RegionVid, TyVid};
  pub use self::sty::{ExistentialPredicate, InferTy, ParamConst, ParamTy, ProjectionTy};
  pub use self::sty::{ExistentialProjection, PolyExistentialProjection};
@@@ -73,8 -72,8 +73,8 @@@ pub use self::binding::BindingMode::*
  
  pub use self::context::{tls, FreeRegionInfo, TyCtxt};
  pub use self::context::{
-     CanonicalUserType, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations, ResolvedOpaqueTy,
-     UserType, UserTypeAnnotationIndex,
+     CanonicalUserType, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations,
+     DelaySpanBugEmitted, ResolvedOpaqueTy, UserType, UserTypeAnnotationIndex,
  };
  pub use self::context::{
      CtxtInterners, GeneratorInteriorTypeCause, GlobalCtxt, Lift, TypeckResults,
index 71da5506315af66a126abc01d510350cd5e1f4e5,1bcdf2dc0421f0f51afb62360f7aec9373995d89..82160681ee8cf374f40d2abcb6e211a645cc4fa4
@@@ -10,7 -10,7 +10,7 @@@ use crate::ty::subst::{GenericArg, Inte
  use crate::ty::{
      self, AdtDef, DefIdTree, Discr, Ty, TyCtxt, TypeFlags, TypeFoldable, WithConstness,
  };
- use crate::ty::{List, ParamEnv, TyS};
+ use crate::ty::{DelaySpanBugEmitted, List, ParamEnv, TyS};
  use polonius_engine::Atom;
  use rustc_ast::ast;
  use rustc_data_structures::captures::Captures;
@@@ -212,12 -212,6 +212,6 @@@ impl TyKind<'tcx> 
      }
  }
  
- /// A type that is not publicly constructable. This prevents people from making `TyKind::Error`
- /// except through `tcx.err*()`.
- #[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
- #[derive(TyEncodable, TyDecodable, HashStable)]
- pub struct DelaySpanBugEmitted(pub(super) ());
  // `TyKind` is used a lot. Make sure it doesn't unintentionally get bigger.
  #[cfg(target_arch = "x86_64")]
  static_assert_size!(TyKind<'_>, 24);
@@@ -325,39 -319,24 +319,39 @@@ pub struct ClosureSubsts<'tcx> 
      pub substs: SubstsRef<'tcx>,
  }
  
 -/// Struct returned by `split()`. Note that these are subslices of the
 -/// parent slice and not canonical substs themselves.
 -struct SplitClosureSubsts<'tcx> {
 -    parent: &'tcx [GenericArg<'tcx>],
 -    closure_kind_ty: GenericArg<'tcx>,
 -    closure_sig_as_fn_ptr_ty: GenericArg<'tcx>,
 -    tupled_upvars_ty: GenericArg<'tcx>,
 +/// Struct returned by `split()`.
 +pub struct ClosureSubstsParts<'tcx, T> {
 +    pub parent_substs: &'tcx [GenericArg<'tcx>],
 +    pub closure_kind_ty: T,
 +    pub closure_sig_as_fn_ptr_ty: T,
 +    pub tupled_upvars_ty: T,
  }
  
  impl<'tcx> ClosureSubsts<'tcx> {
 -    /// Divides the closure substs into their respective
 -    /// components. Single source of truth with respect to the
 -    /// ordering.
 -    fn split(self) -> SplitClosureSubsts<'tcx> {
 +    /// Construct `ClosureSubsts` from `ClosureSubstsParts`, containing `Substs`
 +    /// for the closure parent, alongside additional closure-specific components.
 +    pub fn new(
 +        tcx: TyCtxt<'tcx>,
 +        parts: ClosureSubstsParts<'tcx, Ty<'tcx>>,
 +    ) -> ClosureSubsts<'tcx> {
 +        ClosureSubsts {
 +            substs: tcx.mk_substs(
 +                parts.parent_substs.iter().copied().chain(
 +                    [parts.closure_kind_ty, parts.closure_sig_as_fn_ptr_ty, parts.tupled_upvars_ty]
 +                        .iter()
 +                        .map(|&ty| ty.into()),
 +                ),
 +            ),
 +        }
 +    }
 +
 +    /// Divides the closure substs into their respective components.
 +    /// The ordering assumed here must match that used by `ClosureSubsts::new` above.
 +    fn split(self) -> ClosureSubstsParts<'tcx, GenericArg<'tcx>> {
          match self.substs[..] {
 -            [ref parent @ .., closure_kind_ty, closure_sig_as_fn_ptr_ty, tupled_upvars_ty] => {
 -                SplitClosureSubsts {
 -                    parent,
 +            [ref parent_substs @ .., closure_kind_ty, closure_sig_as_fn_ptr_ty, tupled_upvars_ty] => {
 +                ClosureSubstsParts {
 +                    parent_substs,
                      closure_kind_ty,
                      closure_sig_as_fn_ptr_ty,
                      tupled_upvars_ty,
  
      /// Returns the substitutions of the closure's parent.
      pub fn parent_substs(self) -> &'tcx [GenericArg<'tcx>] {
 -        self.split().parent
 +        self.split().parent_substs
      }
  
      #[inline]
@@@ -433,46 -412,21 +427,46 @@@ pub struct GeneratorSubsts<'tcx> 
      pub substs: SubstsRef<'tcx>,
  }
  
 -struct SplitGeneratorSubsts<'tcx> {
 -    parent: &'tcx [GenericArg<'tcx>],
 -    resume_ty: GenericArg<'tcx>,
 -    yield_ty: GenericArg<'tcx>,
 -    return_ty: GenericArg<'tcx>,
 -    witness: GenericArg<'tcx>,
 -    tupled_upvars_ty: GenericArg<'tcx>,
 +pub struct GeneratorSubstsParts<'tcx, T> {
 +    pub parent_substs: &'tcx [GenericArg<'tcx>],
 +    pub resume_ty: T,
 +    pub yield_ty: T,
 +    pub return_ty: T,
 +    pub witness: T,
 +    pub tupled_upvars_ty: T,
  }
  
  impl<'tcx> GeneratorSubsts<'tcx> {
 -    fn split(self) -> SplitGeneratorSubsts<'tcx> {
 +    /// Construct `GeneratorSubsts` from `GeneratorSubstsParts`, containing `Substs`
 +    /// for the generator parent, alongside additional generator-specific components.
 +    pub fn new(
 +        tcx: TyCtxt<'tcx>,
 +        parts: GeneratorSubstsParts<'tcx, Ty<'tcx>>,
 +    ) -> GeneratorSubsts<'tcx> {
 +        GeneratorSubsts {
 +            substs: tcx.mk_substs(
 +                parts.parent_substs.iter().copied().chain(
 +                    [
 +                        parts.resume_ty,
 +                        parts.yield_ty,
 +                        parts.return_ty,
 +                        parts.witness,
 +                        parts.tupled_upvars_ty,
 +                    ]
 +                    .iter()
 +                    .map(|&ty| ty.into()),
 +                ),
 +            ),
 +        }
 +    }
 +
 +    /// Divides the generator substs into their respective components.
 +    /// The ordering assumed here must match that used by `GeneratorSubsts::new` above.
 +    fn split(self) -> GeneratorSubstsParts<'tcx, GenericArg<'tcx>> {
          match self.substs[..] {
 -            [ref parent @ .., resume_ty, yield_ty, return_ty, witness, tupled_upvars_ty] => {
 -                SplitGeneratorSubsts {
 -                    parent,
 +            [ref parent_substs @ .., resume_ty, yield_ty, return_ty, witness, tupled_upvars_ty] => {
 +                GeneratorSubstsParts {
 +                    parent_substs,
                      resume_ty,
                      yield_ty,
                      return_ty,
  
      /// Returns the substitutions of the generator's parent.
      pub fn parent_substs(self) -> &'tcx [GenericArg<'tcx>] {
 -        self.split().parent
 +        self.split().parent_substs
      }
  
      /// This describes the types that can be contained in a generator.