Add and use cfg(target_has_threads) to enforce no_thread impl usage#158782
Conversation
|
Some changes occurred in cfg and check-cfg configuration cc @Urgau |
| | (sym::target_pointer_width, Some(_)) | ||
| | (sym::target_vendor, None | Some(_)) | ||
| | (sym::target_has_atomic, Some(_)) | ||
| | (sym::target_has_threads, Some(_)) |
There was a problem hiding this comment.
We want to prevent --cfg target_has_threads so we need to lint on the none value.
| | (sym::target_has_threads, Some(_)) | |
| | (sym::target_has_threads, None | Some(_)) |
Can you also add a test for it in tests/ui/cfg/disallowed-cli-cfgs.rs.
There was a problem hiding this comment.
Hm, interesting. I'll fix this but is it intentional that we allow users to pass e.g. --cfg 'windows="foo"'? That seems a bit confusing / probably unintentional, but from a quick test seems allowed?
IOW, why aren't all the options here restrictive? I guess it's a breaking change; do we normally FCP / search github / something for changes like this that stop allowing a cfg?
There was a problem hiding this comment.
is it intentional that we allow users to pass e.g.
--cfg 'windows="foo"'?
Yes, it's intentional, when we introduced the lint I was conservative and only linted against the cfgs we set, to not break people that might do --cfg 'windows="foo"', however il-advised that might be.
But this conservative logic doesn't apply to newly added cfgs, as it's very unlikely that someone is doing some shenanigans on not yet added builtin cfgs.
We could try and lint not matter the value.
I guess it's a breaking change
It's a lint change, and lint changes are not considered breaking changes.
We also document that the explicit_builtin_cfgs_in_flags lint against the builtin cfgs (that includes new ones), and have added other cfgs in that list without doing an FCP.
|
r? Urgau |
Unfortunately it seems the target spec field isn't the full truth of what we tell LLVM: rust/compiler/rustc_codegen_llvm/src/back/write.rs Lines 214 to 218 in c397dae |
cb02160 to
ee37726
Compare
|
These commits modify compiler targets. |
|
So with this, how would you enable threads in wasm32-emscripten? |
This shouldn't change anything about behavior, if the target supports threads then it will work. See the previous comment (and fix to the PR since it) around wasm being special in having a target modifier changing the thread support. @bors try jobs=dist-various-* |
This comment has been minimized.
This comment has been minimized.
Add and use cfg(target_has_threads) to enforce no_thread impl usage try-job: dist-various-*
wasm32-emscripten's support of threads depends on which CFLAGS (or RUSTFLAGS, I forget which) you use. |
|
Right, that should be handled by the edit to consider whether the atomics target feature is set or not. This PR really isn't changing any behavior, it's just exposing what was already true, so it shouldn't have any meaningful effect. It may make it easier to see that code is broken (hence the try job as a quick check if there's targets with bad std impls). |
|
Yes, but wasm32-emscripten always says it has atomics, as you pointed out previously. This PR makes a lot of sense, I'm just not sure it will really improve things for our Emscripten target. Should there really be a second Emscripten target for threads? |
It "has" atomics (i.e., the atomic types are defined), but unless Regardless of that, this PR is an improvement for all targets because it means that we would know at compile-time of std if we get the cfg_select wrong and map a target to use the no-thread Mutex/etc. implementations despite the target actually having threads. |
|
@bors r+ |
… r=Urgau Add and use cfg(target_has_threads) to enforce no_thread impl usage The standard library has fallback code for targets without threads (e.g., using a Cell-based Mutex and similar). Today there's no enforcement in std that those targets truly don't have threads which makes that code potentially unsound. This will let us add a static assertion that the target spec agrees that the target is non-threaded. Getting the target spec wrong is already unsound (e.g., LLVM can make use of that) so it's a reasonable source of truth. This pulls in the atomics target feature into the target code and makes the field itself private to encourage going via the method. For now the cfg is added as unstable but if we have a use case for user code to use this, happy to cut a tracking issue and make it a regular unstable feature. Setting a compiler reviewer since the std changes are pretty trivial, and I think anyone can review those reasonably. cc rust-lang#156366 which prompted looking into this r? compiler
|
💔 I suspect this PR failed tests as part of a rollup After fixing the problem, consider running a try job for the failed job before re-approving. Link to failure: #158815 (comment) |
|
This pull request was unapproved. This PR was contained in a rollup (#158815), which was unapproved. |
The standard library has fallback code for targets without threads (e.g., using a Cell-based Mutex and similar). Today there's no enforcement in std that those targets truly don't have threads which makes that code potentially unsound. This will let us add a static assertion that the target spec agrees that the target is non-threaded.
This avoids compiler/rustc_target/src/spec/mod.rs hitting tidy's file length check by moving out a chunk of code into a separate file. It's probably not the last such move needed (this only offsets ~100 lines), but it's a reasonable chunk of code to separate.
ee37726 to
fe2db9c
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
No need for try builds, the failing "test" was a tidy file length check that didn't fail PR CI since that file grew on main after this opened. I split out some content from the relevant file (TargetTuple) in a second commit here which should bring us back under the limit (from ~3001 non-comment+non-blank lines to ~2889). |
|
@bors r=Urgau (The code movement commit doesn't seem like it needs review, we can shuffle code again if there's any issues). |
… r=Urgau Add and use cfg(target_has_threads) to enforce no_thread impl usage The standard library has fallback code for targets without threads (e.g., using a Cell-based Mutex and similar). Today there's no enforcement in std that those targets truly don't have threads which makes that code potentially unsound. This will let us add a static assertion that the target spec agrees that the target is non-threaded. Getting the target spec wrong is already unsound (e.g., LLVM can make use of that) so it's a reasonable source of truth. This pulls in the atomics target feature into the target code and makes the field itself private to encourage going via the method. For now the cfg is added as unstable but if we have a use case for user code to use this, happy to cut a tracking issue and make it a regular unstable feature. Setting a compiler reviewer since the std changes are pretty trivial, and I think anyone can review those reasonably. cc rust-lang#156366 which prompted looking into this r? compiler
Rollup of 20 pull requests Successful merges: - #158377 (add `-Zforce-intrinsic-fallback` flag) - #158642 (Clarify some interning details) - #158743 (Look for cdb location in the registry first) - #158775 (bootstrap: only encode RUSTFLAGS when a flag contains a space) - #158782 (Add and use cfg(target_has_threads) to enforce no_thread impl usage) - #158785 (hook intrinsic-test into aarch64-gnu) - #157734 (Stabilize `local_key_cell_update`) - #158183 (std: allocate less memory in `current_exe` for OpenBSD) - #158671 (Move tests batch 17) - #158730 (Update `FIXME(static_mut_refs)` comments) - #158752 (Reorganize `tests/ui/issues` [18/N]) - #158755 (Use `ThinVec` more in the AST) - #158757 (Fix incorrect tracking issue for `read_le`/`read_be`) - #158765 (Fix ICE on non-ident path in `doc(auto_cfg values)`) - #158771 (library: expand HashSet::extract_if coverage) - #158772 (rustc-dev-guide subtree update) - #158776 (fix: emit diagnostic for AVR target without target-cpu) - #158786 (Add regression test for builtin attr macro values) - #158807 (Add regression test for CString::clone_into unwind safety) - #158825 (Fix typo)
Rollup of 24 pull requests Successful merges: - #158377 (add `-Zforce-intrinsic-fallback` flag) - #158642 (Clarify some interning details) - #158694 (Positive test for closures needing expectations) - #158743 (Look for cdb location in the registry first) - #158775 (bootstrap: only encode RUSTFLAGS when a flag contains a space) - #158782 (Add and use cfg(target_has_threads) to enforce no_thread impl usage) - #158785 (hook intrinsic-test into aarch64-gnu) - #158819 (Put `InhabitedPredicate::NotInModule` earlier in disjunction since it can be a lot faster) - #157734 (Stabilize `local_key_cell_update`) - #158183 (std: allocate less memory in `current_exe` for OpenBSD) - #158310 (Remove unexpected usage of Unambig in non-infer variants) - #158671 (Move tests batch 17) - #158730 (Update `FIXME(static_mut_refs)` comments) - #158752 (Reorganize `tests/ui/issues` [18/N]) - #158755 (Use `ThinVec` more in the AST) - #158757 (Fix incorrect tracking issue for `read_le`/`read_be`) - #158765 (Fix ICE on non-ident path in `doc(auto_cfg values)`) - #158771 (library: expand HashSet::extract_if coverage) - #158772 (rustc-dev-guide subtree update) - #158776 (fix: emit diagnostic for AVR target without target-cpu) - #158786 (Add regression test for builtin attr macro values) - #158810 (Add supplementary information for get_unchecked(mut)) - #158825 (Fix typo) - #158838 (tidy: Use `empty_alternate = true` for triagebot mention glob check)
Rollup of 24 pull requests Successful merges: - #158377 (add `-Zforce-intrinsic-fallback` flag) - #158642 (Clarify some interning details) - #158694 (Positive test for closures needing expectations) - #158743 (Look for cdb location in the registry first) - #158775 (bootstrap: only encode RUSTFLAGS when a flag contains a space) - #158782 (Add and use cfg(target_has_threads) to enforce no_thread impl usage) - #158785 (hook intrinsic-test into aarch64-gnu) - #158819 (Put `InhabitedPredicate::NotInModule` earlier in disjunction since it can be a lot faster) - #157734 (Stabilize `local_key_cell_update`) - #158183 (std: allocate less memory in `current_exe` for OpenBSD) - #158310 (Remove unexpected usage of Unambig in non-infer variants) - #158671 (Move tests batch 17) - #158730 (Update `FIXME(static_mut_refs)` comments) - #158752 (Reorganize `tests/ui/issues` [18/N]) - #158755 (Use `ThinVec` more in the AST) - #158757 (Fix incorrect tracking issue for `read_le`/`read_be`) - #158765 (Fix ICE on non-ident path in `doc(auto_cfg values)`) - #158771 (library: expand HashSet::extract_if coverage) - #158772 (rustc-dev-guide subtree update) - #158776 (fix: emit diagnostic for AVR target without target-cpu) - #158786 (Add regression test for builtin attr macro values) - #158810 (Add supplementary information for get_unchecked(mut)) - #158825 (Fix typo) - #158838 (tidy: Use `empty_alternate = true` for triagebot mention glob check)
Rollup of 24 pull requests Successful merges: - #158377 (add `-Zforce-intrinsic-fallback` flag) - #158642 (Clarify some interning details) - #158694 (Positive test for closures needing expectations) - #158743 (Look for cdb location in the registry first) - #158775 (bootstrap: only encode RUSTFLAGS when a flag contains a space) - #158782 (Add and use cfg(target_has_threads) to enforce no_thread impl usage) - #158785 (hook intrinsic-test into aarch64-gnu) - #158819 (Put `InhabitedPredicate::NotInModule` earlier in disjunction since it can be a lot faster) - #157734 (Stabilize `local_key_cell_update`) - #158183 (std: allocate less memory in `current_exe` for OpenBSD) - #158310 (Remove unexpected usage of Unambig in non-infer variants) - #158671 (Move tests batch 17) - #158730 (Update `FIXME(static_mut_refs)` comments) - #158752 (Reorganize `tests/ui/issues` [18/N]) - #158755 (Use `ThinVec` more in the AST) - #158757 (Fix incorrect tracking issue for `read_le`/`read_be`) - #158765 (Fix ICE on non-ident path in `doc(auto_cfg values)`) - #158771 (library: expand HashSet::extract_if coverage) - #158772 (rustc-dev-guide subtree update) - #158776 (fix: emit diagnostic for AVR target without target-cpu) - #158786 (Add regression test for builtin attr macro values) - #158810 (Add supplementary information for get_unchecked(mut)) - #158825 (Fix typo) - #158838 (tidy: Use `empty_alternate = true` for triagebot mention glob check)
Rollup merge of #158782 - Mark-Simulacrum:expose-no-threads, r=Urgau Add and use cfg(target_has_threads) to enforce no_thread impl usage The standard library has fallback code for targets without threads (e.g., using a Cell-based Mutex and similar). Today there's no enforcement in std that those targets truly don't have threads which makes that code potentially unsound. This will let us add a static assertion that the target spec agrees that the target is non-threaded. Getting the target spec wrong is already unsound (e.g., LLVM can make use of that) so it's a reasonable source of truth. This pulls in the atomics target feature into the target code and makes the field itself private to encourage going via the method. For now the cfg is added as unstable but if we have a use case for user code to use this, happy to cut a tracking issue and make it a regular unstable feature. Setting a compiler reviewer since the std changes are pretty trivial, and I think anyone can review those reasonably. cc #156366 which prompted looking into this r? compiler
View all comments
The standard library has fallback code for targets without threads (e.g., using a Cell-based Mutex and similar). Today there's no enforcement in std that those targets truly don't have threads which makes that code potentially unsound. This will let us add a static assertion that the target spec agrees that the target is non-threaded. Getting the target spec wrong is already unsound (e.g., LLVM can make use of that) so it's a reasonable source of truth. This pulls in the atomics target feature into the target code and makes the field itself private to encourage going via the method.
For now the cfg is added as unstable but if we have a use case for user code to use this, happy to cut a tracking issue and make it a regular unstable feature.
Setting a compiler reviewer since the std changes are pretty trivial, and I think anyone can review those reasonably.
cc #156366 which prompted looking into this
r? compiler