Skip to content

Commit c1b4d07

Browse files
authored
Rollup merge of #112506 - compiler-errors:const-infer-ice, r=b-naber
Properly check associated consts for infer placeholders We only reported an error if it was in a "suggestable" position (according to `is_suggestable_infer_ty`) -- this isn't correct for infer tys that can show up in other places in the constant's type, like behind a dyn trait. fixes #112491
2 parents 38ed4e5 + 2b40268 commit c1b4d07

12 files changed

+77
-34
lines changed

compiler/rustc_hir_analysis/src/collect.rs

+16-11
Original file line numberDiff line numberDiff line change
@@ -666,17 +666,15 @@ fn convert_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::TraitItemId) {
666666
tcx.ensure().fn_sig(def_id);
667667
}
668668

669-
hir::TraitItemKind::Const(.., Some(_)) => {
669+
hir::TraitItemKind::Const(ty, body_id) => {
670670
tcx.ensure().type_of(def_id);
671-
}
672-
673-
hir::TraitItemKind::Const(hir_ty, _) => {
674-
tcx.ensure().type_of(def_id);
675-
// Account for `const C: _;`.
676-
let mut visitor = HirPlaceholderCollector::default();
677-
visitor.visit_trait_item(trait_item);
678-
if !tcx.sess.diagnostic().has_stashed_diagnostic(hir_ty.span, StashKey::ItemNoType) {
679-
placeholder_type_error(tcx, None, visitor.0, false, None, "constant");
671+
if !tcx.sess.diagnostic().has_stashed_diagnostic(ty.span, StashKey::ItemNoType)
672+
&& !(is_suggestable_infer_ty(ty) && body_id.is_some())
673+
{
674+
// Account for `const C: _;`.
675+
let mut visitor = HirPlaceholderCollector::default();
676+
visitor.visit_trait_item(trait_item);
677+
placeholder_type_error(tcx, None, visitor.0, false, None, "associated constant");
680678
}
681679
}
682680

@@ -721,7 +719,14 @@ fn convert_impl_item(tcx: TyCtxt<'_>, impl_item_id: hir::ImplItemId) {
721719

722720
placeholder_type_error(tcx, None, visitor.0, false, None, "associated type");
723721
}
724-
hir::ImplItemKind::Const(..) => {}
722+
hir::ImplItemKind::Const(ty, _) => {
723+
// Account for `const T: _ = ..;`
724+
if !is_suggestable_infer_ty(ty) {
725+
let mut visitor = HirPlaceholderCollector::default();
726+
visitor.visit_impl_item(impl_item);
727+
placeholder_type_error(tcx, None, visitor.0, false, None, "associated constant");
728+
}
729+
}
725730
}
726731
}
727732

compiler/rustc_hir_analysis/src/collect/type_of.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,12 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<Ty
341341
.and_then(|body_id| {
342342
is_suggestable_infer_ty(ty).then(|| {
343343
infer_placeholder_type(
344-
tcx, def_id, body_id, ty.span, item.ident, "constant",
344+
tcx,
345+
def_id,
346+
body_id,
347+
ty.span,
348+
item.ident,
349+
"associated constant",
345350
)
346351
})
347352
})
@@ -359,7 +364,14 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<Ty
359364
}
360365
ImplItemKind::Const(ty, body_id) => {
361366
if is_suggestable_infer_ty(ty) {
362-
infer_placeholder_type(tcx, def_id, body_id, ty.span, item.ident, "constant")
367+
infer_placeholder_type(
368+
tcx,
369+
def_id,
370+
body_id,
371+
ty.span,
372+
item.ident,
373+
"associated constant",
374+
)
363375
} else {
364376
icx.to_ty(ty)
365377
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
trait Trait {
2+
const ASSOC: i32;
3+
}
4+
5+
impl Trait for () {
6+
const ASSOC: &dyn Fn(_) = 1i32;
7+
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for associated constants
8+
}
9+
10+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated constants
2+
--> $DIR/infer-placeholder-in-non-suggestable-pos.rs:6:26
3+
|
4+
LL | const ASSOC: &dyn Fn(_) = 1i32;
5+
| ^ not allowed in type signatures
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0121`.

tests/ui/const-generics/generic_arg_infer/in-signature.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ static TY_STATIC_MIXED: Bar<_, _> = Bar::<i32, 3>(0);
3333
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for static variables
3434
trait ArrAssocConst {
3535
const ARR: [u8; _];
36-
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for constants
36+
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for associated constants
3737
}
3838
trait TyAssocConst {
3939
const ARR: Bar<i32, _>;
40-
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for constants
40+
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for associated constants
4141
}
4242
trait TyAssocConstMixed {
4343
const ARR: Bar<_, _>;
44-
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for constants
44+
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for associated constants
4545
}
4646

4747
trait AssocTy {

tests/ui/const-generics/generic_arg_infer/in-signature.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,19 @@ LL | static TY_STATIC_MIXED: Bar<_, _> = Bar::<i32, 3>(0);
7474
| not allowed in type signatures
7575
| help: replace with the correct type: `Bar<i32, 3>`
7676

77-
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constants
77+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated constants
7878
--> $DIR/in-signature.rs:35:21
7979
|
8080
LL | const ARR: [u8; _];
8181
| ^ not allowed in type signatures
8282

83-
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constants
83+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated constants
8484
--> $DIR/in-signature.rs:39:25
8585
|
8686
LL | const ARR: Bar<i32, _>;
8787
| ^ not allowed in type signatures
8888

89-
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constants
89+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated constants
9090
--> $DIR/in-signature.rs:43:20
9191
|
9292
LL | const ARR: Bar<_, _>;

tests/ui/typeck/type-placeholder-fn-in-const.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ struct MyStruct;
33
trait Test {
44
const TEST: fn() -> _;
55
//~^ ERROR: the placeholder `_` is not allowed within types on item signatures for functions [E0121]
6-
//~| ERROR: the placeholder `_` is not allowed within types on item signatures for constants [E0121]
6+
//~| ERROR: the placeholder `_` is not allowed within types on item signatures for associated constants [E0121]
77
}
88

99
impl Test for MyStruct {
1010
const TEST: fn() -> _ = 42;
1111
//~^ ERROR: the placeholder `_` is not allowed within types on item signatures for functions [E0121]
12+
//~| ERROR: the placeholder `_` is not allowed within types on item signatures for associated constants [E0121]
1213
}
1314

1415
fn main() {}

tests/ui/typeck/type-placeholder-fn-in-const.stderr

+8-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0121]: the placeholder `_` is not allowed within types on item signatures
44
LL | const TEST: fn() -> _;
55
| ^ not allowed in type signatures
66

7-
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constants
7+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated constants
88
--> $DIR/type-placeholder-fn-in-const.rs:4:25
99
|
1010
LL | const TEST: fn() -> _;
@@ -16,6 +16,12 @@ error[E0121]: the placeholder `_` is not allowed within types on item signatures
1616
LL | const TEST: fn() -> _ = 42;
1717
| ^ not allowed in type signatures
1818

19-
error: aborting due to 3 previous errors
19+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated constants
20+
--> $DIR/type-placeholder-fn-in-const.rs:10:25
21+
|
22+
LL | const TEST: fn() -> _ = 42;
23+
| ^ not allowed in type signatures
24+
25+
error: aborting due to 4 previous errors
2026

2127
For more information about this error, try `rustc --explain E0121`.

tests/ui/typeck/typeck_type_placeholder_item.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,9 @@ trait Qux {
190190
type B = _;
191191
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for associated types
192192
const C: _;
193-
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for constants
193+
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for associated constants
194194
const D: _ = 42;
195-
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for constants
195+
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for associated constants
196196
// type E: _; // FIXME: make the parser propagate the existence of `B`
197197
type F: std::ops::Fn(_);
198198
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for associated types
@@ -203,10 +203,10 @@ impl Qux for Struct {
203203
type B = _;
204204
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for associated types
205205
const C: _;
206-
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for constants
206+
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for associated constants
207207
//~| ERROR associated constant in `impl` without body
208208
const D: _ = 42;
209-
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for constants
209+
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for associated constants
210210
}
211211

212212
fn map<T>(_: fn() -> Option<&'static T>) -> Option<T> {

tests/ui/typeck/typeck_type_placeholder_item.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -525,13 +525,13 @@ error[E0121]: the placeholder `_` is not allowed within types on item signatures
525525
LL | type B = _;
526526
| ^ not allowed in type signatures
527527

528-
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constants
528+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated constants
529529
--> $DIR/typeck_type_placeholder_item.rs:192:14
530530
|
531531
LL | const C: _;
532532
| ^ not allowed in type signatures
533533

534-
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constants
534+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated constants
535535
--> $DIR/typeck_type_placeholder_item.rs:194:14
536536
|
537537
LL | const D: _ = 42;
@@ -642,13 +642,13 @@ error[E0121]: the placeholder `_` is not allowed within types on item signatures
642642
LL | type B = _;
643643
| ^ not allowed in type signatures
644644

645-
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constants
645+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated constants
646646
--> $DIR/typeck_type_placeholder_item.rs:205:14
647647
|
648648
LL | const C: _;
649649
| ^ not allowed in type signatures
650650

651-
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constants
651+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated constants
652652
--> $DIR/typeck_type_placeholder_item.rs:208:14
653653
|
654654
LL | const D: _ = 42;

tests/ui/typeck/typeck_type_placeholder_item_help.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ const TEST4: fn() -> _ = 42;
1616

1717
trait Test5 {
1818
const TEST5: _ = 42;
19-
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for constants
19+
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for associated constants
2020
}
2121

2222
struct Test6;
2323

2424
impl Test6 {
2525
const TEST6: _ = 13;
26-
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for constants
26+
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for associated constants
2727
}
2828

2929
pub fn main() {

tests/ui/typeck/typeck_type_placeholder_item_help.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ error[E0121]: the placeholder `_` is not allowed within types on item signatures
3737
LL | const TEST4: fn() -> _ = 42;
3838
| ^ not allowed in type signatures
3939

40-
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constants
40+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated constants
4141
--> $DIR/typeck_type_placeholder_item_help.rs:18:18
4242
|
4343
LL | const TEST5: _ = 42;
@@ -46,7 +46,7 @@ LL | const TEST5: _ = 42;
4646
| not allowed in type signatures
4747
| help: replace with the correct type: `i32`
4848

49-
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constants
49+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated constants
5050
--> $DIR/typeck_type_placeholder_item_help.rs:25:18
5151
|
5252
LL | const TEST6: _ = 13;

0 commit comments

Comments
 (0)