Skip to content

Commit 00d46d2

Browse files
authored
Unrolled build for rust-lang#133218
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 318f96a + 5eeaf2e commit 00d46d2

File tree

20 files changed

+144
-31
lines changed

20 files changed

+144
-31
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

+2
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

+1-1
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

+9-1
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

+10-3
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

+23-4
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

+1-1
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

+8-4
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

+1-1
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

+1-1
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

+2-2
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

compiler/rustc_middle/src/ty/mod.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -2110,7 +2110,13 @@ impl<'tcx> TyCtxt<'tcx> {
21102110
_ => bug!("unexpected parent item of associated item: {parent_def_id:?}"),
21112111
}
21122112
}
2113-
DefKind::Closure | DefKind::OpaqueTy => {
2113+
DefKind::OpaqueTy => match self.opaque_ty_origin(def_id) {
2114+
hir::OpaqueTyOrigin::FnReturn { parent, .. } => self.is_conditionally_const(parent),
2115+
hir::OpaqueTyOrigin::AsyncFn { .. } => false,
2116+
// FIXME(const_trait_impl): ATPITs could be conditionally const?
2117+
hir::OpaqueTyOrigin::TyAlias { .. } => false,
2118+
},
2119+
DefKind::Closure => {
21142120
// Closures and RPITs will eventually have const conditions
21152121
// for `~const` bounds.
21162122
false

compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ where
102102

103103
/// Assemble additional assumptions for an alias that are not included
104104
/// in the item bounds of the alias. For now, this is limited to the
105-
/// `implied_const_bounds` for an associated type.
105+
/// `explicit_implied_const_bounds` for an associated type.
106106
fn consider_additional_alias_assumptions(
107107
ecx: &mut EvalCtxt<'_, D>,
108108
goal: Goal<I, Self>,

compiler/rustc_next_trait_solver/src/solve/effect_goals.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,9 @@ where
8484
let cx = ecx.cx();
8585
let mut candidates = vec![];
8686

87-
// FIXME(const_trait_impl): We elaborate here because the implied const bounds
88-
// aren't necessarily elaborated. We probably should prefix this query
89-
// with `explicit_`...
9087
for clause in elaborate::elaborate(
9188
cx,
92-
cx.implied_const_bounds(alias_ty.def_id)
89+
cx.explicit_implied_const_bounds(alias_ty.def_id)
9390
.iter_instantiated(cx, alias_ty.args)
9491
.map(|trait_ref| trait_ref.to_host_effect_clause(cx, goal.predicate.constness)),
9592
) {

compiler/rustc_type_ir/src/elaborate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ impl<I: Interner, O: Elaboratable<I>> Elaborator<I, O> {
157157
}
158158
// `T: ~const Trait` implies `T: ~const Supertrait`.
159159
ty::ClauseKind::HostEffect(data) => self.extend_deduped(
160-
cx.implied_const_bounds(data.def_id()).iter_identity().map(|trait_ref| {
160+
cx.explicit_implied_const_bounds(data.def_id()).iter_identity().map(|trait_ref| {
161161
elaboratable.child(
162162
trait_ref
163163
.to_host_effect_clause(cx, data.constness)

compiler/rustc_type_ir/src/interner.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ pub trait Interner:
229229
self,
230230
def_id: Self::DefId,
231231
) -> ty::EarlyBinder<Self, impl IntoIterator<Item = ty::Binder<Self, ty::TraitRef<Self>>>>;
232-
fn implied_const_bounds(
232+
fn explicit_implied_const_bounds(
233233
self,
234234
def_id: Self::DefId,
235235
) -> ty::EarlyBinder<Self, impl IntoIterator<Item = ty::Binder<Self, ty::TraitRef<Self>>>>;

tests/ui/traits/const-traits/const-closure-parse-not-item.stderr

+9-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,13 @@ LL | const fn test() -> impl ~const Fn() {
1212
|
1313
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
1414

15-
error: aborting due to 2 previous errors
15+
error: `~const` can only be applied to `#[const_trait]` traits
16+
--> $DIR/const-closure-parse-not-item.rs:7:25
17+
|
18+
LL | const fn test() -> impl ~const Fn() {
19+
| ^^^^^^
20+
|
21+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
22+
23+
error: aborting due to 3 previous errors
1624

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0277]: the trait bound `(): const Foo` is not satisfied
2+
--> $DIR/const-opaque.rs:31:18
3+
|
4+
LL | let opaque = bar(());
5+
| ^^^^^^^
6+
7+
error[E0277]: the trait bound `(): const Foo` is not satisfied
8+
--> $DIR/const-opaque.rs:33:5
9+
|
10+
LL | opaque.method();
11+
| ^^^^^^^^^^^^^^^
12+
13+
error: aborting due to 2 previous errors
14+
15+
For more information about this error, try `rustc --explain E0277`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//@ revisions: yes no
2+
//@ compile-flags: -Znext-solver
3+
//@[yes] check-pass
4+
5+
#![feature(const_trait_impl)]
6+
7+
#[const_trait]
8+
trait Foo {
9+
fn method(&self);
10+
}
11+
12+
impl<T: ~const Foo> const Foo for (T,) {
13+
fn method(&self) {}
14+
}
15+
16+
#[cfg(yes)]
17+
impl const Foo for () {
18+
fn method(&self) {}
19+
}
20+
21+
#[cfg(no)]
22+
impl Foo for () {
23+
fn method(&self) {}
24+
}
25+
26+
const fn bar<T: ~const Foo>(t: T) -> impl ~const Foo {
27+
(t,)
28+
}
29+
30+
const _: () = {
31+
let opaque = bar(());
32+
//[no]~^ ERROR the trait bound `(): const Foo` is not satisfied
33+
opaque.method();
34+
//[no]~^ ERROR the trait bound `(): const Foo` is not satisfied
35+
std::mem::forget(opaque);
36+
};
37+
38+
fn main() {}

tests/ui/traits/const-traits/effects/ice-112822-expected-type-for-param.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const fn test() -> impl ~const Fn() {
44
//~^ ERROR `~const` can only be applied to `#[const_trait]` traits
55
//~| ERROR `~const` can only be applied to `#[const_trait]` traits
6+
//~| ERROR `~const` can only be applied to `#[const_trait]` traits
67
const move || { //~ ERROR const closures are experimental
78
let sl: &[u8] = b"foo";
89

tests/ui/traits/const-traits/effects/ice-112822-expected-type-for-param.stderr

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0658]: const closures are experimental
2-
--> $DIR/ice-112822-expected-type-for-param.rs:6:5
2+
--> $DIR/ice-112822-expected-type-for-param.rs:7:5
33
|
44
LL | const move || {
55
| ^^^^^
@@ -22,8 +22,16 @@ LL | const fn test() -> impl ~const Fn() {
2222
|
2323
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
2424

25+
error: `~const` can only be applied to `#[const_trait]` traits
26+
--> $DIR/ice-112822-expected-type-for-param.rs:3:25
27+
|
28+
LL | const fn test() -> impl ~const Fn() {
29+
| ^^^^^^
30+
|
31+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
32+
2533
error[E0015]: cannot call non-const operator in constant functions
26-
--> $DIR/ice-112822-expected-type-for-param.rs:11:17
34+
--> $DIR/ice-112822-expected-type-for-param.rs:12:17
2735
|
2836
LL | assert_eq!(first, &b'f');
2937
| ^^^^^^^^^^^^^^^^^^^^^^^^
@@ -32,15 +40,15 @@ LL | assert_eq!(first, &b'f');
3240
= note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
3341

3442
error[E0015]: cannot call non-const fn `core::panicking::assert_failed::<&u8, &u8>` in constant functions
35-
--> $DIR/ice-112822-expected-type-for-param.rs:11:17
43+
--> $DIR/ice-112822-expected-type-for-param.rs:12:17
3644
|
3745
LL | assert_eq!(first, &b'f');
3846
| ^^^^^^^^^^^^^^^^^^^^^^^^
3947
|
4048
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
4149
= note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
4250

43-
error: aborting due to 5 previous errors
51+
error: aborting due to 6 previous errors
4452

4553
Some errors have detailed explanations: E0015, E0658.
4654
For more information about an error, try `rustc --explain E0015`.

0 commit comments

Comments
 (0)