Skip to content

Read static members and captured member chains as constants - #118

Merged
HowardvanRooijen merged 1 commit into
mainfrom
fix/static-member-constants
Oct 28, 2026
Merged

HowardvanRooijen merged 1 commit into
mainfrom
fix/static-member-constants

Conversation

@HowardvanRooijen

@HowardvanRooijen HowardvanRooijen commented Oct 27, 2026 •

Copy link
Copy Markdown
Member

Two ways a constraint that names a value, rather than a symbol, failed in VisitMember.

Static fields and properties

context.NewTheorem<Symbols<DateTime, int>>()
    .Where(t => t.X1 == DateTime.MaxValue)
    .Solve();   // NotSupportedException: Unknown parameter encountered: MaxValue.

The compiler folds a const (such as int.MaxValue), but it does not fold a static readonly field or a static property. DateTime.MaxValue, MinValue and UnixEpoch are all static readonly, so the tree carries a member access with no instance, and the visitor had no case for one. The same failure hit any user-defined static readonly field or static property. The existing DateTime tests avoided it by copying the boundary into a local first.

A chain that starts at a static member is now read and translated as a constant, like one that starts at a captured variable. It is read when the theorem is solved, as a captured local is, so a change between Where and Solve is seen.

Member chains on a captured variable

var holder = new Holder();
context.NewTheorem<Symbols<int, int>>()
    .Where(t => t.X1 == holder.In.Value)
    .Solve();   // TargetException: Object type Holder does not match target type <>c__DisplayClass...

The loop over the chain read every member from the closure instead of from the previous member's value, so any chain longer than one member threw. Each member is now read from the value before it. A link that is null is refused with a NotSupportedException that names it.

Tests

