Skip to content

Report an undeclared type as undeclared, not as a type failing to match itself - #571

Merged
godofecht merged 1 commit into
mainfrom
unknown-type-diagnostic
Aug 19, 2026
Merged

godofecht merged 1 commit into
mainfrom
unknown-type-diagnostic

Conversation

@godofecht

@godofecht godofecht commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor

The message

let p: Point = Point { x: 3, y: 4 }   # no Point in scope

gave:

Variable 'p' initialized with Point but annotated as Point

A SemanticType renders by its name. The struct literal produced STRUCT(name='Point') and the annotation, naming nothing in scope, produced UNKNOWN(name='Point'), so both sides printed the same string and the message described a type as failing to match itself. That reads like a compiler malfunction rather than a missing declaration.

It now reads:

Variable 'p' is annotated with unknown type 'Point', which is not declared in this scope

The same shape existed for return types: -> Widget with no Widget declared gave returns Widget but should return Widget.

Scope

What the checker accepts is unchanged. Both programs were already rejected, correctly, since the annotation names nothing. Only the message was wrong. A mismatch between two declared types keeps the original wording:

struct P { x: i32 }
function f() -> P { return 5 }
→ Function 'f' returns i32 but should return P     (unchanged)

How it was found

Running the documentation examples. Six blocks failed this way, across docs/book/06-pipelines-and-results.md, docs/book/09-expressions-and-matching.md, docs/book/10-memory-and-lifetimes.md, docs/library/dynamics.md and docs/language/types.md. Each is a fragment that names a type declared elsewhere in the page, so each produced a message about a type not matching itself.

Verification

Check Result
pytest tests/ 1594 passed, 6 skipped
ruff check src clean

Five new tests in tests/unit/test_unknown_type_diagnostic.py cover the variable case, the return-type case, that a real mismatch between declared types keeps its original wording, and that a correctly declared struct still assigns cleanly.

…ch itself

    let p: Point = Point { x: 3, y: 4 }   # no Point in scope

gave:

    Variable 'p' initialized with Point but annotated as Point

A SemanticType renders by its name. The struct literal produced
STRUCT(name='Point') and the annotation, naming nothing in scope, produced
UNKNOWN(name='Point'), so both sides printed the same string and the message
described a type as failing to match itself. It now reads:

    Variable 'p' is annotated with unknown type 'Point', which is not
    declared in this scope

The same shape existed for return types, where `-> Widget` with no Widget
declared gave "returns Widget but should return Widget".

What the checker accepts is unchanged. Both programs were already rejected,
correctly, since the annotation names nothing; only the message was wrong.
A mismatch between two declared types keeps the original wording.

Found by running the documentation examples: six blocks across
docs/book/, docs/library/dynamics.md and docs/language/ failed this way.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@godofecht
godofecht merged commit 263538a into main Aug 19, 2026
19 checks passed
@godofecht
godofecht deleted the unknown-type-diagnostic branch August 19, 2026 09:44
@godofecht godofecht mentioned this pull request Aug 19, 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