Skip to content

Commit d36bdf2

Browse files
Rollup merge of #107486 - compiler-errors:bound-ty-keep-name, r=oli-obk
Track bound types like bound regions When we instantiate bound types into placeholder types, we throw away the names for some reason. These names are particularly useful for error reporting once we have `for<T>` binders. r? types
2 parents 53bb632 + 0e98a16 commit d36bdf2

File tree

13 files changed

+52
-38
lines changed

13 files changed

+52
-38
lines changed

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1930,7 +1930,7 @@ pub(super) fn check_type_bounds<'tcx>(
19301930
smallvec::SmallVec::with_capacity(defs.count());
19311931
InternalSubsts::fill_single(&mut substs, defs, &mut |param, _| match param.kind {
19321932
GenericParamDefKind::Type { .. } => {
1933-
let kind = ty::BoundTyKind::Param(param.name);
1933+
let kind = ty::BoundTyKind::Param(param.def_id, param.name);
19341934
let bound_var = ty::BoundVariableKind::Ty(kind);
19351935
bound_vars.push(bound_var);
19361936
tcx.mk_ty(ty::Bound(

compiler/rustc_infer/src/infer/higher_ranked/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl<'tcx> InferCtxt<'tcx> {
9090
types: &mut |bound_ty: ty::BoundTy| {
9191
self.tcx.mk_ty(ty::Placeholder(ty::PlaceholderType {
9292
universe: next_universe,
93-
name: bound_ty.var,
93+
name: bound_ty.kind,
9494
}))
9595
},
9696
consts: &mut |bound_var: ty::BoundVar, ty| {

compiler/rustc_infer/src/infer/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2044,7 +2044,7 @@ fn replace_param_and_infer_substs_with_placeholder<'tcx>(
20442044
) -> SubstsRef<'tcx> {
20452045
struct ReplaceParamAndInferWithPlaceholder<'tcx> {
20462046
tcx: TyCtxt<'tcx>,
2047-
idx: usize,
2047+
idx: u32,
20482048
}
20492049

20502050
impl<'tcx> TypeFolder<'tcx> for ReplaceParamAndInferWithPlaceholder<'tcx> {
@@ -2056,7 +2056,7 @@ fn replace_param_and_infer_substs_with_placeholder<'tcx>(
20562056
if let ty::Infer(_) = t.kind() {
20572057
self.tcx.mk_ty(ty::Placeholder(ty::PlaceholderType {
20582058
universe: ty::UniverseIndex::ROOT,
2059-
name: ty::BoundVar::from_usize({
2059+
name: ty::BoundTyKind::Anon({
20602060
let idx = self.idx;
20612061
self.idx += 1;
20622062
idx
@@ -2077,7 +2077,7 @@ fn replace_param_and_infer_substs_with_placeholder<'tcx>(
20772077
self.tcx.mk_const(
20782078
ty::PlaceholderConst {
20792079
universe: ty::UniverseIndex::ROOT,
2080-
name: ty::BoundVar::from_usize({
2080+
name: ty::BoundVar::from_u32({
20812081
let idx = self.idx;
20822082
self.idx += 1;
20832083
idx

compiler/rustc_middle/src/ty/fold.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,9 @@ impl<'tcx> TyCtxt<'tcx> {
610610
let index = entry.index();
611611
let var = ty::BoundVar::from_usize(index);
612612
let kind = entry
613-
.or_insert_with(|| ty::BoundVariableKind::Ty(ty::BoundTyKind::Anon))
613+
.or_insert_with(|| {
614+
ty::BoundVariableKind::Ty(ty::BoundTyKind::Anon(index as u32))
615+
})
614616
.expect_ty();
615617
self.tcx.mk_ty(ty::Bound(ty::INNERMOST, BoundTy { var, kind }))
616618
}

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1369,7 +1369,7 @@ pub struct Placeholder<T> {
13691369

13701370
pub type PlaceholderRegion = Placeholder<BoundRegionKind>;
13711371

1372-
pub type PlaceholderType = Placeholder<BoundVar>;
1372+
pub type PlaceholderType = Placeholder<BoundTyKind>;
13731373

13741374
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, HashStable)]
13751375
#[derive(TyEncodable, TyDecodable, PartialOrd, Ord)]

compiler/rustc_middle/src/ty/print/pretty.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -698,8 +698,10 @@ pub trait PrettyPrinter<'tcx>:
698698
ty::Error(_) => p!("[type error]"),
699699
ty::Param(ref param_ty) => p!(print(param_ty)),
700700
ty::Bound(debruijn, bound_ty) => match bound_ty.kind {
701-
ty::BoundTyKind::Anon => self.pretty_print_bound_var(debruijn, bound_ty.var)?,
702-
ty::BoundTyKind::Param(p) => p!(write("{}", p)),
701+
ty::BoundTyKind::Anon(bv) => {
702+
self.pretty_print_bound_var(debruijn, ty::BoundVar::from_u32(bv))?
703+
}
704+
ty::BoundTyKind::Param(_, s) => p!(write("{}", s)),
703705
},
704706
ty::Adt(def, substs) => {
705707
p!(print_def_path(def.did(), substs));

compiler/rustc_middle/src/ty/structural_impls.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ TrivialTypeTraversalAndLiftImpls! {
240240
crate::ty::AssocKind,
241241
crate::ty::AliasKind,
242242
crate::ty::Placeholder<crate::ty::BoundRegionKind>,
243+
crate::ty::Placeholder<crate::ty::BoundTyKind>,
243244
crate::ty::ClosureKind,
244245
crate::ty::FreeRegion,
245246
crate::ty::InferTy,

compiler/rustc_middle/src/ty/sty.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,13 +1504,22 @@ pub struct BoundTy {
15041504
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, TyEncodable, TyDecodable)]
15051505
#[derive(HashStable)]
15061506
pub enum BoundTyKind {
1507-
Anon,
1508-
Param(Symbol),
1507+
Anon(u32),
1508+
Param(DefId, Symbol),
1509+
}
1510+
1511+
impl BoundTyKind {
1512+
pub fn expect_anon(self) -> u32 {
1513+
match self {
1514+
BoundTyKind::Anon(i) => i,
1515+
_ => bug!(),
1516+
}
1517+
}
15091518
}
15101519

15111520
impl From<BoundVar> for BoundTy {
15121521
fn from(var: BoundVar) -> Self {
1513-
BoundTy { var, kind: BoundTyKind::Anon }
1522+
BoundTy { var, kind: BoundTyKind::Anon(var.as_u32()) }
15141523
}
15151524
}
15161525

compiler/rustc_trait_selection/src/traits/project.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ impl<'tcx> TypeFolder<'tcx> for BoundVarReplacer<'_, 'tcx> {
783783
}
784784
ty::Bound(debruijn, bound_ty) if debruijn >= self.current_index => {
785785
let universe = self.universe_for(debruijn);
786-
let p = ty::PlaceholderType { universe, name: bound_ty.var };
786+
let p = ty::PlaceholderType { universe, name: bound_ty.kind };
787787
self.mapped_types.insert(p, bound_ty);
788788
self.infcx.tcx.mk_ty(ty::Placeholder(p))
789789
}

compiler/rustc_trait_selection/src/traits/select/confirmation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
524524
.kind
525525
{
526526
GenericParamDefKind::Type { .. } => {
527-
let kind = ty::BoundTyKind::Param(param.name);
527+
let kind = ty::BoundTyKind::Param(param.def_id, param.name);
528528
let bound_var = ty::BoundVariableKind::Ty(kind);
529529
bound_vars.push(bound_var);
530530
tcx.mk_ty(ty::Bound(

0 commit comments

Comments
 (0)