Skip to content

Commit 8e226e0

Browse files
committed
Add some regression tests for opaque types and const generics
1 parent ba316a9 commit 8e226e0

File tree

6 files changed

+174
-2
lines changed

6 files changed

+174
-2
lines changed

compiler/rustc_trait_selection/src/traits/fulfill.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,8 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
429429
// as the cause of an overflow.
430430
ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(ct, ty)) => {
431431
match self.selcx.infcx.at(&obligation.cause, obligation.param_env).eq(
432-
DefineOpaqueTypes::No,
432+
// Only really excercised by generic_const_exprs
433+
DefineOpaqueTypes::Yes,
433434
ct.ty(),
434435
ty,
435436
) {

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
982982
ty::PredicateKind::Ambiguous => Ok(EvaluatedToAmbig),
983983
ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType(ct, ty)) => {
984984
match self.infcx.at(&obligation.cause, obligation.param_env).eq(
985-
DefineOpaqueTypes::No,
985+
// Only really excercised by generic_const_exprs
986+
DefineOpaqueTypes::Yes,
986987
ct.ty(),
987988
ty,
988989
) {
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![feature(type_alias_impl_trait)]
2+
3+
type Foo = impl Sized;
4+
//~^ ERROR: cycle
5+
//~| ERROR: cycle
6+
7+
fn foo<const C: Foo>() {}
8+
//~^ ERROR: `Foo` is forbidden as the type of a const generic parameter
9+
10+
fn main() {
11+
foo::<42>();
12+
//~^ ERROR: mismatched types
13+
}
+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/opaque_types.rs:11:11
3+
|
4+
LL | type Foo = impl Sized;
5+
| ---------- the expected opaque type
6+
...
7+
LL | foo::<42>();
8+
| ^^ expected opaque type, found integer
9+
|
10+
= note: expected opaque type `Foo`
11+
found type `{integer}`
12+
13+
error[E0391]: cycle detected when computing type of `Foo::{opaque#0}`
14+
--> $DIR/opaque_types.rs:3:12
15+
|
16+
LL | type Foo = impl Sized;
17+
| ^^^^^^^^^^
18+
|
19+
note: ...which requires computing type of opaque `Foo::{opaque#0}`...
20+
--> $DIR/opaque_types.rs:3:12
21+
|
22+
LL | type Foo = impl Sized;
23+
| ^^^^^^^^^^
24+
note: ...which requires type-checking `main`...
25+
--> $DIR/opaque_types.rs:10:1
26+
|
27+
LL | fn main() {
28+
| ^^^^^^^^^
29+
note: ...which requires evaluating type-level constant...
30+
--> $DIR/opaque_types.rs:11:11
31+
|
32+
LL | foo::<42>();
33+
| ^^
34+
note: ...which requires const-evaluating + checking `main::{constant#0}`...
35+
--> $DIR/opaque_types.rs:11:11
36+
|
37+
LL | foo::<42>();
38+
| ^^
39+
note: ...which requires caching mir of `main::{constant#0}` for CTFE...
40+
--> $DIR/opaque_types.rs:11:11
41+
|
42+
LL | foo::<42>();
43+
| ^^
44+
note: ...which requires elaborating drops for `main::{constant#0}`...
45+
--> $DIR/opaque_types.rs:11:11
46+
|
47+
LL | foo::<42>();
48+
| ^^
49+
= note: ...which requires normalizing `Foo`...
50+
= note: ...which again requires computing type of `Foo::{opaque#0}`, completing the cycle
51+
note: cycle used when checking that `Foo::{opaque#0}` is well-formed
52+
--> $DIR/opaque_types.rs:3:12
53+
|
54+
LL | type Foo = impl Sized;
55+
| ^^^^^^^^^^
56+
= 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
57+
58+
error: `Foo` is forbidden as the type of a const generic parameter
59+
--> $DIR/opaque_types.rs:7:17
60+
|
61+
LL | fn foo<const C: Foo>() {}
62+
| ^^^
63+
|
64+
= note: the only supported types are integers, `bool` and `char`
65+
66+
error[E0391]: cycle detected when computing type of opaque `Foo::{opaque#0}`
67+
--> $DIR/opaque_types.rs:3:12
68+
|
69+
LL | type Foo = impl Sized;
70+
| ^^^^^^^^^^
71+
|
72+
note: ...which requires type-checking `main`...
73+
--> $DIR/opaque_types.rs:10:1
74+
|
75+
LL | fn main() {
76+
| ^^^^^^^^^
77+
note: ...which requires evaluating type-level constant...
78+
--> $DIR/opaque_types.rs:11:11
79+
|
80+
LL | foo::<42>();
81+
| ^^
82+
note: ...which requires const-evaluating + checking `main::{constant#0}`...
83+
--> $DIR/opaque_types.rs:11:11
84+
|
85+
LL | foo::<42>();
86+
| ^^
87+
note: ...which requires caching mir of `main::{constant#0}` for CTFE...
88+
--> $DIR/opaque_types.rs:11:11
89+
|
90+
LL | foo::<42>();
91+
| ^^
92+
note: ...which requires elaborating drops for `main::{constant#0}`...
93+
--> $DIR/opaque_types.rs:11:11
94+
|
95+
LL | foo::<42>();
96+
| ^^
97+
note: ...which requires borrow-checking `main::{constant#0}`...
98+
--> $DIR/opaque_types.rs:11:11
99+
|
100+
LL | foo::<42>();
101+
| ^^
102+
note: ...which requires promoting constants in MIR for `main::{constant#0}`...
103+
--> $DIR/opaque_types.rs:11:11
104+
|
105+
LL | foo::<42>();
106+
| ^^
107+
note: ...which requires const checking `main::{constant#0}`...
108+
--> $DIR/opaque_types.rs:11:11
109+
|
110+
LL | foo::<42>();
111+
| ^^
112+
= note: ...which requires computing whether `Foo` is freeze...
113+
= note: ...which requires evaluating trait selection obligation `Foo: core::marker::Freeze`...
114+
= note: ...which again requires computing type of opaque `Foo::{opaque#0}`, completing the cycle
115+
note: cycle used when computing type of `Foo::{opaque#0}`
116+
--> $DIR/opaque_types.rs:3:12
117+
|
118+
LL | type Foo = impl Sized;
119+
| ^^^^^^^^^^
120+
= 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
121+
122+
error: aborting due to 4 previous errors
123+
124+
Some errors have detailed explanations: E0308, E0391.
125+
For more information about an error, try `rustc --explain E0308`.
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#![feature(type_alias_impl_trait)]
2+
3+
type Foo = impl Sized;
4+
5+
fn foo<const C: u32>() {}
6+
7+
const C: Foo = 42;
8+
9+
fn bar()
10+
where
11+
Foo:,
12+
{
13+
foo::<C>();
14+
//~^ ERROR: mismatched types
15+
}
16+
17+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/opaque_types2.rs:13:11
3+
|
4+
LL | type Foo = impl Sized;
5+
| ---------- the found opaque type
6+
...
7+
LL | foo::<C>();
8+
| ^ expected `u32`, found opaque type
9+
|
10+
= note: expected type `u32`
11+
found opaque type `Foo`
12+
13+
error: aborting due to 1 previous error
14+
15+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)