The new ConstantMemberTests has 10 tests, and all of them failed before the change: inline DateTime.MaxValue, MinValue and UnixEpoch; t.X1 > DateTime.MaxValue, which now reaches Z3 and has no solution because the symbol is bounded to the DateTime range (#98); a static readonly field; a static property; a mutable static field, which is read at solve time; member chains on a captured variable and on a static member; and a chain through a null. 368 tests pass, and all six demos run.

Relation to #113 and #43

jsboige reported this on #110, and their #113 fixes the DateTime fields specifically, on feature/datetime-ticks. This PR covers every static field and property, at the top of the stack. #43, which partially evaluates each constraint before visiting it, would also fold these accesses. This change stands on its own either way.

🤖 Generated with Claude Code

Copilot AI balanced review requested due to automatic review settings October 27, 2026 08:58

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The focused implementation and regression tests support the intended behavior, with no unresolved blocking issues identified.

No open issues 🎉

What changed in this PR

This PR extends constant-member translation so constraints can use static fields and properties directly, while fixing member chains rooted in captured variables.

Changes:

  • Evaluate static and captured member chains at solve time, reading each member from the preceding value.
  • Report null links with a NotSupportedException identifying the expression.
  • Add 10 regression tests covering DateTime boundaries, static members, chained access, and null handling.
File Description
solutions/​Z3.Linq/​ExpressionVisitor.cs Resolve static and captured member chains as constants with explicit null diagnostics.
solutions/​Z3.Linq.Tests/​ConstantMemberTests.cs Add regression coverage for constant-member evaluation and solve-time behavior.

Review effort: Balanced

@github-actions

github-actions Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Test Results

  1 files    1 suites   11s ⏱️
347 tests 347 ✅ 0 💤 0 ❌
368 runs  368 ✅ 0 💤 0 ❌

Results for commit 03d264d.

♻️ This comment has been updated with latest results.

@HowardvanRooijen HowardvanRooijen added no_release Suppresses auto_release functionality and removed pending_release labels Oct 27, 2026
MikeEvansLarah
MikeEvansLarah previously approved these changes Oct 27, 2026
Base automatically changed from fix/real-to-int-truncation to main October 28, 2026 07:38
Two ways a constraint naming a value, rather than a symbol, failed in
VisitMember.

A static field or property threw "Unknown parameter encountered". A
const is folded by the compiler, but a static readonly field is not -
DateTime.MaxValue, MinValue and UnixEpoch are all static readonly - and
nor is a static property, so the tree carries a member access with no
instance, which the visitor had no case for. So `t.X1 ==
DateTime.MaxValue` could not be written inline, and the existing
DateTime tests got round it by copying the boundary into a local. A
chain that starts at a static member is now read and translated as a
constant, like one that starts at a captured variable. It is read when
the theorem is solved, as a captured local is.

A chain of members on a captured variable threw TargetException.
`holder.In.Value` is three members starting at the closure constant,
and the loop read each of them from the closure instead of from the
previous member's value, so any chain longer than one failed. Each
member is now read from the value before it, and a link that is null is
refused with a message naming it.

Inline `t.X1 > DateTime.MaxValue` now reaches Z3 and has no solution,
because the symbol is bounded to the DateTime range (#98).

Reported by jsboige on #110. jsboige's #113 fixes the DateTime fields on
feature/datetime-ticks; this covers every static field and property.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@HowardvanRooijen
HowardvanRooijen dismissed MikeEvansLarah’s stale review October 28, 2026 07:38

The merge-base changed after approval.

@HowardvanRooijen
HowardvanRooijen force-pushed the fix/static-member-constants branch from 18978e7 to 03d264d Compare October 28, 2026 07:38
@github-actions

Copy link
Copy Markdown

Code Coverage Summary Report - Linux (No TFM)

Summary
Generated on: 10/28/2026 - 07:42:22
Parser: Cobertura
Assemblies: 2
Classes: 19
Files: 16
Line coverage: 91.3% (887 of 971)
Covered lines: 887
Uncovered lines: 84
Coverable lines: 971
Total lines: 2958
Branch coverage: 80.5% (619 of 768)
Covered branches: 619
Total branches: 768
Method coverage: Feature is only available for sponsors

Coverage

Z3.Linq - 93.6%
Name Line Branch
Z3.Linq 93.6% 81.5%
Z3.Linq.Environment 100%
Z3.Linq.ExpressionVisitor 95.1% 81.5%
Z3.Linq.SolveableExtensions 100% 100%
Z3.Linq.Symbols 100% 66.6%
Z3.Linq.Theorem 92.2% 80.7%
Z3.Linq.Theorem.DeferredSolvable 100%
Z3.Linq.Theorem.TypeMappingEntry 100%
Z3.Linq.Theorem<T, TResult> 92.2% 80.7%
Z3.Linq.Theorem 100%
Z3.Linq.TheoremGlobalRewriterAttribute 100%
Z3.Linq.TheoremPredicateRewriterAttribute 100%
Z3.Linq.TheoremUndecidedException 100%
Z3.Linq.TheoremVariableTypeMappingAttribute 100%
Z3.Linq.Z3Context 100% 100%
Z3.Linq.Z3Methods 100%
Z3.Linq.Examples - 70.7%
Name Line Branch
Z3.Linq.Examples 70.7% 50%
Z3.Linq.Examples.RiverCrossing.MissionariesAndCannibals 87.5% 66.6%
Z3.Linq.Examples.Sudoku.SudokuTable 0% 0%
Z3.Linq.Examples.Sudoku.SudokuTheorem 100% 100%
Z3.Linq.Examples.Sudoku.SudokuTheoremRewriter 100%

@HowardvanRooijen
HowardvanRooijen merged commit 964f177 into main Oct 28, 2026
7 checks passed
@HowardvanRooijen
HowardvanRooijen deleted the fix/static-member-constants branch October 28, 2026 09:34
jsboige pushed a commit to MyIntelligenceAgency/Z3.Linq that referenced this pull request Nov 4, 2026
Build green (0 warnings under TreatWarningsAsErrors), 371/372 tests passing. Aligns the rebased partial-eval branch with the newer upstream base: GetEnvironment/GetSolution call sites after the expression-visitor rewrite (endjin#100); ExtractCollection now uses upstream template-length sizing, the complete ReadZ3Value scalar conversion, EvaluateWithCompletion for free elements, and the no-length NotSupportedException parity. Keystone CollectionHandling tests moved to MSTest.Sdk/Shouldly.

One expected failure remains: Solve_MemberChainThroughANull_ThrowsNamingTheExpression (endjin#118) - the global PartialEval fold preempts the null member-chain path.

Co-Authored-By: Claude-Code <noreply@anthropic.com>
jsboige added a commit to MyIntelligenceAgency/Z3.Linq that referenced this pull request Nov 5, 2026
Rebase onto current main (964f177, including the endjin#100 expression-visitor
rewrite and endjin#118) and port the xUnit smoke suite to MSTest/MTP + Shouldly,
aligned with the rest of the solution, as requested in review.

The ten facts keep their semantics: integer and boolean symbols over
anonymous-type environments, chained Where constraints, tuple readback,
contradiction-to-null, Distinct, and Optimize in both directions. The
xUnit project is gone - the tests now live in the existing MTP test
project as a single file, so the PR adds coverage without a second
harness.

Measured: full suite 378/378 passing (368 on main + these 10), 4.9s;
BasicTheoremTests alone 10/10, 328ms.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no_release Suppresses auto_release functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants