Skip to content

Commit c20a3fb

Browse files
Remove diagnostic_only_typeck and fix placeholder suggestion for const/static
1 parent c7dad16 commit c20a3fb

File tree

14 files changed

+85
-82
lines changed

14 files changed

+85
-82
lines changed

compiler/rustc_hir_analysis/src/collect/type_of.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ fn infer_placeholder_type<'tcx>(
412412
kind: &'static str,
413413
) -> Ty<'tcx> {
414414
let tcx = cx.tcx();
415-
let ty = tcx.diagnostic_only_typeck(def_id).node_type(body_id.hir_id);
415+
let ty = tcx.typeck(def_id).node_type(body_id.hir_id);
416416

417417
// If this came from a free `const` or `static mut?` item,
418418
// then the user may have written e.g. `const A = 42;`.

compiler/rustc_hir_typeck/src/lib.rs

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -87,21 +87,7 @@ fn used_trait_imports(tcx: TyCtxt<'_>, def_id: LocalDefId) -> &UnordSet<LocalDef
8787
}
8888

8989
fn typeck<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> &'tcx ty::TypeckResults<'tcx> {
90-
let fallback = move || tcx.type_of(def_id.to_def_id()).instantiate_identity();
91-
typeck_with_fallback(tcx, def_id, fallback, None)
92-
}
93-
94-
/// Used only to get `TypeckResults` for type inference during error recovery.
95-
/// Currently only used for type inference of `static`s and `const`s to avoid type cycle errors.
96-
fn diagnostic_only_typeck<'tcx>(
97-
tcx: TyCtxt<'tcx>,
98-
def_id: LocalDefId,
99-
) -> &'tcx ty::TypeckResults<'tcx> {
100-
let fallback = move || {
101-
let span = tcx.hir().span(tcx.local_def_id_to_hir_id(def_id));
102-
Ty::new_error_with_message(tcx, span, "diagnostic only typeck table used")
103-
};
104-
typeck_with_fallback(tcx, def_id, fallback, None)
90+
typeck_with_fallback(tcx, def_id, None)
10591
}
10692

10793
/// Same as `typeck` but `inspect` is invoked on evaluation of each root obligation.
@@ -113,15 +99,13 @@ pub fn inspect_typeck<'tcx>(
11399
def_id: LocalDefId,
114100
inspect: ObligationInspector<'tcx>,
115101
) -> &'tcx ty::TypeckResults<'tcx> {
116-
let fallback = move || tcx.type_of(def_id.to_def_id()).instantiate_identity();
117-
typeck_with_fallback(tcx, def_id, fallback, Some(inspect))
102+
typeck_with_fallback(tcx, def_id, Some(inspect))
118103
}
119104

120-
#[instrument(level = "debug", skip(tcx, fallback, inspector), ret)]
105+
#[instrument(level = "debug", skip(tcx, inspector), ret)]
121106
fn typeck_with_fallback<'tcx>(
122107
tcx: TyCtxt<'tcx>,
123108
def_id: LocalDefId,
124-
fallback: impl Fn() -> Ty<'tcx> + 'tcx,
125109
inspector: Option<ObligationInspector<'tcx>>,
126110
) -> &'tcx ty::TypeckResults<'tcx> {
127111
// Closures' typeck results come from their outermost function,
@@ -151,6 +135,10 @@ fn typeck_with_fallback<'tcx>(
151135

152136
if let Some(hir::FnSig { header, decl, .. }) = node.fn_sig() {
153137
let fn_sig = if decl.output.is_suggestable_infer_ty().is_some() {
138+
// In the case that we're recovering `fn() -> W<_>` or some other return
139+
// type that has an infer in it, lower the type directly so that it'll
140+
// be correctly filled with infer. We'll use this inference to provide
141+
// a suggestion later on.
154142
fcx.lowerer().lower_fn_ty(id, header.safety, header.abi, decl, None, None)
155143
} else {
156144
tcx.fn_sig(def_id).instantiate_identity()
@@ -164,8 +152,19 @@ fn typeck_with_fallback<'tcx>(
164152

165153
check_fn(&mut fcx, fn_sig, None, decl, def_id, body, tcx.features().unsized_fn_params());
166154
} else {
167-
let expected_type = infer_type_if_missing(&fcx, node);
168-
let expected_type = expected_type.unwrap_or_else(fallback);
155+
let expected_type = if let Some(infer_ty) = infer_type_if_missing(&fcx, node) {
156+
infer_ty
157+
} else if let Some(ty) = node.ty()
158+
&& ty.is_suggestable_infer_ty()
159+
{
160+
// In the case that we're recovering `const X: [T; _]` or some other
161+
// type that has an infer in it, lower the type directly so that it'll
162+
// be correctly filled with infer. We'll use this inference to provide
163+
// a suggestion later on.
164+
fcx.lowerer().lower_ty(ty)
165+
} else {
166+
tcx.type_of(def_id).instantiate_identity()
167+
};
169168

170169
let expected_type = fcx.normalize(body.value.span, expected_type);
171170

@@ -506,5 +505,5 @@ fn fatally_break_rust(tcx: TyCtxt<'_>, span: Span) -> ! {
506505

507506
pub fn provide(providers: &mut Providers) {
508507
method::provide(providers);
509-
*providers = Providers { typeck, diagnostic_only_typeck, used_trait_imports, ..*providers };
508+
*providers = Providers { typeck, used_trait_imports, ..*providers };
510509
}

compiler/rustc_middle/src/query/mod.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,9 +1100,6 @@ rustc_queries! {
11001100
desc { |tcx| "type-checking `{}`", tcx.def_path_str(key) }
11011101
cache_on_disk_if(tcx) { !tcx.is_typeck_child(key.to_def_id()) }
11021102
}
1103-
query diagnostic_only_typeck(key: LocalDefId) -> &'tcx ty::TypeckResults<'tcx> {
1104-
desc { |tcx| "type-checking `{}`", tcx.def_path_str(key) }
1105-
}
11061103

11071104
query used_trait_imports(key: LocalDefId) -> &'tcx UnordSet<LocalDefId> {
11081105
desc { |tcx| "finding used_trait_imports `{}`", tcx.def_path_str(key) }

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,19 @@ error[E0121]: the placeholder `_` is not allowed within types on item signatures
3030
--> $DIR/in-signature.rs:22:15
3131
|
3232
LL | const ARR_CT: [u8; _] = [0; 3];
33-
| ^^^^^^^ not allowed in type signatures
33+
| ^^^^^^^
34+
| |
35+
| not allowed in type signatures
36+
| help: replace with the correct type: `[u8; 3]`
3437

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

4147
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constants
4248
--> $DIR/in-signature.rs:26:14

tests/ui/consts/issue-104768.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const A: &_ = 0_u32;
22
//~^ ERROR: the placeholder `_` is not allowed within types on item signatures for constants
3+
//~| ERROR: mismatched types
34

45
fn main() {}

tests/ui/consts/issue-104768.stderr

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/issue-104768.rs:1:15
3+
|
4+
LL | const A: &_ = 0_u32;
5+
| ^^^^^ expected `&_`, found `u32`
6+
|
7+
= note: expected reference `&'static _`
8+
found type `u32`
9+
help: consider borrowing here
10+
|
11+
LL | const A: &_ = &0_u32;
12+
| +
13+
114
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constants
215
--> $DIR/issue-104768.rs:1:10
316
|
@@ -7,6 +20,7 @@ LL | const A: &_ = 0_u32;
720
| not allowed in type signatures
821
| help: replace with the correct type: `u32`
922

10-
error: aborting due to 1 previous error
23+
error: aborting due to 2 previous errors
1124

12-
For more information about this error, try `rustc --explain E0121`.
25+
Some errors have detailed explanations: E0121, E0308.
26+
For more information about an error, try `rustc --explain E0121`.

tests/ui/generic-const-items/assoc-const-missing-type.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ impl Trait for () {
1212
const K<T> = ();
1313
//~^ ERROR missing type for `const` item
1414
//~| ERROR mismatched types
15-
//~| ERROR mismatched types
1615
const Q = "";
1716
//~^ ERROR missing type for `const` item
1817
//~| ERROR lifetime parameters or bounds on const `Q` do not match the trait declaration

tests/ui/generic-const-items/assoc-const-missing-type.stderr

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ LL | const K<T> = ();
1616
| ^ help: provide a type for the associated constant: `()`
1717

1818
error[E0195]: lifetime parameters or bounds on const `Q` do not match the trait declaration
19-
--> $DIR/assoc-const-missing-type.rs:16:12
19+
--> $DIR/assoc-const-missing-type.rs:15:12
2020
|
2121
LL | const Q<'a>: &'a str;
2222
| ---- lifetimes in impl do not match this const in trait
@@ -25,24 +25,12 @@ LL | const Q = "";
2525
| ^ lifetimes do not match const in trait
2626

2727
error: missing type for `const` item
28-
--> $DIR/assoc-const-missing-type.rs:16:12
28+
--> $DIR/assoc-const-missing-type.rs:15:12
2929
|
3030
LL | const Q = "";
3131
| ^ help: provide a type for the associated constant: `: &str`
3232

33-
error[E0308]: mismatched types
34-
--> $DIR/assoc-const-missing-type.rs:12:18
35-
|
36-
LL | const K<T> = ();
37-
| - ^^ expected type parameter `T`, found `()`
38-
| |
39-
| expected this type parameter
40-
|
41-
= note: expected type parameter `T`
42-
found unit type `()`
43-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
44-
45-
error: aborting due to 5 previous errors
33+
error: aborting due to 4 previous errors
4634

4735
Some errors have detailed explanations: E0195, E0308.
4836
For more information about an error, try `rustc --explain E0195`.

tests/ui/parser/issues/issue-89574.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@ fn main() {
22
const EMPTY_ARRAY = [];
33
//~^ missing type for `const` item
44
//~| ERROR type annotations needed
5-
//~| ERROR type annotations needed
65
}

tests/ui/parser/issues/issue-89574.stderr

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,6 @@ help: provide a type for the item
1515
LL | const EMPTY_ARRAY: <type> = [];
1616
| ++++++++
1717

18-
error[E0282]: type annotations needed
19-
--> $DIR/issue-89574.rs:2:25
20-
|
21-
LL | const EMPTY_ARRAY = [];
22-
| ^^ cannot infer type
23-
|
24-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
25-
26-
error: aborting due to 3 previous errors
18+
error: aborting due to 2 previous errors
2719

2820
For more information about this error, try `rustc --explain E0282`.

tests/ui/typeck/issue-79040.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
fn main() {
2-
const FOO = "hello" + 1; //~ ERROR cannot add `{integer}` to `&str`
3-
//~^ missing type for `const` item
4-
//~| ERROR cannot add `{integer}` to `&str`
2+
const FOO = "hello" + 1;
3+
//~^ ERROR cannot add `{integer}` to `&str`
4+
//~| missing type for `const` item
55
println!("{}", FOO);
66
}

tests/ui/typeck/issue-79040.stderr

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,6 @@ help: provide a type for the item
1717
LL | const FOO: <type> = "hello" + 1;
1818
| ++++++++
1919

20-
error[E0369]: cannot add `{integer}` to `&str`
21-
--> $DIR/issue-79040.rs:2:25
22-
|
23-
LL | const FOO = "hello" + 1;
24-
| ------- ^ - {integer}
25-
| |
26-
| &str
27-
|
28-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
29-
30-
error: aborting due to 3 previous errors
20+
error: aborting due to 2 previous errors
3121

3222
For more information about this error, try `rustc --explain E0369`.

tests/ui/typeck/typeck_type_placeholder_item.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ fn value() -> Option<&'static _> {
221221

222222
const _: Option<_> = map(value);
223223
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for constants
224+
//~| ERROR cannot call non-const function `map::<u8>` in constants
224225

225226
fn evens_squared(n: usize) -> _ {
226227
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for return types

tests/ui/typeck/typeck_type_placeholder_item.stderr

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ error[E0121]: the placeholder `_` is not allowed within types on item signatures
8585
--> $DIR/typeck_type_placeholder_item.rs:19:15
8686
|
8787
LL | static TEST5: (_, _) = (1, 2);
88-
| ^^^^^^ not allowed in type signatures
88+
| ^^^^^^
89+
| |
90+
| not allowed in type signatures
91+
| help: replace with the correct type: `(i32, i32)`
8992

9093
error[E0121]: the placeholder `_` is not allowed within types on item signatures for functions
9194
--> $DIR/typeck_type_placeholder_item.rs:22:13
@@ -229,7 +232,10 @@ error[E0121]: the placeholder `_` is not allowed within types on item signatures
229232
--> $DIR/typeck_type_placeholder_item.rs:77:15
230233
|
231234
LL | static C: Option<_> = Some(42);
232-
| ^^^^^^^^^ not allowed in type signatures
235+
| ^^^^^^^^^
236+
| |
237+
| not allowed in type signatures
238+
| help: replace with the correct type: `Option<i32>`
233239

234240
error[E0121]: the placeholder `_` is not allowed within types on item signatures for return types
235241
--> $DIR/typeck_type_placeholder_item.rs:79:21
@@ -272,7 +278,10 @@ error[E0121]: the placeholder `_` is not allowed within types on item signatures
272278
--> $DIR/typeck_type_placeholder_item.rs:91:22
273279
|
274280
LL | static FN_TEST5: (_, _) = (1, 2);
275-
| ^^^^^^ not allowed in type signatures
281+
| ^^^^^^
282+
| |
283+
| not allowed in type signatures
284+
| help: replace with the correct type: `(i32, i32)`
276285

277286
error[E0121]: the placeholder `_` is not allowed within types on item signatures for functions
278287
--> $DIR/typeck_type_placeholder_item.rs:94:20
@@ -578,7 +587,7 @@ LL | const _: Option<_> = map(value);
578587
| help: replace with the correct type: `Option<u8>`
579588

580589
error[E0121]: the placeholder `_` is not allowed within types on item signatures for return types
581-
--> $DIR/typeck_type_placeholder_item.rs:225:31
590+
--> $DIR/typeck_type_placeholder_item.rs:226:31
582591
|
583592
LL | fn evens_squared(n: usize) -> _ {
584593
| ^
@@ -587,13 +596,13 @@ LL | fn evens_squared(n: usize) -> _ {
587596
| help: replace with an appropriate return type: `impl Iterator<Item = usize>`
588597

589598
error[E0121]: the placeholder `_` is not allowed within types on item signatures for constants
590-
--> $DIR/typeck_type_placeholder_item.rs:230:10
599+
--> $DIR/typeck_type_placeholder_item.rs:231:10
591600
|
592601
LL | const _: _ = (1..10).filter(|x| x % 2 == 0).map(|x| x * x);
593602
| ^ not allowed in type signatures
594603
|
595-
note: however, the inferred type `Map<Filter<Range<i32>, {closure@typeck_type_placeholder_item.rs:230:29}>, {closure@typeck_type_placeholder_item.rs:230:49}>` cannot be named
596-
--> $DIR/typeck_type_placeholder_item.rs:230:14
604+
note: however, the inferred type `Map<Filter<Range<i32>, {closure@typeck_type_placeholder_item.rs:231:29}>, {closure@typeck_type_placeholder_item.rs:231:49}>` cannot be named
605+
--> $DIR/typeck_type_placeholder_item.rs:231:14
597606
|
598607
LL | const _: _ = (1..10).filter(|x| x % 2 == 0).map(|x| x * x);
599608
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -668,23 +677,31 @@ error[E0121]: the placeholder `_` is not allowed within types on item signatures
668677
LL | type F: std::ops::Fn(_);
669678
| ^ not allowed in type signatures
670679

671-
error[E0015]: cannot call non-const method `<std::ops::Range<i32> as Iterator>::filter::<{closure@$DIR/typeck_type_placeholder_item.rs:230:29: 230:32}>` in constants
672-
--> $DIR/typeck_type_placeholder_item.rs:230:22
680+
error[E0015]: cannot call non-const function `map::<u8>` in constants
681+
--> $DIR/typeck_type_placeholder_item.rs:222:22
682+
|
683+
LL | const _: Option<_> = map(value);
684+
| ^^^^^^^^^^
685+
|
686+
= note: calls in constants are limited to constant functions, tuple structs and tuple variants
687+
688+
error[E0015]: cannot call non-const method `<std::ops::Range<i32> as Iterator>::filter::<{closure@$DIR/typeck_type_placeholder_item.rs:231:29: 231:32}>` in constants
689+
--> $DIR/typeck_type_placeholder_item.rs:231:22
673690
|
674691
LL | const _: _ = (1..10).filter(|x| x % 2 == 0).map(|x| x * x);
675692
| ^^^^^^^^^^^^^^^^^^^^^^
676693
|
677694
= note: calls in constants are limited to constant functions, tuple structs and tuple variants
678695

679-
error[E0015]: cannot call non-const method `<Filter<std::ops::Range<i32>, {closure@$DIR/typeck_type_placeholder_item.rs:230:29: 230:32}> as Iterator>::map::<i32, {closure@$DIR/typeck_type_placeholder_item.rs:230:49: 230:52}>` in constants
680-
--> $DIR/typeck_type_placeholder_item.rs:230:45
696+
error[E0015]: cannot call non-const method `<Filter<std::ops::Range<i32>, {closure@$DIR/typeck_type_placeholder_item.rs:231:29: 231:32}> as Iterator>::map::<i32, {closure@$DIR/typeck_type_placeholder_item.rs:231:49: 231:52}>` in constants
697+
--> $DIR/typeck_type_placeholder_item.rs:231:45
681698
|
682699
LL | const _: _ = (1..10).filter(|x| x % 2 == 0).map(|x| x * x);
683700
| ^^^^^^^^^^^^^^
684701
|
685702
= note: calls in constants are limited to constant functions, tuple structs and tuple variants
686703

687-
error: aborting due to 74 previous errors
704+
error: aborting due to 75 previous errors
688705

689706
Some errors have detailed explanations: E0015, E0046, E0121, E0282, E0403.
690707
For more information about an error, try `rustc --explain E0015`.

0 commit comments

Comments
 (0)