Skip to content

Commit 824e444

Browse files
committed
Allow defining opaque types when checking const equality bounds
1 parent a8a7eed commit 824e444

File tree

4 files changed

+61
-23
lines changed

4 files changed

+61
-23
lines changed

compiler/rustc_trait_selection/src/traits/fulfill.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,9 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
577577
if let Ok(new_obligations) = infcx
578578
.at(&obligation.cause, obligation.param_env)
579579
.trace(c1, c2)
580-
.eq(DefineOpaqueTypes::No, a.args, b.args)
580+
// Can define opaque types as this is only reachable with
581+
// `generic_const_exprs`
582+
.eq(DefineOpaqueTypes::Yes, a.args, b.args)
581583
{
582584
return ProcessResult::Changed(mk_pending(
583585
new_obligations.into_obligations(),
@@ -588,7 +590,9 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
588590
(_, _) => {
589591
if let Ok(new_obligations) = infcx
590592
.at(&obligation.cause, obligation.param_env)
591-
.eq(DefineOpaqueTypes::No, c1, c2)
593+
// Can define opaque types as this is only reachable with
594+
// `generic_const_exprs`
595+
.eq(DefineOpaqueTypes::Yes, c1, c2)
592596
{
593597
return ProcessResult::Changed(mk_pending(
594598
new_obligations.into_obligations(),
@@ -629,7 +633,9 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
629633
match (evaluate(c1), evaluate(c2)) {
630634
(Ok(c1), Ok(c2)) => {
631635
match self.selcx.infcx.at(&obligation.cause, obligation.param_env).eq(
632-
DefineOpaqueTypes::No,
636+
// Can define opaque types as this is only reachable with
637+
// `generic_const_exprs`
638+
DefineOpaqueTypes::Yes,
633639
c1,
634640
c2,
635641
) {

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
922922
.infcx
923923
.at(&obligation.cause, obligation.param_env)
924924
.trace(c1, c2)
925-
.eq(DefineOpaqueTypes::No, a.args, b.args)
925+
// Can define opaque types as this is only reachable with
926+
// `generic_const_exprs`
927+
.eq(DefineOpaqueTypes::Yes, a.args, b.args)
926928
{
927929
return self.evaluate_predicates_recursively(
928930
previous_stack,
@@ -935,7 +937,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
935937
if let Ok(InferOk { obligations, value: () }) = self
936938
.infcx
937939
.at(&obligation.cause, obligation.param_env)
938-
.eq(DefineOpaqueTypes::No, c1, c2)
940+
// Can define opaque types as this is only reachable with
941+
// `generic_const_exprs`
942+
.eq(DefineOpaqueTypes::Yes, c1, c2)
939943
{
940944
return self.evaluate_predicates_recursively(
941945
previous_stack,
@@ -965,7 +969,9 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
965969
match (evaluate(c1), evaluate(c2)) {
966970
(Ok(c1), Ok(c2)) => {
967971
match self.infcx.at(&obligation.cause, obligation.param_env).eq(
968-
DefineOpaqueTypes::No,
972+
// Can define opaque types as this is only reachable with
973+
// `generic_const_exprs`
974+
DefineOpaqueTypes::Yes,
969975
c1,
970976
c2,
971977
) {

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

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

+43-16
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)