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
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.
Copy file name to clipboardExpand all lines: src/attributes/codegen.md
+53-36Lines changed: 53 additions & 36 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,11 +3,12 @@ r[attributes.codegen]
3
3
4
4
The following [attributes] are used for controlling code generation.
5
5
6
+
<!-- template:attributes -->
6
7
r[attributes.codegen.inline]
7
8
### The `inline` attribute
8
9
9
10
r[attributes.codegen.inline.intro]
10
-
The *`inline`[attribute]* suggests that a copy of the attributed functionshould 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.
11
12
12
13
> [!EXAMPLE]
13
14
> ```rust
@@ -22,7 +23,7 @@ The *`inline` [attribute]* suggests that a copy of the attributed function shoul
@@ -35,44 +36,53 @@ The syntax for the `inline` attribute is:
35
36
```
36
37
37
38
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] .
-[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.
45
43
46
44
> [!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
+
> letf= #[inline] || (); // ERROR
56
+
> ```
48
57
49
58
r[attributes.codegen.inline.duplicates]
50
-
Only the first instance of `inline` on an item is honored. Subsequent `inline` attributes are ignored.
59
+
Onlythefirstuse of `inline` on a function has effect.
51
60
52
61
> [!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.
54
63
55
64
r[attributes.codegen.inline.modes]
56
-
There are three ways to use the inline attribute:
65
+
The `inline` attribute supports these modes:
57
66
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.
- `#[inline(always)]` *suggests* that inline expansion should always be performed.
69
+
- `#[inline(never)]` *suggests* that inline expansion should never be performed.
61
70
62
71
> [!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
+
> Inevery form the attribute is a hint.The compiler may ignore it.
64
73
65
74
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].
67
76
68
77
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 exportedwith [`no_mangle`] or [`export_name`].
70
79
80
+
<!-- template:attributes -->
71
81
r[attributes.codegen.cold]
72
82
### The `cold` attribute
73
83
74
84
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.
76
86
77
87
> [!EXAMPLE]
78
88
> ```rust
@@ -81,31 +91,27 @@ The *`cold` [attribute]* suggests that the attributed function is unlikely to be
81
91
> ```
82
92
83
93
r[attributes.codegen.cold.syntax]
84
-
The `cold` attributeusesthe [MetaWord] syntaxandthusdoesnottakeanyinputs.
94
+
The `cold` attribute uses the [MetaWord] syntax.
85
95
86
96
r[attributes.codegen.cold.allowed-positions]
87
-
The `cold` attributemayonlybeusedon:
97
+
The `cold` attribute may only be applied to functions with [bodies] --- [closures], [async blocks], [free functions], [associated functions] in an [inherent impl] or [traitimpl], and associated functions in a [trait definition] when those functions have a [default definition] .
> 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.
98
104
99
105
<!--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).-->
> `rustc` lints against any use following the first.This may become an error in the future.
106
112
107
113
r[attributes.codegen.cold.trait]
108
-
When `cold` isappliedtoafunctionina [traitdefinition], itappliesonlytothatfunctionwhenusedasadefaultfunctionforatraitimplementationandnottoalltraitimplementations.
114
+
When `cold` is applied to a function in a [trait], it applies only to the code of the [default definition].
The `instruction_set` attribute uses the [MetaListPaths] syntax to specify a single path consisting of the architecture family name and instruction set name.
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] .
720
726
721
727
> [!NOTE]
722
728
> `rustc` ignores use in other positions but lints against it. This may become an error in the future.
723
729
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
+
724
733
r[attributes.codegen.instruction_set.duplicates]
725
734
The `instruction_set` attribute may be used only once on a function.
726
735
@@ -745,21 +754,29 @@ If the address of the function is taken as a function pointer, the low bit of th
0 commit comments