diff options
| author | Camille GILLOT <gillot.camille@gmail.com> | 2025-07-26 21:46:20 +0000 |
|---|---|---|
| committer | Camille GILLOT <gillot.camille@gmail.com> | 2025-07-26 21:53:13 +0000 |
| commit | 8817572b4595df352e6a7fdd56422fb07cd28d89 (patch) | |
| tree | 5375a7a84cab2e711becf0bded7836c8381a0b4a | |
| parent | 7c6496145f86133655a941e0dafd5dfa368514d4 (diff) | |
| download | rust-8817572b4595df352e6a7fdd56422fb07cd28d89.tar.gz rust-8817572b4595df352e6a7fdd56422fb07cd28d89.zip | |
Do not check Sync during type_of.
| -rw-r--r-- | compiler/rustc_hir_analysis/src/check/check.rs | 2 | ||||
| -rw-r--r-- | compiler/rustc_hir_analysis/src/check/wfcheck.rs | 5 | ||||
| -rw-r--r-- | compiler/rustc_hir_analysis/src/collect/type_of.rs | 4 | ||||
| -rw-r--r-- | tests/ui/coroutine/layout-error.rs | 2 | ||||
| -rw-r--r-- | tests/ui/coroutine/layout-error.stderr | 92 | ||||
| -rw-r--r-- | tests/ui/coroutine/metadata-sufficient-for-layout.rs | 2 | ||||
| -rw-r--r-- | tests/ui/coroutine/metadata-sufficient-for-layout.stderr | 38 | ||||
| -rw-r--r-- | tests/ui/issues/issue-7364.rs | 1 | ||||
| -rw-r--r-- | tests/ui/issues/issue-7364.stderr | 14 | ||||
| -rw-r--r-- | tests/ui/static/issue-24446.rs | 1 | ||||
| -rw-r--r-- | tests/ui/static/issue-24446.stderr | 11 | ||||
| -rw-r--r-- | tests/ui/type-alias-impl-trait/type-alias-impl-trait-const.rs | 5 | ||||
| -rw-r--r-- | tests/ui/type-alias-impl-trait/type-alias-impl-trait-const.stderr | 113 |
13 files changed, 26 insertions, 264 deletions
diff --git a/compiler/rustc_hir_analysis/src/check/check.rs b/compiler/rustc_hir_analysis/src/check/check.rs index 07ca64e6144..f2be2a65716 100644 --- a/compiler/rustc_hir_analysis/src/check/check.rs +++ b/compiler/rustc_hir_analysis/src/check/check.rs @@ -767,6 +767,8 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Result<(), DefKind::Static { .. } => { check_static_inhabited(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)), _ => unreachable!(), diff --git a/compiler/rustc_hir_analysis/src/check/wfcheck.rs b/compiler/rustc_hir_analysis/src/check/wfcheck.rs index e8d962467b3..a62efed13bc 100644 --- a/compiler/rustc_hir_analysis/src/check/wfcheck.rs +++ b/compiler/rustc_hir_analysis/src/check/wfcheck.rs @@ -1184,6 +1184,7 @@ pub(crate) fn check_static_item<'tcx>( tcx: TyCtxt<'tcx>, item_id: LocalDefId, ty: Ty<'tcx>, + should_check_for_sync: bool, ) -> Result<(), ErrorGuaranteed> { enter_wf_checking_ctxt(tcx, item_id, |wfcx| { 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`. - let should_check_for_sync = tcx.static_mutability(item_id.to_def_id()) - == Some(hir::Mutability::Not) + let should_check_for_sync = should_check_for_sync && !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()); if should_check_for_sync { diff --git a/compiler/rustc_hir_analysis/src/collect/type_of.rs b/compiler/rustc_hir_analysis/src/collect/type_of.rs index f84bd2a5fb3..68a91212e50 100644 --- a/compiler/rustc_hir_analysis/src/collect/type_of.rs +++ b/compiler/rustc_hir_analysis/src/collect/type_of.rs @@ -221,7 +221,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_ let ty = icx.lower_ty(ty); // MIR relies on references to statics being scalars. // 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, 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); // MIR relies on references to statics being scalars. // 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, Err(guar) => Ty::new_error(tcx, guar), } diff --git a/tests/ui/coroutine/layout-error.rs b/tests/ui/coroutine/layout-error.rs index 137c47490f6..7f6714542c1 100644 --- a/tests/ui/coroutine/layout-error.rs +++ b/tests/ui/coroutine/layout-error.rs @@ -32,7 +32,5 @@ where // Check that statics are inhabited computes they layout. static POOL: Task<F> = Task::new(); -//~^ ERROR cycle detected when computing type of `POOL` -//~| ERROR cycle detected when computing type of `POOL` fn main() {} diff --git a/tests/ui/coroutine/layout-error.stderr b/tests/ui/coroutine/layout-error.stderr index c0ededfd3fe..f3b3843de89 100644 --- a/tests/ui/coroutine/layout-error.stderr +++ b/tests/ui/coroutine/layout-error.stderr @@ -4,94 +4,6 @@ error[E0425]: cannot find value `Foo` in this scope LL | let a = Foo; | ^^^ not found in this scope -error[E0391]: cycle detected when computing type of `POOL` - --> $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` - --> $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 +error: aborting due to 1 previous error -Some errors have detailed explanations: E0391, E0425. -For more information about an error, try `rustc --explain E0391`. +For more information about this error, try `rustc --explain E0425`. diff --git a/tests/ui/coroutine/metadata-sufficient-for-layout.rs b/tests/ui/coroutine/metadata-sufficient-for-layout.rs index 0c074029faa..b7d8575c761 100644 --- a/tests/ui/coroutine/metadata-sufficient-for-layout.rs +++ b/tests/ui/coroutine/metadata-sufficient-for-layout.rs @@ -4,6 +4,7 @@ // Regression test for #80998. // //@ aux-build:metadata-sufficient-for-layout.rs +//@ check-pass #![feature(type_alias_impl_trait, rustc_attrs)] #![feature(coroutine_trait)] @@ -22,6 +23,5 @@ mod helper { // Static queries the layout of the coroutine. static A: Option<helper::F> = None; -//~^ ERROR cycle detected when computing type of `A` fn main() {} diff --git a/tests/ui/coroutine/metadata-sufficient-for-layout.stderr b/tests/ui/coroutine/metadata-sufficient-for-layout.stderr deleted file mode 100644 index 94f90098510..00000000000 --- a/tests/ui/coroutine/metadata-sufficient-for-layout.stderr +++ /dev/null @@ -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`. diff --git a/tests/ui/issues/issue-7364.rs b/tests/ui/issues/issue-7364.rs index 79642bd411b..4ce9beb68cd 100644 --- a/tests/ui/issues/issue-7364.rs +++ b/tests/ui/issues/issue-7364.rs @@ -3,5 +3,6 @@ use std::cell::RefCell; // Regression test for issue 7364 static boxed: Box<RefCell<isize>> = Box::new(RefCell::new(0)); //~^ ERROR `RefCell<isize>` cannot be shared between threads safely [E0277] +//~| ERROR cannot call non-const associated function fn main() { } diff --git a/tests/ui/issues/issue-7364.stderr b/tests/ui/issues/issue-7364.stderr index 7371e2105de..a47a90c90ce 100644 --- a/tests/ui/issues/issue-7364.stderr +++ b/tests/ui/issues/issue-7364.stderr @@ -11,6 +11,16 @@ note: required because it appears within the type `Box<RefCell<isize>>` --> $SRC_DIR/alloc/src/boxed.rs:LL:COL = 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(|| ...)` + +error: aborting due to 2 previous errors -For more information about this error, try `rustc --explain E0277`. +Some errors have detailed explanations: E0015, E0277. +For more information about an error, try `rustc --explain E0015`. diff --git a/tests/ui/static/issue-24446.rs b/tests/ui/static/issue-24446.rs index 9ab952ade9c..ffd6dfabc28 100644 --- a/tests/ui/static/issue-24446.rs +++ b/tests/ui/static/issue-24446.rs @@ -1,7 +1,6 @@ fn main() { static foo: dyn Fn() -> u32 = || -> u32 { //~^ ERROR the size for values of type - //~| ERROR cannot be shared between threads safely 0 }; } diff --git a/tests/ui/static/issue-24446.stderr b/tests/ui/static/issue-24446.stderr index 359952dcf66..497ce8ccfb6 100644 --- a/tests/ui/static/issue-24446.stderr +++ b/tests/ui/static/issue-24446.stderr @@ -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 --> $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)` = 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`. diff --git a/tests/ui/type-alias-impl-trait/type-alias-impl-trait-const.rs b/tests/ui/type-alias-impl-trait/type-alias-impl-trait-const.rs index bfe30f3706f..e21627e14b0 100644 --- a/tests/ui/type-alias-impl-trait/type-alias-impl-trait-const.rs +++ b/tests/ui/type-alias-impl-trait/type-alias-impl-trait-const.rs @@ -1,3 +1,5 @@ +//@ check-pass + #![feature(type_alias_impl_trait)] use std::fmt::Debug; @@ -9,8 +11,5 @@ const _FOO: Foo = 5; #[define_opaque(Foo)] 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() {} diff --git a/tests/ui/type-alias-impl-trait/type-alias-impl-trait-const.stderr b/tests/ui/type-alias-impl-trait/type-alias-impl-trait-const.stderr deleted file mode 100644 index 6537f0f35a3..00000000000 --- a/tests/ui/type-alias-impl-trait/type-alias-impl-trait-const.stderr +++ /dev/null @@ -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`. |
