Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 9200925

Browse files
authored
Rollup merge of rust-lang#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
2 parents 9d70af5 + 5eeaf2e commit 9200925

File tree

20 files changed

+144
-31
lines changed

20 files changed

+144
-31
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,8 @@ fn check_opaque_meets_bounds<'tcx>(
339339

340340
let misc_cause = ObligationCause::misc(span, def_id);
341341
// FIXME: We should just register the item bounds here, rather than equating.
342+
// FIXME(const_trait_impl): When we do that, please make sure to also register
343+
// the `~const` bounds.
342344
match ocx.eq(&misc_cause, param_env, opaque_ty, hidden_ty) {
343345
Ok(()) => {}
344346
Err(ty_err) => {

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2083,7 +2083,7 @@ pub(super) fn check_type_bounds<'tcx>(
20832083
// Only in a const implementation do we need to check that the `~const` item bounds hold.
20842084
if tcx.is_conditionally_const(impl_ty_def_id) {
20852085
obligations.extend(
2086-
tcx.implied_const_bounds(trait_ty.def_id)
2086+
tcx.explicit_implied_const_bounds(trait_ty.def_id)
20872087
.iter_instantiated_copied(tcx, rebased_args)
20882088
.map(|(c, span)| {
20892089
traits::Obligation::new(

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub fn provide(providers: &mut Providers) {
7878
predicates_of::explicit_supertraits_containing_assoc_item,
7979
trait_explicit_predicates_and_bounds: predicates_of::trait_explicit_predicates_and_bounds,
8080
const_conditions: predicates_of::const_conditions,
81-
implied_const_bounds: predicates_of::implied_const_bounds,
81+
explicit_implied_const_bounds: predicates_of::explicit_implied_const_bounds,
8282
type_param_predicates: predicates_of::type_param_predicates,
8383
trait_def,
8484
adt_def,
@@ -340,6 +340,10 @@ impl<'tcx> Visitor<'tcx> for CollectItemTypesVisitor<'tcx> {
340340
self.tcx.ensure().explicit_item_super_predicates(def_id);
341341
self.tcx.ensure().item_bounds(def_id);
342342
self.tcx.ensure().item_super_predicates(def_id);
343+
if self.tcx.is_conditionally_const(def_id) {
344+
self.tcx.ensure().explicit_implied_const_bounds(def_id);
345+
self.tcx.ensure().const_conditions(def_id);
346+
}
343347
intravisit::walk_opaque_ty(self, opaque);
344348
}
345349

@@ -682,6 +686,10 @@ fn lower_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) {
682686
tcx.ensure().generics_of(item.owner_id);
683687
tcx.ensure().type_of(item.owner_id);
684688
tcx.ensure().predicates_of(item.owner_id);
689+
if tcx.is_conditionally_const(def_id) {
690+
tcx.ensure().explicit_implied_const_bounds(def_id);
691+
tcx.ensure().const_conditions(def_id);
692+
}
685693
match item.kind {
686694
hir::ForeignItemKind::Fn(..) => {
687695
tcx.ensure().codegen_fn_attrs(item.owner_id);

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,12 @@ pub(super) fn const_conditions<'tcx>(
959959
hir::ForeignItemKind::Fn(_, _, generics) => (generics, None, false),
960960
_ => bug!("const_conditions called on wrong item: {def_id:?}"),
961961
},
962+
Node::OpaqueTy(opaque) => match opaque.origin {
963+
hir::OpaqueTyOrigin::FnReturn { parent, .. } => return tcx.const_conditions(parent),
964+
hir::OpaqueTyOrigin::AsyncFn { .. } | hir::OpaqueTyOrigin::TyAlias { .. } => {
965+
unreachable!()
966+
}
967+
},
962968
// N.B. Tuple ctors are unconditionally constant.
963969
Node::Ctor(hir::VariantData::Tuple { .. }) => return Default::default(),
964970
_ => bug!("const_conditions called on wrong item: {def_id:?}"),
@@ -1018,7 +1024,7 @@ pub(super) fn const_conditions<'tcx>(
10181024
}
10191025
}
10201026

1021-
pub(super) fn implied_const_bounds<'tcx>(
1027+
pub(super) fn explicit_implied_const_bounds<'tcx>(
10221028
tcx: TyCtxt<'tcx>,
10231029
def_id: LocalDefId,
10241030
) -> ty::EarlyBinder<'tcx, &'tcx [(ty::PolyTraitRef<'tcx>, Span)]> {
@@ -1034,10 +1040,11 @@ pub(super) fn implied_const_bounds<'tcx>(
10341040
PredicateFilter::SelfConstIfConst,
10351041
)
10361042
}
1037-
Node::TraitItem(hir::TraitItem { kind: hir::TraitItemKind::Type(..), .. }) => {
1043+
Node::TraitItem(hir::TraitItem { kind: hir::TraitItemKind::Type(..), .. })
1044+
| Node::OpaqueTy(_) => {
10381045
explicit_item_bounds_with_filter(tcx, def_id, PredicateFilter::ConstIfConst)
10391046
}
1040-
_ => bug!("implied_const_bounds called on wrong item: {def_id:?}"),
1047+
_ => bug!("explicit_implied_const_bounds called on wrong item: {def_id:?}"),
10411048
};
10421049

10431050
bounds.map_bound(|bounds| {

compiler/rustc_infer/src/infer/opaque_types/mod.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -574,9 +574,8 @@ impl<'tcx> InferCtxt<'tcx> {
574574
// unexpected region errors.
575575
goals.push(Goal::new(tcx, param_env, ty::ClauseKind::WellFormed(hidden_ty.into())));
576576

577-
let item_bounds = tcx.explicit_item_bounds(def_id);
578-
for (predicate, _) in item_bounds.iter_instantiated_copied(tcx, args) {
579-
let predicate = predicate.fold_with(&mut BottomUpFolder {
577+
let replace_opaques_in = |clause: ty::Clause<'tcx>, goals: &mut Vec<_>| {
578+
clause.fold_with(&mut BottomUpFolder {
580579
tcx,
581580
ty_op: |ty| match *ty.kind() {
582581
// We can't normalize associated types from `rustc_infer`,
@@ -612,11 +611,31 @@ impl<'tcx> InferCtxt<'tcx> {
612611
},
613612
lt_op: |lt| lt,
614613
ct_op: |ct| ct,
615-
});
614+
})
615+
};
616+
617+
let item_bounds = tcx.explicit_item_bounds(def_id);
618+
for (predicate, _) in item_bounds.iter_instantiated_copied(tcx, args) {
619+
let predicate = replace_opaques_in(predicate, goals);
616620

617621
// Require that the predicate holds for the concrete type.
618622
debug!(?predicate);
619623
goals.push(Goal::new(self.tcx, param_env, predicate));
620624
}
625+
626+
// If this opaque is being defined and it's conditionally const,
627+
if self.tcx.is_conditionally_const(def_id) {
628+
let item_bounds = tcx.explicit_implied_const_bounds(def_id);
629+
for (predicate, _) in item_bounds.iter_instantiated_copied(tcx, args) {
630+
let predicate = replace_opaques_in(
631+
predicate.to_host_effect_clause(self.tcx, ty::BoundConstness::Maybe),
632+
goals,
633+
);
634+
635+
// Require that the predicate holds for the concrete type.
636+
debug!(?predicate);
637+
goals.push(Goal::new(self.tcx, param_env, predicate));
638+
}
639+
}
621640
}
622641
}

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ provide! { tcx, def_id, other, cdata,
275275
defaultness => { table_direct }
276276
constness => { table_direct }
277277
const_conditions => { table }
278-
implied_const_bounds => { table_defaulted_array }
278+
explicit_implied_const_bounds => { table_defaulted_array }
279279
coerce_unsized_info => {
280280
Ok(cdata
281281
.root

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,8 +1463,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
14631463
record_array!(self.tables.module_children_non_reexports[def_id] <-
14641464
module_children.iter().map(|child| child.res.def_id().index));
14651465
if self.tcx.is_const_trait(def_id) {
1466-
record_defaulted_array!(self.tables.implied_const_bounds[def_id]
1467-
<- self.tcx.implied_const_bounds(def_id).skip_binder());
1466+
record_defaulted_array!(self.tables.explicit_implied_const_bounds[def_id]
1467+
<- self.tcx.explicit_implied_const_bounds(def_id).skip_binder());
14681468
}
14691469
}
14701470
if let DefKind::TraitAlias = def_kind {
@@ -1532,6 +1532,10 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
15321532
self.encode_explicit_item_super_predicates(def_id);
15331533
record!(self.tables.opaque_ty_origin[def_id] <- self.tcx.opaque_ty_origin(def_id));
15341534
self.encode_precise_capturing_args(def_id);
1535+
if tcx.is_conditionally_const(def_id) {
1536+
record_defaulted_array!(self.tables.explicit_implied_const_bounds[def_id]
1537+
<- tcx.explicit_implied_const_bounds(def_id).skip_binder());
1538+
}
15351539
}
15361540
if tcx.impl_method_has_trait_impl_trait_tys(def_id)
15371541
&& let Ok(table) = self.tcx.collect_return_position_impl_trait_in_trait_tys(def_id)
@@ -1654,8 +1658,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
16541658
self.encode_explicit_item_bounds(def_id);
16551659
self.encode_explicit_item_super_predicates(def_id);
16561660
if tcx.is_conditionally_const(def_id) {
1657-
record_defaulted_array!(self.tables.implied_const_bounds[def_id]
1658-
<- self.tcx.implied_const_bounds(def_id).skip_binder());
1661+
record_defaulted_array!(self.tables.explicit_implied_const_bounds[def_id]
1662+
<- self.tcx.explicit_implied_const_bounds(def_id).skip_binder());
16591663
}
16601664
}
16611665
}

compiler/rustc_metadata/src/rmeta/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ define_tables! {
391391
inferred_outlives_of: Table<DefIndex, LazyArray<(ty::Clause<'static>, Span)>>,
392392
explicit_super_predicates_of: Table<DefIndex, LazyArray<(ty::Clause<'static>, Span)>>,
393393
explicit_implied_predicates_of: Table<DefIndex, LazyArray<(ty::Clause<'static>, Span)>>,
394-
implied_const_bounds: Table<DefIndex, LazyArray<(ty::PolyTraitRef<'static>, Span)>>,
394+
explicit_implied_const_bounds: Table<DefIndex, LazyArray<(ty::PolyTraitRef<'static>, Span)>>,
395395
inherent_impls: Table<DefIndex, LazyArray<DefIndex>>,
396396
associated_types_for_impl_traits_in_associated_fn: Table<DefIndex, LazyArray<DefId>>,
397397
opt_rpitit_info: Table<DefIndex, Option<LazyValue<ty::ImplTraitInTraitData>>>,

compiler/rustc_middle/src/query/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ rustc_queries! {
697697
separate_provide_extern
698698
}
699699

700-
query implied_const_bounds(
700+
query explicit_implied_const_bounds(
701701
key: DefId
702702
) -> ty::EarlyBinder<'tcx, &'tcx [(ty::PolyTraitRef<'tcx>, Span)]> {
703703
desc { |tcx| "computing the implied `~const` bounds for `{}`",

compiler/rustc_middle/src/ty/context.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,12 +393,12 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
393393
)
394394
}
395395

396-
fn implied_const_bounds(
396+
fn explicit_implied_const_bounds(
397397
self,
398398
def_id: DefId,
399399
) -> ty::EarlyBinder<'tcx, impl IntoIterator<Item = ty::Binder<'tcx, ty::TraitRef<'tcx>>>> {
400400
ty::EarlyBinder::bind(
401-
self.implied_const_bounds(def_id).iter_identity_copied().map(|(c, _)| c),
401+
self.explicit_implied_const_bounds(def_id).iter_identity_copied().map(|(c, _)| c),
402402
)
403403
}
404404

0 commit comments

Comments
 (0)