Skip to content

Commit dc18547

Browse files
committed
[feed type of assoc const binding]
1 parent 6172aa7 commit dc18547

File tree

9 files changed

+84
-38
lines changed

9 files changed

+84
-38
lines changed

compiler/rustc_hir_analysis/src/astconv/bounds.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,10 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
401401
let bound_vars = tcx.late_bound_vars(binding.hir_id);
402402
ty::Binder::bind_with_vars(subst_output, bound_vars)
403403
} else {
404-
// Append the generic arguments of the associated type or const to the `trait_ref`.
405-
candidate.map_bound(|trait_ref| {
404+
// Create the generic arguments for the associated type or constant by joining the
405+
// parent arguments (the arguments of the trait) and the own arguments (the ones of
406+
// the associated item itself) and construct an alias type using them.
407+
let alias_ty = candidate.map_bound(|trait_ref| {
406408
let ident = Ident::new(assoc_item.name, binding.ident.span);
407409
let item_segment = hir::PathSegment {
408410
ident,
@@ -424,7 +426,20 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
424426
// *constants* to represent *const projections*. Alias *term* would be a more
425427
// appropriate name but alas.
426428
ty::AliasTy::new(tcx, assoc_item.def_id, alias_args)
427-
})
429+
});
430+
431+
// Provide the resolved type of the associated constant to `type_of(AnonConst)`.
432+
if !speculative && let ty::AssocKind::Const = assoc_kind {
433+
// FIXME(fmease): Is this `map_bound` + `instantiate` correct? Consider:
434+
// `const K<T>: for<'a> fn(&'a T);`, `P<K<for<'x> fn(&'x ())> = {…}>` or similar.
435+
let ty = alias_ty.map_bound(|ty| tcx.type_of(ty.def_id).instantiate(tcx, ty.args));
436+
// Since the arguments passed to the alias type above may contain early-bound
437+
// generic parameters, the instantiated type may contain some as well.
438+
// Therefore wrap it in `EarlyBinder`.
439+
tcx.feed_type_of_assoc_const_binding(binding.hir_id, ty::EarlyBinder::bind(ty));
440+
}
441+
442+
alias_ty
428443
};
429444

430445
match binding.kind {

compiler/rustc_hir_analysis/src/collect.rs

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ pub fn provide(providers: &mut Providers) {
6060
*providers = Providers {
6161
type_of: type_of::type_of,
6262
type_of_opaque: type_of::type_of_opaque,
63+
type_of_assoc_const_binding: type_of::type_of_assoc_const_binding,
6364
type_alias_is_lazy: type_of::type_alias_is_lazy,
6465
item_bounds: item_bounds::item_bounds,
6566
explicit_item_bounds: item_bounds::explicit_item_bounds,

compiler/rustc_hir_analysis/src/collect/type_of.rs

+19-29
Original file line numberDiff line numberDiff line change
@@ -66,35 +66,10 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> {
6666
.expect("const parameter types cannot be generic");
6767
}
6868

69-
Node::TypeBinding(binding @ &TypeBinding { hir_id: binding_id, .. })
70-
if let Node::TraitRef(trait_ref) = tcx.hir_node(tcx.hir().parent_id(binding_id)) =>
71-
{
72-
let Some(trait_def_id) = trait_ref.trait_def_id() else {
73-
return Ty::new_error_with_message(
74-
tcx,
75-
tcx.def_span(def_id),
76-
"Could not find trait",
77-
);
78-
};
79-
let assoc_items = tcx.associated_items(trait_def_id);
80-
let assoc_item = assoc_items.find_by_name_and_kind(
81-
tcx,
82-
binding.ident,
83-
ty::AssocKind::Const,
84-
def_id.to_def_id(),
85-
);
86-
return if let Some(assoc_item) = assoc_item {
87-
tcx.type_of(assoc_item.def_id)
88-
.no_bound_vars()
89-
.expect("const parameter types cannot be generic")
90-
} else {
91-
// FIXME(associated_const_equality): add a useful error message here.
92-
Ty::new_error_with_message(
93-
tcx,
94-
tcx.def_span(def_id),
95-
"Could not find associated const on trait",
96-
)
97-
};
69+
Node::TypeBinding(&TypeBinding { hir_id, .. }) => {
70+
// FIXME(fmease): Reject “escaping” early-bound generic parameters.
71+
// FIXME(fmease): Reject escaping late-bound vars.
72+
return tcx.type_of_assoc_const_binding(hir_id).skip_binder().skip_binder();
9873
}
9974

10075
// This match arm is for when the def_id appears in a GAT whose
@@ -303,6 +278,21 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> {
303278
}
304279
}
305280

281+
pub(super) fn type_of_assoc_const_binding<'tcx>(
282+
tcx: TyCtxt<'tcx>,
283+
hir_id: HirId,
284+
) -> ty::EarlyBinder<ty::Binder<'tcx, Ty<'tcx>>> {
285+
let guar = tcx.dcx().span_delayed_bug(
286+
rustc_span::DUMMY_SP,
287+
format!(
288+
"attempt to obtain type of assoc const binding `{hir_id}` before \
289+
it was resolved by `add_predicates_for_ast_type_binding`"
290+
),
291+
);
292+
293+
ty::EarlyBinder::bind(ty::Binder::dummy(Ty::new_error(tcx, guar)))
294+
}
295+
306296
fn get_path_containing_arg_in_pat<'hir>(
307297
pat: &'hir hir::Pat<'hir>,
308298
arg_id: HirId,

compiler/rustc_middle/src/query/erase.rs

+4
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,10 @@ impl<T: EraseType> EraseType for ty::EarlyBinder<T> {
184184
type Result = T::Result;
185185
}
186186

187+
impl EraseType for ty::Binder<'_, Ty<'_>> {
188+
type Result = [u8; size_of::<ty::Binder<'static, Ty<'static>>>()];
189+
}
190+
187191
impl EraseType for ty::Binder<'_, ty::FnSig<'_>> {
188192
type Result = [u8; size_of::<ty::Binder<'static, ty::FnSig<'static>>>()];
189193
}

compiler/rustc_middle/src/query/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,11 @@ rustc_queries! {
255255
cycle_stash
256256
}
257257

258+
query type_of_assoc_const_binding(key: hir::HirId) -> ty::EarlyBinder<ty::Binder<'tcx, Ty<'tcx>>> {
259+
desc { |tcx| "getting type of associated constant binding `{key:?}`" }
260+
feedable
261+
}
262+
258263
query type_alias_is_lazy(key: DefId) -> bool {
259264
desc { |tcx|
260265
"computing whether `{path}` is a lazy type alias",

compiler/rustc_middle/src/ty/context.rs

+8
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,14 @@ impl<'tcx> TyCtxt<'tcx> {
533533
debug_assert_eq!(self.def_kind(key), DefKind::AnonConst);
534534
TyCtxtFeed { tcx: self, key }.type_of(value)
535535
}
536+
537+
pub fn feed_type_of_assoc_const_binding(
538+
self,
539+
key: hir::HirId,
540+
value: ty::EarlyBinder<ty::Binder<'tcx, Ty<'tcx>>>,
541+
) {
542+
TyCtxtFeed { tcx: self, key }.type_of_assoc_const_binding(value)
543+
}
536544
}
537545

538546
impl<'tcx, KEY: Copy> TyCtxtFeed<'tcx, KEY> {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Regression test for issue #118040.
2+
// Ensure that we support assoc const eq bounds where the assoc const comes from a supertrait.
3+
4+
// check-pass
5+
6+
#![feature(associated_const_equality)]
7+
8+
trait Trait: SuperTrait {}
9+
trait SuperTrait: SuperSuperTrait<i32> {}
10+
trait SuperSuperTrait<T> {
11+
const K: T;
12+
}
13+
14+
fn take(_: impl Trait<K = 0>) {}
15+
16+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
// Regression test for issue #112560.
22
// Respect the fact that (associated) types and constants live in different namespaces and
33
// therefore equality bounds involving identically named associated items don't conflict if
4-
// their kind (type vs. const) differs.
5-
6-
// FIXME(fmease): Extend this test to cover supertraits again
7-
// once #118040 is fixed. See initial version of PR #118360.
4+
// their kind (type vs. const) differs. This obviously extends to supertraits.
85

96
// check-pass
107

118
#![feature(associated_const_equality)]
129

13-
trait Trait {
10+
trait Trait: SuperTrait {
1411
type N;
12+
type Q;
1513

1614
const N: usize;
1715
}
1816

19-
fn take(_: impl Trait<N = 0, N = ()>) {}
17+
trait SuperTrait {
18+
const Q: &'static str;
19+
}
20+
21+
fn take0(_: impl Trait<N = 0, N = ()>) {}
22+
23+
fn take1(_: impl Trait<Q = "...", Q = [()]>) {}
2024

2125
fn main() {}

tests/ui/generic-const-items/associated-const-equality.rs

+3
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@
66
trait Owner {
77
const C<const N: u32>: u32;
88
const K<const N: u32>: u32;
9+
const Q<T>: Option<T>;
910
}
1011

1112
impl Owner for () {
1213
const C<const N: u32>: u32 = N;
1314
const K<const N: u32>: u32 = N + 1;
15+
const Q<T>: Option<T> = None;
1416
}
1517

1618
fn take0<const N: u32>(_: impl Owner<C<N> = { N }>) {}
1719
fn take1(_: impl Owner<K<99> = 100>) {}
20+
fn take2(_: impl Owner<Q<()> = { Some(()) }>) {}
1821

1922
fn main() {
2023
take0::<128>(());

0 commit comments

Comments
 (0)