Skip to content

Commit 28b83ee

Browse files
committed
Auto merge of #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 c62239a + 8bb574f commit 28b83ee

File tree

4 files changed

+40
-53
lines changed

4 files changed

+40
-53
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
@@ -1494,7 +1494,7 @@ pub struct SourceScopeLocalData {
14941494
/// &'static str`.
14951495
#[derive(Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable)]
14961496
pub struct UserTypeProjections {
1497-
pub contents: Vec<(UserTypeProjection, Span)>,
1497+
pub contents: Vec<UserTypeProjection>,
14981498
}
14991499

15001500
impl<'tcx> UserTypeProjections {
@@ -1506,26 +1506,17 @@ impl<'tcx> UserTypeProjections {
15061506
self.contents.is_empty()
15071507
}
15081508

1509-
pub fn projections_and_spans(
1510-
&self,
1511-
) -> impl Iterator<Item = &(UserTypeProjection, Span)> + ExactSizeIterator {
1512-
self.contents.iter()
1513-
}
1514-
15151509
pub fn projections(&self) -> impl Iterator<Item = &UserTypeProjection> + ExactSizeIterator {
1516-
self.contents.iter().map(|&(ref user_type, _span)| user_type)
1510+
self.contents.iter()
15171511
}
15181512

1519-
pub fn push_projection(mut self, user_ty: &UserTypeProjection, span: Span) -> Self {
1520-
self.contents.push((user_ty.clone(), span));
1513+
pub fn push_user_type(mut self, base_user_type: UserTypeAnnotationIndex) -> Self {
1514+
self.contents.push(UserTypeProjection { base: base_user_type, projs: vec![] });
15211515
self
15221516
}
15231517

1524-
fn map_projections(
1525-
mut self,
1526-
mut f: impl FnMut(UserTypeProjection) -> UserTypeProjection,
1527-
) -> Self {
1528-
self.contents = self.contents.into_iter().map(|(proj, span)| (f(proj), span)).collect();
1518+
fn map_projections(mut self, f: impl FnMut(UserTypeProjection) -> UserTypeProjection) -> Self {
1519+
self.contents = self.contents.into_iter().map(f).collect();
15291520
self
15301521
}
15311522

compiler/rustc_middle/src/mir/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,7 @@ macro_rules! make_mir_visitor {
857857
source_info: *source_info,
858858
});
859859
if let Some(user_ty) = user_ty {
860-
for (user_ty, _) in & $($mutability)? user_ty.contents {
860+
for user_ty in & $($mutability)? user_ty.contents {
861861
self.visit_user_type_projection(user_ty);
862862
}
863863
}

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)