Skip to content

Commit 7e107b3

Browse files
Rollup merge of rust-lang#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 037d3e4 + 0e98a16 commit 7e107b3

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

+1-1
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

+1-1
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

+3-3
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

+3-1
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

+1-1
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

+4-2
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

+1
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

+12-3
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

+1-1
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

+1-1
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(

compiler/rustc_traits/src/chalk/db.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ fn bound_vars_for_item(tcx: TyCtxt<'_>, def_id: DefId) -> SubstsRef<'_> {
725725
ty::INNERMOST,
726726
ty::BoundTy {
727727
var: ty::BoundVar::from(param.index),
728-
kind: ty::BoundTyKind::Param(param.name),
728+
kind: ty::BoundTyKind::Param(param.def_id, param.name),
729729
},
730730
))
731731
.into(),

compiler/rustc_traits/src/chalk/lowering.rs

+21-19
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::Ty<RustInterner<'tcx>>> for Ty<'tcx> {
370370
ty::Placeholder(_placeholder) => {
371371
chalk_ir::TyKind::Placeholder(chalk_ir::PlaceholderIndex {
372372
ui: chalk_ir::UniverseIndex { counter: _placeholder.universe.as_usize() },
373-
idx: _placeholder.name.as_usize(),
373+
idx: _placeholder.name.expect_anon() as usize,
374374
})
375375
}
376376
ty::Infer(_infer) => unimplemented!(),
@@ -452,10 +452,6 @@ impl<'tcx> LowerInto<'tcx, Ty<'tcx>> for &chalk_ir::Ty<RustInterner<'tcx>> {
452452
),
453453
TyKind::Foreign(def_id) => ty::Foreign(def_id.0),
454454
TyKind::Error => return interner.tcx.ty_error(),
455-
TyKind::Placeholder(placeholder) => ty::Placeholder(ty::Placeholder {
456-
universe: ty::UniverseIndex::from_usize(placeholder.ui.counter),
457-
name: ty::BoundVar::from_usize(placeholder.idx),
458-
}),
459455
TyKind::Alias(alias_ty) => match alias_ty {
460456
chalk_ir::AliasTy::Projection(projection) => ty::Alias(
461457
ty::Projection,
@@ -473,13 +469,17 @@ impl<'tcx> LowerInto<'tcx, Ty<'tcx>> for &chalk_ir::Ty<RustInterner<'tcx>> {
473469
),
474470
},
475471
TyKind::Function(_quantified_ty) => unimplemented!(),
476-
TyKind::BoundVar(_bound) => ty::Bound(
477-
ty::DebruijnIndex::from_usize(_bound.debruijn.depth() as usize),
472+
TyKind::BoundVar(bound) => ty::Bound(
473+
ty::DebruijnIndex::from_usize(bound.debruijn.depth() as usize),
478474
ty::BoundTy {
479-
var: ty::BoundVar::from_usize(_bound.index),
480-
kind: ty::BoundTyKind::Anon,
475+
var: ty::BoundVar::from_usize(bound.index),
476+
kind: ty::BoundTyKind::Anon(bound.index as u32),
481477
},
482478
),
479+
TyKind::Placeholder(placeholder) => ty::Placeholder(ty::Placeholder {
480+
universe: ty::UniverseIndex::from_usize(placeholder.ui.counter),
481+
name: ty::BoundTyKind::Anon(placeholder.idx as u32),
482+
}),
483483
TyKind::InferenceVar(_, _) => unimplemented!(),
484484
TyKind::Dyn(_) => unimplemented!(),
485485
};
@@ -504,7 +504,7 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::Lifetime<RustInterner<'tcx>>> for Region<'t
504504
ty::RePlaceholder(placeholder_region) => {
505505
chalk_ir::LifetimeData::Placeholder(chalk_ir::PlaceholderIndex {
506506
ui: chalk_ir::UniverseIndex { counter: placeholder_region.universe.index() },
507-
idx: 0,
507+
idx: 0, // FIXME: This `idx: 0` is sus.
508508
})
509509
.intern(interner)
510510
}
@@ -674,7 +674,7 @@ impl<'tcx> LowerInto<'tcx, chalk_ir::Binders<chalk_ir::QuantifiedWhereClauses<Ru
674674
let self_ty = interner.tcx.mk_ty(ty::Bound(
675675
// This is going to be wrapped in a binder
676676
ty::DebruijnIndex::from_usize(1),
677-
ty::BoundTy { var: ty::BoundVar::from_usize(0), kind: ty::BoundTyKind::Anon },
677+
ty::BoundTy { var: ty::BoundVar::from_usize(0), kind: ty::BoundTyKind::Anon(0) },
678678
));
679679
let where_clauses = predicates.into_iter().map(|predicate| {
680680
let (predicate, binders, _named_regions) =
@@ -1038,7 +1038,7 @@ pub(crate) struct ParamsSubstitutor<'tcx> {
10381038
binder_index: ty::DebruijnIndex,
10391039
list: Vec<rustc_middle::ty::ParamTy>,
10401040
next_ty_placeholder: usize,
1041-
pub(crate) params: rustc_data_structures::fx::FxHashMap<usize, rustc_middle::ty::ParamTy>,
1041+
pub(crate) params: rustc_data_structures::fx::FxHashMap<u32, rustc_middle::ty::ParamTy>,
10421042
pub(crate) named_regions: BTreeMap<DefId, u32>,
10431043
}
10441044

@@ -1072,15 +1072,15 @@ impl<'tcx> TypeFolder<'tcx> for ParamsSubstitutor<'tcx> {
10721072
ty::Param(param) => match self.list.iter().position(|r| r == &param) {
10731073
Some(idx) => self.tcx.mk_ty(ty::Placeholder(ty::PlaceholderType {
10741074
universe: ty::UniverseIndex::from_usize(0),
1075-
name: ty::BoundVar::from_usize(idx),
1075+
name: ty::BoundTyKind::Anon(idx as u32),
10761076
})),
10771077
None => {
10781078
self.list.push(param);
10791079
let idx = self.list.len() - 1 + self.next_ty_placeholder;
1080-
self.params.insert(idx, param);
1080+
self.params.insert(idx as u32, param);
10811081
self.tcx.mk_ty(ty::Placeholder(ty::PlaceholderType {
10821082
universe: ty::UniverseIndex::from_usize(0),
1083-
name: ty::BoundVar::from_usize(idx),
1083+
name: ty::BoundTyKind::Anon(idx as u32),
10841084
}))
10851085
}
10861086
},
@@ -1119,13 +1119,13 @@ impl<'tcx> TypeFolder<'tcx> for ParamsSubstitutor<'tcx> {
11191119

11201120
pub(crate) struct ReverseParamsSubstitutor<'tcx> {
11211121
tcx: TyCtxt<'tcx>,
1122-
params: rustc_data_structures::fx::FxHashMap<usize, rustc_middle::ty::ParamTy>,
1122+
params: rustc_data_structures::fx::FxHashMap<u32, rustc_middle::ty::ParamTy>,
11231123
}
11241124

11251125
impl<'tcx> ReverseParamsSubstitutor<'tcx> {
11261126
pub(crate) fn new(
11271127
tcx: TyCtxt<'tcx>,
1128-
params: rustc_data_structures::fx::FxHashMap<usize, rustc_middle::ty::ParamTy>,
1128+
params: rustc_data_structures::fx::FxHashMap<u32, rustc_middle::ty::ParamTy>,
11291129
) -> Self {
11301130
Self { tcx, params }
11311131
}
@@ -1139,7 +1139,7 @@ impl<'tcx> TypeFolder<'tcx> for ReverseParamsSubstitutor<'tcx> {
11391139
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
11401140
match *t.kind() {
11411141
ty::Placeholder(ty::PlaceholderType { universe: ty::UniverseIndex::ROOT, name }) => {
1142-
match self.params.get(&name.as_usize()) {
1142+
match self.params.get(&name.expect_anon()) {
11431143
Some(param) => self.tcx.mk_ty(ty::Param(*param)),
11441144
None => t,
11451145
}
@@ -1171,7 +1171,8 @@ impl<'tcx> TypeVisitor<'tcx> for PlaceholdersCollector {
11711171
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
11721172
match t.kind() {
11731173
ty::Placeholder(p) if p.universe == self.universe_index => {
1174-
self.next_ty_placeholder = self.next_ty_placeholder.max(p.name.as_usize() + 1);
1174+
self.next_ty_placeholder =
1175+
self.next_ty_placeholder.max(p.name.expect_anon() as usize + 1);
11751176
}
11761177

11771178
_ => (),
@@ -1186,6 +1187,7 @@ impl<'tcx> TypeVisitor<'tcx> for PlaceholdersCollector {
11861187
if let ty::BoundRegionKind::BrAnon(anon, _) = p.name {
11871188
self.next_anon_region_placeholder = self.next_anon_region_placeholder.max(anon);
11881189
}
1190+
// FIXME: This doesn't seem to handle BrNamed at all?
11891191
}
11901192

11911193
_ => (),

compiler/rustc_traits/src/chalk/mod.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66
pub(crate) mod db;
77
pub(crate) mod lowering;
88

9-
use rustc_data_structures::fx::FxHashMap;
10-
119
use rustc_middle::infer::canonical::{CanonicalTyVarKind, CanonicalVarKind};
1210
use rustc_middle::traits::ChalkRustInterner;
1311
use rustc_middle::ty::query::Providers;
14-
use rustc_middle::ty::{self, ParamTy, TyCtxt, TypeFoldable, TypeVisitable};
12+
use rustc_middle::ty::{self, TyCtxt, TypeFoldable, TypeVisitable};
1513

1614
use rustc_infer::infer::canonical::{
1715
Canonical, CanonicalVarValues, Certainty, QueryRegionConstraints, QueryResponse,
@@ -41,7 +39,7 @@ pub(crate) fn evaluate_goal<'tcx>(
4139
let mut params_substitutor =
4240
ParamsSubstitutor::new(tcx, placeholders_collector.next_ty_placeholder);
4341
let obligation = obligation.fold_with(&mut params_substitutor);
44-
let params: FxHashMap<usize, ParamTy> = params_substitutor.params;
42+
let params = params_substitutor.params;
4543

4644
let max_universe = obligation.max_universe.index();
4745

0 commit comments

Comments
 (0)