Skip to content

Commit 0183d92

Browse files
committed
Allow defining opaque types when checking const equality bounds
1 parent 29fba9f commit 0183d92

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

compiler/rustc_trait_selection/src/traits/fulfill.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,9 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
572572
if let Ok(new_obligations) = infcx
573573
.at(&obligation.cause, obligation.param_env)
574574
.trace(c1, c2)
575-
.eq(DefineOpaqueTypes::No, a.args, b.args)
575+
// Can define opaque types as this is only reachable with
576+
// `generic_const_exprs`
577+
.eq(DefineOpaqueTypes::Yes, a.args, b.args)
576578
{
577579
return ProcessResult::Changed(mk_pending(
578580
new_obligations.into_obligations(),
@@ -583,7 +585,9 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
583585
(_, _) => {
584586
if let Ok(new_obligations) = infcx
585587
.at(&obligation.cause, obligation.param_env)
586-
.eq(DefineOpaqueTypes::No, c1, c2)
588+
// Can define opaque types as this is only reachable with
589+
// `generic_const_exprs`
590+
.eq(DefineOpaqueTypes::Yes, c1, c2)
587591
{
588592
return ProcessResult::Changed(mk_pending(
589593
new_obligations.into_obligations(),
@@ -624,7 +628,9 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
624628
match (evaluate(c1), evaluate(c2)) {
625629
(Ok(c1), Ok(c2)) => {
626630
match self.selcx.infcx.at(&obligation.cause, obligation.param_env).eq(
627-
DefineOpaqueTypes::No,
631+
// Can define opaque types as this is only reachable with
632+
// `generic_const_exprs`
633+
DefineOpaqueTypes::Yes,
628634
c1,
629635
c2,
630636
) {

compiler/rustc_trait_selection/src/traits/select/mod.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
906906
.infcx
907907
.at(&obligation.cause, obligation.param_env)
908908
.trace(c1, c2)
909-
.eq(DefineOpaqueTypes::No, a.args, b.args)
909+
// Can define opaque types as this is only reachable with
910+
// `generic_const_exprs`
911+
.eq(DefineOpaqueTypes::Yes, a.args, b.args)
910912
{
911913
return self.evaluate_predicates_recursively(
912914
previous_stack,
@@ -919,7 +921,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
919921
if let Ok(InferOk { obligations, value: () }) = self
920922
.infcx
921923
.at(&obligation.cause, obligation.param_env)
922-
.eq(DefineOpaqueTypes::No, c1, c2)
924+
// Can define opaque types as this is only reachable with
925+
// `generic_const_exprs`
926+
.eq(DefineOpaqueTypes::Yes, c1, c2)
923927
{
924928
return self.evaluate_predicates_recursively(
925929
previous_stack,
@@ -949,7 +953,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
949953
match (evaluate(c1), evaluate(c2)) {
950954
(Ok(c1), Ok(c2)) => {
951955
match self.infcx.at(&obligation.cause, obligation.param_env).eq(
952-
DefineOpaqueTypes::No,
956+
// Can define opaque types as this is only reachable with
957+
// `generic_const_exprs`
958+
DefineOpaqueTypes::Yes,
953959
c1,
954960
c2,
955961
) {

tests/ui/const-generics/generic_const_exprs/opaque_type.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
#![allow(incomplete_features)]
33

44
type Foo = impl Sized;
5-
//~^ ERROR: cycle detected
65

76
fn with_bound<const N: usize>() -> Foo
87
where
98
[u8; (N / 2) as usize]: Sized,
109
{
1110
let _: [u8; (N / 2) as Foo] = [0; (N / 2) as usize];
12-
//~^ ERROR: mismatched types
11+
//~^ ERROR mismatched types
12+
//~| ERROR non-primitive cast: `usize` as `Foo`
1313
todo!()
1414
}
1515

tests/ui/const-generics/generic_const_exprs/opaque_type.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0308]: mismatched types
2-
--> $DIR/opaque_type.rs:11:17
2+
--> $DIR/opaque_type.rs:10:17
33
|
44
LL | type Foo = impl Sized;
55
| ---------- the found opaque type
@@ -11,7 +11,7 @@ LL | let _: [u8; (N / 2) as Foo] = [0; (N / 2) as usize];
1111
found opaque type `Foo`
1212

1313
error[E0605]: non-primitive cast: `usize` as `Foo`
14-
--> $DIR/opaque_type.rs:11:17
14+
--> $DIR/opaque_type.rs:10:17
1515
|
1616
LL | let _: [u8; (N / 2) as Foo] = [0; (N / 2) as usize];
1717
| ^^^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object

0 commit comments

Comments
 (0)