Skip to content

Commit e0f1684

Browse files
committed
Fix ICE when collecting opaques from trait method declarations
1 parent 683dd08 commit e0f1684

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

compiler/rustc_ty_utils/src/opaque_types.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ impl<'tcx> OpaqueTypeCollector<'tcx> {
6464

6565
#[instrument(level = "trace", skip(self))]
6666
fn collect_taits_declared_in_body(&mut self) {
67-
let body = self.tcx.hir_body_owned_by(self.item).value;
67+
let Some(body) = self.tcx.hir_maybe_body_owned_by(self.item) else {
68+
return;
69+
};
70+
let body = body.value;
6871
struct TaitInBodyFinder<'a, 'tcx> {
6972
collector: &'a mut OpaqueTypeCollector<'tcx>,
7073
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#![feature(type_alias_impl_trait)]
2+
3+
pub type Opaque = impl std::future::Future;
4+
//~^ ERROR: unconstrained opaque type
5+
6+
trait Foo<const N: Opaque> {
7+
//~^ ERROR: `Opaque` is forbidden as the type of a const generic parameter
8+
fn do_x(&self) -> [Opaque; N];
9+
//~^ ERROR: the constant `N` is not of type `usize`
10+
}
11+
12+
struct Bar;
13+
14+
impl Foo<3> for Bar {
15+
//~^ ERROR: mismatched types
16+
fn do_x(&self) -> [u8; 3] {
17+
[0u8; 3]
18+
}
19+
}
20+
21+
fn main() {}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
error: `Opaque` is forbidden as the type of a const generic parameter
2+
--> $DIR/ice-148622-opaque-as-const-generics.rs:6:20
3+
|
4+
LL | trait Foo<const N: Opaque> {
5+
| ^^^^^^
6+
|
7+
= note: the only supported types are integers, `bool`, and `char`
8+
9+
error[E0308]: mismatched types
10+
--> $DIR/ice-148622-opaque-as-const-generics.rs:14:10
11+
|
12+
LL | pub type Opaque = impl std::future::Future;
13+
| ------------------------ the expected future
14+
...
15+
LL | impl Foo<3> for Bar {
16+
| ^ expected future, found integer
17+
|
18+
= note: expected opaque type `Opaque`
19+
found type `{integer}`
20+
21+
error: unconstrained opaque type
22+
--> $DIR/ice-148622-opaque-as-const-generics.rs:3:19
23+
|
24+
LL | pub type Opaque = impl std::future::Future;
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^
26+
|
27+
= note: `Opaque` must be used in combination with a concrete type within the same crate
28+
29+
error: the constant `N` is not of type `usize`
30+
--> $DIR/ice-148622-opaque-as-const-generics.rs:8:23
31+
|
32+
LL | fn do_x(&self) -> [Opaque; N];
33+
| ^^^^^^^^^^^ expected `usize`, found future
34+
|
35+
note: this item must have a `#[define_opaque(Opaque)]` attribute to be able to define hidden types
36+
--> $DIR/ice-148622-opaque-as-const-generics.rs:8:8
37+
|
38+
LL | fn do_x(&self) -> [Opaque; N];
39+
| ^^^^
40+
= note: the length of array `[Opaque; N]` must be type `usize`
41+
42+
error: aborting due to 4 previous errors
43+
44+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)