Skip to content

analysis: diagnostics carry the answer (DiagnosticContext) - #298

Merged
vito merged 3 commits into
mainfrom
diagnostic-context
Nov 12, 2026
Merged

vito merged 3 commits into
mainfrom
diagnostic-context

Conversation

@vito

@vito vito commented Nov 11, 2026 •

Copy link
Copy Markdown
Owner

Type errors about a name can now carry the answer. With the new opt-in analysis.Options{DiagnosticContext: true}, analysis.Diagnostic gains a Context field, which is filled in after inference from the AST node the *dang.InferError sits on. The option is off by default, and when it's off no context work is done at all, so editors pay nothing. The type checker and its error strings are unchanged. Whether a name is really missing is decided by Lookup, never by parsing the message.

This is the hook that the engine's planned DangCheck/DangEval tools and enriched module-load errors build on (design: gist 0588bb5f, "The one hook, in vito/dang"). The LSP and any CLI can use it too.

Before / after

Render(diags, RenderOptions{}) output, analyzed with DiagnosticContext: true:

main.dang:2:1: error: list does not have method "findFirst"
  closest: find { item: a => Boolean! }: a?  returns the first element for which the predicate returns true, or null if none…
  [String!]! has 25 members: all any contains dropFirst dropLast dropWhile each filter find flatMap
    indexOf isEmpty join length map reduce reject reverse sort sortBy sum takeFirst takeLast
    takeWhile uniq
main.dang:2:1: error: field "substring" not found in String
  docs matching "substring": String.contains(substring: String!): Boolean!  checks if the string contains the specified substring
  String! has 28 members: center contains containsMatch fromBase64 hasPrefix hasSuffix isEmpty
    length match matchAll padLeft padRight replace replaceMatches rewriteMatches split splitMatches
    toBase64 toInt toLower toUpper trim trimLeft trimPrefix trimRight trimSpace trimSuffix truncate
main.dang:1:1: error: "sortStrings" not found
  docs matching "sort strings": List.sort[a: Ordered](descending: Boolean = false): [a]!  returns a new list in ascending order (descending: true reverses it), keeping e…
                                JSON.encode(value: a, indent: String = "", escapeHTML: Boolean = true, sortKeys: Boolean = false): String!  serializes a value to a JSON string. Fields come out in declaration order (a ty…
main.dang:1:9: error: function requires a block argument
  signature: List.reduce(initial: b) { acc: b, item: a => b }: b  reduces the list to a single value using an accumulator function

Before this change, each of these was the first line alone. With the option off, it still is.

API

  • Options.DiagnosticContext bool: the opt-in, default off.
  • Diagnostic.Context *DiagnosticContext (json context,omitempty), set only with the opt-in. Its fields:
    • Name, NameRange: the name as written and its range.
    • Member: what the name refers to, when it exists.
    • Receiver, Has: the type being selected from, and its members.
    • Near: the closest names by spelling.
    • Query, Search: doc-search hits on the name's camelCase words (words of 3 or more letters).
  • analysis.Render(diags, RenderOptions{MaxNames, Width}) string and Diagnostic.Format(opts).
  • nearest (unexported) ranks names by spelling. Case-only differences and small edit distances (a transposition counts as one edit) come first, then prefixes. Names that only share a camelCase word are a fallback, used when nothing else matches.

Notes for review

  • The gist describes a single Near list. This PR splits it into Near (spelling) and Search (doc hits), because they render differently, and because a spelling quick fix should only offer names that are close in spelling.
  • An unknown free name is looked up in ScopeAt. That scope also contains names declared later in the block, and a declaration's own name inside its value. Those are left out using the symbol index, so let a = total + 1; let total = 2 doesn't suggest total.
  • The context is opt-in because finding it can be costly: Search over an unknown free name walks every reachable named type, which matters when a large schema (Dagger) is imported. The LSP doesn't opt in. A later LSP "Change lenght to length" quick fix could opt in and use Near and NameRange.
  • v2.4.1 has no dang check command, so no CLI is wired to Render yet.

nearest ranks members by closeness to a name that wasn't found: case-only
differences and a few edits (a transposition counts as one, fewer allowed
for short names) first, then prefixes, and only when neither matches, names
sharing a camelCase word. It backs the closest-name suggestions of
diagnostic context.

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

cloudflare-workers-and-pages Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Deploying dang with  Cloudflare Pages  Cloudflare Pages

Latest commit: 48ee8a6
Status: ✅  Deploy successful!
Preview URL: https://8e6e8e95.dang-3kk.pages.dev
Branch Preview URL: https://diagnostic-context.dang-3kk.pages.dev

View logs

vito added 2 commits November 11, 2026 21:18
With Options.DiagnosticContext, a type error on a selection or a name
carries Diagnostic.Context, found after inference from the node the
*dang.InferError is on: the member the name does name (e.g. the signature a
call missing its block needs), or, when Lookup finds nothing, the
receiver's members, the closest names by spelling, and the members whose
names or docs match the name's camelCase words. An unknown free name is
looked up in the scope at its position, leaving out what the Source
declares there but the reference can't see yet (a block's later locals, a
declaration's own name in its value).

It's off by default: it serves readers that can't ask a follow-up question,
like a model reading a tool's output, and finding it can walk every type
the source reaches, which editors shouldn't pay for. Off, no context work
is done at all.

The type checker and its messages are untouched: whether a name is missing
is decided by Lookup, never by reading the message.

Signed-off-by: Alex Suraci <[email protected]>
Render and Diagnostic.Format print `path:line:col: severity: message` with
the diagnostic's context, when it has one, on indented lines: the signature
it names, the closest names, doc-search hits, and the receiver's member
names, capped and wrapped. It's the one shape for a CLI, an engine tool, or
a model to read.

Signed-off-by: Alex Suraci <[email protected]>
@vito
vito force-pushed the diagnostic-context branch from 6061532 to 48ee8a6 Compare November 11, 2026 21:18
@vito
vito merged commit 97fd808 into main Nov 12, 2026
9 checks passed
vito added a commit to dagger/dagger that referenced this pull request Nov 12, 2026
vito/dang main past v2.4.1: analysis's opt-in diagnostic context and
Render (vito/dang#298), and rule-2 snapshot reads, so `let before = ws`
stays readable after `ws = …` (vito/dang#301). No language version
changes: Dang's current version is still 2.4.

Signed-off-by: Alex Suraci <[email protected]>
vito added a commit that referenced this pull request Nov 12, 2026
The 2.5 page covered only the string change. It now covers everything
merged since v2.4.1: declaration snapshots (#301), the editor and
syntax-error changes from #300, and a Go embedders section with
DiagnosticContext (#298) and the AST's new string spelling. Sources and
verification name the full range.

Signed-off-by: Alex Suraci <[email protected]>
vito added a commit that referenced this pull request Nov 13, 2026
The 2.5 page covered only the string change. It now covers everything
merged since v2.4.1: declaration snapshots (#301), the editor and
syntax-error changes from #300, and a Go embedders section with
DiagnosticContext (#298) and the AST's new string spelling. Sources and
verification name the full range.

Signed-off-by: Alex Suraci <[email protected]>
vito added a commit to dagger/dagger that referenced this pull request Nov 14, 2026
vito/dang main past v2.4.1: analysis's opt-in diagnostic context and
Render (vito/dang#298), and rule-2 snapshot reads, so `let before = ws`
stays readable after `ws = …` (vito/dang#301). No language version
changes: Dang's current version is still 2.4.

Signed-off-by: Alex Suraci <[email protected]>
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