Skip to content

Commit 01b0ee7

Browse files
authored
Merge pull request rust-lang#1908 from ehuss/naked
Update `naked` to use the attribute template
2 parents 3fde038 + 2609387 commit 01b0ee7

1 file changed

Lines changed: 59 additions & 15 deletions

File tree

src/attributes/codegen.md

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -119,41 +119,80 @@ Only the first use of `cold` on a function has effect.
119119
r[attributes.codegen.cold.trait]
120120
When `cold` is applied to a function in a [trait], it applies only to the code of the [default definition].
121121
122+
<!-- template:attributes -->
122123
r[attributes.codegen.naked]
123124
## The `naked` attribute
124125
125126
r[attributes.codegen.naked.intro]
126-
The *`naked` [attribute]* prevents the compiler from emitting a function prologue and epilogue for the attributed function.
127+
The *`naked` [attribute]* prevents the compiler from emitting a function prologue and epilogue for the attributed function --- a *naked function*.
128+
129+
> [!EXAMPLE]
130+
> ```rust
131+
> # #[cfg(target_arch = "x86_64")] {
132+
> /// Adds 3 to the given number.
133+
> // SAFETY: The body respects the "sysv64" calling convention,
134+
> // upholds the signature, and does not fall through.
135+
> #[unsafe(naked)]
136+
> pub extern "sysv64" fn add_n(number: u64) -> u64 {
137+
> core::arch::naked_asm!(
138+
> "add rdi, {}",
139+
> "mov rax, rdi",
140+
> "ret",
141+
> const 3,
142+
> )
143+
> }
144+
> # }
145+
> ```
146+
147+
r[attributes.codegen.naked.syntax]
148+
The `naked` attribute uses the [MetaWord] syntax.
149+
150+
r[attributes.codegen.naked.allowed-positions]
151+
The `naked` attribute may only be applied to [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].
152+
153+
r[attributes.codegen.naked.duplicates]
154+
Only the first use of `naked` on a function has effect.
155+
156+
> [!NOTE]
157+
> `rustc` lints against any use following the first.
158+
159+
r[attributes.codegen.naked.unsafe]
160+
The `naked` attribute must be marked with [`unsafe`][attributes.safety] because the body must respect the function's calling convention, uphold its signature, and either return or diverge (i.e., not fall through past the end of the assembly code).
127161
128162
r[attributes.codegen.naked.body]
129163
The [function body] must consist of exactly one [`naked_asm!`] macro invocation.
130164
131165
r[attributes.codegen.naked.prologue-epilogue]
132-
No function prologue or epilogue is generated for the attributed function. The assembly code in the `naked_asm!` block constitutes the full body of a naked function.
133-
134-
r[attributes.codegen.naked.unsafe-attribute]
135-
The `naked` attribute is an [unsafe attribute]. Annotating a function with `#[unsafe(naked)]` comes with the safety obligation that the body must respect the function's calling convention, uphold its signature, and either return or diverge (i.e., not fall through past the end of the assembly code).
166+
The compiler emits no prologue or epilogue for a naked function: the assembly code in the [`naked_asm!`] invocation constitutes its entire body.
136167
137168
r[attributes.codegen.naked.call-stack]
138-
The assembly code may assume that the call stack and register state are valid on entry as per the signature and calling convention of the function.
169+
On entry the assembly code may assume that the call stack and register state are valid per the function's signature and calling convention.
139170
140171
r[attributes.codegen.naked.no-duplication]
141-
The assembly code may not be duplicated by the compiler except when monomorphizing polymorphic functions.
172+
The compiler may not duplicate the assembly code except when monomorphizing a polymorphic function.
142173
143174
> [!NOTE]
144-
> Guaranteeing when the assembly code may or may not be duplicated is important for naked functions that define symbols.
175+
> This guarantee matters for naked functions that define symbols.
145176
146177
r[attributes.codegen.naked.unused-variables]
147-
The [`unused_variables`] lint is suppressed within naked functions.
178+
The [`unused_variables` lint] is suppressed in naked functions.
148179
149180
r[attributes.codegen.naked.inline]
150-
The [`inline`](#the-inline-attribute) attribute cannot by applied to a naked function.
181+
The [`inline` attribute] cannot be applied to a naked function.
151182
152183
r[attributes.codegen.naked.track_caller]
153-
The [`track_caller`](#the-track_caller-attribute) attribute cannot be applied to a naked function.
184+
The [`track_caller` attribute] cannot be applied to a naked function.
154185
155186
r[attributes.codegen.naked.testing]
156-
The [testing attributes](testing.md) cannot be applied to a naked function.
187+
The [testing attributes] cannot be applied to a naked function.
188+
189+
r[attributes.codegen.naked.target_feature]
190+
The [`target_feature` attribute] cannot be applied to a naked function.
191+
192+
<!-- TODO: Reflexive rules? -->
193+
194+
r[attributes.codegen.naked.abi]
195+
A naked function cannot use the ["Rust" ABI].
157196
158197
<!-- template:attributes -->
159198
r[attributes.codegen.no_builtins]
@@ -851,13 +890,16 @@ If the address of the function is taken as a function pointer, the low bit of th
851890
[`-C target-cpu`]: ../../rustc/codegen-options/index.html#target-cpu
852891
[`-C target-feature`]: ../../rustc/codegen-options/index.html#target-feature
853892
[`export_name`]: abi.export_name
893+
[`inline` attribute]: attributes.codegen.inline
854894
[`is_aarch64_feature_detected`]: ../../std/arch/macro.is_aarch64_feature_detected.html
855895
[`is_x86_feature_detected`]: ../../std/arch/macro.is_x86_feature_detected.html
856896
[`Location`]: core::panic::Location
857-
[`naked_asm!`]: ../inline-assembly.md
897+
[`naked_asm!`]: asm
858898
[`no_mangle`]: abi.no_mangle
899+
[`target_feature` attribute]: attributes.codegen.target_feature
859900
[`target_feature` conditional compilation option]: ../conditional-compilation.md#target_feature
860-
[`unused_variables`]: ../../rustc/lints/listing/warn-by-default.html#unused-variables
901+
[`track_caller` attribute]: attributes.codegen.track_caller
902+
[`unused_variables` lint]: ../../rustc/lints/listing/warn-by-default.html#unused-variables
861903
[associated functions]: items.associated.fn
862904
[async blocks]: expr.block.async
863905
[async closure]: expr.closure.async
@@ -868,11 +910,13 @@ If the address of the function is taken as a function pointer, the low bit of th
868910
[closures]: expr.closure
869911
[default definition]: items.traits.associated-item-decls
870912
[free functions]: items.fn
871-
[function body]: ../items/functions.md#function-body
913+
[function body]: items.fn.body
872914
[functions]: ../items/functions.md
873915
[inherent impl]: items.impl.inherent
916+
["Rust" ABI]: items.extern.abi.rust
874917
[rust-abi]: ../items/external-blocks.md#abi
875918
[target architecture]: ../conditional-compilation.md#target_arch
919+
[testing attributes]: attributes.testing
876920
[trait]: items.traits
877921
[trait definition]: items.traits
878922
[trait impl]: items.impl.trait

0 commit comments

Comments
 (0)