Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,26 @@ if(BUILD_TESTING)
--llvm "${NIL_ENABLE_LLVM}"
)
set_tests_properties(nilbuild.project-contract PROPERTIES LABELS "tools;project;analysis;rc" TIMEOUT 180)
add_test(NAME nilbuild.incremental-contract
COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/tools/incremental_build_contract.py"
--builder $<TARGET_FILE:nilbuild> --compiler $<TARGET_FILE:nilcompiler>
--interpreter $<TARGET_FILE:nilinterpreter> --stdlib "${CMAKE_CURRENT_SOURCE_DIR}/nilstd/src"
--llvm "${NIL_ENABLE_LLVM}")
set_tests_properties(nilbuild.incremental-contract PROPERTIES LABELS "tools;project;rc" TIMEOUT 180)
add_test(NAME nilbuild.parallel-process-contract
COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/tools/parallel_process_contract.py"
--root "${CMAKE_CURRENT_SOURCE_DIR}" --cxx "${CMAKE_CXX_COMPILER}")
set_tests_properties(nilbuild.parallel-process-contract PROPERTIES LABELS "tools;project;rc" TIMEOUT 60)
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
add_test(NAME nilbuild.toolchain-inputs
COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/tools/toolchain_inputs_contract.py"
--root "${CMAKE_CURRENT_SOURCE_DIR}" --cxx "${CMAKE_CXX_COMPILER}" --driver "${CMAKE_CXX_COMPILER}")
set_tests_properties(nilbuild.toolchain-inputs PROPERTIES LABELS "tools;project;rc" TIMEOUT 90)
endif()
add_test(NAME nilbuild.analysis-dependencies
COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/tools/analysis_dependencies.py"
--compiler $<TARGET_FILE:nilcompiler>)
set_tests_properties(nilbuild.analysis-dependencies PROPERTIES LABELS "tools;analysis;project;rc" TIMEOUT 60)
add_test(
NAME ny.inline-assembly
COMMAND "${Python3_EXECUTABLE}"
Expand Down Expand Up @@ -550,6 +570,19 @@ if(BUILD_TESTING)
--tool $<TARGET_FILE:matrix_lex_dump>
--root "${CMAKE_CURRENT_SOURCE_DIR}")
set_tests_properties(matrix.lexer PROPERTIES LABELS "matrix;lexer" TIMEOUT 60)
add_test(NAME matrix.annotations
COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/matrix/annotation_contract.py"
--compiler $<TARGET_FILE:nilcompiler>
--interpreter $<TARGET_FILE:nilinterpreter>
--llvm "${NIL_ENABLE_LLVM}")
set_tests_properties(matrix.annotations PROPERTIES LABELS "matrix;reflection;routes" TIMEOUT 180)
add_test(NAME nil.switch-contract
COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tests/tools/switch_contract.py"
--compiler $<TARGET_FILE:nilcompiler>
--interpreter $<TARGET_FILE:nilinterpreter>
--debugger $<TARGET_FILE:nildebugger>
--llvm "${NIL_ENABLE_LLVM}")
set_tests_properties(nil.switch-contract PROPERTIES LABELS "matrix;ny;routes;typecheck;debugger" TIMEOUT 240)
# Matrix execution conformance: each page lowers to NIL and runs on the
# interpreter with exact expected output. Acceptance is covered by the
# lexer test; this checks that the lowering means what the surface says.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ checked bindings across both languages; references and rename are available for
locals whose complete occurrence sets the compiler can certify.

For projects with multiple source modules, add a
[`nil.project.json`](docs/tools/projects.md) manifest. Project check, build and
[`nil.project.nildata`](docs/tools/projects.md) manifest. Project check, build and
run commands use the same import paths and analyzer policy as live checks.

## Examples
Expand Down
71 changes: 71 additions & 0 deletions common/content_digest.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#pragma once

// SHA-256 content identities for build inputs. Timestamps are not cache keys.
#include <array>
#include <cstdint>
#include <filesystem>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <stdexcept>
#include <string>

