33This section is about the stability attributes and schemes that allow stable APIs to use unstable
44APIs internally in the rustc standard library.
55
6- For instructions on stabilizing a language feature see
6+ For instructions on stabilizing a language feature see
77[ Stabilizing Features] ( ./stabilization_guide.md ) .
88
9- # unstable
9+ ## unstable
1010
1111The ` #[unstable(feature = "foo", issue = "1234", reason = "lorem ipsum")] ` attribute explicitly
12- marks an item as unstable. This infects all sub-items, where the attribute doesn't have to be
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.
16+
17+ The ` unstable ` attribute infects all sub-items, where the attribute doesn't have to be
1318reapplied. So if you apply this to a module, all items in the module will be unstable.
1419
1520You can make specific sub-items stable by using the ` #[stable] ` attribute on them.
@@ -21,17 +26,22 @@ Note, however, that due to a [rustc bug], stable items inside unstable modules
2126can import ` core::intrinsics::transmute ` even though ` intrinsics ` is an unstable
2227module. Thus, this kind of nesting should be avoided when possible.
2328
29+ The ` unstable ` attribute may also have the ` soft ` value, which makes it a
30+ future-incompatible deny-by-default lint instead of a hard error. This is used
31+ by the ` bench ` attribute which was accidentally accepted in the past. This
32+ prevents breaking dependencies by leveraging Cargo's lint capping.
33+
2434[ rustc bug ] : https://github.com/rust-lang/rust/issues/15702
2535
26- # stable
36+ ## stable
2737
2838The ` #[stable(feature = "foo", "since = "1.420.69")] ` attribute explicitly marks an item as
2939stabilized. To do this, follow the instructions in
3040[ Stabilizing Features] ( ./stabilization_guide.md ) .
3141
3242Note that stable functions may use unstable things in their body.
3343
34- # allow_internal_unstable
44+ ## allow_internal_unstable
3545
3646Macros, compiler desugarings and ` const fn ` s expose their bodies to the call site. To
3747work around not being able to use unstable things in the standard library's macros, there's the
@@ -49,4 +59,67 @@ are nondeterministic and often unknown at compile-time.
4959Always ping @oli-obk , @RalfJung , and @Centril if you are adding more ` allow_internal_unstable `
5060attributes to any ` const fn `
5161
62+ ## staged_api
63+
64+ Any crate that uses the ` stable ` , ` unstable ` , or ` rustc_deprecated ` attributes
65+ must include the ` #![feature(staged_api)] ` attribute on the crate.
66+
67+ ## rustc_deprecated
68+
69+ The deprecation system shares the same infrastructure as the stable/unstable
70+ attributes. The ` rustc_deprecated ` attribute is similar to the [ ` deprecated `
71+ attribute] . It was previously called ` deprecated ` , but was split off when
72+ ` deprecated ` was stabilized. The ` deprecated ` attribute cannot be used in a
73+ ` staged_api ` crate, ` rustc_deprecated ` must be used instead. The deprecated
74+ item must also have a ` stable ` or ` unstable ` attribute.
75+
76+ ` rustc_deprecated ` has the following form:
77+
78+ ``` rust,ignore
79+ #[rustc_deprecated(
80+ since = "1.38.0",
81+ reason = "explanation for deprecation",
82+ suggestion = "other_function"
83+ )]
84+ ```
85+
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.
90+
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
93+ future version, then the ` deprecated_in_future ` lint is triggered which is
94+ default ` allow ` , but most of the standard library raises it to a warning with
95+ ` #![warn(deprecated_in_future)] ` .
96+
97+ [ `deprecated` attribute ] : https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-deprecated-attribute
98+
99+ ## -Zforce-unstable-if-unmarked
100+
101+ The ` -Zforce-unstable-if-unmarked ` flag has a variety of purposes to help
102+ enforce that the correct crates are marked as unstable, but can still use
103+ private crates without special attributes. It was introduced primarily to
104+ allow rustc and the standard library to link to arbitrary crates on crates.io
105+ which do not themselves use ` staged_api ` . ` rustc ` also relies on this flag to
106+ mark all of its crates as unstable with the ` rustc_private ` feature so that
107+ each crate does not need to be carefully marked with ` unstable ` .
108+
109+ This flag is automatically applied to all of ` rustc ` and the standard library
110+ by the bootstrap scripts. This is needed because the compiler and all of its
111+ dependencies are shipped in the sysroot to all users.
112+
113+ This flag has the following effects:
114+
115+ - Marks the crate as "unstable" with the ` rustc_private ` feature if it is not
116+ itself marked as stable or unstable.
117+ - Allows these crates to access other forced-unstable crates without any need
118+ for attributes.
119+
120+ Code which does not use ` -Zforce-unstable-if-unmarked ` should include the
121+ ` #![feature(rustc_private)] ` crate attribute to access these force-unstable
122+ crates. This is needed for things that link ` rustc ` , such as ` miri ` , ` rls ` , or
123+ ` clippy ` .
124+
52125[ blog ] : https://www.ralfj.de/blog/2018/07/19/const.html
0 commit comments