Skip to content

Commit 5d73196

Browse files
committed
Fix ICE in normalizing inherent associated consts with #[type_const]
1 parent c4dc073 commit 5d73196

3 files changed

Lines changed: 59 additions & 1 deletion

File tree

compiler/rustc_trait_selection/src/traits/project.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,13 @@ pub fn compute_inherent_assoc_term_args<'a, 'b, 'tcx>(
586586

587587
// Infer the generic parameters of the impl by unifying the
588588
// impl type with the self type of the projection.
589-
let mut self_ty = alias_term.self_ty();
589+
// For inherent associated consts, the args may not have a Self type at position 0.
590+
// In that case, we use the impl_ty directly instead of calling self_ty().
591+
let mut self_ty = if alias_term.args.is_empty() || alias_term.args[0].as_type().is_none() {
592+
impl_ty
593+
} else {
594+
alias_term.self_ty()
595+
};
590596
if !selcx.infcx.next_trait_solver() {
591597
self_ty = normalize_with_depth_to(
592598
selcx,
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
struct S<const N: usize>;
2+
impl<const N: usize> S<N> {
3+
#[type_const]
4+
//~^ ERROR: the `#[type_const]` attribute is an experimental feature
5+
const LEN: usize = 1;
6+
fn arr() {
7+
[8; Self::LEN]
8+
//~^ ERROR: constant expression depends on a generic parameter
9+
//~| ERROR: constant expression depends on a generic parameter
10+
//~| ERROR: mismatched types
11+
}
12+
}
13+
14+
pub fn main() {}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
error[E0658]: the `#[type_const]` attribute is an experimental feature
2+
--> $DIR/type-const-inherent-impl-normalize.rs:3:5
3+
|
4+
LL | #[type_const]
5+
| ^^^^^^^^^^^^^
6+
|
7+
= note: see issue #132980 <https://github.com/rust-lang/rust/issues/132980> for more information
8+
= help: add `#![feature(min_generic_const_args)]` to the crate attributes to enable
9+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
10+
11+
error: constant expression depends on a generic parameter
12+
--> $DIR/type-const-inherent-impl-normalize.rs:7:13
13+
|
14+
LL | [8; Self::LEN]
15+
| ^^^^^^^^^
16+
|
17+
= note: this may fail depending on what value the parameter takes
18+
19+
error: constant expression depends on a generic parameter
20+
--> $DIR/type-const-inherent-impl-normalize.rs:7:9
21+
|
22+
LL | [8; Self::LEN]
23+
| ^^^^^^^^^^^^^^
24+
|
25+
= note: this may fail depending on what value the parameter takes
26+
27+
error[E0308]: mismatched types
28+
--> $DIR/type-const-inherent-impl-normalize.rs:7:9
29+
|
30+
LL | fn arr() {
31+
| - help: try adding a return type: `-> [i32; Self::LEN]`
32+
LL | [8; Self::LEN]
33+
| ^^^^^^^^^^^^^^ expected `()`, found `[{integer}; Self::LEN]`
34+
35+
error: aborting due to 4 previous errors
36+
37+
Some errors have detailed explanations: E0308, E0658.
38+
For more information about an error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)