namespace nil::detail {
class sha256 {
std::array<std::uint32_t, 8> state{0x6a09e667,0xbb67ae85,0x3c6ef372,0xa54ff53a,
0x510e527f,0x9b05688c,0x1f83d9ab,0x5be0cd19};
std::array<unsigned char, 64> block{};
std::uint64_t bytes = 0;
std::size_t used = 0;
static std::uint32_t rotate(std::uint32_t x, unsigned n) { return (x >> n) | (x << (32 - n)); }
void transform() {
static constexpr std::uint32_t k[] = {
0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,
0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,
0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,
0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,
0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,
0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070,
0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,
0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2};
std::array<std::uint32_t,64> w{};
for (unsigned i=0; i<16; ++i) w[i]=(std::uint32_t(block[4*i])<<24)|(std::uint32_t(block[4*i+1])<<16)|(std::uint32_t(block[4*i+2])<<8)|block[4*i+3];
for (unsigned i=16; i<64; ++i) {
const auto a=w[i-15], b=w[i-2];
w[i]=w[i-16]+(rotate(a,7)^rotate(a,18)^(a>>3))+w[i-7]+(rotate(b,17)^rotate(b,19)^(b>>10));
}
auto a=state[0],b=state[1],c=state[2],d=state[3],e=state[4],f=state[5],g=state[6],h=state[7];
for (unsigned i=0; i<64; ++i) {
const auto t1=h+(rotate(e,6)^rotate(e,11)^rotate(e,25))+((e&f)^(~e&g))+k[i]+w[i];
const auto t2=(rotate(a,2)^rotate(a,13)^rotate(a,22))+((a&b)^(a&c)^(b&c));
h=g;g=f;f=e;e=d+t1;d=c;c=b;b=a;a=t1+t2;
}
state[0]+=a;state[1]+=b;state[2]+=c;state[3]+=d;state[4]+=e;state[5]+=f;state[6]+=g;state[7]+=h;
}
public:
void update(const char *data, std::size_t length) {
bytes+=length;
for (std::size_t i=0;i<length;++i) { block[used++]=static_cast<unsigned char>(data[i]); if(used==64){transform();used=0;} }
}
std::string finish() {
const auto bits=bytes*8;
const char padding=static_cast<char>(0x80), zero=0;
update(&padding,1);
while(used!=56) update(&zero,1);
for(int i=7;i>=0;--i) { const char byte=static_cast<char>(bits>>(i*8));update(&byte,1); }
std::ostringstream out; out<<std::hex<<std::setfill('0');
for(auto word:state) out<<std::setw(8)<<word;
return out.str();
}
};
inline std::string content_digest(const std::string &text) { sha256 hash; hash.update(text.data(),text.size());return hash.finish(); }
inline std::string file_digest(const std::filesystem::path &path) {
if(!std::filesystem::is_regular_file(path)) throw std::runtime_error("expected a regular build input: "+path.string());
std::ifstream input(path,std::ios::binary);
if(!input) throw std::runtime_error("cannot read build input: "+path.string());
sha256 hash; char buffer[65536];
while(input.read(buffer,sizeof(buffer))||input.gcount()) hash.update(buffer,static_cast<std::size_t>(input.gcount()));
if(input.bad()) throw std::runtime_error("cannot read build input: "+path.string());
return hash.finish();
}
} // namespace nil::detail
2 changes: 2 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ do not assume every documented combination is accepted by every backend yet.
- [Classes and polymorphism](language-reference/classes.md)
- [Expressions](language-reference/expressions.md)
- [Statements](language-reference/statements.md)
- [Switch statements and expressions](language-reference/switch.md)
- [Faults and cleanup](language-reference/faults.md)
- [Functions](language-reference/functions.md)
- [Annotations](language-reference/annotations.md)
- [Modules](language-reference/modules.md)
- [Traits](language-reference/traits.md)

Expand Down
9 changes: 9 additions & 0 deletions docs/advanced/nil-format.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ declaration-line record/header. Misplaced, malformed and orphan records reject;
older readers ignore this optional annotation comment rather than treating
its contents as instructions.

Record, class, and enum annotations containing the same line controls or
trailing whitespace use
`# declaration_annotation "<NIL-escaped exact annotation>"` before their schema
declaration. This additive text record preserves metadata that cannot fit
losslessly on an ordinary `@name(...)` line. `function_annotation` remains
specific to functions; annotation strings that fit ordinary text records keep
their existing spelling. Binary schema annotations retain their existing
encoding.

