1
Fork 0

Rollup merge of #133926 - compiler-errors:const-conditions, r=lcnr

Fix const conditions for RPITITs

Fixes #133918

r? lcnr
This commit is contained in:
许杰友 Jieyou Xu (Joe) 2024-12-18 22:56:52 +08:00 committed by GitHub
commit 81fccef2b2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 254 additions and 28 deletions

View file

@ -371,10 +371,9 @@ pub(super) fn explicit_item_bounds_with_filter(
associated_type_bounds(tcx, def_id, opaque_ty.bounds, opaque_ty.span, filter);
return ty::EarlyBinder::bind(bounds);
}
Some(ty::ImplTraitInTraitData::Impl { .. }) => span_bug!(
tcx.def_span(def_id),
"item bounds for RPITIT in impl to be fed on def-id creation"
),
Some(ty::ImplTraitInTraitData::Impl { .. }) => {
span_bug!(tcx.def_span(def_id), "RPITIT in impl should not have item bounds")
}
None => {}
}

View file

@ -956,6 +956,15 @@ pub(super) fn const_conditions<'tcx>(
bug!("const_conditions invoked for item that is not conditionally const: {def_id:?}");
}
match tcx.opt_rpitit_info(def_id.to_def_id()) {
// RPITITs inherit const conditions of their parent fn
Some(
ty::ImplTraitInTraitData::Impl { fn_def_id }
| ty::ImplTraitInTraitData::Trait { fn_def_id, .. },
) => return tcx.const_conditions(fn_def_id),
None => {}
}
let (generics, trait_def_id_and_supertraits, has_parent) = match tcx.hir_node_by_def_id(def_id)
{
Node::Item(item) => match item.kind {
@ -1059,19 +1068,29 @@ pub(super) fn explicit_implied_const_bounds<'tcx>(
bug!("const_conditions invoked for item that is not conditionally const: {def_id:?}");
}
let bounds = match tcx.hir_node_by_def_id(def_id) {
Node::Item(hir::Item { kind: hir::ItemKind::Trait(..), .. }) => {
implied_predicates_with_filter(
tcx,
def_id.to_def_id(),
PredicateFilter::SelfConstIfConst,
)
}
Node::TraitItem(hir::TraitItem { kind: hir::TraitItemKind::Type(..), .. })
| Node::OpaqueTy(_) => {
let bounds = match tcx.opt_rpitit_info(def_id.to_def_id()) {
// RPITIT's bounds are the same as opaque type bounds, but with
// a projection self type.
Some(ty::ImplTraitInTraitData::Trait { .. }) => {
explicit_item_bounds_with_filter(tcx, def_id, PredicateFilter::ConstIfConst)
}
_ => bug!("explicit_implied_const_bounds called on wrong item: {def_id:?}"),
Some(ty::ImplTraitInTraitData::Impl { .. }) => {
span_bug!(tcx.def_span(def_id), "RPITIT in impl should not have item bounds")
}
None => match tcx.hir_node_by_def_id(def_id) {
Node::Item(hir::Item { kind: hir::ItemKind::Trait(..), .. }) => {
implied_predicates_with_filter(
tcx,
def_id.to_def_id(),
PredicateFilter::SelfConstIfConst,
)
}
Node::TraitItem(hir::TraitItem { kind: hir::TraitItemKind::Type(..), .. })
| Node::OpaqueTy(_) => {
explicit_item_bounds_with_filter(tcx, def_id, PredicateFilter::ConstIfConst)
}
_ => bug!("explicit_implied_const_bounds called on wrong item: {def_id:?}"),
},
};
bounds.map_bound(|bounds| {