1
1
# Stability attributes
2
2
3
- This section is about the stability attributes and schemes that allow stable APIs to use unstable
4
- APIs internally in the rustc standard library.
3
+ This section is about the stability attributes and schemes that allow stable
4
+ APIs to use unstable APIs internally in the rustc standard library.
5
5
6
- For instructions on stabilizing a language feature see
7
- [ Stabilizing Features] ( ./stabilization_guide.md ) .
6
+ For instructions on stabilizing a language feature see [ Stabilizing
7
+ Features] ( ./stabilization_guide.md ) .
8
8
9
9
## unstable
10
10
11
- The ` #[unstable(feature = "foo", issue = "1234", reason = "lorem ipsum")] ` attribute explicitly
12
- marks an item as unstable. Items that are marked as "unstable" cannot be used
13
- without a corresponding ` #![feature] ` attribute on the crate, even on a
14
- nightly compiler. This restriction only applies across crate boundaries, unstable
15
- items may be used within the crate they are defined.
11
+ The ` #[unstable(feature = "foo", issue = "1234", reason = "lorem ipsum")] `
12
+ attribute explicitly marks an item as unstable. Items that are marked as
13
+ "unstable" cannot be used without a corresponding ` #![feature] ` attribute on
14
+ the crate, even on a nightly compiler. This restriction only applies across
15
+ crate boundaries, unstable items may be used within the crate they are defined.
16
16
17
- The ` unstable ` attribute infects all sub-items, where the attribute doesn't have to be
18
- reapplied. So if you apply this to a module, all items in the module will be unstable.
17
+ The ` unstable ` attribute infects all sub-items, where the attribute doesn't
18
+ have to be reapplied. So if you apply this to a module, all items in the module
19
+ will be unstable.
19
20
20
- You can make specific sub-items stable by using the ` #[stable] ` attribute on them.
21
- The stability scheme works similarly to how ` pub ` works. You can have public functions of
22
- nonpublic modules and you can have stable functions in unstable modules or vice versa.
21
+ You can make specific sub-items stable by using the ` #[stable] ` attribute on
22
+ them. The stability scheme works similarly to how ` pub ` works. You can have
23
+ public functions of nonpublic modules and you can have stable functions in
24
+ unstable modules or vice versa.
23
25
24
26
Note, however, that due to a [ rustc bug] , stable items inside unstable modules
25
27
* are* available to stable code in that location! So, for example, stable code
26
- can import ` core::intrinsics::transmute ` even though ` intrinsics ` is an unstable
27
- module. Thus, this kind of nesting should be avoided when possible.
28
+ can import ` core::intrinsics::transmute ` even though ` intrinsics ` is an
29
+ unstable module. Thus, this kind of nesting should be avoided when possible.
28
30
29
31
The ` unstable ` attribute may also have the ` soft ` value, which makes it a
30
32
future-incompatible deny-by-default lint instead of a hard error. This is used
@@ -35,29 +37,32 @@ prevents breaking dependencies by leveraging Cargo's lint capping.
35
37
36
38
## stable
37
39
38
- The ` #[stable(feature = "foo", "since = "1.420.69")] ` attribute explicitly marks an item as
39
- stabilized. To do this, follow the instructions in
40
+ The ` #[stable(feature = "foo", "since = "1.420.69")] ` attribute explicitly
41
+ marks an item as stabilized. To do this, follow the instructions in
40
42
[ Stabilizing Features] ( ./stabilization_guide.md ) .
41
43
42
44
Note that stable functions may use unstable things in their body.
43
45
44
46
## allow_internal_unstable
45
47
46
- Macros, compiler desugarings and ` const fn ` s expose their bodies to the call site. To
47
- work around not being able to use unstable things in the standard library's macros, there's the
48
- ` #[allow_internal_unstable(feature1, feature2)] ` attribute that whitelists the given features for
49
- usage in stable macros or ` const fn ` s.
50
-
51
- Note that ` const fn ` s are even more special in this regard. You can't just whitelist any feature,
52
- the features need an implementation in ` qualify_min_const_fn.rs ` . For example the ` const_fn_union `
53
- feature gate allows accessing fields of unions inside stable ` const fn ` s. The rules for when it's
54
- ok to use such a feature gate are that behavior matches the runtime behavior of the same code
55
- (see also [ this blog post] [ blog ] ). This means that you may not create a
56
- ` const fn ` that e.g. transmutes a memory address to an integer, because the addresses of things
57
- are nondeterministic and often unknown at compile-time.
58
-
59
- Always ping @oli-obk , @RalfJung , and @Centril if you are adding more ` allow_internal_unstable `
60
- attributes to any ` const fn `
48
+ Macros, compiler desugarings and ` const fn ` s expose their bodies to the call
49
+ site. To work around not being able to use unstable things in the standard
50
+ library's macros, there's the ` #[allow_internal_unstable(feature1, feature2)] `
51
+ attribute that whitelists the given features for usage in stable macros or
52
+ ` const fn ` s.
53
+
54
+ Note that ` const fn ` s are even more special in this regard. You can't just
55
+ whitelist any feature, the features need an implementation in
56
+ ` qualify_min_const_fn.rs ` . For example the ` const_fn_union ` feature gate allows
57
+ accessing fields of unions inside stable ` const fn ` s. The rules for when it's
58
+ ok to use such a feature gate are that behavior matches the runtime behavior of
59
+ the same code (see also [ this blog post] [ blog ] ). This means that you may not
60
+ create a ` const fn ` that e.g. transmutes a memory address to an integer,
61
+ because the addresses of things are nondeterministic and often unknown at
62
+ compile-time.
63
+
64
+ Always ping @oli-obk , @RalfJung , and @Centril if you are adding more
65
+ ` allow_internal_unstable ` attributes to any ` const fn `
61
66
62
67
## staged_api
63
68
@@ -83,13 +88,13 @@ item must also have a `stable` or `unstable` attribute.
83
88
)]
84
89
```
85
90
86
- The ` suggestion ` field is optional. If given, it should be a string that can
87
- be used as a machine-applicable suggestion to correct the warning. This is
88
- typically used when the identifier is renamed, but no other significant
89
- changes are necessary.
91
+ The ` suggestion ` field is optional. If given, it should be a string that can be
92
+ used as a machine-applicable suggestion to correct the warning. This is
93
+ typically used when the identifier is renamed, but no other significant changes
94
+ are necessary.
90
95
91
- Another difference from the ` deprecated ` attribute is that the ` since ` field
92
- is actually checked against the current version of ` rustc ` . If ` since ` is in a
96
+ Another difference from the ` deprecated ` attribute is that the ` since ` field is
97
+ actually checked against the current version of ` rustc ` . If ` since ` is in a
93
98
future version, then the ` deprecated_in_future ` lint is triggered which is
94
99
default ` allow ` , but most of the standard library raises it to a warning with
95
100
` #![warn(deprecated_in_future)] ` .
0 commit comments