Skip to content

Commit aa71be1

Browse files
authored
Rollup merge of #97508 - JohnTitor:more-strict-placeholder-dyn-obj, r=pnkfelix
Harden bad placeholder checks on statics/consts Resubmission of #89161 Fixes #88643 In #83739, I added a check for trait objects on statics/consts but it wasn't robust. `is_suggestable_infer_ty` fn does a more strict check and finds more bad placeholders. See #89161 (comment) for the more detailed explanation. r? `@pnkfelix` as you're the reviewer of the previous PR
2 parents 9688594 + 344feef commit aa71be1

9 files changed

+75
-9
lines changed

compiler/rustc_typeck/src/collect.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -806,8 +806,7 @@ fn convert_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) {
806806
hir::ItemKind::Fn(..) => tcx.ensure().fn_sig(def_id),
807807
hir::ItemKind::OpaqueTy(..) => tcx.ensure().item_bounds(def_id),
808808
hir::ItemKind::Const(ty, ..) | hir::ItemKind::Static(ty, ..) => {
809-
// (#75889): Account for `const C: dyn Fn() -> _ = "";`
810-
if let hir::TyKind::TraitObject(..) = ty.kind {
809+
if !is_suggestable_infer_ty(ty) {
811810
let mut visitor = HirPlaceholderCollector::default();
812811
visitor.visit_item(it);
813812
placeholder_type_error(tcx, None, visitor.0, false, None, it.kind.descr());

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

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
fn main() {
22
static BUG: fn(_) -> u8 = |_| 8;
33
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions [E0121]
4+
//~| ERROR the placeholder `_` is not allowed within types on item signatures for static items
45
}

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

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

7-
error: aborting due to previous error
7+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for static items
8+
--> $DIR/issue-74086.rs:2:20
9+
|
10+
LL | static BUG: fn(_) -> u8 = |_| 8;
11+
| ^ not allowed in type signatures
12+
13+
error: aborting due to 2 previous errors
814

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

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
const TEST4: fn() -> _ = 42;
22
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
3+
//~| ERROR the placeholder `_` is not allowed within types on item signatures for constant items
34

45
fn main() {
56
const TEST5: fn() -> _ = 42;
67
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
7-
8+
//~| ERROR the placeholder `_` is not allowed within types on item signatures for constant items
89
}

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

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

7+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constant items
8+
--> $DIR/issue-81885.rs:1:22
9+
|
10+
LL | const TEST4: fn() -> _ = 42;
11+
| ^ not allowed in type signatures
12+
713
error[E0121]: the placeholder `_` is not allowed within types on item signatures for functions
8-
--> $DIR/issue-81885.rs:5:26
14+
--> $DIR/issue-81885.rs:6:26
15+
|
16+
LL | const TEST5: fn() -> _ = 42;
17+
| ^ not allowed in type signatures
18+
19+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constant items
20+
--> $DIR/issue-81885.rs:6:26
921
|
1022
LL | const TEST5: fn() -> _ = 42;
1123
| ^ not allowed in type signatures
1224

13-
error: aborting due to 2 previous errors
25+
error: aborting due to 4 previous errors
1426

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

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

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Regression test for the ICE described in #88643. Specifically:
2+
// https://github.com/rust-lang/rust/issues/88643#issuecomment-913128893
3+
// and https://github.com/rust-lang/rust/issues/88643#issuecomment-913171935
4+
// and https://github.com/rust-lang/rust/issues/88643#issuecomment-913765984
5+
6+
use std::collections::HashMap;
7+
8+
pub trait T {}
9+
10+
static CALLBACKS: HashMap<*const dyn T, dyn FnMut(&mut _) + 'static> = HashMap::new();
11+
//~^ ERROR: the placeholder `_` is not allowed within types on item signatures for static items [E0121]
12+
13+
static CALLBACKS2: Vec<dyn Fn(& _)> = Vec::new();
14+
//~^ ERROR: the placeholder `_` is not allowed within types on item signatures for static items [E0121]
15+
16+
static CALLBACKS3: Option<dyn Fn(& _)> = None;
17+
//~^ ERROR: the placeholder `_` is not allowed within types on item signatures for static items [E0121]
18+
19+
fn main() {}

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

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for static items
2+
--> $DIR/issue-88643.rs:10:56
3+
|
4+
LL | static CALLBACKS: HashMap<*const dyn T, dyn FnMut(&mut _) + 'static> = HashMap::new();
5+
| ^ not allowed in type signatures
6+
7+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for static items
8+
--> $DIR/issue-88643.rs:13:33
9+
|
10+
LL | static CALLBACKS2: Vec<dyn Fn(& _)> = Vec::new();
11+
| ^ not allowed in type signatures
12+
13+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for static items
14+
--> $DIR/issue-88643.rs:16:36
15+
|
16+
LL | static CALLBACKS3: Option<dyn Fn(& _)> = None;
17+
| ^ not allowed in type signatures
18+
19+
error: aborting due to 3 previous errors
20+
21+
For more information about this error, try `rustc --explain E0121`.

src/test/ui/typeck/typeck_type_placeholder_item_help.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const TEST3: _ = Some(42);
1212

1313
const TEST4: fn() -> _ = 42;
1414
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions
15+
//~| ERROR the placeholder `_` is not allowed within types on item signatures for constant items
1516

1617
trait Test5 {
1718
const TEST5: _ = 42;

src/test/ui/typeck/typeck_type_placeholder_item_help.stderr

+9-3
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,14 @@ error[E0121]: the placeholder `_` is not allowed within types on item signatures
3131
LL | const TEST4: fn() -> _ = 42;
3232
| ^ not allowed in type signatures
3333

34+
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constant items
35+
--> $DIR/typeck_type_placeholder_item_help.rs:13:22
36+
|
37+
LL | const TEST4: fn() -> _ = 42;
38+
| ^ not allowed in type signatures
39+
3440
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constants
35-
--> $DIR/typeck_type_placeholder_item_help.rs:17:18
41+
--> $DIR/typeck_type_placeholder_item_help.rs:18:18
3642
|
3743
LL | const TEST5: _ = 42;
3844
| ^
@@ -41,14 +47,14 @@ LL | const TEST5: _ = 42;
4147
| help: replace with the correct type: `i32`
4248

4349
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constants
44-
--> $DIR/typeck_type_placeholder_item_help.rs:24:18
50+
--> $DIR/typeck_type_placeholder_item_help.rs:25:18
4551
|
4652
LL | const TEST6: _ = 13;
4753
| ^
4854
| |
4955
| not allowed in type signatures
5056
| help: replace with the correct type: `i32`
5157

52-
error: aborting due to 6 previous errors
58+
error: aborting due to 7 previous errors
5359

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

0 commit comments

Comments
 (0)