Skip to content

Commit ec7f149

Browse files
committed
Auto merge of rust-lang#137123 - Zalathar:user-type-span, r=oli-obk
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 ed49386 + 9f3fdb1 commit ec7f149

File tree

4 files changed

+41
-73
lines changed

4 files changed

+41
-73
lines changed

compiler/rustc_borrowck/src/type_check/mod.rs

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

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

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

compiler/rustc_middle/src/mir/mod.rs

+6-15
Original file line numberDiff line numberDiff line change
@@ -1499,7 +1499,7 @@ pub struct SourceScopeLocalData {
14991499
/// &'static str`.
15001500
#[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable)]
15011501
pub struct UserTypeProjections {
1502-
pub contents: Vec<(UserTypeProjection, Span)>,
1502+
pub contents: Vec<UserTypeProjection>,
15031503
}
15041504

15051505
impl<'tcx> UserTypeProjections {
@@ -1511,26 +1511,17 @@ impl<'tcx> UserTypeProjections {
15111511
self.contents.is_empty()
15121512
}
15131513

1514-
pub fn projections_and_spans(
1515-
&self,
1516-
) -> impl Iterator<Item = &(UserTypeProjection, Span)> + ExactSizeIterator {
1517-
self.contents.iter()
1518-
}
1519-
15201514
pub fn projections(&self) -> impl Iterator<Item = &UserTypeProjection> + ExactSizeIterator {
1521-
self.contents.iter().map(|&(ref user_type, _span)| user_type)
1515+
self.contents.iter()
15221516
}
15231517

1524-
pub fn push_projection(mut self, user_ty: &UserTypeProjection, span: Span) -> Self {
1525-
self.contents.push((user_ty.clone(), span));
1518+
pub fn push_user_type(mut self, base_user_type: UserTypeAnnotationIndex) -> Self {
1519+
self.contents.push(UserTypeProjection { base: base_user_type, projs: vec![] });
15261520
self
15271521
}
15281522

1529-
fn map_projections(
1530-
mut self,
1531-
mut f: impl FnMut(UserTypeProjection) -> UserTypeProjection,
1532-
) -> Self {
1533-
self.contents = self.contents.into_iter().map(|(proj, span)| (f(proj), span)).collect();
1523+
fn map_projections(mut self, f: impl FnMut(UserTypeProjection) -> UserTypeProjection) -> Self {
1524+
self.contents = self.contents.into_iter().map(f).collect();
15341525
self
15351526
}
15361527

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,

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)