Skip to content

Commit 0baee24

Browse files
Don't typeck during WF, instead check outside of WF in check_crate
1 parent 9d3d5a7 commit 0baee24

File tree

13 files changed

+88
-64
lines changed

13 files changed

+88
-64
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -745,14 +745,10 @@ fn check_static_linkage(tcx: TyCtxt<'_>, def_id: LocalDefId) {
745745
pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
746746
match tcx.def_kind(def_id) {
747747
DefKind::Static { .. } => {
748-
tcx.ensure_ok().typeck(def_id);
749-
maybe_check_static_with_link_section(tcx, def_id);
750748
check_static_inhabited(tcx, def_id);
751749
check_static_linkage(tcx, def_id);
752750
}
753-
DefKind::Const => {
754-
tcx.ensure_ok().typeck(def_id);
755-
}
751+
DefKind::Const => {}
756752
DefKind::Enum => {
757753
check_enum(tcx, def_id);
758754
}
@@ -766,7 +762,6 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
766762
ExternAbi::Rust,
767763
)
768764
}
769-
// Everything else is checked entirely within check_item_body
770765
}
771766
DefKind::Impl { of_trait } => {
772767
if of_trait && let Some(impl_trait_header) = tcx.impl_trait_header(def_id) {

compiler/rustc_hir_analysis/src/check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ pub fn forbid_intrinsic_abi(tcx: TyCtxt<'_>, sp: Span, abi: ExternAbi) {
145145
}
146146
}
147147

148-
fn maybe_check_static_with_link_section(tcx: TyCtxt<'_>, id: LocalDefId) {
148+
pub(super) fn maybe_check_static_with_link_section(tcx: TyCtxt<'_>, id: LocalDefId) {
149149
// Only restricted on wasm target for now
150150
if !tcx.sess.target.is_like_wasm {
151151
return;

compiler/rustc_hir_analysis/src/lib.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,10 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
212212
tcx.par_hir_body_owners(|item_def_id| {
213213
let def_kind = tcx.def_kind(item_def_id);
214214
match def_kind {
215-
DefKind::Static { .. } => tcx.ensure_ok().eval_static_initializer(item_def_id),
215+
DefKind::Static { .. } => {
216+
tcx.ensure_ok().eval_static_initializer(item_def_id);
217+
check::maybe_check_static_with_link_section(tcx, item_def_id);
218+
}
216219
DefKind::Const if tcx.generics_of(item_def_id).is_empty() => {
217220
let instance = ty::Instance::new(item_def_id.into(), ty::GenericArgs::empty());
218221
let cid = GlobalId { instance, promoted: None };
@@ -223,12 +226,9 @@ pub fn check_crate(tcx: TyCtxt<'_>) {
223226
}
224227
});
225228

226-
// FIXME: Remove this when we implement creating `DefId`s
227-
// for anon constants during their parents' typeck.
228-
// Typeck all body owners in parallel will produce queries
229-
// cycle errors because it may typeck on anon constants directly.
230229
tcx.par_hir_body_owners(|item_def_id| {
231230
let def_kind = tcx.def_kind(item_def_id);
231+
// Skip `AnonConst`s because we feed their `type_of`.
232232
if !matches!(def_kind, DefKind::AnonConst) {
233233
tcx.ensure_ok().typeck(item_def_id);
234234
}

tests/rustdoc-ui/issues/issue-79494.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
#![feature(const_transmute)]
44

5-
const ZST: &[u8] = unsafe { std::mem::transmute(1usize) }; //~ ERROR cannot transmute between types of different sizes, or dependently-sized types
5+
pub const ZST: &[u8] = unsafe { std::mem::transmute(1usize) }; //~ ERROR cannot transmute between types of different sizes, or dependently-sized types

tests/rustdoc-ui/issues/issue-79494.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
2-
--> $DIR/issue-79494.rs:5:29
2+
--> $DIR/issue-79494.rs:5:33
33
|
4-
LL | const ZST: &[u8] = unsafe { std::mem::transmute(1usize) };
5-
| ^^^^^^^^^^^^^^^^^^^
4+
LL | pub const ZST: &[u8] = unsafe { std::mem::transmute(1usize) };
5+
| ^^^^^^^^^^^^^^^^^^^
66
|
77
= note: source type: `usize` (64 bits)
88
= note: target type: `&[u8]` (128 bits)

tests/rustdoc-ui/track-diagnostics.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77

88
struct A;
99
struct B;
10-
const S: A = B;
10+
11+
pub const S: A = B;

tests/rustdoc-ui/track-diagnostics.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0308]: mismatched types
22
--> $DIR/track-diagnostics.rs:LL:CC
33
|
4-
LL | const S: A = B;
5-
| ^ expected `A`, found `B`
4+
LL | pub const S: A = B;
5+
| ^ expected `A`, found `B`
66
-Ztrack-diagnostics: created at compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs:LL:CC
77

88
error: aborting due to 1 previous error

tests/ui/const-generics/generic_const_exprs/unevaluated-const-ice-119731.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,6 @@ warning: type `v17` should have an upper camel case name
5454
LL | pub struct v17<const v10: usize, const v7: v11> {
5555
| ^^^ help: convert the identifier to upper camel case (notice the capitalization): `V17`
5656

57-
error[E0425]: cannot find function `v6` in this scope
58-
--> $DIR/unevaluated-const-ice-119731.rs:13:35
59-
|
60-
LL | const v0: [[usize; v4]; v4] = v6(v8);
61-
| ^^ not found in this scope
62-
6357
error: `[[usize; v4]; v4]` is forbidden as the type of a const generic parameter
6458
--> $DIR/unevaluated-const-ice-119731.rs:16:48
6559
|
@@ -72,6 +66,12 @@ help: add `#![feature(adt_const_params)]` to the crate attributes to enable more
7266
LL + #![feature(adt_const_params)]
7367
|
7468

69+
error[E0425]: cannot find function `v6` in this scope
70+
--> $DIR/unevaluated-const-ice-119731.rs:13:35
71+
|
72+
LL | const v0: [[usize; v4]; v4] = v6(v8);
73+
| ^^ not found in this scope
74+
7575
error: maximum number of nodes exceeded in constant v20::v17::<v10, v2>::{constant#0}
7676
--> $DIR/unevaluated-const-ice-119731.rs:28:37
7777
|

tests/ui/consts/issue-39974.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/issue-39974.rs:5:19
3+
|
4+
LL | f: [[f64; 2]; LENGTH],
5+
| ^^^^^^ expected `usize`, found `f64`
6+
17
error[E0308]: mismatched types
28
--> $DIR/issue-39974.rs:1:21
39
|
@@ -9,12 +15,6 @@ help: use a float literal
915
LL | const LENGTH: f64 = 2.0;
1016
| ++
1117

12-
error[E0308]: mismatched types
13-
--> $DIR/issue-39974.rs:5:19
14-
|
15-
LL | f: [[f64; 2]; LENGTH],
16-
| ^^^^^^ expected `usize`, found `f64`
17-
1818
error: aborting due to 2 previous errors
1919

2020
For more information about this error, try `rustc --explain E0308`.

tests/ui/layout/issue-84108.stderr

+9-9
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ LL | const BAR: (&Path, [u8], usize) = ("hello", [], 42);
2929
= help: the trait `Sized` is not implemented for `[u8]`
3030
= note: only the last element of a tuple may have a dynamically sized type
3131

32+
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
33+
--> $DIR/issue-84108.rs:15:13
34+
|
35+
LL | static BAZ: ([u8], usize) = ([], 0);
36+
| ^^^^^^^^^^^^^ doesn't have a size known at compile-time
37+
|
38+
= help: the trait `Sized` is not implemented for `[u8]`
39+
= note: only the last element of a tuple may have a dynamically sized type
40+
3241
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
3342
--> $DIR/issue-84108.rs:9:12
3443
|
@@ -48,15 +57,6 @@ LL | const BAR: (&Path, [u8], usize) = ("hello", [], 42);
4857
= note: expected slice `[u8]`
4958
found array `[_; 0]`
5059

51-
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
52-
--> $DIR/issue-84108.rs:15:13
53-
|
54-
LL | static BAZ: ([u8], usize) = ([], 0);
55-
| ^^^^^^^^^^^^^ doesn't have a size known at compile-time
56-
|
57-
= help: the trait `Sized` is not implemented for `[u8]`
58-
= note: only the last element of a tuple may have a dynamically sized type
59-
6060
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
6161
--> $DIR/issue-84108.rs:15:13
6262
|

tests/ui/type-alias-impl-trait/issue-53092-2.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
#![allow(dead_code)]
33

44
type Bug<T, U> = impl Fn(T) -> U + Copy;
5+
//~^ ERROR cycle detected
56

67
const CONST_BUG: Bug<u8, ()> = unsafe { std::mem::transmute(|_: u8| ()) };
7-
//~^ ERROR cycle detected
8-
//~| ERROR: non-defining opaque type use
8+
//~^ ERROR item does not constrain `Bug::{opaque#0}`, but has it in its signature
9+
//~| ERROR item does not constrain `Bug::{opaque#0}`, but has it in its signature
10+
//~| ERROR non-defining opaque type use in defining scope
911

1012
fn make_bug<T, U: From<T>>() -> Bug<T, U> {
1113
|x| x.into()
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0792]: non-defining opaque type use in defining scope
2-
--> $DIR/issue-53092-2.rs:6:18
2+
--> $DIR/issue-53092-2.rs:7:18
33
|
44
LL | const CONST_BUG: Bug<u8, ()> = unsafe { std::mem::transmute(|_: u8| ()) };
55
| ^^^^^^^^^^^ argument `u8` is not a generic parameter
@@ -10,33 +10,59 @@ note: for this opaque type
1010
LL | type Bug<T, U> = impl Fn(T) -> U + Copy;
1111
| ^^^^^^^^^^^^^^^^^^^^^^
1212

13-
error[E0391]: cycle detected when type-checking `CONST_BUG`
14-
--> $DIR/issue-53092-2.rs:6:1
13+
error[E0391]: cycle detected when computing type of `Bug::{opaque#0}`
14+
--> $DIR/issue-53092-2.rs:4:18
15+
|
16+
LL | type Bug<T, U> = impl Fn(T) -> U + Copy;
17+
| ^^^^^^^^^^^^^^^^^^^^^^
18+
|
19+
note: ...which requires computing type of opaque `Bug::{opaque#0}`...
20+
--> $DIR/issue-53092-2.rs:4:18
21+
|
22+
LL | type Bug<T, U> = impl Fn(T) -> U + Copy;
23+
| ^^^^^^^^^^^^^^^^^^^^^^
24+
note: ...which requires type-checking `CONST_BUG`...
25+
--> $DIR/issue-53092-2.rs:7:1
1526
|
1627
LL | const CONST_BUG: Bug<u8, ()> = unsafe { std::mem::transmute(|_: u8| ()) };
1728
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18-
|
1929
= note: ...which requires computing layout of `Bug<u8, ()>`...
2030
= note: ...which requires normalizing `Bug<u8, ()>`...
21-
note: ...which requires computing type of `Bug::{opaque#0}`...
31+
= note: ...which again requires computing type of `Bug::{opaque#0}`, completing the cycle
32+
note: cycle used when checking that `Bug::{opaque#0}` is well-formed
2233
--> $DIR/issue-53092-2.rs:4:18
2334
|
2435
LL | type Bug<T, U> = impl Fn(T) -> U + Copy;
2536
| ^^^^^^^^^^^^^^^^^^^^^^
26-
note: ...which requires computing type of opaque `Bug::{opaque#0}`...
37+
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
38+
39+
error: item does not constrain `Bug::{opaque#0}`, but has it in its signature
40+
--> $DIR/issue-53092-2.rs:7:7
41+
|
42+
LL | const CONST_BUG: Bug<u8, ()> = unsafe { std::mem::transmute(|_: u8| ()) };
43+
| ^^^^^^^^^
44+
|
45+
= note: consider moving the opaque type's declaration and defining uses into a separate module
46+
note: this opaque type is in the signature
2747
--> $DIR/issue-53092-2.rs:4:18
2848
|
2949
LL | type Bug<T, U> = impl Fn(T) -> U + Copy;
3050
| ^^^^^^^^^^^^^^^^^^^^^^
31-
= note: ...which again requires type-checking `CONST_BUG`, completing the cycle
32-
note: cycle used when checking that `CONST_BUG` is well-formed
33-
--> $DIR/issue-53092-2.rs:6:1
51+
52+
error: item does not constrain `Bug::{opaque#0}`, but has it in its signature
53+
--> $DIR/issue-53092-2.rs:7:61
3454
|
3555
LL | const CONST_BUG: Bug<u8, ()> = unsafe { std::mem::transmute(|_: u8| ()) };
36-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
37-
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
56+
| ^^^^^^^
57+
|
58+
= note: consider moving the opaque type's declaration and defining uses into a separate module
59+
note: this opaque type is in the signature
60+
--> $DIR/issue-53092-2.rs:4:18
61+
|
62+
LL | type Bug<T, U> = impl Fn(T) -> U + Copy;
63+
| ^^^^^^^^^^^^^^^^^^^^^^
3864

39-
error: aborting due to 2 previous errors
65+
error: aborting due to 4 previous errors
4066

4167
Some errors have detailed explanations: E0391, E0792.
4268
For more information about an error, try `rustc --explain E0391`.

tests/ui/wf/wf-normalization-sized.next.stderr

+10-10
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,6 @@ LL | const _: <[[[[[[u8]]]]]] as WellUnformed>::RequestNormalize = ();
77
= help: the trait `Sized` is not implemented for `[[[[[u8]]]]]`
88
= note: slice and array elements must have `Sized` type
99

10-
error[E0277]: the size for values of type `[[[[[u8]]]]]` cannot be known at compilation time
11-
--> $DIR/wf-normalization-sized.rs:19:11
12-
|
13-
LL | const _: <[[[[[[u8]]]]]] as WellUnformed>::RequestNormalize = ();
14-
| ^^^^^^^^^^^^^^ doesn't have a size known at compile-time
15-
|
16-
= help: the trait `Sized` is not implemented for `[[[[[u8]]]]]`
17-
= note: slice and array elements must have `Sized` type
18-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
19-
2010
error[E0277]: the size for values of type `str` cannot be known at compilation time
2111
--> $DIR/wf-normalization-sized.rs:22:11
2212
|
@@ -27,6 +17,16 @@ LL | const _: <Vec<str> as WellUnformed>::RequestNormalize = ();
2717
note: required by an implicit `Sized` bound in `Vec`
2818
--> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
2919

20+
error[E0277]: the size for values of type `[[[[[u8]]]]]` cannot be known at compilation time
21+
--> $DIR/wf-normalization-sized.rs:19:11
22+
|
23+
LL | const _: <[[[[[[u8]]]]]] as WellUnformed>::RequestNormalize = ();
24+
| ^^^^^^^^^^^^^^ doesn't have a size known at compile-time
25+
|
26+
= help: the trait `Sized` is not implemented for `[[[[[u8]]]]]`
27+
= note: slice and array elements must have `Sized` type
28+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
29+
3030
error[E0277]: the size for values of type `str` cannot be known at compilation time
3131
--> $DIR/wf-normalization-sized.rs:22:11
3232
|

0 commit comments

Comments
 (0)