Contents

@cfg selects code at compile time based on the target. It is not runtime control flow. A disabled declaration is removed before semantic analysis, a disabled statement block is emptied, and an expression form chooses one arm during parsing.

Declaration Form#

An attribute before a top-level declaration includes or excludes the declaration.

@cfg(family == unix)
fn platform_value() i32 => 1

@cfg(family == windows)
fn platform_value() i32 => 2

The attribute may appear before or after pub and pure, and may gate fn, type, const, extern, contract, trait, impl, use, and test declarations. A disabled declaration is still parsed for syntax, but it is not added to the program and is not type-checked.

Statement Form#

A statement block may be guarded by @cfg.

@cfg(family == unix) {
    print("unix")
}

@cfg(family == windows) {
    print("windows")
}

Only a brace block is accepted after a statement-level @cfg; a single unbraced statement is not. When the condition is false, the block is parsed and then emptied. The surrounding block remains.

Expression Form#

An expression may choose among compile-time arms.

const separator = @cfg {
    family == windows => "\\"
    family == unix => "/"
}

The arms are tested in order, and the first matching condition supplies the expression. If no arm matches, parsing fails with no @cfg arm matches the target. An unselected arm is parsed but not evaluated or type-checked.

Condition Language#

Key Kind Values
os String linux, macos, windows, freebsd, dragonflybsd, openbsd, netbsd, unknown
arch String x86_64, aarch64, riscv64, unknown
family String unix, windows
endian String little, big
ptr_width Integer Pointer width in bits

A bare identifier other than a key is treated as a string, so os == linux works without declaring linux. Quoted strings work as well. Boolean and integer literals are also allowed.

Operator or predicate Meaning
! Boolean negation
unary - Integer negation
&&, \|\| Boolean and, or
==, != Equality
<, <=, >, >= Integer ordering
not(x) Boolean negation as a call
all(a, b, ...) Every argument is true
any(a, b, ...) At least one argument is true

The condition must produce a boolean. A non-boolean condition is an error.

Examples#

@cfg(any(os == linux, os == macos, os == freebsd))
fn unix_like() bool => true

@cfg(not(os == windows))
const SEPARATOR str = "/"

@cfg(ptr_width == 64)
fn wide() bool => true

A guarded import is useful when a module exists only on one platform.

@cfg(family == unix)
use std/env {args}

Target Selection#

The condition is evaluated against the compile target. -target selects the architecture, and the target name determines the platform when it contains apple or win. A target such as arm64 on a Linux host keeps the host platform, while arm64_apple selects macOS and arm64_win selects Windows. QBE targets report a 64-bit pointer width and little-endian byte order.

hz ast parses and evaluates @cfg against the host target. hz fmt does not parse or evaluate @cfg at all; it is a token-based formatter.

What Is Checked#

A disabled declaration is parsed for syntax but not checked for types. This means a platform-specific declaration can refer to a type or function that exists only on that platform, as long as the file is syntactically valid. It also means a typo in a disabled declaration is not reported until that platform is built.

An expression arm that is not selected is not evaluated. It is still parsed, so its syntax must be valid. A statement block that is disabled is emptied after parsing, so its body must be syntactically valid even when it is not compiled.

Summary#

Form Placement Disabled behaviour
Attribute Before a top-level declaration Declaration removed
Statement block Statement position Block emptied
Expression Expression position First matching arm selected

@cfg is a compile-time selection mechanism, not a runtime branch. It does not introduce a condition into the generated program, and it cannot depend on a runtime value.