Skip to content

Attributes needing new infrastructure: #when, #test, #bench, #on_start/#on_exit #5

Description

@chrischtel

Phase 3 added the tractable built-in attributes: #cold, #section("…"), #weak, #link_name("…"), #keep (backend), and #must_use (sema) — plus #maybe_unused is accepted. These remaining ones each need infrastructure beyond a simple attribute flag:

#when(cond) — conditional declaration inclusion

Drop a top-level declaration when a comptime cond folds to false (e.g. #when(core::os == "windows")). Top-level #if is not currently supported, so this fills a real gap. Needs a pre-sema pass (like runCompilerHookPass): tolerant sema → ComptimeVm → evaluate each #when cond to a bool → filter the decls before the strict pass. The dropped decls must be removed before sema so platform-specific code in the inactive branch isn't type-checked. (src/pipeline.zig — mirror the compiler-hook pass; reuse ComptimeVm.evalToImm.)

#test / #bench

Mark functions for collection by a test/benchmark runner. Needs: a collection pass that gathers #test/#bench functions, a generated entry point that runs them, and wiring into the build system's test_dir step (which currently compiles+runs whole files). Related to the existing b.test_dir(...) mechanism.

#on_start / #on_exit

Run a fn() (void, no args) at program startup / shutdown. Maps to LLVM @llvm.global_ctors / @llvm.global_dtors with a priority. Needs backend support to append the function to those global arrays. (src/backend/llvm — add a global_ctors/dtors emitter; the IrFunction would carry an on_start/on_exit priority.)

All are parsed today (unknown attributes are silently accepted), so adding them is additive — no migration. Priority is roughly #when > #on_start/#on_exit > #test/#bench.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions