Skip to content

Emit spec'd diagnostic codes (E0901, E0951-3, E1101-2, E1201) (§29.12) #429

Closed

Description

@ehartford

Emit spec'd diagnostic codes (E0901, E0951-3, E1101-2, E1201) (§29.12)

Summary

All seven §29.12 diagnostic behaviors already exist and fire correctly; none of them carries its spec'd stable error code. The Diagnostic.code plumbing and the [code]-suffix rendering already work end-to-end (the unused-label warning proves it), so this is a mechanical wiring task: add code-aware emit_error variants to Sema and Parser and attach the seven codes at their existing emission sites. Stable codes let tests, tooling, and editors match diagnostics without depending on message wording.

Spec

§29.12 Error Codes (docs/with-specification.md:10979-10991):

Code Description
E0901 Non-local control flow (return, break, continue, goto, ?) inside defer/errdefer
E0951 Nested implicit it is ambiguous — use explicit param => expr for inner closure
E0952 it used in context expecting N != 1 parameters
E0953 it is a reserved keyword and cannot be used as an identifier
E1101 Orphan rule violation: impl requires a local trait or local type
E1102 Duplicate implementation of trait for type
E1201 Overlapping trait implementations

Requirement IDs 9.3.1.13-15 are the same E0951/E0952/E0953 codes referenced from §9.3 (implicit it closures).

Current state

Plumbing (all verified):

  • Diagnostic has a code: str field (src/Diagnostic.w:26-34) and Diagnostic.set_code (src/Diagnostic.w:64-65).
  • render_diag_header appends [code] after the message when code is non-empty (src/DiagnosticRender.w:8-12). Verified end-to-end: an unused label renders warning: unused label 'aloop [unused-label] under with check.
  • The only set_code caller today is the unused-label warning (src/SemaCheck.w:1301).
  • Sema.emit_error (src/SemaDiag.w:97-102) and Parser.emit_error (src/Parser.w:369-371) have no code parameter — that is the entire gap.

Behavior emission sites (all verified at HEAD):

  • E0901 — five sites in src/SemaCheck.w, all guarded by self.in_defer, which is set for both NK_DEFER and NK_ERRDEFER (SemaCheck.w:3106-3110), so errdefer is covered:
    • goto not allowed in defer (SemaCheck.w:2902)
    • break not allowed in defer (SemaCheck.w:2981)
    • continue not allowed in defer (SemaCheck.w:2996)
    • ? operator not allowed in defer (SemaCheck.w:4308)
    • return not allowed in defer (SemaCheck.w:4987)
  • E0951 — nested implicit closure is ambiguous; use explicit parameter for inner closure, emitted via a directly-constructed Diagnostic.err (src/Parser.w:3127).
  • E0952 — `it` used in context expecting {expected_params} parameter(s) (src/SemaCheck.w:6943).
  • E0953 — 'it' is a reserved keyword and cannot be used as a parameter name (src/Parser.w:6378 and 6420). Other identifier positions (let it = 42, fn it:) currently produce the generic expected identifier (pinned by test/behavior/it_keyword_let.w and it_keyword_fn.w).
  • E1101 — orphan rule violation: impl requires a local trait or local type (src/SemaDecl.w:1574; only enforced for local impls, SemaDecl.w:1570-1573).
  • E1102 — duplicate implementation of trait for type (src/SemaDecl.w:1716).
  • E1201 — overlapping implementations of '<trait>' in both check_direct_overlap (src/SemaDecl.w:1528) and check_blanket_overlap (src/SemaDecl.w:1555).

Because the renderer appends the code after the message, every existing //! expect-error: / //! expect-check-fail: substring test keeps passing unchanged.

