Attributes

An attribute is a #Name(...) call written before what it describes. 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. The parentheses are always written, even when empty:

import Core::Panic;

#NoReturn()
#Warn("Abort skips the cleanup that Exit runs")
pub func Abort() {
    Panic("aborted");
}

The attributes

AttributeArgumentsApplies to
#Linklibrary, optional symbolextern func, extern { } blocks
#Abi.C, .SysV or .Win64functions, asm func, extern func, extern { } blocks
#NoReturnnonefunctions and extern func without a result
#Warna message stringfunctions, methods, extern func
#Errora message stringfunctions, methods, extern func
#Allowa lint rule namedeclarations (rule-dependent)
#Formatnonea char8[..] parameter
#Testnonefunctions without parameters, outside extend and when
#Unsendablenonestruct, 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:

MistakeError
a name Rux does not haveunknown attribute call '#Foo'
an attribute on the wrong declaration'#Abi' can only be applied to a function or extern block
the same attribute twiceduplicate '#Abi' attribute, duplicate '#Link' attribute
arguments where none are taken'#NoReturn' does not accept arguments
an unknown parameter attributeunknown 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