Skip to content

Commit 1da411e

Browse files
Split UserTypeAnnotation to have a kind
1 parent 4a204be commit 1da411e

File tree

13 files changed

+60
-36
lines changed

13 files changed

+60
-36
lines changed

compiler/rustc_borrowck/src/type_check/canonical.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
275275
user_ty: ty::UserType<'tcx>,
276276
span: Span,
277277
) {
278-
let ty::UserType::Ty(user_ty) = user_ty else { bug!() };
278+
let ty::UserTypeKind::Ty(user_ty) = user_ty.kind else { bug!() };
279279

280280
// A fast path for a common case with closure input/output types.
281281
if let ty::Infer(_) = user_ty.kind() {

compiler/rustc_borrowck/src/type_check/input_output.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
110110
) {
111111
self.ascribe_user_type_skip_wf(
112112
arg_decl.ty,
113-
ty::UserType::Ty(user_ty),
113+
ty::UserType::new(ty::UserTypeKind::Ty(user_ty)),
114114
arg_decl.source_info.span,
115115
);
116116
}
@@ -119,7 +119,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
119119
let output_decl = &body.local_decls[RETURN_PLACE];
120120
self.ascribe_user_type_skip_wf(
121121
output_decl.ty,
122-
ty::UserType::Ty(user_provided_sig.output()),
122+
ty::UserType::new(ty::UserTypeKind::Ty(user_provided_sig.output())),
123123
output_decl.source_info.span,
124124
);
125125
}