Implementation plan

  1. src/SemaDiag.w — add Sema.emit_error_code(self, msg: str, node: i32, code: str) next to Sema.emit_error (line 97): same body, but build var diag = Diagnostic.err(...), call diag.set_code(code), then self.diags.emit(diag). Respect suppress_errors exactly as emit_error does.
  2. src/Parser.w — add Parser.emit_error_code(self, msg: str, code: str) next to Parser.emit_error (line 369), same pattern.
  3. Switch the emission sites:
    • SemaCheck.w:2902, 2981, 2996, 4308, 4987 → emit_error_code(..., "E0901").
    • Parser.w:3127 → change to var diag = Diagnostic.err(...), diag.set_code("E0951"), self.diags.emit(diag).
    • SemaCheck.w:6943 → emit_error_code(..., "E0952").
    • Parser.w:6378 and 6420 → emit_error_code(..., "E0953").
    • SemaDecl.w:1574 → "E1101"; SemaDecl.w:1716 → "E1102"; SemaDecl.w:1528 and 1555 → "E1201".
  4. Phase 2 (recommended, same issue): make E0953 cover general identifier positions per its spec text ("cannot be used as an identifier"): where the parser expects an identifier and sees TK_KW_IT (let bindings, fn names), emit 'it' is a reserved keyword and cannot be used as an identifier with code E0953 instead of the generic expected identifier. Update test/behavior/it_keyword_let.w and it_keyword_fn.w expectations accordingly. If this phase is dropped, codes on the two parameter-position sites still satisfy 29.12.1.4.
  5. No renderer changes. Pure compiler change (no rt/Link/seed hazard); normal with build / :fixpoint discipline applies.

Error-message texts do not change (only codes are added), except the optional Phase 2 message above.

Test plan

Update existing tests to pin message+code in one directive line (the directive field is single-valued, so put the code in the same substring):

  • test/compile_errors/err_defer_return.w → //! expect-check-fail: return not allowed in defer [E0901]; same treatment for err_defer_break.w, err_defer_continue.w, err_defer_question.w, err_goto_in_defer.w.
  • NEW test/compile_errors/err_errdefer_return.w — return inside errdefer: body; expect return not allowed in defer [E0901] (proves the errdefer path shares the code).
  • test/behavior/it_nested_error.w → //! expect-error: nested implicit closure is ambiguous; use explicit parameter for inner closure [E0951].
  • test/behavior/it_arity_error.w → //! expect-error: `it` used in context expecting 2 parameter(s) [E0952].
  • test/behavior/it_keyword_param.w → //! expect-error: 'it' is a reserved keyword and cannot be used as a parameter name [E0953].
  • NEW test/compile_errors/err_orphan_foreign_trait_foreign_type.w — helper sibling module defines trait Foreign; main file uses it and writes impl Foreign for i32 → //! expect-error: orphan rule violation: impl requires a local trait or local type [E1101] (follow the multi-file pattern used by err_enum_constructor_import_*.w).
  • NEW test/compile_errors/err_duplicate_trait_impl.w — two impl Show for i32 blocks in one file → //! expect-error: duplicate implementation of trait for type [E1102] (and update test/compile_errors/err_iter_duplicate_impl_item_type.w directive to include [E1102]).
  • test/behavior/orphan_overlap_error.w → //! expect-check-fail: overlapping implementations extended with [E1201] (covers blanket-vs-direct); add a direct-vs-blanket variant if not already exercised.
  • Positive control: existing green tests (e.g. test/behavior/orphan_rule_ok.w) must stay green — codes must never appear on successful compiles.

Acceptance criteria

  • Each of the seven behaviors renders its §29.12 code as error: <message> [E....] under with check.
  • All updated/new directive tests pass; no existing expect-error test regresses (codes are appended after messages).
  • Diagnostic.set_code is used for every code; no message text rewritten in Phase 1.
  • with build && with build :fixpoint && with build :test pass.

Dependencies

Overlaps #488 (Tests: coherence errors coverage — E1101/E1102/E1201 test files can be shared with it; coordinate file names). No blocking dependencies.

Traceability

From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 29.12.1.1, 29.12.1.2, 29.12.1.3, 29.12.1.4, 29.12.1.5, 29.12.1.6, 29.12.1.7, 9.3.1.13, 9.3.1.14, 9.3.1.15.

Activity

  1. ehartford commented on Jun 10, 2026

    @ehartford
    ContributorAuthor

    Implemented in eb67dbb (Emit stable diagnostic codes). Added coded diagnostics and regression coverage for E0901, E0951, E0952, E0953, E1101, E1102, and E1201. Verification run: WITH=out/tmp/seed-current/with-darwin-aarch64 out/bin/with build; WITH=out/tmp/seed-current/with-darwin-aarch64 out/release/bin/with build :fixpoint; WITH=out/tmp/seed-current/with-darwin-aarch64 out/release/bin/with build :test; WITH=out/tmp/seed-current/with-darwin-aarch64 out/release/bin/with build :test-green.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions