You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The following restrictions apply unless otherwise specified by the platform rules below:
91
+
92
+
- Safe `#[target_feature]` functions (and closures that inherit the attribute) can only be safely called within a caller that enables all the `target_feature`s that the callee enables.
93
+
This restriction does not apply in an `unsafe` context.
94
+
- Safe `#[target_feature]` functions (and closures that inherit the attribute) can only be coerced to *safe* function pointers in contexts that enable all the `target_feature`s that the coercee enables.
95
+
This restriction does not apply to `unsafe` function pointers.
96
+
97
+
Implicitly enabled features are included in this rule. For example an `sse2` function can call ones marked with `sse`.
98
+
99
+
```rust
100
+
# #[cfg(target_feature ="avx2")] {
101
+
#[target_feature(enable ="avx")]
102
+
fnfoo_avx() {}
103
+
104
+
fnbar() {
105
+
// Calling `foo_avx` here is unsafe, as we must ensure that AVX is
106
+
// available first, even if `avx` is enabled by default on the target
107
+
// platform or manually enabled as compiler flags.
108
+
unsafe {
109
+
foo_avx();
110
+
}
111
+
}
112
+
113
+
#[target_feature(enable ="avx")]
114
+
fnbar_avx() {
115
+
// Calling `foo_avx` here is safe.
116
+
foo_avx();
117
+
||foo_avx();
118
+
}
119
+
120
+
#[target_feature(enable ="avx2")]
121
+
fnbar_avx2() {
122
+
// Calling `foo_avx` here is safe because `avx2` implies `avx`.
123
+
foo_avx();
124
+
}
125
+
# }
126
+
```
127
+
128
+
r[attributes.codegen.target_feature.fn-traits]
129
+
A function with a `#[target_feature]` attribute *never* implements the `Fn` family of traits, although closures inheriting features from the enclosing function do.
Copy file name to clipboardExpand all lines: src/unsafe-keyword.md
+3-1
Original file line number
Diff line number
Diff line change
@@ -27,7 +27,7 @@ r[unsafe.block]
27
27
## Unsafe blocks (`unsafe {}`)
28
28
29
29
r[unsafe.block.intro]
30
-
A block of code can be prefixed with the `unsafe` keyword, to permit calling `unsafe` functions or dereferencing raw pointers.
30
+
A block of code can be prefixed with the `unsafe` keyword to permit using the unsafe actions as defined in the [Unsafety] chapter, such as calling other unsafe functions or dereferencing raw pointers.
31
31
32
32
r[unsafe.block.fn-body]
33
33
By default, the body of an unsafe function is also considered to be an unsafe block;
@@ -48,6 +48,8 @@ For example, a doubly-linked list is not a tree structure and can only be repres
48
48
By using `unsafe` blocks to represent the reverse links as raw pointers, it can be implemented without reference counting.
49
49
(See ["Learn Rust With Entirely Too Many Linked Lists"](https://rust-unofficial.github.io/too-many-lists/) for a more in-depth exploration of this particular example.)
Copy file name to clipboardExpand all lines: src/unsafety.md
+3
Original file line number
Diff line number
Diff line change
@@ -21,6 +21,9 @@ r[safety.unsafe-union-access]
21
21
r[safety.unsafe-call]
22
22
- Calling an unsafe function (including an intrinsic or foreign function).
23
23
24
+
r[safety.unsafe-target-feature-call]
25
+
- Calling a safe function marked with a [`target_feature`][attributes.codegen.target_feature] from a function that does not have a `target_feature` attribute enabling the same features (see [attributes.codegen.target_feature.safety-restrictions]).
0 commit comments