Skip to content

std::compiler

Compiler and analyzer operations for test tooling and code generators.

Import with import std::compiler.

struct Diagnostic
    line: Int
    col: Int
    message: String
end

A compiler diagnostic with 1-based source coordinates.

impl Debug for Diagnostic

fn inspect(value: Diagnostic) -> String

impl Debug.inspect

struct Hover
    signature: String
    markdown: String
end

Hover text returned by the same renderer used by the editor.

impl Debug for Hover

fn inspect(value: Hover) -> String

impl Debug.inspect

struct Project
    entry_point: String
    files: Map<String, String>
    manifest: Maybe<String> = None
end

An in-memory Nomi project for compiler tests.

entry_point names the file in files to check as the entry file. File keys are import paths without .nomi, so "main" represents main.nomi and "api/user" represents api/user.nomi.

manifest is optional nomi.toml content. When present, checks that depend on package metadata, such as entry_points, use it.

impl Debug for Project

fn inspect(value: Project) -> String

impl Debug.inspect

struct RunFile
    entry_point: String
    env: Map<String, String> = {}
end

Options for running a .nomi file under the current test project root.

impl Debug for RunFile

fn inspect(value: RunFile) -> String

impl Debug.inspect

fn check(source: String) -> List<Diagnostic>

Checks source as a standalone Nomi entry file and returns every diagnostic as data.

The source is not a file of the current test project: its imports resolve against the project root, and the project files it imports are checked under the project’s nomi.toml, but entry_points does not apply to the source itself, so it may declare a fn main wherever the calling test lives.

import std::compiler
import std::strings.String

diagnostics = compiler.check(
    """
    fn main()
      dbg missing
    end
    """
)

assert [diagnostic] = diagnostics
assert String.contains(diagnostic.message, "undefined variable 'missing'")
fn format(source: String) -> Result<String, String>

Formats source as nomi fmt formats a file.

A tool that generates Nomi source passes its text through format before writing it, so the file it writes is the one nomi fmt would leave unchanged. Err(message) is the parse error when source does not parse.

import std::compiler

formatted = compiler.format(
    """
    fn  double(n:Int)->Int
    n*2
    end
    """
)?

assert formatted == "fn double(n: Int) -> Int\n    n * 2\nend\n"
assert Err(_) = compiler.format("fn (")
fn run(source: String) -> Result<String, String>

Runs source as a standalone Nomi entry file and captures stdout.

The source is placed in the current test project as compiler.check places it: imports resolve against the project root, and entry_points does not apply to the source.

Err(message) is returned for parse, analysis, or runtime errors. A runtime error is what nomi run reports for the same source, including error: <text> for a main that returns Err and a deadline that runs out while main blocks; output printed before it is not returned. Use compiler.check when the diagnostic itself is the behavior under test; use compiler.run when output is the behavior under test.

import std::compiler
import std::strings.String

case compiler.run(
    """
    fn main()
      _ = dbg 41 + 1
    end
    """
)
    Ok(out) -> assert String.contains(out, "41 + 1 = 42")
    Err(e)  -> assert String.contains(e, "unreachable: the program above runs")
end
fn run_file(run: RunFile) -> Result<String, String>

Runs a .nomi file under the current test project root and captures stdout.

fn hover(source: String) -> Result<Hover, String>

Returns hover content for the single ˇ marker in source.

The marker is removed before parsing. Use it only in compiler/editor tests, where it marks the cursor position without making the fixture depend on line and column numbers.

import std::compiler

hover = compiler.hover(
    """
    fn main()
      ansˇwer = 42
      _ = answer
    end
    """
)?

assert hover.signature == "answer: Int"
fn check_project(project: Project) -> List<Diagnostic>

Checks an in-memory project and returns every diagnostic as data. A project that cannot be assembled, a manifest that does not parse or an entry point that is not one of the files, is one diagnostic at line 0, column 0, naming the problem.

import std::compiler
import std::strings.String

diagnostics = compiler.check_project(Project{
    entry_point: "main",
    manifest: Maybe.Some(
        """
        [module]
        name = "demo"
        entry_points = ["main"]
        """
    ),
    files: {
        "main" => """
            import api.{Widget, make}

            fn main()
              w = make("Ada")
              Widget.secret(w)
            end
            """,
        "api" => """
            pub struct Widget
              name: String
            end

            fn secret(w: Widget) -> String
              "secret " + w.name
            end

            pub fn make(name: String) -> Widget
              Widget{name:}
            end
            """,
    },
})

assert [diagnostic] = diagnostics
assert String.contains(diagnostic.message, "no member 'secret'")

assert [unread] = compiler.check_project(Project{
    entry_point: "main",
    manifest:    Maybe.Some("[module"),
    files:       {"main" => "fn main()\n  Unit\nend\n"},
})

assert unread.line == 0
assert String.contains(unread.message, "compiler.Project manifest")