Attributes (v0.2.1)

Declaration modifiers for linkage, calling convention, and compile-time behavior.

Latest

Overview

Attributes add extra instructions to a declaration. They can make a symbol public, bind it to a C name, change how a function is called, or give the compiler a code generation hint.

They are written next to the declaration they affect, so the rule is visible where the function, constant, static, struct, or enum is defined.

Most code only needs a few attributes, usually @public, @extern, @convention, @align, or @packed. Keep experimental attributes in narrow places where their purpose is clear.

Primary source: syntax/attributes/attributes.md

Syntax Signatures

fn printf(fmt: const array[char]) s32
    @public
    @arbitraryArgs
    @extern("printf")
    @convention("C");

@if(isLinux()) const PLATFORM: u32 = 2;

Behavior and Use

Attribute order does not change what they mean, but using a consistent order makes declarations easier to read.

Be careful with @extern and @convention. They describe how Thrust code connects to external symbols, so changing them can break linking or calls into C code.

Best Practices

Complete Attribute Catalog

This list is aligned with the compiler sources in thrustc_attributes, thrustc_token_type, and thrustc_attribute_checker.

Where Each Attribute Applies

For functions, the compiler accepts attributes for visibility, linking, calling convention, inlining, stack behavior, floating-point behavior, constructors, destructors, cuda, and promote.

Assembler functions also accept asm-specific attributes such as @asmSyntax, @asmAlignStack, @asmSideEffects, and asm-throw behavior.

Static and const declarations accept a narrower set: @public, @extern, @linkage, and @align.

Struct declarations accept @public and @packed. Enum declarations accept @public. Local declarations accept @heap and @align.

Linkage and Visibility

Visibility attributes decide whether a symbol remains internal or is visible outside the module.

Calling and Code Generation

Some attributes affect how a function is called or emitted by the backend.

Layout and Storage

Layout attributes should be tied to the data format or ABI that requires them.

Example

fn c_puts(text: CString) s32
    @public
    @extern("puts")
    @convention("C");
fn exported_add(a: s32, b: s32) s32
    @public
    @convention("C") {
    return a + b;
}

Back to language reference