Skip to content

Commit aa2aeba

Browse files
committed
Auto merge of #137123 - Zalathar:user-type-span, r=<try>
Don't store a redundant span in user-type projections While experimenting with some larger changes, I noticed that storing this span here is unnecessary, because it is also present in the corresponding `CanonicalUserTypeAnnotation` and can be retrieved via the annotation's ID.
2 parents 4229b80 + 9f3fdb1 commit aa2aeba

File tree

4 files changed

+41
-73
lines changed

4 files changed

+41
-73
lines changed

Diff for: compiler/rustc_borrowck/src/type_check/mod.rs

+31-31
Original file line numberDiff line numberDiff line change
@@ -457,38 +457,38 @@ impl<'a, 'b, 'tcx> Visitor<'tcx> for TypeVerifier<'a, 'b, 'tcx> {
457457
fn visit_local_decl(&mut self, local: Local, local_decl: &LocalDecl<'tcx>) {
458458
self.super_local_decl(local, local_decl);
459459

460-
if let Some(user_ty) = &local_decl.user_ty {
461-
for (user_ty, span) in user_ty.projections_and_spans() {
462-
let ty = if !local_decl.is_nonref_binding() {
463-
// If we have a binding of the form `let ref x: T = ..`
464-
// then remove the outermost reference so we can check the
465-
// type annotation for the remaining type.
466-
if let ty::Ref(_, rty, _) = local_decl.ty.kind() {
467-
*rty
468-
} else {
469-
bug!("{:?} with ref binding has wrong type {}", local, local_decl.ty);
470-
}
471-
} else {
472-
local_decl.ty
473-
};
460+
for user_ty in
461+
local_decl.user_ty.as_deref().into_iter().flat_map(UserTypeProjections::projections)
462+
{
463+
let span = self.typeck.user_type_annotations[user_ty.base].span;
464+
465+
let ty = if local_decl.is_nonref_binding() {
466+
local_decl.ty
467+
} else if let &ty::Ref(_, rty, _) = local_decl.ty.kind() {
468+
// If we have a binding of the form `let ref x: T = ..`
469+
// then remove the outermost reference so we can check the
470+
// type annotation for the remaining type.
471+
rty
472+
} else {
473+
bug!("{:?} with ref binding has wrong type {}", local, local_decl.ty);
474+
};
474475

475-
if let Err(terr) = self.typeck.relate_type_and_user_type(
476-
ty,
477-
ty::Invariant,
478-
user_ty,
479-
Locations::All(*span),
480-
ConstraintCategory::TypeAnnotation(AnnotationSource::Declaration),
481-
) {
482-
span_mirbug!(
483-
self,
484-
local,
485-
"bad user type on variable {:?}: {:?} != {:?} ({:?})",
486-
local,
487-
local_decl.ty,
488-
local_decl.user_ty,
489-
terr,
490-
);
491-
}
476+
if let Err(terr) = self.typeck.relate_type_and_user_type(
477+
ty,
478+
ty::Invariant,
479+
user_ty,
480+
Locations::All(span),
481+
ConstraintCategory::TypeAnnotation(AnnotationSource::Declaration),
482+
) {
483+
span_mirbug!(
484+
self,
485+
local,
486+
"bad user type on variable {:?}: {:?} != {:?} ({:?})",
487+
local,
488+
local_decl.ty,
489+
local_decl.user_ty,
490+
terr,
491+
);
492492
}
493493
}
494494
}

Diff for: compiler/rustc_middle/src/mir/mod.rs

+6-15
Original file line numberDiff line numberDiff line change
@@ -1560,7 +1560,7 @@ pub struct SourceScopeLocalData {
15601560
/// &'static str`.
15611561
#[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable)]
15621562
pub struct UserTypeProjections {
1563-
pub contents: Vec<(UserTypeProjection, Span)>,
1563+
pub contents: Vec<UserTypeProjection>,
15641564
}
15651565

15661566
impl<'tcx> UserTypeProjections {
@@ -1572,26 +1572,17 @@ impl<'tcx> UserTypeProjections {
15721572
self.contents.is_empty()
15731573
}
15741574

1575-
pub fn projections_and_spans(
1576-
&self,
1577-
) -> impl Iterator<Item = &(UserTypeProjection, Span)> + ExactSizeIterator {
1578-
self.contents.iter()
1579-
}
1580-
15811575
pub fn projections(&self) -> impl Iterator<Item = &UserTypeProjection> + ExactSizeIterator {
1582-
self.contents.iter().map(|&(ref user_type, _span)| user_type)
1576+
self.contents.iter()
15831577
}
15841578

1585-
pub fn push_projection(mut self, user_ty: &UserTypeProjection, span: Span) -> Self {
1586-
self.contents.push((user_ty.clone(), span));
1579+
pub fn push_user_type(mut self, base_user_type: UserTypeAnnotationIndex) -> Self {
1580+
self.contents.push(UserTypeProjection { base: base_user_type, projs: vec![] });
15871581
self
15881582
}
15891583

1590-
fn map_projections(
1591-
mut self,
1592-
mut f: impl FnMut(UserTypeProjection) -> UserTypeProjection,
1593-
) -> Self {
1594-
self.contents = self.contents.into_iter().map(|(proj, span)| (f(proj), span)).collect();
1584+
fn map_projections(mut self, f: impl FnMut(UserTypeProjection) -> UserTypeProjection) -> Self {
1585+
self.contents = self.contents.into_iter().map(f).collect();
15951586
self
15961587
}
15971588

Diff for: compiler/rustc_middle/src/mir/visit.rs

+2-21
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,6 @@ macro_rules! make_mir_visitor {
222222
self.super_ty(ty);
223223
}
224224

225-
fn visit_user_type_projection(
226-
&mut self,
227-
ty: & $($mutability)? UserTypeProjection,
228-
) {
229-
self.super_user_type_projection(ty);
230-
}
231-
232225
fn visit_user_type_annotation(
233226
&mut self,
234227
index: UserTypeAnnotationIndex,
@@ -818,14 +811,13 @@ macro_rules! make_mir_visitor {
818811
fn super_ascribe_user_ty(&mut self,
819812
place: & $($mutability)? Place<'tcx>,
820813
variance: $(& $mutability)? ty::Variance,
821-
user_ty: & $($mutability)? UserTypeProjection,
814+
_user_ty: & $($mutability)? UserTypeProjection,
822815
location: Location) {
823816
self.visit_place(
824817
place,
825818
PlaceContext::NonUse(NonUseContext::AscribeUserTy($(* &$mutability *)? variance)),
826819
location
827820
);
828-
self.visit_user_type_projection(user_ty);
829821
}
830822

831823
fn super_coverage(&mut self,
@@ -850,7 +842,7 @@ macro_rules! make_mir_visitor {
850842
let LocalDecl {
851843
mutability: _,
852844
ty,
853-
user_ty,
845+
user_ty: _,
854846
source_info,
855847
local_info: _,
856848
} = local_decl;
@@ -861,11 +853,6 @@ macro_rules! make_mir_visitor {
861853
local,
862854
source_info: *source_info,
863855
});
864-
if let Some(user_ty) = user_ty {
865-
for (user_ty, _) in & $($mutability)? user_ty.contents {
866-
self.visit_user_type_projection(user_ty);
867-
}
868-
}
869856
}
870857

871858
fn super_var_debug_info(
@@ -945,12 +932,6 @@ macro_rules! make_mir_visitor {
945932
self.visit_source_scope($(& $mutability)? *scope);
946933
}
947934

948-
fn super_user_type_projection(
949-
&mut self,
950-
_ty: & $($mutability)? UserTypeProjection,
951-
) {
952-
}
953-
954935
fn super_user_type_annotation(
955936
&mut self,
956937
_index: UserTypeAnnotationIndex,

Diff for: compiler/rustc_mir_build/src/builder/matches/mod.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -926,12 +926,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
926926
// Note that the variance doesn't apply here, as we are tracking the effect
927927
// of `user_ty` on any bindings contained with subpattern.
928928

929-
let projection = UserTypeProjection {
930-
base: self.canonical_user_type_annotations.push(annotation.clone()),
931-
projs: Vec::new(),
932-
};
933-
let subpattern_user_ty =
934-
pattern_user_ty.push_projection(&projection, annotation.span);
929+
let base_user_ty = self.canonical_user_type_annotations.push(annotation.clone());
930+
let subpattern_user_ty = pattern_user_ty.push_user_type(base_user_ty);
935931
self.visit_primary_bindings(subpattern, subpattern_user_ty, f)
936932
}
937933

0 commit comments

Comments
 (0)