-
Notifications
You must be signed in to change notification settings - Fork 13k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Stabilize target_feature_11 #134090
base: master
Are you sure you want to change the base?
Stabilize target_feature_11 #134090
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
Oddly, this means this compiles #[target_feature(enable = "avx2")]
fn bar() -> fn() {
|| avx2()
} but this does not #[target_feature(enable = "avx2")]
fn bar() -> fn() {
avx2
} That's not a blocker but might be worth an issue. (But changing this may mean we have to refine the safety condition.)
The definition of when something "live"s is subtle. I would make it about execution: you must guarantee that the target feature is available while the function or any closure defined inside that function executes. This is generally ensured because target features, once available, cannot usually be taken back; if you work in an environment where they can be taken back, it is your responsibility to ensure that no code inside a target feature function (including inside a closure) runs after this (until the feature is enabled again).
There is an enumeration saying that the attribute is allowed on main etc, only to then state that it is forbidden. This is confusing. Have we covered all special functions? |
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as outdated.
This comment was marked as outdated.
7006f05
to
1f28e68
Compare
This comment was marked as resolved.
This comment was marked as resolved.
This comment has been minimized.
This comment has been minimized.
1f28e68
to
668ab73
Compare
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
Add some convenience helper methods on `hir::Safety` Makes a lot of call sites simpler and should make any refactorings needed for rust-lang#134090 (comment) simpler, as fewer sites have to be touched in case we end up storing some information in the variants of `hir::Safety`
Rollup merge of rust-lang#134285 - oli-obk:push-vwrqsqlwnuxo, r=Urgau Add some convenience helper methods on `hir::Safety` Makes a lot of call sites simpler and should make any refactorings needed for rust-lang#134090 (comment) simpler, as fewer sites have to be touched in case we end up storing some information in the variants of `hir::Safety`
This comment was marked as resolved.
This comment was marked as resolved.
I don't think target_feature is special here. You have the same "issue" for unsafe functions: unsafe fn foo() {}
fn main() {
let f = if true {
None
} else {
unsafe{Some(|| foo())}
}.unwrap_or_default();
f();
} |
No, I meant closures implicitly implementing default if they could :-)
This would indeed fix the requirement for |
Ah right, unsafe fn cannot have |
…-ptr, r=oli-obk Allow coercing safe-to-call target_feature functions to safe fn pointers r? oli-obk `@oli-obk:` this is based on your PR rust-lang#134353 :-) See rust-lang#134090 (comment) for the motivation behind this change.
Rollup merge of rust-lang#135504 - veluca93:target-feature-cast-to-fn-ptr, r=oli-obk Allow coercing safe-to-call target_feature functions to safe fn pointers r? oli-obk `@oli-obk:` this is based on your PR rust-lang#134353 :-) See rust-lang#134090 (comment) for the motivation behind this change.
This comment has been minimized.
This comment has been minimized.
My worry here is not even necessary having the unsafe in dead code and then reconstructing the closure otherwise. I expect it to be likely that you're able to extract the closure type out of a target feature fn without ever using unsafe using opaque types. I am not confident that the type system does not allow that for RPIT and it definitely allows it with TAITs. So if closures implement |
Sure, but as Oli was pointing out, that situation would be broken already, right? In particular it would allow you to synthetize closures generated in unsafe blocks, bypassing checks people might have put there:
If closure types ever implemented Default, both of those examples would be unsound. |
No, because the function only returns // Unsound without ever actually using `unsafe`
type Tait = impl FnOnce() + Default;
#[target_feature(whatever)]
fn f() -> Tait {
|| target_feature_fn()
} |
Even if target_feature would create a slightly worse situation were Default to be implemented for all closures that could implement Default, I am not convinced that doing so would ever be a good idea, because it would make it significantly harder to write sound unsafe code. If the eventual decision is to make closures implement Default if possible, it would be fairly easy to have closures that do inherit target_features not implement Default, as Ralf suggested, no? |
"No unsafe block" and "unsafe block only in dead code" does not make much of a difference IMO.
|
☔ The latest upstream changes (presumably #134299) made this pull request unmergeable. Please resolve the merge conflicts. |
@rustbot labels +S-waiting-on-documentation Note that we don't want to merge this, even after the FCP completes, until the PR to the Reference (rust-lang/reference#1181) has been approved (it needs some updates for that to happen). So for the moment, this is blocked on documentation. |
I opened rust-lang/reference#1720 for this. While writing the documentation, I noticed another small inconsistency in the behaviour of target_feature_11 which we might want to resolve before merging (@RalfJung / @workingjubilee / @lcnr for comments); consider the following snippet:
Currently, |
The final comment period, with a disposition to merge, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. This will be merged soon. |
See rust-lang#134090 (comment) for the motivation behind this suggestion.
As discussed on Zulip, having fn items implement Fn traits conditionally would require significant changes to trait resolution, so the best solution is to explicitly mention coercion to fn pointer in the error message (#136064). I also updated the reference PR accordingly. |
…, r=wesleywiser Treat safe target_feature functions as unsafe by default [less invasive variant] This unblocks * #134090 As I stated in rust-lang/rust#134090 (comment) I think the previous impl was too easy to get wrong, as by default it treated safe target feature functions as safe and had to add additional checks for when they weren't. Now the logic is inverted. By default they are unsafe and you have to explicitly handle safe target feature functions. This is the less (imo) invasive variant of #134317, as it doesn't require changing the Safety enum, so it only affects FnDefs and nothing else, as it should.
Stabilization report
This is an updated version of #116114, which is itself a redo of #99767. Most of this commit and report were copied from those PRs. Thanks @LeSeulArtichaut and @calebzulawski!
Summary
Allows for safe functions to be marked with
#[target_feature]
attributes.Functions marked with
#[target_feature]
are generally considered as unsafe functions: they are unsafe to call, cannot generally be assigned to safe function pointers, and don't implement theFn*
traits.However, calling them from other
#[target_feature]
functions with a superset of features is safe.Moreover, once #135504 is merged, they can be converted to safe function pointers in a context in which calling them is safe:
See the section "Closures" below for justification of this behaviour.
Test cases
Tests for this feature can be found in
tests/ui/target_feature/
.Edge cases
Closures
Closures defined inside functions marked with #[target_feature] inherit the target features of their parent function. They can still be assigned to safe function pointers and implement the appropriate
Fn*
traits.This means that in order to call a function with #[target_feature], you must guarantee that the target-feature is available while the function, any closures defined inside it, as well as any safe function pointers obtained from target-feature functions inside it, execute.
This is usually ensured because target features are assumed to never disappear, and:
#[target_feature]
function, presence of the target feature is guaranteed by the programmer through the safety requirements of the unsafe call.If you work in an environment where target features can be disabled, it is your responsibility to ensure that no code inside a target feature function (including inside a closure) runs after this (until the feature is enabled again).
Note: this has an effect on existing code, as nowadays closures do not inherit features from the enclosing function, and thus this strengthens a safety requirement. It was originally proposed in #73631 to solve this by adding a new type of UB: “taking a target feature away from your process after having run code that uses that target feature is UB” .
This was motivated by userspace code already assuming in a few places that CPU features never disappear from a program during execution (see i.e. https://github.com/rust-lang/stdarch/blob/2e29bdf90832931ea499755bb4ad7a6b0809295a/crates/std_detect/src/detect/arch/x86.rs); however, concerns were raised in the context of the Linux kernel; thus, we propose to relax that requirement to "causing the set of usable features to be reduced is unsafe; when doing so, the programmer is required to ensure that no closures or safe fn pointers that use removed features are still in scope".
Closures accept
#[inline(always)]
, even within functions marked with#[target_feature]
. Since these attributes conflict,#[inline(always)]
wins out to maintain compatibility.ABI concerns
The ABI of some types can change when compiling a function with different target features. This could have introduced unsoundness with target_feature_11, but recent fixes (#133102, #132173) either make those situations invalid or make the ABI no longer dependent on features. Thus, those issues should no longer occur.
Special functions
The
#[target_feature]
attribute is forbidden from a variety of special functions, such as main, current and future lang items (e.g.#[start]
,#[panic_handler]
), safe default trait implementations and safe trait methods.This was not disallowed at the time of the first stabilization PR for target_features_11, and resulted in the following issues/PRs:
#[target_feature]
is allowed onmain
#108645#[target_feature]
is allowed on default implementations #108646#[target_feature]
on lang item functions #115910Documentation
target_feature_11
feature reference#1181cc tracking issue #69098
cc @workingjubilee
cc @RalfJung
r? @rust-lang/lang