From 99d35312bf21420a7a0840b0e408e08198a0279d Mon Sep 17 00:00:00 2001 From: Hans Behrens Date: Thu, 2 Jul 2026 18:31:02 -0700 Subject: [PATCH 1/2] fix: refuse to finalize spec when an assertion declaration failed to elaborate An assertion whose body fails to elaborate is reported as an error but is not registered in the module. #gen_spec then finalized the specification without it and #check_invariants reported all green, silently omitting the property the user wrote. elabAssertion now records the names of failed assertion declarations, and Module.ensureSpecIsFinalized refuses to finalize while any exist. Since all checking commands require a finalized spec, nothing downstream can run against a specification that silently lost an assertion. Co-Authored-By: Claude Fable 5 --- Veil/Frontend/DSL/Module/Elaborators.lean | 14 ++++++-- Veil/Frontend/DSL/Module/Representation.lean | 6 ++++ VeilTest/Regression/DroppedAssertion.lean | 34 ++++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 VeilTest/Regression/DroppedAssertion.lean diff --git a/Veil/Frontend/DSL/Module/Elaborators.lean b/Veil/Frontend/DSL/Module/Elaborators.lean index 2a322f56..d2f743d3 100644 --- a/Veil/Frontend/DSL/Module/Elaborators.lean +++ b/Veil/Frontend/DSL/Module/Elaborators.lean @@ -268,6 +268,8 @@ generating compiled model source. -/ def Module.ensureSpecIsFinalized (mod : Module) (stx : Syntax) : CommandElabM Module := do if mod.isSpecFinalized then return mod + unless mod._failedAssertions.isEmpty do + throwError "cannot finalize the specification: the following assertion declaration(s) failed to elaborate: {mod._failedAssertions.toList}" let mod ← mod.ensureStateIsDefined throwIfNoInitializerDefined mod warnIfNoInvariantsDefined mod @@ -590,9 +592,17 @@ def elabAssertion : CommandElab := fun stx => do | .stateConstraint => "state_constraint" withTraceNode (`veil.perf.elaborator.assertion ++ assertion.name) (fun _ => return s!"{kindStr} {assertion.name}") do -- Elaborate the assertion in the Lean environment - let mod' ← mod.defineAssertion assertion + try + let mod' ← mod.defineAssertion assertion -- dbg_trace s!"Elaborated assertion: {← liftTermElabM <|Lean.PrettyPrinter.formatTactic stx}" - localEnv.modifyModule (fun _ => mod') + localEnv.modifyModule (fun _ => mod') + catch ex => + -- A failed assertion is not registered in the module. Record its name so + -- that `#gen_spec` refuses to finalize a specification that would + -- silently omit it (`#check_invariants` would then report all-green + -- without it). + localEnv.modifyModule (fun _ => { mod with _failedAssertions := mod._failedAssertions.push assertion.name }) + throw ex @[command_elab Veil.genSpec] def elabGenSpec : CommandElab := fun stx => do diff --git a/Veil/Frontend/DSL/Module/Representation.lean b/Veil/Frontend/DSL/Module/Representation.lean index cd2bb312..7dab973a 100644 --- a/Veil/Frontend/DSL/Module/Representation.lean +++ b/Veil/Frontend/DSL/Module/Representation.lean @@ -374,6 +374,12 @@ structure Module where can still be added after this. -/ protected _specFinalizedAt : Option Syntax := none + /-- Implementation detail. Names of assertion declarations that failed to + elaborate. Such assertions are not registered in `assertions`, so the + specification must not be finalized while any exist; otherwise the failed + assertion would be silently omitted from all subsequent checks. -/ + protected _failedAssertions : Array Name := #[] + /-- Assertions can be grouped into "sets", which are checked independently of each other. Sets are per-module. By default, all assertions are added to the same set. -/ diff --git a/VeilTest/Regression/DroppedAssertion.lean b/VeilTest/Regression/DroppedAssertion.lean new file mode 100644 index 00000000..10e3c3ca --- /dev/null +++ b/VeilTest/Regression/DroppedAssertion.lean @@ -0,0 +1,34 @@ +import Veil + +-- An assertion whose body fails to elaborate is not registered in the module. +-- Previously, `#gen_spec` would then finalize the specification without it and +-- `#check_invariants` would report all-green, silently omitting the property. +-- Now `#gen_spec` refuses to finalize while failed assertion declarations exist. + +veil module DroppedAssertion + +type node + +relation r : node → Bool + +#gen_state + +after_init { + r N := false +} + +action flip (n : node) { + r n := true +} + +/-- error: Unbound uncapitalized variable: N' -/ +#guard_msgs in +invariant [uniqueness] r N ∧ r N' → N = N' + +/-- +error: cannot finalize the specification: the following assertion declaration(s) failed to elaborate: [uniqueness] +-/ +#guard_msgs in +#gen_spec + +end DroppedAssertion From d2785c8e7e4d4703dc253179728cf5097a99e218 Mon Sep 17 00:00:00 2001 From: Hans Behrens Date: Thu, 2 Jul 2026 19:38:48 -0700 Subject: [PATCH 2/2] fix: require the trace proof term to start on the line of the closing brace The trace command's optional proof term was a bare (term)?, so the parser greedily tried to consume whatever followed the trace command as its proof term. A subsequent set_option ... in parses as a term up to 'in' and then fails, taking the whole trace command down with it: the trace never runs, its result message is lost, and error recovery then runs the inner command without the option applied. A term-shaped follow-up line was silently swallowed as the proof term. Guard the tail with withPosition/lineEq so the proof term must start on the same line as the closing brace. (A bare lineEq without withPosition is a no-op, since there is no saved position to compare against.) Co-Authored-By: Claude Fable 5 --- .../ModelChecker/Symbolic/TraceLang.lean | 7 +++- VeilTest/Regression/TraceTermTail.lean | 40 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 VeilTest/Regression/TraceTermTail.lean diff --git a/Veil/Core/Tools/ModelChecker/Symbolic/TraceLang.lean b/Veil/Core/Tools/ModelChecker/Symbolic/TraceLang.lean index 0e7aa7c7..9443e211 100644 --- a/Veil/Core/Tools/ModelChecker/Symbolic/TraceLang.lean +++ b/Veil/Core/Tools/ModelChecker/Symbolic/TraceLang.lean @@ -62,9 +62,14 @@ syntax (name := traceAssertion) "assert " term:max : trace_line syntax traceLine := (traceAction <|> traceAssertion) syntax traceSpec := (traceLine colEq)* +/-- The optional proof term must start on the same line as the closing brace. +Without the `lineEq` guard, the parser greedily tries to consume whatever +follows the trace command as its proof term; e.g. a subsequent +`set_option ... in ` parses as a term up to `in` and then fails, +taking the whole trace command down with it. -/ syntax expected_smt_result "trace" ("[" ident "]")? "{" traceSpec -"}" (term)? : command +withPosition("}" (lineEq term)?) : command namespace Veil diff --git a/VeilTest/Regression/TraceTermTail.lean b/VeilTest/Regression/TraceTermTail.lean new file mode 100644 index 00000000..9414292d --- /dev/null +++ b/VeilTest/Regression/TraceTermTail.lean @@ -0,0 +1,40 @@ +import Veil + +-- The trace command's optional proof term must start on the same line as the +-- closing brace. Previously the parser greedily tried to consume whatever +-- followed the trace command as its proof term: a subsequent +-- `set_option ... in ` parsed as a term up to `in` and then failed +-- ("unexpected token ...; expected spec"), taking the whole trace command down +-- with it; error recovery then ran the inner command without the option. + +veil module TraceTermTail + +individual flag : Bool + +#gen_state + +after_init { + flag := false +} + +action set_flag { + flag := true +} + +invariant [flag_unset] ¬ flag + +#gen_spec + +#guard_msgs(drop warning, drop info) in +sat trace [first] { + set_flag +} + +#guard_msgs(drop warning, drop info) in +set_option veil.violationIsError false in +sat trace [second] { + set_flag + set_flag +} + +end TraceTermTail