Skip to content

Commit d340ea5

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

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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 bar(&self) -> [u8; N];
9+
//~^ ERROR: the constant `N` is not of type `usize`
10+
}
11+
12+
fn main() {}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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: the constant `N` is not of type `usize`
10+
--> $DIR/ice-148622-opaque-as-const-generics.rs:8:22
11+
|
12+
LL | fn bar(&self) -> [u8; N];
13+
| ^^^^^^^ expected `usize`, found future
14+
|
15+
note: this item must have a `#[define_opaque(Opaque)]` attribute to be able to define hidden types
16+
--> $DIR/ice-148622-opaque-as-const-generics.rs:8:8
17+
|
18+
LL | fn bar(&self) -> [u8; N];
19+
| ^^^
20+
= note: the length of array `[u8; N]` must be type `usize`
21+
22+
error: unconstrained opaque type
23+
--> $DIR/ice-148622-opaque-as-const-generics.rs:3:19
24+
|
25+
LL | pub type Opaque = impl std::future::Future;
26+
| ^^^^^^^^^^^^^^^^^^^^^^^^
27+
|
28+
= note: `Opaque` must be used in combination with a concrete type within the same crate
29+
30+
error: aborting due to 3 previous errors
31+

0 commit comments

Comments
 (0)