T0024 — Static analysis and strict mode
Part of the roadmap. Phase 1 · wave 5 · size L.
- Depends on: T0018, T0019
- Area:
area:core - Tier hint: likely
risk:high: a publicanalyze()and diagnostics oncompile(), so the core tier with a second reviewer from the other vendor. - Parallel with: T0020, T0021
Goal
Semantic checks and type inference with a model (design notes).
Split
The implementation grew far beyond a few hundred hand-written lines, so it was split on 2026-11-12 along the three levels of the design notes, as CONTRIBUTING asks (#35):
- T0024a: levels 1 and 2, the type inference, the diagnostics of
compile(), the publicanalyze(), the fraction warning and the conformance runner's static check of every test. - T0024b: level 3, strict mode, including the order-dependent operations of #72, and the public
strictoption.
This file keeps the scope, the design notes and the acceptance criteria they share; each child lists the criteria it delivers. The task is complete, and #35 closes, when both have merged.
Scope
src/analysis/: type and cardinality inference over the AST, using the model, the function return types (returnsinFunctionDefinition) and the operator rules.- Diagnostics:
semantic/unknown-elementsemantic/unknown-type- a wrong root type
- choice suffixes, with a hint
- obvious operator type errors
- a warning when date/time arithmetic ignores a fraction written in the expression (3.0.0 Date/Time Arithmetic: "Implementers SHOULD produce a warning when decimal fractions are ignored"; spec notes). The owner decided this on 2026-11-11 (#52):
- It applies when the right operand of
+or-is a quantity literal and the analyzer knows that the left operand is a Date, DateTime or Time, which it does for a literal or a function such astoday(), and for an element only with a model. When the analyzer cannot tell, it reports nothing. isFractionIgnored()invalues/datetime/arithmetic.tsdecides. It gets the value's precision when the value is a literal too (@2014 + 23 months), and no precision otherwise (today() + 7.7 days), so it then checks only the quantity.- The diagnostic is a warning, also in strict mode: the expression compiles and evaluates as before, and the fraction is ignored as the spec note describes.
- A quantity computed during evaluation gets no warning. A runtime warning would need a host hook in
EvaluationHooks(T0021), which is not planned for now;isFractionIgnored()stays internal so that a later hook can use it.
- It applies when the right operand of
- Strict mode (N1 §11) for operations that are unsafe on singletons.
- A public
analyze().compile()throwsFhirPathSemanticErrorwith all diagnostics attached. - FHIR versions (roadmap): what is a known element, type or choice type comes from the provider, so the analyzer has no version-specific code. The model-aware analysis cases also run against the real
r4,r5andr6providers, with the differences of the pinned sources:Observation.triggeredByis an element in R5 and R6 and unknown in R4;Observation.instantiatesis an element in R5 only (the R6 draft removed it);DeviceAlertis aninputTypeand root type in R6 only;Observation.valueallowsReferencein R5 only andAttachmentin R5 and R6. R6 results are engine tests on the pinned draft, not conformance.
Design notes
Analysis runs at three levels:
- Always, as part of
compile(). Is the function known? Is its arity right? Are lambda and type-specifier arguments where they should be? Is$totalused outsideaggregate()? Failures throwFhirPathSemanticError. Invalid literal values are deliberately not on this list (T0008: invalid literal values). - Model-aware, opt-in with
model+inputType. Type inference over the AST: every node gets a set of possible types and a cardinality (0..1or0..*). This catches:- unknown elements (
name.given1) - choice elements used with a type suffix (
Observation.valueQuantity), with the hint "usevalue.ofType(Quantity)" - a root type that does not match the input (
Encounter.nameon a Patient) - unknown types in
is/as/ofType, assemantic/unknown-typewarnings: T0008 resolves targets at compile time but raiseseval/unknown-typeonly when evaluated, as the phase-1 execution-error fixtures require. These warnings do not become compile errors in strict mode. Without a model, an unqualified resource-name target is valid, not an unknown type (#58, #59) - obvious operator type errors (
1 + 'a')
- unknown elements (
- Strict mode (
strict: true, N1 §11). Warnings become errors for the unsafe uses listed in N1 §11. The main example is a singleton-only operator or function given an operand that may repeat, such asPatient.name.given < 'X'orPatient.name.given.substring(1). Equality is only flagged in the narrower case §11 describes: one operand may repeat and the other is a single item (Patient.name.given = 'Wouter'). Comparing two collections, as inPatient.name = Patient.name, is well-defined (T0009: equality and equivalence) and stays valid. The basic version comes in phase 1; the full version later.
The result is a list of Diagnostic { severity: 'error' | 'warning'; code; message; span }. compile() throws on errors; analyze() returns the diagnostics without throwing, for tooling. The test suite's invalid="semantic" cases and mode="strict" tests are covered by levels 2 and 3.
Acceptance criteria
-
name.given1andEncounter.name.givenoninputType: 'Patient'are semantic errors (teststestSimpleFail,testSimpleWithWrongContext), and so isObservation.valueQuantity.unit(testPolymorphismB). - Conformance group
isSemanticCheckspasses, as do allinvalid="semantic"tests in phase 1, in R4 and R5. - With the real
r4,r5andr6providers, the same expression gets each version's diagnostics:Observation.triggeredByissemantic/unknown-elementunder R4 only,Observation.instantiatesunder R4 and R6, andinputType: 'DeviceAlert'is valid under R6 only. No R6 conformance result is claimed. - Analysis without a model never produces false errors for valid expressions in the suite.
- Unknown-type warnings leave compilation successful and produce execution errors only at evaluated use sites, even in strict mode; model-less unqualified resource-name targets produce no unknown-type diagnostic. Known nonmatching types are not unknown-type errors.
-
analyze()reports a warning for each ignored fraction written in the expression (spec notes):@1973-12-25 + 7.7 days(testPlusDate2),@T10:00 + 1.5 hours,@2014 + 23 monthsandtoday() - 7.7 days. It reports none for@1973-12-25 + 7 days, for@1973-12-25T00:00:00.000+10:00 + 0.1 's'(testPlusDate19, which keeps the fraction) or for a quantity from the input data. In strict mode too, the warned expressions compile, and they evaluate to the same results as before.