1
Fork 0

Rollup merge of #133218 - compiler-errors:const-opaque, r=fee1-dead

Implement `~const` item bounds in RPIT

an RPIT in a `const fn` is allowed to be conditionally const itself :)

r? fee1-dead or reroll
This commit is contained in:
Matthias Krüger 2024-11-21 07:56:13 +01:00 committed by GitHub
commit 920092531f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
20 changed files with 144 additions and 31 deletions

View file

@ -339,6 +339,8 @@ fn check_opaque_meets_bounds<'tcx>(
let misc_cause = ObligationCause::misc(span, def_id);
// FIXME: We should just register the item bounds here, rather than equating.
// FIXME(const_trait_impl): When we do that, please make sure to also register
// the `~const` bounds.
match ocx.eq(&misc_cause, param_env, opaque_ty, hidden_ty) {
Ok(()) => {}
Err(ty_err) => {

View file

@ -2083,7 +2083,7 @@ pub(super) fn check_type_bounds<'tcx>(
// Only in a const implementation do we need to check that the `~const` item bounds hold.
if tcx.is_conditionally_const(impl_ty_def_id) {
obligations.extend(
tcx.implied_const_bounds(trait_ty.def_id)
tcx.explicit_implied_const_bounds(trait_ty.def_id)
.iter_instantiated_copied(tcx, rebased_args)
.map(|(c, span)| {
traits::Obligation::new(

View file

@ -78,7 +78,7 @@ pub fn provide(providers: &mut Providers) {
predicates_of::explicit_supertraits_containing_assoc_item,
trait_explicit_predicates_and_bounds: predicates_of::trait_explicit_predicates_and_bounds,
const_conditions: predicates_of::const_conditions,
implied_const_bounds: predicates_of::implied_const_bounds,
explicit_implied_const_bounds: predicates_of::explicit_implied_const_bounds,
type_param_predicates: predicates_of::type_param_predicates,
trait_def,
adt_def,
@ -340,6 +340,10 @@ impl<'tcx> Visitor<'tcx> for CollectItemTypesVisitor<'tcx> {
self.tcx.ensure().explicit_item_super_predicates(def_id);
self.tcx.ensure().item_bounds(def_id);
self.tcx.ensure().item_super_predicates(def_id);
if self.tcx.is_conditionally_const(def_id) {
self.tcx.ensure().explicit_implied_const_bounds(def_id);
self.tcx.ensure().const_conditions(def_id);
}
intravisit::walk_opaque_ty(self, opaque);
}
@ -682,6 +686,10 @@ fn lower_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) {
tcx.ensure().generics_of(item.owner_id);
tcx.ensure().type_of(item.owner_id);
tcx.ensure().predicates_of(item.owner_id);
if tcx.is_conditionally_const(def_id) {
tcx.ensure().explicit_implied_const_bounds(def_id);
tcx.ensure().const_conditions(def_id);
}
match item.kind {
hir::ForeignItemKind::Fn(..) => {
tcx.ensure().codegen_fn_attrs(item.owner_id);

View file

@ -959,6 +959,12 @@ pub(super) fn const_conditions<'tcx>(
hir::ForeignItemKind::Fn(_, _, generics) => (generics, None, false),
_ => bug!("const_conditions called on wrong item: {def_id:?}"),
},
Node::OpaqueTy(opaque) => match opaque.origin {
hir::OpaqueTyOrigin::FnReturn { parent, .. } => return tcx.const_conditions(parent),
hir::OpaqueTyOrigin::AsyncFn { .. } | hir::OpaqueTyOrigin::TyAlias { .. } => {
unreachable!()
}
},
// N.B. Tuple ctors are unconditionally constant.
Node::Ctor(hir::VariantData::Tuple { .. }) => return Default::default(),
_ => bug!("const_conditions called on wrong item: {def_id:?}"),
@ -1018,7 +1024,7 @@ pub(super) fn const_conditions<'tcx>(
}
}
pub(super) fn implied_const_bounds<'tcx>(
pub(super) fn explicit_implied_const_bounds<'tcx>(
tcx: TyCtxt<'tcx>,
def_id: LocalDefId,
) -> ty::EarlyBinder<'tcx, &'tcx [(ty::PolyTraitRef<'tcx>, Span)]> {
@ -1034,10 +1040,11 @@ pub(super) fn implied_const_bounds<'tcx>(
PredicateFilter::SelfConstIfConst,
)
}
Node::TraitItem(hir::TraitItem { kind: hir::TraitItemKind::Type(..), .. }) => {
Node::TraitItem(hir::TraitItem { kind: hir::TraitItemKind::Type(..), .. })
| Node::OpaqueTy(_) => {
explicit_item_bounds_with_filter(tcx, def_id, PredicateFilter::ConstIfConst)
}
_ => bug!("implied_const_bounds called on wrong item: {def_id:?}"),
_ => bug!("explicit_implied_const_bounds called on wrong item: {def_id:?}"),
};
bounds.map_bound(|bounds| {