Skip to content

Commit 4e3d6b2

Browse files
committed
Revise text for inline and cold
Note in particular that `inline` and similar attributes can be applied to `async` blocks as well as to closures. Along with the editorial revisions, let's say that and also discuss the current limitations that affect how these can be applied.
1 parent 48ffb2e commit 4e3d6b2

File tree

1 file changed

+53
-36
lines changed

1 file changed

+53
-36
lines changed

src/attributes/codegen.md

Lines changed: 53 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ r[attributes.codegen]
33

44
The following [attributes] are used for controlling code generation.
55

6+
<!-- template:attributes -->
67
r[attributes.codegen.inline]
78
### The `inline` attribute
89

910
r[attributes.codegen.inline.intro]
10-
The *`inline` [attribute]* suggests that a copy of the attributed function should be placed in the caller, rather than generating code to call the function where it is defined.
11+
The *`inline` [attribute]* suggests whether a copy of the attributed function's code should be placed in the caller rather than generating a call to the function.
1112

1213
> [!EXAMPLE]
1314
> ```rust
@@ -22,7 +23,7 @@ The *`inline` [attribute]* suggests that a copy of the attributed function shoul
2223
> ```
2324
2425
> [!NOTE]
25-
> The `rustc` compiler automatically inlines functions based on internal heuristics. Incorrectly inlining functions can make the program slower, so this attribute should be used with care.
26+
> `rustc` automatically inlines functions when doing so seems worthwhile. Use this attribute carefully as poor decisions about what to inline can slow down programs.
2627
2728
r[attributes.codegen.inline.syntax]
2829
The syntax for the `inline` attribute is:
@@ -35,44 +36,53 @@ The syntax for the `inline` attribute is:
3536
```
3637
3738
r[attributes.codegen.inline.allowed-positions]
38-
The `inline` attribute may only be used on:
39+
The `inline` attribute may only be applied to functions with [bodies] --- [closures], [async blocks], [free functions], [associated functions] in an [inherent impl] or [trait impl], and associated functions in a [trait definition] when those functions have a [default definition] .
3940

40-
- [Free functions][items.fn]
41-
- [Inherent associated functions][items.associated.fn]
42-
- [Trait impl functions][items.impl.trait]
43-
- [Trait definition functions][items.traits] with a body
44-
- [Closures][expr.closure]
41+
> [!NOTE]
42+
> `rustc` ignores use in other positions but lints against it. This may become an error in the future.
4543
4644
> [!NOTE]
47-
> `rustc` currently warns when `inline` is used in some other positions. This may become an error in the future.
45+
> Though the attribute can be applied to [closures] and [async blocks], the usefulness of this is limited as we do not yet support attributes on expressions.
46+
>
47+
> ```rust
48+
> // We allow attributes on statements.
49+
> #[inline] || (); // OK
50+
> #[inline] async {}; // OK
51+
> ```
52+
>
53+
> ```rust,compile_fail,E0658
54+
> // We don't yet allow attributes on expressions.
55+
> let f = #[inline] || (); // ERROR
56+
> ```
4857
4958
r[attributes.codegen.inline.duplicates]
50-
Only the first instance of `inline` on an item is honored. Subsequent `inline` attributes are ignored.
59+
Only the first use of `inline` on a function has effect.
5160
5261
> [!NOTE]
53-
> `rustc` currently warns on duplicate `inline` attributes. This may become an error in the future.
62+
> `rustc` lints against any use following the first. This may become an error in the future.
5463
5564
r[attributes.codegen.inline.modes]
56-
There are three ways to use the inline attribute:
65+
The `inline` attribute supports these modes:
5766
58-
* `#[inline]` *suggests* performing an inline expansion.
59-
* `#[inline(always)]` *suggests* that an inline expansion should always be performed.
60-
* `#[inline(never)]` *suggests* that an inline expansion should never be performed.
67+
- `#[inline]` *suggests* performing inline expansion.
68+
- `#[inline(always)]` *suggests* that inline expansion should always be performed.
69+
- `#[inline(never)]` *suggests* that inline expansion should never be performed.
6170
6271
> [!NOTE]
63-
> `#[inline]` in every form is a hint, with no *requirements* on the language to place a copy of the attributed function in the caller.
72+
> In every form the attribute is a hint. The compiler may ignore it.
6473
6574
r[attributes.codegen.inline.trait]
66-
When `inline` is applied to a function in a [trait definition], it applies only to that function when used as a default function for a trait implementation and not to all trait implementations.
75+
When `inline` is applied to a function in a [trait], it applies only to the code of the [default definition].
6776
6877
r[attributes.codegen.inline.externally-exported]
69-
The `inline` attribute is ignored if the function is externally exported. This may happen with the [`no_mangle`] or [`export_name`] attribute.
78+
The `inline` attribute is ignored if the function is externally exported with [`no_mangle`] or [`export_name`].
7079
80+
<!-- template:attributes -->
7181
r[attributes.codegen.cold]
7282
### The `cold` attribute
7383
7484
r[attributes.codegen.cold.intro]
75-
The *`cold` [attribute]* suggests that the attributed function is unlikely to be called.
85+
The *`cold` [attribute]* suggests that the attributed function is unlikely to be called which may help the compiler produce better code.
7686
7787
> [!EXAMPLE]
7888
> ```rust
@@ -81,31 +91,27 @@ The *`cold` [attribute]* suggests that the attributed function is unlikely to be
8191
> ```
8292
8393
r[attributes.codegen.cold.syntax]
84-
The `cold` attribute uses the [MetaWord] syntax and thus does not take any inputs.
94+
The `cold` attribute uses the [MetaWord] syntax.
8595
8696
r[attributes.codegen.cold.allowed-positions]
87-
The `cold` attribute may only be used on:
97+
The `cold` attribute may only be applied to functions with [bodies] --- [closures], [async blocks], [free functions], [associated functions] in an [inherent impl] or [trait impl], and associated functions in a [trait definition] when those functions have a [default definition] .
8898
89-
- [Free functions][items.fn]
90-
- [Inherent associated functions][items.associated.fn]
91-
- [Trait impl functions][items.impl.trait]
92-
- [Trait definition functions][items.traits] with a body
93-
- [External block functions][items.extern.fn]
94-
- [Closures][expr.closure]
99+
> [!NOTE]
100+
> `rustc` ignores use in other positions but lints against it. This may become an error in the future.
95101
96102
> [!NOTE]
97-
> `rustc` currently warns when `inline` is used in some other positions. This may become an error in the future.
103+
> Though the attribute can be applied to [closures] and [async blocks], the usefulness of this is limited as we do not yet support attributes on expressions.
98104
99105
<!-- TODO: rustc currently seems to allow cold on a trait function without a body, but it appears to be ignored. I think that may be a bug, and it should at least warn if not reject (like inline does). -->
100106
101107
r[attributes.codegen.cold.duplicates]
102-
Duplicate instances of the `cold` attribute are ignored.
108+
Only the first use of `cold` on a function has effect.
103109
104110
> [!NOTE]
105-
> `rustc` currently warns on duplicate `cold` attributes.
111+
> `rustc` lints against any use following the first. This may become an error in the future.
106112
107113
r[attributes.codegen.cold.trait]
108-
When `cold` is applied to a function in a [trait definition], it applies only to that function when used as a default function for a trait implementation and not to all trait implementations.
114+
When `cold` is applied to a function in a [trait], it applies only to the code of the [default definition].
109115
110116
r[attributes.codegen.naked]
111117
## The `naked` attribute
@@ -716,11 +722,14 @@ r[attributes.codegen.instruction_set.syntax]
716722
The `instruction_set` attribute uses the [MetaListPaths] syntax to specify a single path consisting of the architecture family name and instruction set name.
717723
718724
r[attributes.codegen.instruction_set.allowed-positions]
719-
The `instruction_set` attribute may only be applied to functions, including [closures][expr.closure], [free functions][items.fn], and associated functions defined (i.e. with a body) in [inherent impls][items.associated.fn], [trait impls][items.impl.trait], and [trait definitions][items.traits].
725+
The `instruction_set` attribute may only be applied to functions with [bodies] --- [closures], [async blocks], [free functions], [associated functions] in an [inherent impl] or [trait impl], and associated functions in a [trait definition] when those functions have a [default definition] .
720726
721727
> [!NOTE]
722728
> `rustc` ignores use in other positions but lints against it. This may become an error in the future.
723729
730+
> [!NOTE]
731+
> Though the attribute can be applied to [closures] and [async blocks], the usefulness of this is limited as we do not yet support attributes on expressions.
732+
724733
r[attributes.codegen.instruction_set.duplicates]
725734
The `instruction_set` attribute may be used only once on a function.
726735
@@ -745,21 +754,29 @@ If the address of the function is taken as a function pointer, the low bit of th
745754
746755
[`-C target-cpu`]: ../../rustc/codegen-options/index.html#target-cpu
747756
[`-C target-feature`]: ../../rustc/codegen-options/index.html#target-feature
748-
[`export_name`]: ../abi.md#the-export_name-attribute
757+
[`export_name`]: abi.export_name
749758
[`is_aarch64_feature_detected`]: ../../std/arch/macro.is_aarch64_feature_detected.html
750759
[`is_x86_feature_detected`]: ../../std/arch/macro.is_x86_feature_detected.html
751760
[`Location`]: core::panic::Location
752761
[`naked_asm!`]: ../inline-assembly.md
753-
[`no_mangle`]: ../abi.md#the-no_mangle-attribute
762+
[`no_mangle`]: abi.no_mangle
754763
[`target_feature` conditional compilation option]: ../conditional-compilation.md#target_feature
755764
[`unused_variables`]: ../../rustc/lints/listing/warn-by-default.html#unused-variables
765+
[async blocks]: expr.block.async
766+
[associated functions]: items.associated.fn
756767
[attribute]: ../attributes.md
757768
[attributes]: ../attributes.md
769+
[bodies]: items.fn.body
770+
[closures]: expr.closure
771+
[default definition]: items.traits.associated-item-decls
772+
[free functions]: items.fn
758773
[function body]: ../items/functions.md#function-body
759774
[functions]: ../items/functions.md
775+
[inherent impl]: items.impl.inherent
760776
[rust-abi]: ../items/external-blocks.md#abi
761777
[target architecture]: ../conditional-compilation.md#target_arch
762-
[trait]: ../items/traits.md
763-
[trait definition]: ../items/traits.md
778+
[trait]: items.traits
779+
[trait definition]: items.traits
780+
[trait impl]: items.impl.trait
764781
[undefined behavior]: ../behavior-considered-undefined.md
765782
[unsafe attribute]: ../attributes.md#r-attributes.safety

0 commit comments

Comments
 (0)