Attributes
An attribute is a #Name written before what it describes, with its arguments in parentheses when it takes any. It instructs the compiler — link a foreign library, fix a calling convention, flag a function at every call — without changing the code of the declaration it annotates. Rux has a fixed set of attributes; a program cannot define new ones.
attribute = "#" identifier [ "(" argument { "," argument } ")" ]
attributes = { attribute }
declaration-with-attributes = attributes [ "pub" ] declaration
parameter-with-attributes = attributes parameter
Attributes come before pub, one after another. An attribute that takes no arguments is written without parentheses:
import Core::Panic;
#NoReturn
#Warn("Abort skips the cleanup that Exit runs")
pub func Abort() {
Panic("aborted");
}
The attributes
| Attribute | Arguments | Applies to |
|---|---|---|
#Link | library, optional symbol | extern func, extern variables, extern { } blocks |
#Abi | .C, .SysV or .Win64 | functions, asm func, extern func, extern { } blocks |
#NoReturn | none | functions and extern func without a result |
#Warn | a message string | functions, methods, extern func |
#Error | a message string | functions, methods, extern func |
#Allow | a lint rule name | declarations (rule-dependent) |
#Format | none | a char8[..] parameter |
#Test | none | functions without parameters, outside extend and when |
#Unsendable | none | struct, variant and union declarations |
#NoCopy | none | struct, variant and union declarations |
#NoMove | none | struct, variant and union declarations |
#Format is the one parameter attribute; it is written before a parameter inside the parameter list. All the others are declaration attributes, written before a declaration at the package root, inside a module, or before a member of an extend block. Members of an extern { } block take no attributes of their own — the attributes on the block apply to all of them:
error: expected 'func' or a variable declaration in the external block before '#'
#Error and #Warn also have a second form, as directives inside a function body or a when arm — see Build diagnostics.
Rules
Each attribute checks where it stands and what it is given, and the errors name the rule:
| Mistake | Error |
|---|---|
| a name Rux does not have | unknown attribute call '#Foo' |
| an attribute on the wrong declaration | '#Abi' can only be applied to a function or extern block |
| the same attribute twice | duplicate '#Abi' attribute, duplicate '#Link' attribute |
| empty parentheses | '#Test' is written without parentheses |
| arguments where none are taken | '#NoReturn' does not accept arguments |
| an unknown parameter attribute | unknown parameter attribute '#Trim' |
Restricting code to one platform is not an attribute. Use conditional compilation: when #target.os == .Windows { … } removes the other platforms' code before it is ever resolved.
See also
- Foreign function interface — where
#Linkand#Abiare used - Build diagnostics —
#Errorand#Warnas directives - Functions — the declarations most attributes annotate
#Test— unit tests thatrux testruns- Learn: Compile error, ABI
Diagnostics
The #Error and #Warn directives: stopping or annotating a build from source, where they may be written, and why they belong inside when.
#Unsendable
The #Unsendable attribute: marking a type whose values stay on the thread that made them, the Sendable bound it takes away, and the errors for misuse.