Skip to content

Commit

Permalink
[feed type of assoc const binding]
Browse files Browse the repository at this point in the history
  • Loading branch information
fmease committed Dec 28, 2023
1 parent 675dae4 commit 80a3ebb
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 30 deletions.
11 changes: 10 additions & 1 deletion compiler/rustc_hir_analysis/src/astconv/bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,16 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
// FIXME(fmease): Explain that we are indeed constructing an alias "type" for
// associated constants with feature `associated_const_equality` and that we
// call them "const projections".
ty::AliasTy::new(tcx, assoc_item.def_id, args_trait_ref_and_assoc_item)
let alias_ty =
ty::AliasTy::new(tcx, assoc_item.def_id, args_trait_ref_and_assoc_item);

if let ty::AssocKind::Const = assoc_kind {
// FIXME(fmease): We're completely ignoring bound vars here!
let ty = tcx.type_of(alias_ty.def_id).instantiate(tcx, alias_ty.args);
tcx.feed_type_of_assoc_const_binding(binding.hir_id, ty);
}

alias_ty
})
};

Expand Down
31 changes: 2 additions & 29 deletions compiler/rustc_hir_analysis/src/collect/type_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,35 +66,8 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> {
.expect("const parameter types cannot be generic");
}

Node::TypeBinding(binding @ &TypeBinding { hir_id: binding_id, .. })
if let Node::TraitRef(trait_ref) = tcx.hir_node(tcx.hir().parent_id(binding_id)) =>
{
let Some(trait_def_id) = trait_ref.trait_def_id() else {
return Ty::new_error_with_message(
tcx,
tcx.def_span(def_id),
"Could not find trait",
);
};
let assoc_items = tcx.associated_items(trait_def_id);
let assoc_item = assoc_items.find_by_name_and_kind(
tcx,
binding.ident,
ty::AssocKind::Const,
def_id.to_def_id(),
);
return if let Some(assoc_item) = assoc_item {
tcx.type_of(assoc_item.def_id)
.no_bound_vars()
.expect("const parameter types cannot be generic")
} else {
// FIXME(associated_const_equality): add a useful error message here.
Ty::new_error_with_message(
tcx,
tcx.def_span(def_id),
"Could not find associated const on trait",
)
};
Node::TypeBinding(&TypeBinding { hir_id, .. }) => {
return tcx.type_of_assoc_const_binding(hir_id);
}

// This match arm is for when the def_id appears in a GAT whose
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,12 @@ rustc_queries! {
cycle_stash
}

// FIXME(fmease): docs
query type_of_assoc_const_binding(key: hir::HirId) -> Ty<'tcx> {
desc { |tcx| "computing type of associated constant binding `{key:?}`" }
feedable
}

query type_alias_is_lazy(key: DefId) -> bool {
desc { |tcx|
"computing whether `{path}` is a lazy type alias",
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,10 @@ impl<'tcx> TyCtxt<'tcx> {
debug_assert_eq!(self.def_kind(key), DefKind::AnonConst);
TyCtxtFeed { tcx: self, key }.type_of(value)
}

pub fn feed_type_of_assoc_const_binding(self, key: hir::HirId, value: Ty<'tcx>) {
TyCtxtFeed { tcx: self, key }.type_of_assoc_const_binding(value)
}
}

impl<'tcx, KEY: Copy> TyCtxtFeed<'tcx, KEY> {
Expand Down

0 comments on commit 80a3ebb

Please sign in to comment.