Skip to content

Commit 2cbcfae

Browse files
authored
Rollup merge of #84913 - estebank:issue-84831, r=varkor
Do not ICE on invalid const param When encountering a path that can't have generics, do not call `generics_of`. This would happen when writing something like `path::this_is_a_mod<const_val>`. Fix #84831.
2 parents 2c7bf41 + 11379f0 commit 2cbcfae

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

compiler/rustc_typeck/src/collect/type_of.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,25 @@ pub(super) fn opt_const_param_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<
191191
Res::Def(DefKind::Ctor(..), def_id) => {
192192
tcx.generics_of(tcx.parent(def_id).unwrap())
193193
}
194-
Res::Def(_, def_id) => tcx.generics_of(def_id),
194+
// Other `DefKind`s don't have generics and would ICE when calling
195+
// `generics_of`.
196+
Res::Def(
197+
DefKind::Struct
198+
| DefKind::Union
199+
| DefKind::Enum
200+
| DefKind::Variant
201+
| DefKind::Trait
202+
| DefKind::OpaqueTy
203+
| DefKind::TyAlias
204+
| DefKind::ForeignTy
205+
| DefKind::TraitAlias
206+
| DefKind::AssocTy
207+
| DefKind::Fn
208+
| DefKind::AssocFn
209+
| DefKind::AssocConst
210+
| DefKind::Impl,
211+
def_id,
212+
) => tcx.generics_of(def_id),
195213
Res::Err => {
196214
tcx.sess.delay_span_bug(tcx.def_span(def_id), "anon const with Res::Err");
197215
return None;

src/test/ui/typeck/issue-84831.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fn f() {
2+
std::<0>; //~ ERROR expected value
3+
}
4+
fn j() {
5+
std::<_ as _>; //~ ERROR expected value
6+
//~^ ERROR expected one of `,` or `>`, found keyword `as`
7+
}
8+
9+
fn main () {}

src/test/ui/typeck/issue-84831.stderr

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error: expected one of `,` or `>`, found keyword `as`
2+
--> $DIR/issue-84831.rs:5:13
3+
|
4+
LL | std::<_ as _>;
5+
| ^^ expected one of `,` or `>`
6+
|
7+
help: expressions must be enclosed in braces to be used as const generic arguments
8+
|
9+
LL | std::<{ _ as _ }>;
10+
| ^ ^
11+
12+
error[E0423]: expected value, found crate `std`
13+
--> $DIR/issue-84831.rs:2:5
14+
|
15+
LL | std::<0>;
16+
| ^^^^^^^^ not a value
17+
18+
error[E0423]: expected value, found crate `std`
19+
--> $DIR/issue-84831.rs:5:5
20+
|
21+
LL | std::<_ as _>;
22+
| ^^^^^^^^^^^^^ not a value
23+
24+
error: aborting due to 3 previous errors
25+
26+
For more information about this error, try `rustc --explain E0423`.

0 commit comments

Comments
 (0)