Skip to content

Commit e38127d

Browse files
committed
Update inline to use the attribute template
1 parent 281f3a4 commit e38127d

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/attributes/codegen.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,49 @@ r[attributes.codegen.inline]
99
r[attributes.codegen.inline.intro]
1010
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.
1111

12+
> [!EXAMPLE]
13+
> ```rust
14+
> #[inline]
15+
> pub fn example1() {}
16+
>
17+
> #[inline(always)]
18+
> pub fn example2() {}
19+
>
20+
> #[inline(never)]
21+
> pub fn example3() {}
22+
> ```
23+
1224
> [!NOTE]
1325
> 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.
1426
27+
r[attributes.codegen.inline.syntax]
28+
The syntax for the `inline` attribute is:
29+
30+
```grammar,attributes
31+
@root InlineAttribute ->
32+
`inline` `(` `always` `)`
33+
| `inline` `(` `never` `)`
34+
| `inline`
35+
```
36+
37+
r[attributes.codegen.inline.allowed-positions]
38+
The `inline` attribute may only be used on:
39+
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]
45+
46+
> [!NOTE]
47+
> `rustc` currently warns when `inline` is used in some other positions. This may become an error in the future.
48+
49+
r[attributes.codegen.inline.duplicates]
50+
Only the first instance of `inline` on an item is honored. Subsequent `inline` attributes are ignored.
51+
52+
> [!NOTE]
53+
> `rustc` currently warns on duplicate `inline` attributes. This may become an error in the future.
54+
1555
r[attributes.codegen.inline.modes]
1656
There are three ways to use the inline attribute:
1757

0 commit comments

Comments
 (0)