Repository navigation
add postfix ! non-null assertion operator - #100
Merged
Merged
Conversation
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]>
Deploying dang with
|
| 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 |
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]>
! non-null assertion operator! non-null assertion operator
This was referenced Jun 6, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds a postfix
!operator that asserts a value is non-null.At the type level
expr!narrowsTtoT!. At runtime it raisesnon-null assertion failed: value is nullif 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
NonNullAssertis a left-recursiveTermalternative, 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 expressionA
!'='lookahead keepsa != banda!=bparsing as inequality rather than(a!) …. Asserting an already-non-null value is a harmless no-op.Changes
pkg/dang/dang.peg): newNonNullAssertrule inTerm; regenerated the pigeon parser.pkg/dang/ast_operators.go):NonNullAssertnode —Inferstrips outer nullability,Evalraises onNullValue.pkg/dang/format.go): emitsexpr!.grammar.json/node-types.json/parser.c(all derive fromdang.peg) plus a corpus test.tests/test_nonnull_assert.dang(passes language + format roundtrip) andtests/errors/nonnull_assert_null.dang(runtime failure golden).Testing
go test ./pkg/dang/ ./tests/(language, format, and error-message suites) andtree-sitter testall 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.