Do not check Sync during type_of.

This commit is contained in:
Camille GILLOT
2025-07-26 21:46:20 +00:00
parent 7c6496145f
commit 8817572b45
13 changed files with 26 additions and 264 deletions

View File

@@ -767,6 +767,8 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(),
DefKind::Static { .. } => { DefKind::Static { .. } => {
check_static_inhabited(tcx, def_id); check_static_inhabited(tcx, def_id);
check_static_linkage(tcx, def_id); check_static_linkage(tcx, def_id);
let ty = tcx.type_of(def_id).instantiate_identity();
res = res.and(wfcheck::check_static_item(tcx, def_id, ty, true));
} }
DefKind::Const => res = res.and(wfcheck::check_const_item(tcx, def_id)), DefKind::Const => res = res.and(wfcheck::check_const_item(tcx, def_id)),
_ => unreachable!(), _ => unreachable!(),

View File

@@ -1184,6 +1184,7 @@ pub(crate) fn check_static_item<'tcx>(
tcx: TyCtxt<'tcx>, tcx: TyCtxt<'tcx>,
item_id: LocalDefId, item_id: LocalDefId,
ty: Ty<'tcx>, ty: Ty<'tcx>,
should_check_for_sync: bool,
) -> Result<(), ErrorGuaranteed> { ) -> Result<(), ErrorGuaranteed> {
enter_wf_checking_ctxt(tcx, item_id, |wfcx| { enter_wf_checking_ctxt(tcx, item_id, |wfcx| {
let span = tcx.ty_span(item_id); let span = tcx.ty_span(item_id);
@@ -1212,9 +1213,9 @@ pub(crate) fn check_static_item<'tcx>(
} }
// Ensure that the end result is `Sync` in a non-thread local `static`. // Ensure that the end result is `Sync` in a non-thread local `static`.
let should_check_for_sync = tcx.static_mutability(item_id.to_def_id()) let should_check_for_sync = should_check_for_sync
== Some(hir::Mutability::Not)
&& !is_foreign_item && !is_foreign_item
&& tcx.static_mutability(item_id.to_def_id()) == Some(hir::Mutability::Not)
&& !tcx.is_thread_local_static(item_id.to_def_id()); && !tcx.is_thread_local_static(item_id.to_def_id());
if should_check_for_sync { if should_check_for_sync {

View File

@@ -221,7 +221,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_
let ty = icx.lower_ty(ty); let ty = icx.lower_ty(ty);
// MIR relies on references to statics being scalars. // MIR relies on references to statics being scalars.
// Verify that here to avoid ill-formed MIR. // Verify that here to avoid ill-formed MIR.
match check_static_item(tcx, def_id, ty) { match check_static_item(tcx, def_id, ty, false) {
Ok(()) => ty, Ok(()) => ty,
Err(guar) => Ty::new_error(tcx, guar), Err(guar) => Ty::new_error(tcx, guar),
} }
@@ -286,7 +286,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_
let ty = icx.lower_ty(ty); let ty = icx.lower_ty(ty);
// MIR relies on references to statics being scalars. // MIR relies on references to statics being scalars.
// Verify that here to avoid ill-formed MIR. // Verify that here to avoid ill-formed MIR.
match check_static_item(tcx, def_id, ty) { match check_static_item(tcx, def_id, ty, false) {
Ok(()) => ty, Ok(()) => ty,
Err(guar) => Ty::new_error(tcx, guar), Err(guar) => Ty::new_error(tcx, guar),
} }

View File

@@ -32,7 +32,5 @@ where
// Check that statics are inhabited computes they layout. // Check that statics are inhabited computes they layout.
static POOL: Task<F> = Task::new(); static POOL: Task<F> = Task::new();
//~^ ERROR cycle detected when computing type of `POOL`
//~| ERROR cycle detected when computing type of `POOL`
fn main() {} fn main() {}

View File

@@ -4,94 +4,6 @@ error[E0425]: cannot find value `Foo` in this scope
LL | let a = Foo; LL | let a = Foo;
| ^^^ not found in this scope | ^^^ not found in this scope
error[E0391]: cycle detected when computing type of `POOL` error: aborting due to 1 previous error
--> $DIR/layout-error.rs:34:14
|
LL | static POOL: Task<F> = Task::new();
| ^^^^^^^
|
= note: ...which requires evaluating trait selection obligation `Task<F>: core::marker::Sync`...
note: ...which requires computing type of opaque `F::{opaque#0}`...
--> $DIR/layout-error.rs:19:14
|
LL | pub type F = impl Future;
| ^^^^^^^^^^^
note: ...which requires borrow-checking `foo`...
--> $DIR/layout-error.rs:22:1
|
LL | / fn foo()
LL | | where
LL | | F:,
| |_______^
note: ...which requires promoting constants in MIR for `foo`...
--> $DIR/layout-error.rs:22:1
|
LL | / fn foo()
LL | | where
LL | | F:,
| |_______^
note: ...which requires checking if `foo` contains FFI-unwind calls...
--> $DIR/layout-error.rs:22:1
|
LL | / fn foo()
LL | | where
LL | | F:,
| |_______^
note: ...which requires building MIR for `foo`...
--> $DIR/layout-error.rs:22:1
|
LL | / fn foo()
LL | | where
LL | | F:,
| |_______^
note: ...which requires match-checking `foo`...
--> $DIR/layout-error.rs:22:1
|
LL | / fn foo()
LL | | where
LL | | F:,
| |_______^
note: ...which requires type-checking `foo`...
--> $DIR/layout-error.rs:22:1
|
LL | / fn foo()
LL | | where
LL | | F:,
| |_______^
= note: ...which again requires computing type of `POOL`, completing the cycle
note: cycle used when checking that `POOL` is well-formed
--> $DIR/layout-error.rs:34:1
|
LL | static POOL: Task<F> = Task::new();
| ^^^^^^^^^^^^^^^^^^^^
= 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
error[E0391]: cycle detected when computing type of `POOL` For more information about this error, try `rustc --explain E0425`.
--> $DIR/layout-error.rs:34:14
|
LL | static POOL: Task<F> = Task::new();
| ^^^^^^^
|
= note: ...which requires evaluating trait selection obligation `Task<F>: core::marker::Sync`...
note: ...which requires computing type of opaque `F::{opaque#0}`...
--> $DIR/layout-error.rs:19:14
|
LL | pub type F = impl Future;
| ^^^^^^^^^^^
note: ...which requires computing the opaque types defined by `POOL`...
--> $DIR/layout-error.rs:34:1
|
LL | static POOL: Task<F> = Task::new();
| ^^^^^^^^^^^^^^^^^^^^
= note: ...which again requires computing type of `POOL`, completing the cycle
note: cycle used when checking that `POOL` is well-formed
--> $DIR/layout-error.rs:34:1
|
LL | static POOL: Task<F> = Task::new();
| ^^^^^^^^^^^^^^^^^^^^
= 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
error: aborting due to 3 previous errors
Some errors have detailed explanations: E0391, E0425.
For more information about an error, try `rustc --explain E0391`.

View File

@@ -4,6 +4,7 @@
// Regression test for #80998. // Regression test for #80998.
// //
//@ aux-build:metadata-sufficient-for-layout.rs //@ aux-build:metadata-sufficient-for-layout.rs
//@ check-pass
#![feature(type_alias_impl_trait, rustc_attrs)] #![feature(type_alias_impl_trait, rustc_attrs)]
#![feature(coroutine_trait)] #![feature(coroutine_trait)]
@@ -22,6 +23,5 @@ mod helper {
// Static queries the layout of the coroutine. // Static queries the layout of the coroutine.
static A: Option<helper::F> = None; static A: Option<helper::F> = None;
//~^ ERROR cycle detected when computing type of `A`
fn main() {} fn main() {}

View File

@@ -1,38 +0,0 @@
error[E0391]: cycle detected when computing type of `A`
--> $DIR/metadata-sufficient-for-layout.rs:24:11
|
LL | static A: Option<helper::F> = None;
| ^^^^^^^^^^^^^^^^^
|
= note: ...which requires evaluating trait selection obligation `core::option::Option<helper::F>: core::marker::Sync`...
note: ...which requires computing type of opaque `helper::F::{opaque#0}`...
--> $DIR/metadata-sufficient-for-layout.rs:15:18
|
LL | pub type F = impl Coroutine<(), Yield = (), Return = ()>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...which requires borrow-checking `helper::f`...
--> $DIR/metadata-sufficient-for-layout.rs:18:5
|
LL | fn f() -> F {
| ^^^^^^^^^^^
note: ...which requires computing type of opaque `helper::F::{opaque#0}` via HIR typeck...
--> $DIR/metadata-sufficient-for-layout.rs:15:18
|
LL | pub type F = impl Coroutine<(), Yield = (), Return = ()>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...which requires computing the opaque types defined by `A`...
--> $DIR/metadata-sufficient-for-layout.rs:24:1
|
LL | static A: Option<helper::F> = None;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: ...which again requires computing type of `A`, completing the cycle
note: cycle used when checking that `A` is well-formed
--> $DIR/metadata-sufficient-for-layout.rs:24:1
|
LL | static A: Option<helper::F> = None;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
= 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
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0391`.

View File

@@ -3,5 +3,6 @@ use std::cell::RefCell;
// Regression test for issue 7364 // Regression test for issue 7364
static boxed: Box<RefCell<isize>> = Box::new(RefCell::new(0)); static boxed: Box<RefCell<isize>> = Box::new(RefCell::new(0));
//~^ ERROR `RefCell<isize>` cannot be shared between threads safely [E0277] //~^ ERROR `RefCell<isize>` cannot be shared between threads safely [E0277]
//~| ERROR cannot call non-const associated function
fn main() { } fn main() { }

View File

@@ -11,6 +11,16 @@ note: required because it appears within the type `Box<RefCell<isize>>`
--> $SRC_DIR/alloc/src/boxed.rs:LL:COL --> $SRC_DIR/alloc/src/boxed.rs:LL:COL
= note: shared static variables must have a type that implements `Sync` = note: shared static variables must have a type that implements `Sync`
error: aborting due to 1 previous error error[E0015]: cannot call non-const associated function `Box::<RefCell<isize>>::new` in statics
--> $DIR/issue-7364.rs:4:37
|
LL | static boxed: Box<RefCell<isize>> = Box::new(RefCell::new(0));
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: calls in statics are limited to constant functions, tuple structs and tuple variants
= note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)`
For more information about this error, try `rustc --explain E0277`. error: aborting due to 2 previous errors
Some errors have detailed explanations: E0015, E0277.
For more information about an error, try `rustc --explain E0015`.

View File

@@ -1,7 +1,6 @@
fn main() { fn main() {
static foo: dyn Fn() -> u32 = || -> u32 { static foo: dyn Fn() -> u32 = || -> u32 {
//~^ ERROR the size for values of type //~^ ERROR the size for values of type
//~| ERROR cannot be shared between threads safely
0 0
}; };
} }

View File

@@ -1,12 +1,3 @@
error[E0277]: `(dyn Fn() -> u32 + 'static)` cannot be shared between threads safely
--> $DIR/issue-24446.rs:2:17
|
LL | static foo: dyn Fn() -> u32 = || -> u32 {
| ^^^^^^^^^^^^^^^ `(dyn Fn() -> u32 + 'static)` cannot be shared between threads safely
|
= help: the trait `Sync` is not implemented for `(dyn Fn() -> u32 + 'static)`
= note: shared static variables must have a type that implements `Sync`
error[E0277]: the size for values of type `(dyn Fn() -> u32 + 'static)` cannot be known at compilation time error[E0277]: the size for values of type `(dyn Fn() -> u32 + 'static)` cannot be known at compilation time
--> $DIR/issue-24446.rs:2:5 --> $DIR/issue-24446.rs:2:5
| |
@@ -16,6 +7,6 @@ LL | static foo: dyn Fn() -> u32 = || -> u32 {
= help: the trait `Sized` is not implemented for `(dyn Fn() -> u32 + 'static)` = help: the trait `Sized` is not implemented for `(dyn Fn() -> u32 + 'static)`
= note: statics and constants must have a statically known size = note: statics and constants must have a statically known size
error: aborting due to 2 previous errors error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0277`. For more information about this error, try `rustc --explain E0277`.

View File

@@ -1,3 +1,5 @@
//@ check-pass
#![feature(type_alias_impl_trait)] #![feature(type_alias_impl_trait)]
use std::fmt::Debug; use std::fmt::Debug;
@@ -9,8 +11,5 @@ const _FOO: Foo = 5;
#[define_opaque(Foo)] #[define_opaque(Foo)]
static _BAR: Foo = 22_i32; static _BAR: Foo = 22_i32;
//~^ ERROR cycle detected when computing type of `_BAR`
//~| ERROR cycle detected when computing type of `_BAR`
//~| ERROR cycle detected when computing type of `_BAR`
fn main() {} fn main() {}

View File

@@ -1,113 +0,0 @@
error[E0391]: cycle detected when computing type of `_BAR`
--> $DIR/type-alias-impl-trait-const.rs:11:14
|
LL | static _BAR: Foo = 22_i32;
| ^^^
|
= note: ...which requires evaluating trait selection obligation `Foo: core::marker::Sync`...
note: ...which requires computing type of opaque `Foo::{opaque#0}`...
--> $DIR/type-alias-impl-trait-const.rs:5:16
|
LL | pub type Foo = impl Debug;
| ^^^^^^^^^^
note: ...which requires borrow-checking `_FOO`...
--> $DIR/type-alias-impl-trait-const.rs:8:1
|
LL | const _FOO: Foo = 5;
| ^^^^^^^^^^^^^^^
note: ...which requires computing type of opaque `Foo::{opaque#0}` via HIR typeck...
--> $DIR/type-alias-impl-trait-const.rs:5:16
|
LL | pub type Foo = impl Debug;
| ^^^^^^^^^^
note: ...which requires computing the opaque types defined by `_BAR`...
--> $DIR/type-alias-impl-trait-const.rs:11:1
|
LL | static _BAR: Foo = 22_i32;
| ^^^^^^^^^^^^^^^^
= note: ...which again requires computing type of `_BAR`, completing the cycle
note: cycle used when checking that `_BAR` is well-formed
--> $DIR/type-alias-impl-trait-const.rs:11:1
|
LL | static _BAR: Foo = 22_i32;
| ^^^^^^^^^^^^^^^^
= 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
error[E0391]: cycle detected when computing type of `_BAR`
--> $DIR/type-alias-impl-trait-const.rs:11:14
|
LL | static _BAR: Foo = 22_i32;
| ^^^
|
= note: ...which requires evaluating trait selection obligation `Foo: core::marker::Sync`...
note: ...which requires computing type of opaque `Foo::{opaque#0}`...
--> $DIR/type-alias-impl-trait-const.rs:5:16
|
LL | pub type Foo = impl Debug;
| ^^^^^^^^^^
note: ...which requires borrow-checking `_FOO`...
--> $DIR/type-alias-impl-trait-const.rs:8:1
|
LL | const _FOO: Foo = 5;
| ^^^^^^^^^^^^^^^
note: ...which requires computing type of opaque `Foo::{opaque#0}` via HIR typeck...
--> $DIR/type-alias-impl-trait-const.rs:5:16
|
LL | pub type Foo = impl Debug;
| ^^^^^^^^^^
note: ...which requires type-checking `_BAR`...
--> $DIR/type-alias-impl-trait-const.rs:11:1
|
LL | static _BAR: Foo = 22_i32;
| ^^^^^^^^^^^^^^^^
= note: ...which again requires computing type of `_BAR`, completing the cycle
note: cycle used when checking that `_BAR` is well-formed
--> $DIR/type-alias-impl-trait-const.rs:11:1
|
LL | static _BAR: Foo = 22_i32;
| ^^^^^^^^^^^^^^^^
= 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
error[E0391]: cycle detected when computing type of `_BAR`
--> $DIR/type-alias-impl-trait-const.rs:11:14
|
LL | static _BAR: Foo = 22_i32;
| ^^^
|
= note: ...which requires evaluating trait selection obligation `Foo: core::marker::Sync`...
note: ...which requires computing type of opaque `Foo::{opaque#0}`...
--> $DIR/type-alias-impl-trait-const.rs:5:16
|
LL | pub type Foo = impl Debug;
| ^^^^^^^^^^
note: ...which requires borrow-checking `_BAR`...
--> $DIR/type-alias-impl-trait-const.rs:11:1
|
LL | static _BAR: Foo = 22_i32;
| ^^^^^^^^^^^^^^^^
note: ...which requires promoting constants in MIR for `_BAR`...
--> $DIR/type-alias-impl-trait-const.rs:11:1
|
LL | static _BAR: Foo = 22_i32;
| ^^^^^^^^^^^^^^^^
note: ...which requires const checking `_BAR`...
--> $DIR/type-alias-impl-trait-const.rs:11:1
|
LL | static _BAR: Foo = 22_i32;
| ^^^^^^^^^^^^^^^^
note: ...which requires building MIR for `_BAR`...
--> $DIR/type-alias-impl-trait-const.rs:11:1
|
LL | static _BAR: Foo = 22_i32;
| ^^^^^^^^^^^^^^^^
= note: ...which again requires computing type of `_BAR`, completing the cycle
note: cycle used when checking that `_BAR` is well-formed
--> $DIR/type-alias-impl-trait-const.rs:11:1
|
LL | static _BAR: Foo = 22_i32;
| ^^^^^^^^^^^^^^^^
= 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
error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0391`.