Skip to content

Add ConstFoldingRewriter to resolve const field references in expression method bodies#251

Merged
Mielek merged 4 commits into
Azure:mainfrom
Jeroen-VdB:feat/expression-constant
Apr 20, 2026
Merged

Add ConstFoldingRewriter to resolve const field references in expression method bodies#251
Mielek merged 4 commits into
Azure:mainfrom
Jeroen-VdB:feat/expression-constant

Conversation

@Jeroen-VdB
Copy link
Copy Markdown
Contributor

As mentioned in #250, expression method bodies that reference const fields were emitted as raw source text.

  • Added ConstFoldingRewriter, a Roslyn syntax rewriter that substitutes const field references with their literal values before the expression is serialized to XML
  • Hooked the rewriter into CompilerUtils.FindCode so it runs on every expression method body, before whitespace normalization and ToFullString()
    • Roslyn's semantic model already knows the value of every const field at compile time. The rewriter walks the syntax tree of the expression method, finds any MemberAccessExpression that resolves to a const field, and replaces it with the literal value (string, int, or bool). The existing path for const fields used directly as config parameters was already correct — this closes the gap for expression bodies.
  • Added ConstFoldingRewriterTests with a string const folded across a separate class

@Jeroen-VdB Jeroen-VdB force-pushed the feat/expression-constant branch from 8dba9b9 to 4568023 Compare April 20, 2026 20:56
@Mielek Mielek merged commit 0e0188a into Azure:main Apr 20, 2026
7 checks passed
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