## Binary executable v3 layout

The source ownership profile uses `OWN_ENTER`/`OWN_LEAVE` lexical scopes,
Expand Down
111 changes: 108 additions & 3 deletions docs/analysis-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ source: `NIL2001` reports unreachable statements after unconditional exits, and
`NIL2002` reports standalone calls whose standard `Option`/`Result` outcomes
are discarded. These are warnings without automatic fixes. Imported modules
receive compiler checks but are not recursively linted by these rules.
These rules traverse named function/method and test bodies; expression-embedded
lambda bodies remain outside this initial traversal.
These rules traverse named function/method and test bodies, nested lambdas,
and switches in expression position.

`nilcompiler --list-analysis-rules` returns a `nil.analyzers.v1` catalog with
the `nil.default` analyzer and each rule's code, title, description, default
Expand All @@ -169,6 +169,46 @@ declarations/annotations and 1 MiB of metadata, and marks truncation explicitly.
The full JSON response is limited to 8 MiB. These are representation limits,
not a compiler execution sandbox or time budget.

## Build input dependencies

Analysis reports also expose metadata for incremental build clients:

| Field | Meaning |
| --- | --- |
| `dependencies` | Sorted, unique `{path, digest}` entries for consumed native files. `path` is canonical and absolute; `digest` is lowercase hexadecimal SHA-256 of the exact consumed bytes. |
| `dependenciesComplete` | True only after successful disk-based analysis with a complete, consistent dependency list containing the primary source. |
| `compilerFiles` | Sorted paths of regular files mapped into the compiler on Linux, including its executable and loaded shared libraries. Empty when unavailable on another host. |
| `nativeRuntime` | Resolved native runtime archive path, or null when unavailable. |
| `nativeLinker` | Resolved configured native linker driver path, or null when unavailable. |

Dependencies include the primary file, imported source modules, textual
preprocessor includes regardless of filename extension, and binary library
containers. Embedded `nillib://` source identities do not appear as filesystem
dependencies; the containing library supplies their content identity. This
list is independent of `sources`, which remains the bounded diagnostic snapshot
array and is not a complete build input inventory.

The list is bounded to 4,096 distinct files. Failed checks, inconsistent reads,
or exceeding that count set `dependenciesComplete` to false. `--source-stdin`
and `--workspace-stdin` always leave it false because their checked bytes need
not match disk. Clients must also treat missing completeness metadata from an
older compiler as unavailable, not as an empty complete graph.

Digests describe what analysis read; clients must compare them against the
current filesystem before reusing an artifact. They do not establish an
immutable snapshot. Native runtime/linker fields identify tools without hashing
them; a build client accounts for their current contents and its effective
settings separately. On Linux, `compilerFiles` lets a client include the loaded
compiler libraries, such as a dynamically linked LLVM, in its toolchain
identity. Those paths also require content hashing; they are not dependency
digests or a complete inventory of the native linker's own inputs. Fresh
analysis is required to discover
changed import resolution before considering an incremental cache entry.

`nilcompiler --analyze` and editor request/response protocols remain JSON. The
project driver's optional NILData/YAML presentation does not change that IPC
contract.

## Semantic source index