compiler/rustc_borrowck/src/type_check/mod.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use rustc_middle::ty::visit::TypeVisitableExt;
3131
use rustc_middle::ty::{
3232
self, Binder, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations, CoroutineArgsExt,
3333
Dynamic, GenericArgsRef, OpaqueHiddenType, OpaqueTypeKey, RegionVid, Ty, TyCtxt, UserArgs,
34-
UserType, UserTypeAnnotationIndex,
34+
UserTypeAnnotationIndex,
3535
};
3636
use rustc_middle::{bug, span_bug};
3737
use rustc_mir_dataflow::ResultsCursor;
@@ -370,7 +370,10 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
370370
} else {
371371
self.cx.ascribe_user_type(
372372
constant.const_.ty(),
373-
UserType::TypeOf(uv.def, UserArgs { args: uv.args, user_self_ty: None }),
373+
ty::UserType::new(ty::UserTypeKind::TypeOf(uv.def, UserArgs {
374+
args: uv.args,
375+
user_self_ty: None,
376+
})),
374377
locations.span(self.cx.body),
375378
);
376379
}
@@ -991,9 +994,10 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
991994
for user_annotation in self.user_type_annotations {
992995
let CanonicalUserTypeAnnotation { span, ref user_ty, inferred_ty } = *user_annotation;
993996
let annotation = self.instantiate_canonical(span, user_ty);
994-
if let ty::UserType::TypeOf(def, args) = annotation
997+
if let ty::UserTypeKind::TypeOf(def, args) = annotation.kind
995998
&& let DefKind::InlineConst = tcx.def_kind(def)
996999
{
1000+
// TODO:
9971001
self.check_inline_const(inferred_ty, def.expect_local(), args, span);
9981002
} else {
9991003
self.ascribe_user_type(inferred_ty, annotation, span);

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use rustc_middle::ty::fold::TypeFoldable;
2525
use rustc_middle::ty::visit::{TypeVisitable, TypeVisitableExt};
2626
use rustc_middle::ty::{
2727
self, AdtKind, CanonicalUserType, GenericArgKind, GenericArgsRef, GenericParamDefKind,
28-
IsIdentity, Ty, TyCtxt, UserArgs, UserSelfTy, UserType,
28+
IsIdentity, Ty, TyCtxt, UserArgs, UserSelfTy,
2929
};
3030
use rustc_middle::{bug, span_bug};
3131
use rustc_session::lint;
@@ -216,11 +216,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
216216
debug!("fcx {}", self.tag());
217217

218218
if Self::can_contain_user_lifetime_bounds((args, user_self_ty)) {
219-
let canonicalized =
220-
self.canonicalize_user_type_annotation(UserType::TypeOf(def_id, UserArgs {
221-
args,
222-
user_self_ty,
223-
}));
219+
let canonicalized = self.canonicalize_user_type_annotation(ty::UserType::new(
220+
ty::UserTypeKind::TypeOf(def_id, UserArgs { args, user_self_ty }),
221+
));
224222
debug!(?canonicalized);
225223
self.write_user_type_annotation(hir_id, canonicalized);
226224
}
@@ -468,7 +466,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
468466
debug!(?ty);
469467

470468
if Self::can_contain_user_lifetime_bounds(ty.raw) {
471-
let c_ty = self.canonicalize_response(UserType::Ty(ty.raw));
469+
let c_ty = self.canonicalize_response(ty::UserType::new(ty::UserTypeKind::Ty(ty.raw)));
472470
debug!(?c_ty);
473471
self.typeck_results.borrow_mut().user_provided_types_mut().insert(hir_ty.hir_id, c_ty);
474472
}

compiler/rustc_hir_typeck/src/gather_locals.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc_hir as hir;
22
use rustc_hir::intravisit::{self, Visitor};
33
use rustc_hir::{HirId, PatKind};
44
use rustc_infer::traits::ObligationCauseCode;
5-
use rustc_middle::ty::{Ty, UserType};
5+
use rustc_middle::ty::{self, Ty};
66
use rustc_span::Span;
77
use rustc_span::def_id::LocalDefId;
88
use tracing::debug;
@@ -92,7 +92,9 @@ impl<'a, 'tcx> GatherLocalsVisitor<'a, 'tcx> {
9292
Some(ref ty) => {
9393
let o_ty = self.fcx.lower_ty(ty);
9494

95-
let c_ty = self.fcx.infcx.canonicalize_user_type_annotation(UserType::Ty(o_ty.raw));
95+
let c_ty = self.fcx.infcx.canonicalize_user_type_annotation(ty::UserType::new(
96+
ty::UserTypeKind::Ty(o_ty.raw),
97+
));
9698
debug!("visit_local: ty.hir_id={:?} o_ty={:?} c_ty={:?}", ty.hir_id, o_ty, c_ty);
9799
self.fcx
98100
.typeck_results

compiler/rustc_hir_typeck/src/method/confirm.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use rustc_middle::ty::adjustment::{
1717
use rustc_middle::ty::fold::TypeFoldable;
1818
use rustc_middle::ty::{
1919
self, GenericArgs, GenericArgsRef, GenericParamDefKind, Ty, TyCtxt, TypeVisitableExt, UserArgs,
20-
UserType,
2120
};
2221
use rustc_middle::{bug, span_bug};
2322
use rustc_span::{DUMMY_SP, Span};
@@ -491,9 +490,8 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
491490
user_self_ty: None, // not relevant here
492491
};
493492

494-
self.fcx.canonicalize_user_type_annotation(UserType::TypeOf(
495-
pick.item.def_id,
496-
user_args,
493+
self.fcx.canonicalize_user_type_annotation(ty::UserType::new(
494+
ty::UserTypeKind::TypeOf(pick.item.def_id, user_args),
497495
))
498496
});
499497

compiler/rustc_hir_typeck/src/writeback.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
476476
for (local_id, c_ty) in sorted_user_provided_types {
477477
let hir_id = HirId { owner: common_hir_owner, local_id };
478478

479-
if let ty::UserType::TypeOf(_, user_args) = c_ty.value {
479+
if let ty::UserTypeKind::TypeOf(_, user_args) = c_ty.value.kind {
480480
// This is a unit-testing mechanism.
481481
let span = self.tcx().hir().span(hir_id);
482482
// We need to buffer the errors in order to guarantee a consistent

compiler/rustc_middle/src/ty/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ pub use self::sty::{
9595
pub use self::trait_def::TraitDef;
9696
pub use self::typeck_results::{
9797
CanonicalUserType, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations, IsIdentity,
98-
TypeckResults, UserType, UserTypeAnnotationIndex,
98+
TypeckResults, UserType, UserTypeAnnotationIndex, UserTypeKind,
9999
};
100100
pub use self::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor};
101101
use crate::error::{OpaqueHiddenTypeMismatch, TypeMismatchReason};

compiler/rustc_middle/src/ty/typeck_results.rs

+26-4
Original file line numberDiff line numberDiff line change
@@ -700,12 +700,24 @@ pub struct CanonicalUserTypeAnnotation<'tcx> {
700700
/// Canonical user type annotation.
701701
pub type CanonicalUserType<'tcx> = Canonical<'tcx, UserType<'tcx>>;
702702

703+
#[derive(Copy, Clone, Debug, PartialEq, TyEncodable, TyDecodable)]
704+
#[derive(Eq, Hash, HashStable, TypeFoldable, TypeVisitable)]
705+
pub struct UserType<'tcx> {
706+
pub kind: UserTypeKind<'tcx>,
707+
}
708+
709+
impl<'tcx> UserType<'tcx> {
710+
pub fn new(kind: UserTypeKind<'tcx>) -> UserType<'tcx> {
711+
UserType { kind }
712+
}
713+
}
714+
703715
/// A user-given type annotation attached to a constant. These arise
704716
/// from constants that are named via paths, like `Foo::<A>::new` and
705717
/// so forth.
706718
#[derive(Copy, Clone, Debug, PartialEq, TyEncodable, TyDecodable)]
707719
#[derive(Eq, Hash, HashStable, TypeFoldable, TypeVisitable)]
708-
pub enum UserType<'tcx> {
720+
pub enum UserTypeKind<'tcx> {
709721
Ty(Ty<'tcx>),
710722

711723
/// The canonical type is the result of `type_of(def_id)` with the
@@ -721,9 +733,11 @@ impl<'tcx> IsIdentity for CanonicalUserType<'tcx> {
721733
/// Returns `true` if this represents the generic parameters of the form `[?0, ?1, ?2]`,
722734
/// i.e., each thing is mapped to a canonical variable with the same index.
723735
fn is_identity(&self) -> bool {
724-
match self.value {
725-
UserType::Ty(_) => false,
726-
UserType::TypeOf(_, user_args) => {
736+
// TODO:
737+
738+
match self.value.kind {
739+
UserTypeKind::Ty(_) => false,
740+
UserTypeKind::TypeOf(_, user_args) => {
727741
if user_args.user_self_ty.is_some() {
728742
return false;
729743
}
@@ -764,6 +778,14 @@ impl<'tcx> IsIdentity for CanonicalUserType<'tcx> {
764778
}
765779

766780
impl<'tcx> std::fmt::Display for UserType<'tcx> {
781+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
782+
// TODO:
783+
784+
self.kind.fmt(f)
785+
}
786+
}
787+
788+
impl<'tcx> std::fmt::Display for UserTypeKind<'tcx> {
767789
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
768790
match self {
769791
Self::Ty(arg0) => {

compiler/rustc_mir_build/src/build/matches/match_pair.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,8 @@ impl<'pat, 'tcx> MatchPairTree<'pat, 'tcx> {
176176
ty: cx.infcx.next_ty_var(span),
177177
})
178178
.args;
179-
let user_ty = cx.infcx.canonicalize_user_type_annotation(ty::UserType::TypeOf(
180-
def_id,
181-
ty::UserArgs { args, user_self_ty: None },
179+
let user_ty = cx.infcx.canonicalize_user_type_annotation(ty::UserType::new(
180+
ty::UserTypeKind::TypeOf(def_id, ty::UserArgs { args, user_self_ty: None }),
182181
));
183182
let annotation = ty::CanonicalUserTypeAnnotation {
184183
inferred_ty: pattern.ty,

compiler/rustc_mir_build/src/thir/cx/expr.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use rustc_middle::ty::adjustment::{
1515
};
1616
use rustc_middle::ty::{
1717
self, AdtKind, GenericArgs, InlineConstArgs, InlineConstArgsParts, ScalarInt, Ty, UpvarArgs,
18-
UserType,
1918
};
2019
use rustc_middle::{bug, span_bug};
2120
use rustc_span::{Span, sym};
@@ -443,7 +442,9 @@ impl<'tcx> Cx<'tcx> {
443442
let user_provided_types = self.typeck_results().user_provided_types();
444443
let user_ty =
445444
user_provided_types.get(fun.hir_id).copied().map(|mut u_ty| {
446-
if let UserType::TypeOf(ref mut did, _) = &mut u_ty.value {
445+
if let ty::UserTypeKind::TypeOf(ref mut did, _) =
446+
&mut u_ty.value.kind
447+
{
447448
*did = adt_def.did();
448449
}
449450
Box::new(u_ty)

compiler/rustc_mir_build/src/thir/util.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_hir as hir;
22
use rustc_middle::bug;
3-
use rustc_middle::ty::{self, CanonicalUserType, TyCtxt, UserType};
3+
use rustc_middle::ty::{self, CanonicalUserType, TyCtxt};
44
use tracing::debug;
55

66
pub(crate) trait UserAnnotatedTyHelpers<'tcx> {
@@ -21,7 +21,7 @@ pub(crate) trait UserAnnotatedTyHelpers<'tcx> {
2121
let ty = self.typeck_results().node_type(hir_id);
2222
match ty.kind() {
2323
ty::Adt(adt_def, ..) => {
24-
if let UserType::TypeOf(ref mut did, _) = &mut user_ty.value {
24+
if let ty::UserTypeKind::TypeOf(ref mut did, _) = &mut user_ty.value.kind {
2525
*did = adt_def.did();
2626
}
2727
Some(user_ty)

compiler/rustc_trait_selection/src/traits/query/type_op/ascribe_user_type.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_infer::traits::Obligation;
33
use rustc_middle::traits::query::NoSolution;
44
pub use rustc_middle::traits::query::type_op::AscribeUserType;
55
use rustc_middle::traits::{ObligationCause, ObligationCauseCode};
6-
use rustc_middle::ty::{self, ParamEnvAnd, Ty, TyCtxt, UserArgs, UserSelfTy, UserType};
6+
use rustc_middle::ty::{self, ParamEnvAnd, Ty, TyCtxt, UserArgs, UserSelfTy, UserTypeKind};
77
use rustc_span::{DUMMY_SP, Span};
88
use tracing::{debug, instrument};
99

@@ -46,9 +46,9 @@ pub fn type_op_ascribe_user_type_with_span<'tcx>(
4646
let (param_env, AscribeUserType { mir_ty, user_ty }) = key.into_parts();
4747
debug!("type_op_ascribe_user_type: mir_ty={:?} user_ty={:?}", mir_ty, user_ty);
4848
let span = span.unwrap_or(DUMMY_SP);
49-
match user_ty {
50-
UserType::Ty(user_ty) => relate_mir_and_user_ty(ocx, param_env, span, mir_ty, user_ty)?,
51-
UserType::TypeOf(def_id, user_args) => {
49+
match user_ty.kind {
50+
UserTypeKind::Ty(user_ty) => relate_mir_and_user_ty(ocx, param_env, span, mir_ty, user_ty)?,
51+
UserTypeKind::TypeOf(def_id, user_args) => {
5252
relate_mir_and_user_args(ocx, param_env, span, mir_ty, def_id, user_args)?
5353
}
5454
};

0 commit comments

Comments
 (0)