Toolchain tests

From carbon-language/carbon-lang · View interactive version

Toolchain tests is an AI agent skill from the GitHub repository carbon-language/carbon-lang. Instructions for authoring, structuring, and running toolchain tests using the file_test infrastructure.

How to use this skill

Skill file

Toolchain tests

<!--

Part of the Carbon Language project, under the Apache License v2.0 with LLVM

Exceptions. See /LICENSE for license information.

SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

-->

Introduction

This skill provides guidelines and patterns for creating and updating tests for

the Carbon toolchain, especially file tests in toolchain/*/testdata/ (for

example, toolchain/check/testdata/).

Toolchain tests evaluate Carbon source files through Lexing, Parsing, Checking,

and optionally Lowering. Output (for example SemIR dumps, Clang errors) is

captured and validated using inline CHECK records.

Language server tests also use file_test, but with quite different

conventions (an LSP message stream, AUTOUPDATE-SPLIT, FROM_FILE_SPLIT).

Refer to the Language server skill

(SKILL.md) for those.

Structure and Authoring

File Layout and Headers

Test files must start with the standard Carbon license, followed by

configuration comments. Separate sections with blank comment lines (//).

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// INCLUDE-FILE: toolchain/testing/testdata/min_prelude/...
//
// AUTOUPDATE

need to hand-write them. It is harmless to add them, but the script will

handle it.

Minimized Preludes

When writing tests entirely unrelated to the Core package, specify a minimal

prelude file using // INCLUDE-FILE. Usually, include

toolchain/testing/testdata/min_prelude/ scripts, such as int.carbon or

primitives.carbon. This significantly speeds up execution and minimizes STDOUT

noise.

<, etc.) are not imported or available inside minimized preludes. To

write tests with a minimal prelude footprint, call primitive builtins

directly (e.g., float.negate, float.div) inside your test code to build

expressions.

Split Tests and `[[@TEST_NAME]]`

A single physical file can test multiple scenarios using split constraints:

// --- passing_case.carbon
library "[[@TEST_NAME]]";

// ...

// --- fail_bad_case.carbon
library "[[@TEST_NAME]]";
// ...

hardcoding the library name. This prevents name conflicts, avoids redefining

the default library, and keeps the test code clean and templateable.

infrastructure automatically replaces it with the split's filename minus

todo_ and fail_ prefixes.

into the same split.** Validation relies on non-failing splits producing

absolutely no errors and failing splits producing the correct compiler

errors independently.

File Prefixing: `fail_` and `todo_`

Expected failures must be differentiated from unexpected failures (and from

bugs). Include prefixes to name individual split files or the main test:

(or produces the wrong errors or otherwise misbehaves with errors).

currently, and shouldn't.

Main File Naming: The main test file (and any split-files) must have a

fail_ prefix if they have an associated error. Exception: The main file

may omit fail_ if it contains a least one split that has a fail_ prefix.

Both the fail_ and todo_ prefixes are stripped from filename properties like

[[@TEST_NAME]].

Constant Evaluation Validation

When testing constant evaluation in semantic checker tests, follow these

conventions to ensure diagnostic stability and accuracy:

(floating-point constants) with identical mathematical values can be

assigned distinct internal representation identifiers based on spelling

variations in source code. To completely prevent literal spelling mismatches

in expected output checks, validation tests must be performed using

canonical comparison methods (for example, passing converted values through

an Expect(X as f64) function).

local runtime variables are rejected as generic function arguments, test

generic type conversions at runtime, and validate compile-time conversions

by passing static literal values directly into primitive builtin calls.

(such as floating-point to integer truncation and rounding), map and execute

test constraints covering every code branch, conditional exit, and fallback

evaluation path.

mathematical boundaries (for example, floating-point literals representing a

tiny fraction above 1.0, such as $2^{30} \times 2^{-30}$ or

$10^{10} \times 10^{-10}$, verifying correct exact truncation down to 1 or

0).

code with exact mathematical precision targeting target thresholds. For

example, if testing the smallest fractional increment above 1.0, use the

exact hex fractional representation (e.g. 0x1.0000000000001p0) or a highly

precise decimal fractional spelling (e.g. 1.0000000000000001) instead of

coarse fractions like 1.1 to ensure correct boundary assertions.

representation limits of target types. Test combinations of mantissas and

exponents that yield values exactly on, just below, or just above the

capacity limits of fixed-size destination types (e.g. signed/unsigned

targets like i32 or u32).

explicitly. Assert that zero inputs are sized and simplified correctly

without triggering calculation underflows, division-by-zero errors, or

underestimating required bit allocations.

Test Code Comments

"train of thought" (for example, "Wait, but...") inside the test files. Any

comments left in tests should be concise and describe what the test _itself_

is validating for human readers.

SemIR Dumps and Minimizing Output

Limit STDOUT checks to the logic under test. Always use //@dump-sem-ir-begin

and //@dump-sem-ir-end around the specific declarations/blocks where SemIR

output is desired. Only use these markers and not

--dump-sem-ir-ranges=if-present or similar extra args—new tests use

//@dump-sem-ir... to naturally filter output to the highlighted segments based

on the default behavior.

//@dump-sem-ir-begin
fn F(x:? form(ref i32));
//@dump-sem-ir-end

Creating/Updating the Output

AI tools should never hand-write or manually touch // CHECK:STDOUT: or

// CHECK:STDERR: comments.

Write your Carbon test code, headers, and // AUTOUPDATE then run the test

updater:

./toolchain/autoupdate_testdata.py toolchain/PATH/TO/YOUR/TEST.carbon

Review the updated test outputs (for example, by way of git diff). Ensure

logic paths are correctly tested rather than producing massive boilerplate

blocks.

Autoupdating makes the tests pass whether or not the new behavior is correct, so

deciding that the new output is the output you wanted is a separate step. See

the Review testdata changes skill.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.