Skip to content

feat(generator): SEM004 diagnostic — flag dimensions.json units missing from units.json#77

Merged
matt-edmondson merged 1 commit intovectorsfrom
claude/sem004-unknown-unit
May 10, 2026
Merged

feat(generator): SEM004 diagnostic — flag dimensions.json units missing from units.json#77
matt-edmondson merged 1 commit intovectorsfrom
claude/sem004-unknown-unit

Conversation

@matt-edmondson
Copy link
Copy Markdown
Contributor

Summary

Adds a fourth generator diagnostic: SEM004 warns when dimensions.json's availableUnits references a unit name that doesn't appear in units.json.

Why

Currently BuildToBaseExpression silently falls back to identity conversion when a unit is missing from units.json. That's wrong for any non-base unit — a typo ("Metre" for "Meter", "Hektopascal" for "Hectopascal") or an unmigrated unit produces a clean build with a From{Unit} factory whose body just passes the value through with no scaling. Verified by deliberately injecting "Metre" into Length's availableUnits:

warning SEM004: Unit 'Metre' (referenced by dimension 'Length'.availableUnits)
is not declared in units.json; the generated FromMetre factory will use an
identity conversion. Add the unit to units.json or fix the spelling.

Reverted to clean metadata before commit; current build is silent (zero SEM00x warnings).

Implementation

  • New UnknownUnitReference descriptor (id SEM004, warning).
  • New ReportUnknownUnitReferences helper called once after BuildUnitMap. Walks every dimension's availableUnits, dedups by (dimension, unitName) so a typo on a unit shared by many dimensions reports once per offending dimension, and skips reporting entirely when unitMap is empty (units.json not loaded — avoids drowning the log if metadata loading misfires).
  • Identity fallback in BuildToBaseExpression preserved — SEM004 is the surfacing mechanism, not a behaviour change. Projects can promote warnings to errors via TreatWarningsAsErrors if they want hard failure on unknown units.

Updates

  • AnalyzerReleases.Unshipped.md — adds the SEM004 row.
  • CLAUDE.md — adds SEM004 to the generator-diagnostics list under "Working with the source generator".

Test plan

  • dotnet build Semantics.SourceGenerators clean.
  • dotnet build Semantics.Quantities clean — no SEM00x warnings on current metadata.
  • Manual fire test: "Meter" → "Metre" in dimensions.json produces the expected SEM004 against Length; revert restores silence.
  • Generated/ tree unchanged vs baseline — diagnostic is additive.

https://claude.ai/code/session_01Tj63Rddvs9frqLUgsjNEP5


Generated by Claude Code

…ts.json

Without this diagnostic, BuildToBaseExpression silently falls back to
identity conversion when an availableUnits entry doesn't match any
unit declared in units.json. That's wrong for any non-base unit — a
typo like "Metre" instead of "Meter", or a unit that hasn't been
added to units.json yet, would compile clean and emit a From{Unit}
factory whose body just passes the value through unchanged.

SEM004 walks every dimension's availableUnits, dedups by
(dimension, unitName), and warns with the unit name plus the
offending dimension so the typo is easy to find. Verified with a
deliberate "Metre" injection: produces the expected diagnostic and
identifies Length as the owner. Reverted to clean metadata; current
build is silent.

Side-effect: the silent identity fallback in BuildToBaseExpression
is preserved so the build still succeeds; SEM004 is the surfacing
mechanism. If we want hard failure on unknown units in CI, treat
warnings as errors at the project level (TreatWarningsAsErrors).

CLAUDE.md generator-diagnostics list updated.
@sonarqubecloud
Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
1 Security Hotspot
0.0% Coverage on New Code (required ≥ 80%)
C Reliability Rating on New Code (required ≥ A)
C Security Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

@matt-edmondson matt-edmondson merged commit 3ac249f into vectors May 10, 2026
4 of 5 checks passed
@matt-edmondson matt-edmondson deleted the claude/sem004-unknown-unit branch May 10, 2026 14:59
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.

2 participants