Skip to content

Commit 8817572

Browse files
committed
Do not check Sync during type_of.
1 parent 7c64961 commit 8817572

File tree

13 files changed

+26
-264
lines changed

13 files changed

+26
-264
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,8 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
767767
DefKind::Static { .. } => {
768768
check_static_inhabited(tcx, def_id);
769769
check_static_linkage(tcx, def_id);
770+
let ty = tcx.type_of(def_id).instantiate_identity();
771+
res = res.and(wfcheck::check_static_item(tcx, def_id, ty, true));
770772
}
771773
DefKind::Const => res = res.and(wfcheck::check_const_item(tcx, def_id)),
772774
_ => unreachable!(),

compiler/rustc_hir_analysis/src/check/wfcheck.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,6 +1184,7 @@ pub(crate) fn check_static_item<'tcx>(
11841184
tcx: TyCtxt<'tcx>,
11851185
item_id: LocalDefId,
11861186
ty: Ty<'tcx>,
1187+
should_check_for_sync: bool,
11871188
) -> Result<(), ErrorGuaranteed> {
11881189
enter_wf_checking_ctxt(tcx, item_id, |wfcx| {
11891190
let span = tcx.ty_span(item_id);
@@ -1212,9 +1213,9 @@ pub(crate) fn check_static_item<'tcx>(
12121213
}
12131214

12141215
// Ensure that the end result is `Sync` in a non-thread local `static`.
1215-
let should_check_for_sync = tcx.static_mutability(item_id.to_def_id())
1216-
== Some(hir::Mutability::Not)
1216+
let should_check_for_sync = should_check_for_sync
12171217
&& !is_foreign_item
1218+
&& tcx.static_mutability(item_id.to_def_id()) == Some(hir::Mutability::Not)
12181219
&& !tcx.is_thread_local_static(item_id.to_def_id());
12191220

12201221
if should_check_for_sync {

compiler/rustc_hir_analysis/src/collect/type_of.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_
221221
let ty = icx.lower_ty(ty);
222222
// MIR relies on references to statics being scalars.
223223
// Verify that here to avoid ill-formed MIR.
224-
match check_static_item(tcx, def_id, ty) {
224+
match check_static_item(tcx, def_id, ty, false) {
225225
Ok(()) => ty,
226226
Err(guar) => Ty::new_error(tcx, guar),
227227
}
@@ -286,7 +286,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_
286286
let ty = icx.lower_ty(ty);
287287
// MIR relies on references to statics being scalars.
288288
// Verify that here to avoid ill-formed MIR.
289-
match check_static_item(tcx, def_id, ty) {
289+
match check_static_item(tcx, def_id, ty, false) {
290290
Ok(()) => ty,
291291
Err(guar) => Ty::new_error(tcx, guar),
292292
}

tests/ui/coroutine/layout-error.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,5 @@ where
3232

3333
// Check that statics are inhabited computes they layout.
3434
static POOL: Task<F> = Task::new();
35-
//~^ ERROR cycle detected when computing type of `POOL`
36-
//~| ERROR cycle detected when computing type of `POOL`
3735

3836
fn main() {}

tests/ui/coroutine/layout-error.stderr

Lines changed: 2 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -4,94 +4,6 @@ error[E0425]: cannot find value `Foo` in this scope
44
LL | let a = Foo;
55
| ^^^ not found in this scope
66

7-
error[E0391]: cycle detected when computing type of `POOL`
8-
--> $DIR/layout-error.rs:34:14
9-
|
10-
LL | static POOL: Task<F> = Task::new();
11-
| ^^^^^^^
12-
|
13-
= note: ...which requires evaluating trait selection obligation `Task<F>: core::marker::Sync`...
14-
note: ...which requires computing type of opaque `F::{opaque#0}`...
15-
--> $DIR/layout-error.rs:19:14
16-
|
17-
LL | pub type F = impl Future;
18-
| ^^^^^^^^^^^
19-
note: ...which requires borrow-checking `foo`...
20-
--> $DIR/layout-error.rs:22:1
21-
|
22-
LL | / fn foo()
23-
LL | | where
24-
LL | | F:,
25-
| |_______^
26-
note: ...which requires promoting constants in MIR for `foo`...
27-
--> $DIR/layout-error.rs:22:1
28-
|
29-
LL | / fn foo()
30-
LL | | where
31-
LL | | F:,
32-
| |_______^
33-
note: ...which requires checking if `foo` contains FFI-unwind calls...
34-
--> $DIR/layout-error.rs:22:1
35-
|
36-
LL | / fn foo()
37-
LL | | where
38-
LL | | F:,
39-
| |_______^
40-
note: ...which requires building MIR for `foo`...
41-
--> $DIR/layout-error.rs:22:1
42-
|
43-
LL | / fn foo()
44-
LL | | where
45-
LL | | F:,
46-
| |_______^
47-
note: ...which requires match-checking `foo`...
48-
--> $DIR/layout-error.rs:22:1
49-
|
50-
LL | / fn foo()
51-
LL | | where
52-
LL | | F:,
53-
| |_______^
54-
note: ...which requires type-checking `foo`...
55-
--> $DIR/layout-error.rs:22:1
56-
|
57-
LL | / fn foo()
58-
LL | | where
59-
LL | | F:,
60-
| |_______^
61-
= note: ...which again requires computing type of `POOL`, completing the cycle
62-
note: cycle used when checking that `POOL` is well-formed
63-
--> $DIR/layout-error.rs:34:1
64-
|
65-
LL | static POOL: Task<F> = Task::new();
66-
| ^^^^^^^^^^^^^^^^^^^^
67-
= 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
68-
69-
error[E0391]: cycle detected when computing type of `POOL`
70-
--> $DIR/layout-error.rs:34:14
71-
|
72-
LL | static POOL: Task<F> = Task::new();
73-
| ^^^^^^^
74-
|
75-
= note: ...which requires evaluating trait selection obligation `Task<F>: core::marker::Sync`...
76-
note: ...which requires computing type of opaque `F::{opaque#0}`...
77-
--> $DIR/layout-error.rs:19:14
78-
|
79-
LL | pub type F = impl Future;
80-
| ^^^^^^^^^^^
81-
note: ...which requires computing the opaque types defined by `POOL`...
82-
--> $DIR/layout-error.rs:34:1
83-
|
84-
LL | static POOL: Task<F> = Task::new();
85-
| ^^^^^^^^^^^^^^^^^^^^
86-
= note: ...which again requires computing type of `POOL`, completing the cycle
87-
note: cycle used when checking that `POOL` is well-formed
88-
--> $DIR/layout-error.rs:34:1
89-
|
90-
LL | static POOL: Task<F> = Task::new();
91-
| ^^^^^^^^^^^^^^^^^^^^
92-
= 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
93-
94-
error: aborting due to 3 previous errors
7+
error: aborting due to 1 previous error
958

96-
Some errors have detailed explanations: E0391, E0425.
97-
For more information about an error, try `rustc --explain E0391`.
9+
For more information about this error, try `rustc --explain E0425`.

tests/ui/coroutine/metadata-sufficient-for-layout.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// Regression test for #80998.
55
//
66
//@ aux-build:metadata-sufficient-for-layout.rs
7+
//@ check-pass
78

89
#![feature(type_alias_impl_trait, rustc_attrs)]
910
#![feature(coroutine_trait)]
@@ -22,6 +23,5 @@ mod helper {
2223

2324
// Static queries the layout of the coroutine.
2425
static A: Option<helper::F> = None;
25-
//~^ ERROR cycle detected when computing type of `A`
2626

2727
fn main() {}

tests/ui/coroutine/metadata-sufficient-for-layout.stderr

Lines changed: 0 additions & 38 deletions
This file was deleted.

tests/ui/issues/issue-7364.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ use std::cell::RefCell;
33
// Regression test for issue 7364
44
static boxed: Box<RefCell<isize>> = Box::new(RefCell::new(0));
55
//~^ ERROR `RefCell<isize>` cannot be shared between threads safely [E0277]
6+
//~| ERROR cannot call non-const associated function
67

78
fn main() { }

tests/ui/issues/issue-7364.stderr

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ note: required because it appears within the type `Box<RefCell<isize>>`
1111
--> $SRC_DIR/alloc/src/boxed.rs:LL:COL
1212
= note: shared static variables must have a type that implements `Sync`
1313

14-
error: aborting due to 1 previous error
14+
error[E0015]: cannot call non-const associated function `Box::<RefCell<isize>>::new` in statics
15+
--> $DIR/issue-7364.rs:4:37
16+
|
17+
LL | static boxed: Box<RefCell<isize>> = Box::new(RefCell::new(0));
18+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
19+
|
20+
= note: calls in statics are limited to constant functions, tuple structs and tuple variants
21+
= note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)`
22+
23+
error: aborting due to 2 previous errors
1524

16-
For more information about this error, try `rustc --explain E0277`.
25+
Some errors have detailed explanations: E0015, E0277.
26+
For more information about an error, try `rustc --explain E0015`.

tests/ui/static/issue-24446.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
fn main() {
22
static foo: dyn Fn() -> u32 = || -> u32 {
33
//~^ ERROR the size for values of type
4-
//~| ERROR cannot be shared between threads safely
54
0
65
};
76
}

0 commit comments

Comments
 (0)