Skip to content

Commit 0b1669d

Browse files
committed
Auto merge of #57714 - matthewjasper:wellformed-unreachable, r=pnkfelix
[NLL] Clean up handling of type annotations * Renames (Canonical)?UserTypeAnnotation -> (Canonical)?UserType so that the name CanonicalUserTypeAnnotation is free. * Keep the inferred type associated to user type annotations in the MIR, so that it can be compared against the annotated type, even when the annotated expression gets removed from the MIR. (#54943) * Use the inferred type to allow infallible handling of user type projections (#57531) * Uses revisions for the tests in #56993 * Check the types of `Unevaluated` constants with no annotations (#46702) * Some drive-by cleanup Closes #46702 Closes #54943 Closes #57531 Closes #57731 cc #56993 leaving this open to track the underlying issue: we are not running tests with full NLL enabled on CI at the moment r? @nikomatsakis
2 parents 7187db6 + 620a03f commit 0b1669d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+849
-291
lines changed

src/librustc/ich/impls_ty.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -1240,16 +1240,22 @@ impl_stable_hash_for!(
12401240
}
12411241
);
12421242

1243-
impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for ty::UserTypeAnnotation<'gcx> {
1243+
impl_stable_hash_for!(
1244+
struct ty::CanonicalUserTypeAnnotation<'tcx> {
1245+
user_ty, span, inferred_ty
1246+
}
1247+
);
1248+
1249+
impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for ty::UserType<'gcx> {
12441250
fn hash_stable<W: StableHasherResult>(&self,
12451251
hcx: &mut StableHashingContext<'a>,
12461252
hasher: &mut StableHasher<W>) {
12471253
mem::discriminant(self).hash_stable(hcx, hasher);
12481254
match *self {
1249-
ty::UserTypeAnnotation::Ty(ref ty) => {
1255+
ty::UserType::Ty(ref ty) => {
12501256
ty.hash_stable(hcx, hasher);
12511257
}
1252-
ty::UserTypeAnnotation::TypeOf(ref def_id, ref substs) => {
1258+
ty::UserType::TypeOf(ref def_id, ref substs) => {
12531259
def_id.hash_stable(hcx, hasher);
12541260
substs.hash_stable(hcx, hasher);
12551261
}

src/librustc/mir/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use ty::subst::{Subst, Substs};
3131
use ty::layout::VariantIdx;
3232
use ty::{
3333
self, AdtDef, CanonicalUserTypeAnnotations, ClosureSubsts, GeneratorSubsts, Region, Ty, TyCtxt,
34-
UserTypeAnnotationIndex, UserTypeAnnotation,
34+
UserTypeAnnotationIndex,
3535
};
3636
use util::ppaux;
3737

src/librustc/mir/tcx.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,20 @@ impl<'a, 'gcx, 'tcx> PlaceTy<'tcx> {
7575
elem: &PlaceElem<'tcx>)
7676
-> PlaceTy<'tcx>
7777
{
78-
self.projection_ty_core(tcx, elem, |_, _, ty| -> Result<Ty<'tcx>, ()> { Ok(ty) })
79-
.unwrap()
78+
self.projection_ty_core(tcx, elem, |_, _, ty| ty)
8079
}
8180

8281
/// `place_ty.projection_ty_core(tcx, elem, |...| { ... })`
8382
/// projects `place_ty` onto `elem`, returning the appropriate
8483
/// `Ty` or downcast variant corresponding to that projection.
8584
/// The `handle_field` callback must map a `Field` to its `Ty`,
8685
/// (which should be trivial when `T` = `Ty`).
87-
pub fn projection_ty_core<V, T, E>(
86+
pub fn projection_ty_core<V, T>(
8887
self,
8988
tcx: TyCtxt<'a, 'gcx, 'tcx>,
9089
elem: &ProjectionElem<'tcx, V, T>,
91-
mut handle_field: impl FnMut(&Self, &Field, &T) -> Result<Ty<'tcx>, E>)
92-
-> Result<PlaceTy<'tcx>, E>
90+
mut handle_field: impl FnMut(&Self, &Field, &T) -> Ty<'tcx>)
91+
-> PlaceTy<'tcx>
9392
where
9493
V: ::std::fmt::Debug, T: ::std::fmt::Debug
9594
{
@@ -140,10 +139,10 @@ impl<'a, 'gcx, 'tcx> PlaceTy<'tcx> {
140139
}
141140
},
142141
ProjectionElem::Field(ref f, ref fty) =>
143-
PlaceTy::Ty { ty: handle_field(&self, f, fty)? },
142+
PlaceTy::Ty { ty: handle_field(&self, f, fty) },
144143
};
145144
debug!("projection_ty self: {:?} elem: {:?} yields: {:?}", self, elem, answer);
146-
Ok(answer)
145+
answer
147146
}
148147
}
149148

src/librustc/mir/visit.rs

+14-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use hir::def_id::DefId;
2-
use infer::canonical::Canonical;
32
use ty::subst::Substs;
4-
use ty::{ClosureSubsts, GeneratorSubsts, Region, Ty};
3+
use ty::{CanonicalUserTypeAnnotation, ClosureSubsts, GeneratorSubsts, Region, Ty};
54
use mir::*;
65
use syntax_pos::Span;
76

@@ -221,7 +220,7 @@ macro_rules! make_mir_visitor {
221220
fn visit_user_type_annotation(
222221
&mut self,
223222
index: UserTypeAnnotationIndex,
224-
ty: & $($mutability)* Canonical<'tcx, UserTypeAnnotation<'tcx>>,
223+
ty: & $($mutability)* CanonicalUserTypeAnnotation<'tcx>,
225224
) {
226225
self.super_user_type_annotation(index, ty);
227226
}
@@ -309,12 +308,15 @@ macro_rules! make_mir_visitor {
309308
self.visit_local_decl(local, & $($mutability)* mir.local_decls[local]);
310309
}
311310

312-
for index in mir.user_type_annotations.indices() {
313-
let (span, annotation) = & $($mutability)* mir.user_type_annotations[index];
311+
macro_rules! type_annotations {
312+
(mut) => (mir.user_type_annotations.iter_enumerated_mut());
313+
() => (mir.user_type_annotations.iter_enumerated());
314+
};
315+
316+
for (index, annotation) in type_annotations!($($mutability)*) {
314317
self.visit_user_type_annotation(
315318
index, annotation
316319
);
317-
self.visit_span(span);
318320
}
319321

320322
self.visit_span(&$($mutability)* mir.span);
@@ -882,8 +884,10 @@ macro_rules! make_mir_visitor {
882884
fn super_user_type_annotation(
883885
&mut self,
884886
_index: UserTypeAnnotationIndex,
885-
_ty: & $($mutability)* Canonical<'tcx, UserTypeAnnotation<'tcx>>,
887+
ty: & $($mutability)* CanonicalUserTypeAnnotation<'tcx>,
886888
) {
889+
self.visit_span(& $($mutability)* ty.span);
890+
self.visit_ty(& $($mutability)* ty.inferred_ty, TyContext::UserTy(ty.span));
887891
}
888892

889893
fn super_ty(&mut self, _ty: & $($mutability)* Ty<'tcx>) {
@@ -964,6 +968,9 @@ pub enum TyContext {
964968
source_info: SourceInfo,
965969
},
966970

971+
/// The inferred type of a user type annotation.
972+
UserTy(Span),
973+
967974
/// The return type of the function.
968975
ReturnTy(SourceInfo),
969976

src/librustc/traits/query/type_op/ascribe_user_type.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
11
use infer::canonical::{Canonical, Canonicalized, CanonicalizedQueryResponse, QueryResponse};
22
use traits::query::Fallible;
33
use hir::def_id::DefId;
4-
use mir::ProjectionKind;
5-
use ty::{self, ParamEnvAnd, Ty, TyCtxt};
4+
use ty::{ParamEnvAnd, Ty, TyCtxt};
65
use ty::subst::UserSubsts;
76

87
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
98
pub struct AscribeUserType<'tcx> {
109
pub mir_ty: Ty<'tcx>,
11-
pub variance: ty::Variance,
1210
pub def_id: DefId,
1311
pub user_substs: UserSubsts<'tcx>,
14-
pub projs: &'tcx ty::List<ProjectionKind<'tcx>>,
1512
}
1613

1714
impl<'tcx> AscribeUserType<'tcx> {
1815
pub fn new(
1916
mir_ty: Ty<'tcx>,
20-
variance: ty::Variance,
2117
def_id: DefId,
2218
user_substs: UserSubsts<'tcx>,
23-
projs: &'tcx ty::List<ProjectionKind<'tcx>>,
2419
) -> Self {
25-
Self { mir_ty, variance, def_id, user_substs, projs }
20+
Self { mir_ty, def_id, user_substs }
2621
}
2722
}
2823

@@ -52,19 +47,19 @@ impl<'gcx: 'tcx, 'tcx> super::QueryTypeOp<'gcx, 'tcx> for AscribeUserType<'tcx>
5247

5348
BraceStructTypeFoldableImpl! {
5449
impl<'tcx> TypeFoldable<'tcx> for AscribeUserType<'tcx> {
55-
mir_ty, variance, def_id, user_substs, projs
50+
mir_ty, def_id, user_substs
5651
}
5752
}
5853

5954
BraceStructLiftImpl! {
6055
impl<'a, 'tcx> Lift<'tcx> for AscribeUserType<'a> {
6156
type Lifted = AscribeUserType<'tcx>;
62-
mir_ty, variance, def_id, user_substs, projs
57+
mir_ty, def_id, user_substs
6358
}
6459
}
6560

6661
impl_stable_hash_for! {
6762
struct AscribeUserType<'tcx> {
68-
mir_ty, variance, def_id, user_substs, projs
63+
mir_ty, def_id, user_substs
6964
}
7065
}

src/librustc/ty/context.rs

+38-17
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ pub struct TypeckTables<'tcx> {
350350
/// canonical substitutions would include only `for<X> { Vec<X> }`.
351351
///
352352
/// See also `AscribeUserType` statement in MIR.
353-
user_provided_types: ItemLocalMap<CanonicalUserTypeAnnotation<'tcx>>,
353+
user_provided_types: ItemLocalMap<CanonicalUserType<'tcx>>,
354354

355355
/// Stores the canonicalized types provided by the user. See also
356356
/// `AscribeUserType` statement in MIR.
@@ -493,7 +493,7 @@ impl<'tcx> TypeckTables<'tcx> {
493493

494494
pub fn user_provided_types(
495495
&self
496-
) -> LocalTableInContext<'_, CanonicalUserTypeAnnotation<'tcx>> {
496+
) -> LocalTableInContext<'_, CanonicalUserType<'tcx>> {
497497
LocalTableInContext {
498498
local_id_root: self.local_id_root,
499499
data: &self.user_provided_types
@@ -502,7 +502,7 @@ impl<'tcx> TypeckTables<'tcx> {
502502

503503
pub fn user_provided_types_mut(
504504
&mut self
505-
) -> LocalTableInContextMut<'_, CanonicalUserTypeAnnotation<'tcx>> {
505+
) -> LocalTableInContextMut<'_, CanonicalUserType<'tcx>> {
506506
LocalTableInContextMut {
507507
local_id_root: self.local_id_root,
508508
data: &mut self.user_provided_types
@@ -800,25 +800,46 @@ impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for TypeckTables<'gcx> {
800800

801801
newtype_index! {
802802
pub struct UserTypeAnnotationIndex {
803-
DEBUG_FORMAT = "UserTypeAnnotation({})",
803+
DEBUG_FORMAT = "UserType({})",
804804
const START_INDEX = 0,
805805
}
806806
}
807807

808808
/// Mapping of type annotation indices to canonical user type annotations.
809809
pub type CanonicalUserTypeAnnotations<'tcx> =
810-
IndexVec<UserTypeAnnotationIndex, (Span, CanonicalUserTypeAnnotation<'tcx>)>;
810+
IndexVec<UserTypeAnnotationIndex, CanonicalUserTypeAnnotation<'tcx>>;
811+
812+
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
813+
pub struct CanonicalUserTypeAnnotation<'tcx> {
814+
pub user_ty: CanonicalUserType<'tcx>,
815+
pub span: Span,
816+
pub inferred_ty: Ty<'tcx>,
817+
}
818+
819+
BraceStructTypeFoldableImpl! {
820+
impl<'tcx> TypeFoldable<'tcx> for CanonicalUserTypeAnnotation<'tcx> {
821+
user_ty, span, inferred_ty
822+
}
823+
}
824+
825+
BraceStructLiftImpl! {
826+
impl<'a, 'tcx> Lift<'tcx> for CanonicalUserTypeAnnotation<'a> {
827+
type Lifted = CanonicalUserTypeAnnotation<'tcx>;
828+
user_ty, span, inferred_ty
829+
}
830+
}
831+
811832

812833
/// Canonicalized user type annotation.
813-
pub type CanonicalUserTypeAnnotation<'gcx> = Canonical<'gcx, UserTypeAnnotation<'gcx>>;
834+
pub type CanonicalUserType<'gcx> = Canonical<'gcx, UserType<'gcx>>;
814835

815-
impl CanonicalUserTypeAnnotation<'gcx> {
836+
impl CanonicalUserType<'gcx> {
816837
/// Returns `true` if this represents a substitution of the form `[?0, ?1, ?2]`,
817838
/// i.e. each thing is mapped to a canonical variable with the same index.
818839
pub fn is_identity(&self) -> bool {
819840
match self.value {
820-
UserTypeAnnotation::Ty(_) => false,
821-
UserTypeAnnotation::TypeOf(_, user_substs) => {
841+
UserType::Ty(_) => false,
842+
UserType::TypeOf(_, user_substs) => {
822843
if user_substs.user_self_ty.is_some() {
823844
return false;
824845
}
@@ -853,7 +874,7 @@ impl CanonicalUserTypeAnnotation<'gcx> {
853874
/// from constants that are named via paths, like `Foo::<A>::new` and
854875
/// so forth.
855876
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
856-
pub enum UserTypeAnnotation<'tcx> {
877+
pub enum UserType<'tcx> {
857878
Ty(Ty<'tcx>),
858879

859880
/// The canonical type is the result of `type_of(def_id)` with the
@@ -862,17 +883,17 @@ pub enum UserTypeAnnotation<'tcx> {
862883
}
863884

864885
EnumTypeFoldableImpl! {
865-
impl<'tcx> TypeFoldable<'tcx> for UserTypeAnnotation<'tcx> {
866-
(UserTypeAnnotation::Ty)(ty),
867-
(UserTypeAnnotation::TypeOf)(def, substs),
886+
impl<'tcx> TypeFoldable<'tcx> for UserType<'tcx> {
887+
(UserType::Ty)(ty),
888+
(UserType::TypeOf)(def, substs),
868889
}
869890
}
870891

871892
EnumLiftImpl! {
872-
impl<'a, 'tcx> Lift<'tcx> for UserTypeAnnotation<'a> {
873-
type Lifted = UserTypeAnnotation<'tcx>;
874-
(UserTypeAnnotation::Ty)(ty),
875-
(UserTypeAnnotation::TypeOf)(def, substs),
893+
impl<'a, 'tcx> Lift<'tcx> for UserType<'a> {
894+
type Lifted = UserType<'tcx>;
895+
(UserType::Ty)(ty),
896+
(UserType::TypeOf)(def, substs),
876897
}
877898
}
878899

src/librustc/ty/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ pub use self::binding::BindingMode::*;
7373
pub use self::context::{TyCtxt, FreeRegionInfo, GlobalArenas, AllArenas, tls, keep_local};
7474
pub use self::context::{Lift, TypeckTables, CtxtInterners};
7575
pub use self::context::{
76-
UserTypeAnnotationIndex, UserTypeAnnotation, CanonicalUserTypeAnnotation,
77-
CanonicalUserTypeAnnotations,
76+
UserTypeAnnotationIndex, UserType, CanonicalUserType,
77+
CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations,
7878
};
7979

8080
pub use self::instance::{Instance, InstanceDef};

src/librustc_mir/borrow_check/nll/constraint_generation.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc::infer::InferCtxt;
77
use rustc::mir::visit::TyContext;
88
use rustc::mir::visit::Visitor;
99
use rustc::mir::{BasicBlock, BasicBlockData, Location, Mir, Place, Rvalue};
10-
use rustc::mir::{Statement, Terminator};
10+
use rustc::mir::{SourceInfo, Statement, Terminator};
1111
use rustc::mir::UserTypeProjection;
1212
use rustc::ty::fold::TypeFoldable;
1313
use rustc::ty::subst::Substs;
@@ -66,11 +66,12 @@ impl<'cg, 'cx, 'gcx, 'tcx> Visitor<'tcx> for ConstraintGeneration<'cg, 'cx, 'gcx
6666
/// call. Make them live at the location where they appear.
6767
fn visit_ty(&mut self, ty: &ty::Ty<'tcx>, ty_context: TyContext) {
6868
match ty_context {
69-
TyContext::ReturnTy(source_info)
70-
| TyContext::YieldTy(source_info)
71-
| TyContext::LocalDecl { source_info, .. } => {
69+
TyContext::ReturnTy(SourceInfo { span, .. })
70+
| TyContext::YieldTy(SourceInfo { span, .. })
71+
| TyContext::UserTy(span)
72+
| TyContext::LocalDecl { source_info: SourceInfo { span, .. }, .. } => {
7273
span_bug!(
73-
source_info.span,
74+
span,
7475
"should not be visiting outside of the CFG: {:?}",
7576
ty_context
7677
);

src/librustc_mir/borrow_check/nll/renumber.rs

+1-17
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
use rustc::infer::canonical::Canonical;
21
use rustc::ty::subst::Substs;
3-
use rustc::ty::{
4-
self, ClosureSubsts, GeneratorSubsts, Ty, TypeFoldable, UserTypeAnnotation,
5-
UserTypeAnnotationIndex,
6-
};
2+
use rustc::ty::{self, ClosureSubsts, GeneratorSubsts, Ty, TypeFoldable};
73
use rustc::mir::{Location, Mir};
84
use rustc::mir::visit::{MutVisitor, TyContext};
95
use rustc::infer::{InferCtxt, NLLRegionVariableOrigin};
@@ -59,18 +55,6 @@ impl<'a, 'gcx, 'tcx> MutVisitor<'tcx> for NLLVisitor<'a, 'gcx, 'tcx> {
5955
debug!("visit_ty: ty={:?}", ty);
6056
}
6157

62-
fn visit_user_type_annotation(
63-
&mut self,
64-
_index: UserTypeAnnotationIndex,
65-
_ty: &mut Canonical<'tcx, UserTypeAnnotation<'tcx>>,
66-
) {
67-
// User type annotations represent the types that the user
68-
// wrote in the progarm. We don't want to erase the regions
69-
// from these types: rather, we want to add them as
70-
// constraints at type-check time.
71-
debug!("visit_user_type_annotation: skipping renumber");
72-
}
73-
7458
fn visit_substs(&mut self, substs: &mut &'tcx Substs<'tcx>, location: Location) {
7559
debug!("visit_substs(substs={:?}, location={:?})", substs, location);
7660

0 commit comments

Comments
 (0)