Skip to content

Commit e85e8fc

Browse files
committed
Allow defining opaque types when checking const equality bounds
1 parent a6b39c1 commit e85e8fc

File tree

4 files changed

+61
-23
lines changed

4 files changed

+61
-23
lines changed

compiler/rustc_trait_selection/src/traits/fulfill.rs

Lines changed: 9 additions & 3 deletions
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

Lines changed: 9 additions & 3 deletions
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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ where
99
[u8; (N / 2) as usize]: Sized,
1010
{
1111
let _: [u8; (N / 2) as Foo] = [0; (N / 2) as usize];
12-
//~^ ERROR: mismatched types
1312
todo!()
1413
}
1514

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

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,48 @@ note: ...which requires computing type of opaque `Foo::{opaque#0}`...
99
|
1010
LL | type Foo = impl Sized;
1111
| ^^^^^^^^^^
12-
note: ...which requires type-checking `with_bound`...
13-
--> $DIR/opaque_type.rs:11:35
12+
note: ...which requires borrow-checking `with_bound`...
13+
--> $DIR/opaque_type.rs:7:1
1414
|
15-
LL | let _: [u8; (N / 2) as Foo] = [0; (N / 2) as usize];
16-
| ^^^^^^^^^^^^^^^^^^^^^
15+
LL | / fn with_bound<const N: usize>() -> Foo
16+
LL | | where
17+
LL | | [u8; (N / 2) as usize]: Sized,
18+
| |__________________________________^
19+
note: ...which requires promoting constants in MIR for `with_bound`...
20+
--> $DIR/opaque_type.rs:7:1
21+
|
22+
LL | / fn with_bound<const N: usize>() -> Foo
23+
LL | | where
24+
LL | | [u8; (N / 2) as usize]: Sized,
25+
| |__________________________________^
26+
note: ...which requires preparing `with_bound` for borrow checking...
27+
--> $DIR/opaque_type.rs:7:1
28+
|
29+
LL | / fn with_bound<const N: usize>() -> Foo
30+
LL | | where
31+
LL | | [u8; (N / 2) as usize]: Sized,
32+
| |__________________________________^
33+
note: ...which requires checking if `with_bound` contains FFI-unwind calls...
34+
--> $DIR/opaque_type.rs:7:1
35+
|
36+
LL | / fn with_bound<const N: usize>() -> Foo
37+
LL | | where
38+
LL | | [u8; (N / 2) as usize]: Sized,
39+
| |__________________________________^
40+
note: ...which requires building MIR for `with_bound`...
41+
--> $DIR/opaque_type.rs:7:1
42+
|
43+
LL | / fn with_bound<const N: usize>() -> Foo
44+
LL | | where
45+
LL | | [u8; (N / 2) as usize]: Sized,
46+
| |__________________________________^
47+
note: ...which requires match-checking `with_bound`...
48+
--> $DIR/opaque_type.rs:7:1
49+
|
50+
LL | / fn with_bound<const N: usize>() -> Foo
51+
LL | | where
52+
LL | | [u8; (N / 2) as usize]: Sized,
53+
| |__________________________________^
1754
note: ...which requires evaluating type-level constant...
1855
--> $DIR/opaque_type.rs:11:17
1956
|
@@ -43,16 +80,6 @@ LL | type Foo = impl Sized;
4380
| ^^^^^^^^^^
4481
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
4582

46-
error[E0308]: mismatched types
47-
--> $DIR/opaque_type.rs:11:35
48-
|
49-
LL | let _: [u8; (N / 2) as Foo] = [0; (N / 2) as usize];
50-
| ^^^^^^^^^^^^^^^^^^^^^ expected `(N / 2) as Foo`, found `(N / 2) as usize`
51-
|
52-
= note: expected constant `(N / 2) as Foo`
53-
found constant `(N / 2) as usize`
54-
55-
error: aborting due to 2 previous errors
83+
error: aborting due to 1 previous error
5684

57-
Some errors have detailed explanations: E0308, E0391.
58-
For more information about an error, try `rustc --explain E0308`.
85+
For more information about this error, try `rustc --explain E0391`.

0 commit comments

Comments
 (0)