`--analyze --semantic-index` adds a `semantic` member to the analysis report.
Expand Down Expand Up @@ -473,6 +513,9 @@ publish diagnostics or make edits. See [NIL-0012](design/NIL-0012-signature-quer
- `declaration_info`: `kind`, `name`, `qualified_name`, `type_name`,
`source_path`, `line`, `parent_id`, `is_public`, `semantic_checked`.
- `annotation_info`: `owner_id`, `name`, `arguments`, `line`.
- `annotation_argument_info`: `annotation_id`, `name`, `is_named`, `kind`,
`value`. Matrix spells this record `annotation-argument-info` and its fields
`annotation-id` and `is-named`.

The JSON snapshot assigns each declaration an `id`. IDs are local to one
snapshot; never persist them across edits. A missing parent is `-1`, an
Expand All @@ -484,6 +527,56 @@ control-flow graph, or runtime object reflection. `semantic_checked` means the
whole compilation passed checking; it does not promise that every type string
has been fully elaborated.

### Typed annotation arguments

The `nil.reflection.v1` JSON annotation records retain `owner_id`, `name`,
`arguments`, and `line`, and add `arguments_parsed` and `parsed_arguments`:

```json
{
"owner_id": 0,
"name": "Company::review",
"arguments": "\"runtime\",revision=2,approved=true",
"line": 3,
"arguments_parsed": true,
"parsed_arguments": [
{"name": null, "kind": "text", "value": "runtime"},
{"name": "revision", "kind": "integer", "value": "2"},
{"name": "approved", "kind": "boolean", "value": "true"}
]
}
```

`arguments` remains the retained argument text for existing consumers; it is
not a source-exact formatting contract. `parsed_arguments` preserves argument
order. Its `name` is null for positional values and a string for named
properties. Every `value` is a string: decoded text or the validated scalar or
qualified-symbol spelling. The kinds are `text`, `boolean`, `integer`,
`floating`, and `symbol`. Symbols describe metadata and are not resolved as
program variables.

Positional arguments precede named properties, duplicate keys are rejected,
integers fit signed 64-bit storage, and floating-point values must be finite
and representable as `f64`. Lists, maps, and computed expressions are outside
the scalar profile. Legacy Ny composite and nested payloads remain available
for any annotation name, including custom names. For example,
`@custom(outer(inner("payload")))` retains inert argument text; no nested call
is executed. Such payloads report `arguments_parsed: false` with an empty
`parsed_arguments` array. Matrix remains scalar-only and rejects these nested
forms. A successfully parsed marker annotation with no arguments reports true
and an empty array.
Consumers must check the flag rather than interpreting an empty array as a
guarantee that no metadata exists.

These fields are additive; the schema remains `nil.reflection.v1` and the
existing `annotation_info` record is unchanged. Older compiler output may omit
the new JSON fields; treat that as unavailable parsed metadata. The annotation
ID is its index in `reflection.annotations`, while `owner_id` indexes
`reflection.declarations`. See the [annotation reference](language-reference/annotations.md)
for source syntax and language-specific string rules.

### Generated snapshot functions

Successful `--emit-reflection` writes an ordinary Ny module named
`analysis_snapshot`, atomically, with these exported functions:

Expand All @@ -492,8 +585,20 @@ count() -> i32
declaration(id: i32) -> declaration_info
annotation_count() -> i32
annotation(id: i32) -> annotation_info
annotation_arguments_parsed(id: i32) -> bool
annotation_argument_count() -> i32
annotation_argument(id: i32) -> annotation_argument_info
```

`annotation_arguments_parsed` takes an annotation ID, not a declaration ID.
`annotation_argument_count` counts parsed arguments across the entire snapshot.
`annotation_argument` addresses that flat sequence, ordered first by annotation
ID and then by argument order. Its `annotation_id` refers back to `annotation`.
For positional arguments the value record has `name: ""` and `is_named: false`;
named properties have `is_named: true`. `kind` and `value` use the same strings
as the JSON representation. Opaque annotations contribute no entries, so use
`annotation_arguments_parsed` to distinguish them from empty parsed annotations.

Invalid IDs panic. Truncated snapshots cannot be exported. A failed analysis
does not replace an existing snapshot, so build scripts must check exit status.
The generated module can be consumed by either frontend on VM or native routes.
Expand Down Expand Up @@ -536,7 +641,7 @@ and applies fixes only to the same document version that produced them.
Compiler `extraArgs` are deliberately
not forwarded to analysis. Existing structural LSP diagnostics remain separate.

Within a `nil.project.json` project, live checks inherit project import roots,
Within a `nil.project.nildata` project, live checks inherit project import roots,
SDK location and rule policy. Project build/run tasks use `nilbuild` and saved
files. The current-file command keeps the active file as its primary source;
other open source buffers supply dependencies. Live analysis requests the
Expand Down
4 changes: 4 additions & 0 deletions docs/design/NIL-0008-project-tooling.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
- **Scope:** Compiler, project driver, SDK and editor integration
- **Date:** 2026-10-18

The initial behavior below is extended by
[NIL-0022](NIL-0022-incremental-project-builds.md), which adds project references,
incremental and parallel builds, and NILData as the preferred manifest format.

## Context

Compiler analysis already exists, but single-file editor tasks and framework
Expand Down
Loading
Loading