Skip to content

Commit 2c2d2ef

Browse files
More refined spans for placeholder error in const/static
1 parent c20a3fb commit 2c2d2ef

File tree

4 files changed

+59
-46
lines changed

4 files changed

+59
-46
lines changed

compiler/rustc_hir_analysis/src/collect/type_of.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use rustc_errors::{Applicability, StashKey, Suggestions};
44
use rustc_hir as hir;
55
use rustc_hir::HirId;
66
use rustc_hir::def_id::{DefId, LocalDefId};
7+
use rustc_hir::intravisit::Visitor;
78
use rustc_middle::query::plumbing::CyclePlaceholder;
89
use rustc_middle::ty::fold::fold_regions;
910
use rustc_middle::ty::print::with_forced_trimmed_paths;
@@ -12,7 +13,7 @@ use rustc_middle::ty::{self, Article, IsSuggestable, Ty, TyCtxt, TypeVisitableEx
1213
use rustc_middle::{bug, span_bug};
1314
use rustc_span::{DUMMY_SP, Ident, Span};
1415

15-
use super::{ItemCtxt, bad_placeholder};
16+
use super::{HirPlaceholderCollector, ItemCtxt, bad_placeholder};
1617
use crate::errors::TypeofReservedKeywordUsed;
1718
use crate::hir_ty_lowering::HirTyLowerer;
1819

@@ -447,7 +448,15 @@ fn infer_placeholder_type<'tcx>(
447448
}
448449
})
449450
.unwrap_or_else(|| {
450-
let mut diag = bad_placeholder(cx, vec![span], kind);
451+
let mut visitor = HirPlaceholderCollector::default();
452+
if let Some(ty) = tcx.hir_node_by_def_id(def_id).ty() {
453+
visitor.visit_ty(ty);
454+
}
455+
// If we didn't find any infer tys, then just fallback to `span``.
456+
if visitor.0.is_empty() {
457+
visitor.0.push(span);
458+
}
459+
let mut diag = bad_placeholder(cx, visitor.0, kind);
451460

452461
if !ty.references_error() {
453462
if let Some(ty) = ty.make_suggestable(tcx, false, None) {

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

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,57 +27,59 @@ LL | fn ty_fn_mixed() -> Bar<_, _> {
2727
| help: replace with the correct return type: `Bar<i32, 3>`
2828

2929
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constants
30-
--> $DIR/in-signature.rs:22:15
30+
--> $DIR/in-signature.rs:22:20
3131
|
3232
LL | const ARR_CT: [u8; _] = [0; 3];
33-
| ^^^^^^^
34-
| |
35-
| not allowed in type signatures
33+
| -----^-
34+
| | |
35+
| | not allowed in type signatures
3636
| help: replace with the correct type: `[u8; 3]`
3737

3838
error[E0121]: the placeholder `_` is not allowed within types on item signatures for static variables
39-
--> $DIR/in-signature.rs:24:20
39+
--> $DIR/in-signature.rs:24:25
4040
|
4141
LL | static ARR_STATIC: [u8; _] = [0; 3];
42-
| ^^^^^^^
43-
| |
44-
| not allowed in type signatures
42+
| -----^-
43+
| | |
44+
| | not allowed in type signatures
4545
| help: replace with the correct type: `[u8; 3]`
4646

4747
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constants
48-
--> $DIR/in-signature.rs:26:14
48+
--> $DIR/in-signature.rs:26:23
4949
|
5050
LL | const TY_CT: Bar<i32, _> = Bar::<i32, 3>(0);
51-
| ^^^^^^^^^^^
52-
| |
53-
| not allowed in type signatures
51+
| ---------^-
52+
| | |
53+
| | not allowed in type signatures
5454
| help: replace with the correct type: `Bar<i32, 3>`
5555

5656
error[E0121]: the placeholder `_` is not allowed within types on item signatures for static variables
57-
--> $DIR/in-signature.rs:28:19
57+
--> $DIR/in-signature.rs:28:28
5858
|
5959
LL | static TY_STATIC: Bar<i32, _> = Bar::<i32, 3>(0);
60-
| ^^^^^^^^^^^
61-
| |
62-
| not allowed in type signatures
60+
| ---------^-
61+
| | |
62+
| | not allowed in type signatures
6363
| help: replace with the correct type: `Bar<i32, 3>`
6464

6565
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constants
66-
--> $DIR/in-signature.rs:30:20
66+
--> $DIR/in-signature.rs:30:24
6767
|
6868
LL | const TY_CT_MIXED: Bar<_, _> = Bar::<i32, 3>(0);
69-
| ^^^^^^^^^
70-
| |
71-
| not allowed in type signatures
69+
| ----^--^-
70+
| | | |
71+
| | | not allowed in type signatures
72+
| | not allowed in type signatures
7273
| help: replace with the correct type: `Bar<i32, 3>`
7374

7475
error[E0121]: the placeholder `_` is not allowed within types on item signatures for static variables
75-
--> $DIR/in-signature.rs:32:25
76+
--> $DIR/in-signature.rs:32:29
7677
|
7778
LL | static TY_STATIC_MIXED: Bar<_, _> = Bar::<i32, 3>(0);
78-
| ^^^^^^^^^
79-
| |
80-
| not allowed in type signatures
79+
| ----^--^-
80+
| | | |
81+
| | | not allowed in type signatures
82+
| | not allowed in type signatures
8183
| help: replace with the correct type: `Bar<i32, 3>`
8284

8385
error[E0121]: the placeholder `_` is not allowed within types on item signatures for associated types

tests/ui/consts/issue-104768.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ LL | const A: &_ = &0_u32;
1212
| +
1313

1414
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constants
15-
--> $DIR/issue-104768.rs:1:10
15+
--> $DIR/issue-104768.rs:1:11
1616
|
1717
LL | const A: &_ = 0_u32;
18-
| ^^
19-
| |
20-
| not allowed in type signatures
18+
| -^
19+
| ||
20+
| |not allowed in type signatures
2121
| help: replace with the correct type: `u32`
2222

2323
error: aborting due to 2 previous errors

tests/ui/typeck/typeck_type_placeholder_item.stderr

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,13 @@ LL | static TEST4: _ = 145;
8282
| help: replace with the correct type: `i32`
8383

8484
error[E0121]: the placeholder `_` is not allowed within types on item signatures for static variables
85-
--> $DIR/typeck_type_placeholder_item.rs:19:15
85+
--> $DIR/typeck_type_placeholder_item.rs:19:16
8686
|
8787
LL | static TEST5: (_, _) = (1, 2);
88-
| ^^^^^^
89-
| |
90-
| not allowed in type signatures
88+
| -^--^-
89+
| || |
90+
| || not allowed in type signatures
91+
| |not allowed in type signatures
9192
| help: replace with the correct type: `(i32, i32)`
9293

9394
error[E0121]: the placeholder `_` is not allowed within types on item signatures for functions
@@ -229,12 +230,12 @@ LL | static B: _ = 42;
229230
| help: replace with the correct type: `i32`
230231

231232
error[E0121]: the placeholder `_` is not allowed within types on item signatures for static variables
232-
--> $DIR/typeck_type_placeholder_item.rs:77:15
233+
--> $DIR/typeck_type_placeholder_item.rs:77:22
233234
|
234235
LL | static C: Option<_> = Some(42);
235-
| ^^^^^^^^^
236-
| |
237-
| not allowed in type signatures
236+
| -------^-
237+
| | |
238+
| | not allowed in type signatures
238239
| help: replace with the correct type: `Option<i32>`
239240

240241
error[E0121]: the placeholder `_` is not allowed within types on item signatures for return types
@@ -275,12 +276,13 @@ LL | static FN_TEST4: _ = 145;
275276
| help: replace with the correct type: `i32`
276277

277278
error[E0121]: the placeholder `_` is not allowed within types on item signatures for static variables
278-
--> $DIR/typeck_type_placeholder_item.rs:91:22
279+
--> $DIR/typeck_type_placeholder_item.rs:91:23
279280
|
280281
LL | static FN_TEST5: (_, _) = (1, 2);
281-
| ^^^^^^
282-
| |
283-
| not allowed in type signatures
282+
| -^--^-
283+
| || |
284+
| || not allowed in type signatures
285+
| |not allowed in type signatures
284286
| help: replace with the correct type: `(i32, i32)`
285287

286288
error[E0121]: the placeholder `_` is not allowed within types on item signatures for functions
@@ -578,12 +580,12 @@ LL | fn value() -> Option<&'static _> {
578580
| help: replace with the correct return type: `Option<&'static u8>`
579581

580582
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constants
581-
--> $DIR/typeck_type_placeholder_item.rs:222:10
583+
--> $DIR/typeck_type_placeholder_item.rs:222:17
582584
|
583585
LL | const _: Option<_> = map(value);
584-
| ^^^^^^^^^
585-
| |
586-
| not allowed in type signatures
586+
| -------^-
587+
| | |
588+
| | not allowed in type signatures
587589
| help: replace with the correct type: `Option<u8>`
588590

589591
error[E0121]: the placeholder `_` is not allowed within types on item signatures for return types

0 commit comments

Comments
 (0)