Skip to content

Add support for inline if expressions#449

Open
Daniel-Tr wants to merge 2 commits into
integrated-application-development:masterfrom
JAM-Software:407-support-if-expressions
Open

Add support for inline if expressions#449
Daniel-Tr wants to merge 2 commits into
integrated-application-development:masterfrom
JAM-Software:407-support-if-expressions

Conversation

@Daniel-Tr

Copy link
Copy Markdown

Resolves #407.

Delphi 13 adds an if <cond> then <expr> else <expr> conditional
expression. This adds parsing for it, models it as a proper branch in
the CFG (only one side actually runs at runtime, so it isn't
evaluated the way a range or array-constructor expression is), and
resolves its type as the compiler's least-upper-bound of the two
branches.

The LUB isn't a "pick whichever branch converts better" comparison -
that can only ever return one of the two input types, but Delphi's
actual promotion rules sometimes land on neither (e.g. Byte and
Word resolve to Integer, not Word), and unrelated class
branches need a common-ancestor walk rather than a closest-match
pick. ExpressionTypeResolverTest encodes the promotion rules as a
table, with every expected result checked against dcc64 directly
(assigning the expression to an incompatible variable and reading the
type off the resulting E2010 error - see the class comment) rather
than inferred from the existing overload-resolution machinery.

Also counts as a decision point in cyclomatic/cognitive complexity,
same as an if statement.

This change has mostly been created by Claude and reviewed manually.
We compiled, installed, and beta-tested this change in our CI environment
against productive code (that fails with the current published version).

Daniel Trierweiler added 2 commits July 23, 2026 14:40
…olution and control flow modeling

The LUB computation picked whichever branch converted more cleanly to
the other, which can only ever return one of the two branch types.
The Delphi compiler's actual promotion rules sometimes resolve to a
type that is neither branch's type (e.g. Byte/Word -> Integer), and
never derive a common ancestor for sibling class types. Replace it
with a table-driven implementation matching the compiler's documented
behavior, verified against dcc64 output.

Also model the expression as a real CFG branch (only one side is
actually evaluated at runtime) instead of a flat both-sides-evaluated
node, count it in cyclomatic/cognitive complexity like an if
statement, and rename getConditionExpression to getGuardExpression
for consistency with IfStatementNode.
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.

Support if expressions

1 participant