Skip to content

add postfix ! non-null assertion operator - #100

Merged
vito merged 4 commits into
mainfrom
postfix-nonnull-operator
Jun 6, 2026
Merged

vito merged 4 commits into
mainfrom
postfix-nonnull-operator

Conversation

@vito

@vito vito commented Jun 6, 2026

Copy link
Copy Markdown
Owner

What

Adds a postfix ! operator that asserts a value is non-null.

let name: String = user.nickname   # nullable
print(name!.length)                # assert non-null, then call

At the type level expr! narrows T to T!. At runtime it raises non-null assertion failed: value is null if the value turns out to be null. This is the explicit escape hatch for the cases flow-sensitive narrowing can't reach — most notably field and call results, which can't be soundly narrowed.

How it parses

NonNullAssert is a left-recursive Term alternative, so it binds tighter than every binary operator and chains naturally:

  • a.b! → (a.b)!
  • xs[0]! → (xs[0])!
  • a! + b → (a!) + b
  • (a ?? b)! works on a grouped expression

A !'=' lookahead keeps a != b and a!=b parsing as inequality rather than (a!) …. Asserting an already-non-null value is a harmless no-op.

Changes

  • Grammar (pkg/dang/dang.peg): new NonNullAssert rule in Term; regenerated the pigeon parser.
  • AST (pkg/dang/ast_operators.go): NonNullAssert node — Infer strips outer nullability, Eval raises on NullValue.
  • Formatter (pkg/dang/format.go): emits expr!.
  • Tree-sitter: regenerated grammar.json/node-types.json/parser.c (all derive from dang.peg) plus a corpus test.
  • Tests: tests/test_nonnull_assert.dang (passes language + format roundtrip) and tests/errors/nonnull_assert_null.dang (runtime failure golden).
  • Docs: operators page (precedence table + section), grammar reference, and a flow-typing cross-reference.

Testing

go test ./pkg/dang/ ./tests/ (language, format, and error-message suites) and tree-sitter test all pass. The pre-existing neovim/zed submodule highlight tests fail only because those submodules aren't checked out locally; they're untouched by this change.

vito added 2 commits June 6, 2026 15:50
Introduce a postfix `!` operator that asserts a value is non-null. At the
type level it narrows `T` to `T!`, and at runtime it raises if the value
turns out to be null. This gives an explicit escape hatch for the cases
flow-sensitive narrowing can't reach, such as field or call results.

The operator parses as a left-recursive `Term` alternative so it binds
tighter than every binary operator and chains with `.field`, `[i]`, and
`(args)`. A `!'='` lookahead keeps `a != b` and `a!=b` parsing as
inequality rather than a non-null assertion followed by stray input.

Regenerates the pigeon parser and the tree-sitter grammar, which both
derive from dang.peg.

Signed-off-by: Alex Suraci <[email protected]>
Add the postfix `!` operator to the precedence table and a dedicated
section in the operators page, note it in the grammar reference, and
point to it from the flow-typing page as the explicit escape hatch when
narrowing can't prove non-nullness.

Signed-off-by: Alex Suraci <[email protected]>
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jun 6, 2026 •

Copy link
Copy Markdown

Deploying dang with  Cloudflare Pages  Cloudflare Pages

Latest commit: 01e3db3
Status: ✅  Deploy successful!
Preview URL: https://35475ba8.dang-3kk.pages.dev
Branch Preview URL: https://postfix-nonnull-operator.dang-3kk.pages.dev

View logs

vito added 2 commits June 6, 2026 15:53
Recompile the tree-sitter wasm so it includes the new non_null_assert
rule. Regenerated with the pinned dagger toolchain to match CI.

Signed-off-by: Alex Suraci <[email protected]>
The NonNullAssert production is self-explanatory; remove the trailing
comment.

Signed-off-by: Alex Suraci <[email protected]>
@vito
vito merged commit 3252afa into main Jun 6, 2026
7 checks passed
@vito vito changed the title feat(parser): add postfix ! non-null assertion operator add postfix ! non-null assertion operator Jun 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant