blackdrag commented on code in PR #2773:
URL: https://github.com/apache/groovy/pull/2773#discussion_r3740440354


##########
src/main/java/org/codehaus/groovy/classgen/InstanceofFlowBindings.java:
##########
@@ -0,0 +1,249 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.codehaus.groovy.classgen;
+
+import org.codehaus.groovy.ast.CodeVisitorSupport;
+import org.codehaus.groovy.ast.expr.BinaryExpression;
+import org.codehaus.groovy.ast.expr.BooleanExpression;
+import org.codehaus.groovy.ast.expr.DeclarationExpression;
+import org.codehaus.groovy.ast.expr.Expression;
+import org.codehaus.groovy.ast.expr.NotExpression;
+import org.codehaus.groovy.ast.expr.VariableExpression;
+import org.codehaus.groovy.syntax.Types;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * Flow-sensitive analysis of JEP 394 {@code instanceof} pattern bindings
+ * (GROOVY-12242).
+ * <p>
+ * This is pure <em>semantic</em> analysis: given a boolean expression, which
+ * pattern variables are <em>definitely bound</em> when the expression is
+ * {@code true} versus {@code false}? (Same idea as compiler “flow info” /
+ * JEP 394 flow scoping — not a bytecode construct.)
+ * <ul>
+ *   <li>{@link #of(Expression)} — true/false binding sets for a condition</li>
+ *   <li>{@link #containsPattern(Expression)} — nested type-pattern presence
+ *       (e.g. whether an expression statement needs CompileStack 
isolation)</li>
+ * </ul>
+ * Covered shapes: {@code e instanceof T t}, negation / {@code !instanceof},
+ * {@code &&} (union of true bindings), {@code ||} (union of false bindings).
+ * Other shapes contribute nothing (conservative).
+ * <p>
+ * Consumers:
+ * <ul>
+ *   <li>{@link VariableScopeVisitor} — declare names on the live path</li>
+ *   <li>{@link org.codehaus.groovy.classgen.asm.InstanceofFlowSlotPublisher} —
+ *       publish/hide CompileStack slots from these bindings</li>
+ * </ul>
+ *
+ * @see org.codehaus.groovy.classgen.asm.InstanceofFlowSlotPublisher
+ * @since 6.0.0
+ */
+public final class InstanceofFlowBindings {

Review Comment:
   if we start adding helper classes that are especially and only for 
VariablescopeVisitor we should maybe consider a new package  like 
org.apache.groovy.classgen.varscope. Disclaimer: I am bad with names! Also is 
it an @Internal? I don´t think we have established yet when exactly to use it.
   Another problem I see is what is VariableScopeVisitor for? The idea is that 
it resolves the variable scopes and then downstream we can easily use the 
information. But that would imply not to have InstanceofFlowBindings exposed 
later on, since it is completely different from the normal structure of 
VariableScopeVisitor and thus requires special checks and handling downstream. 
Or you use this class itself downstream... which you did - as shown in 
StatementWriter. So I am a bit worried about the general design, especially 
about separation of concerns.



##########
src/test/groovy/groovy/InstanceofTest.groovy:
##########
@@ -223,4 +223,279 @@ final class InstanceofTest {
         }
         assert y == 'foobar'
     }
+
+    // GROOVY-12242: Java-aligned flow scoping for negated instanceof (JEP 394)
+    @Test
+    void testVariableScopeNegatedElse() {
+        def f = { Object o ->
+            if (!(o instanceof String s)) {
+                return 'not'
+            } else {
+                return s.toUpperCase()
+            }
+        }
+        assert f('hi') == 'HI'
+        assert f(1) == 'not'
+    }
+
+    // GROOVY-12242: pattern variable remains in scope after abrupt then-branch
+    @Test
+    void testVariableScopeEarlyReturn() {
+        def f = { Object o ->
+            if (!(o instanceof String s)) return 'early'
+            return s.toUpperCase()
+        }
+        assert f('hi') == 'HI'
+        assert f(42) == 'early'
+    }
+
+    // GROOVY-12242: pattern variable remains after else that cannot complete 
normally
+    @Test
+    void testVariableScopeAfterAbruptElse() {
+        def f = { Object o ->
+            if (o instanceof String s) {
+                // matched
+            } else {
+                return 'no'
+            }
+            return s.toUpperCase()
+        }
+        assert f('ab') == 'AB'
+        assert f(9) == 'no'
+    }
+
+    // GROOVY-12242: pattern variable must not leak after a declaration 
statement
+    @Test
+    void testVariableNoLeakAfterDeclaration() {
+        def err = shouldFail MissingPropertyException, '''
+            class C {
+                Object m(Object o) {
+                    boolean b = (o instanceof String s)
+                    return s
+                }
+            }
+            new C().m('hi')
+        '''
+        assert err.message =~ /No such property: s/
+    }
+
+    // GROOVY-12242: pattern variable must not leak after an expression 
statement
+    @Test
+    void testVariableNoLeakAfterExpressionStatement() {
+        def err = shouldFail MissingPropertyException, '''
+            class C {
+                Object m(Object o) {
+                    o instanceof String s && s.length() > 0
+                    return s
+                }
+            }
+            new C().m('hi')
+        '''
+        assert err.message =~ /No such property: s/
+    }
+
+    // GROOVY-12242: true branch of negated instanceof must not see the 
pattern local
+    // (CompileStack polarity must match VariableScope — no silent null ALOAD)
+    @Test
+    void testVariableNegatedIfBranchNotInScope() {
+        def err = shouldFail MissingPropertyException, '''
+            class C {
+                Object m(Object o) {
+                    if (!(o instanceof String s)) {
+                        return s
+                    }
+                    return 'matched'
+                }
+            }
+            new C().m(1)
+        '''
+        assert err.message =~ /No such property: s/
+    }
+
+    // GROOVY-12242: true-path binding of left of || is not in scope on the 
right (Java)
+    @Test
+    void testVariableOrRightHandSideNotInScope() {
+        def shell = GroovyShell.withConfig {
+            ast groovy.transform.TypeChecked
+        }
+        def err = shouldFail shell, '''
+            @groovy.transform.TypeChecked
+            class C {
+                static void m(Object o) {
+                    if (o instanceof String s || s.length() > 0) {
+                    }
+                }
+            }
+        '''
+        assert err.message =~ /The variable .s. is undeclared|Apparent 
variable .s./
+    }
+
+    // GROOVY-12242: false-path binding is in scope on the right of || (Java)
+    @Test
+    void testVariableOrRightHandSideFalsePathInScope() {
+        def f = { Object o ->
+            // when o is String, left is false, right sees s
+            return (!(o instanceof String s) || s.isEmpty())
+        }
+        assert f('') == true
+        assert f('x') == false
+        assert f(1) == true // left true → short-circuit, s not needed
+    }
+
+    // GROOVY-12242: ternary false branch must not see true-path pattern 
variable
+    @Test
+    void testVariableTernaryFalseBranchNotInScope() {
+        def shell = GroovyShell.withConfig {
+            ast groovy.transform.TypeChecked
+        }
+        def err = shouldFail shell, '''
+            @groovy.transform.TypeChecked
+            class C {
+                static Object m(Object o) {
+                    return o instanceof String s ? 'yes' : s
+                }
+            }
+        '''
+        assert err.message =~ /The variable .s. is undeclared|Apparent 
variable .s./
+    }
+
+    // GROOVY-12242: dynamic ternary false branch must not load a pattern local
+    @Test
+    void testVariableTernaryFalseBranchNotInScopeDynamic() {
+        def err = shouldFail MissingPropertyException, '''
+            class C {
+                Object m(Object o) {
+                    return o instanceof String s ? 'yes' : s
+                }
+            }
+            new C().m(1)
+        '''
+        assert err.message =~ /No such property: s/
+    }
+
+    // GROOVY-12242: ternary true branch sees pattern variable
+    @Test
+    void testVariableTernaryTrueBranch() {
+        def f = { Object o -> o instanceof String s ? s.toUpperCase() : 'no' }
+        assert f('ab') == 'AB'
+        assert f(1) == 'no'
+    }
+
+    // GROOVY-12242: reassignment of pattern variable (not implicitly final, 
JEP 394)
+    @Test
+    void testVariableReassignment() {
+        Object o = 'hi'
+        if (o instanceof String s) {
+            s = s + '!'
+            assert s == 'hi!'
+        } else {
+            assert false
+        }
+    }
+
+    // GROOVY-12242: pattern variable shadows a field only where in scope
+    @Test
+    void testVariableFieldShadowing() {
+        def obj = new Object() {
+            String s = 'field'
+            def test(Object o) {
+                if (o instanceof String s) {
+                    return "pv=$s"
+                }
+                return "field=$s"
+            }
+        }
+        assert obj.test('x') == 'pv=x'
+        assert obj.test(1) == 'field=field'
+    }
+
+    // GROOVY-12242: && chain uses pattern variable on subsequent operands
+    @Test
+    void testVariableAndChain() {
+        Object o = 'hello'
+        assert (o instanceof String s && s.length() > 3 && s.startsWith('h'))
+        assert !(o instanceof String s && s.length() > 99)
+    }
+
+    // GROOVY-12242: while body can use true-path pattern variable
+    @Test
+    void testVariableWhileBody() {
+        Object o = 'ab'
+        def n = 0
+        while (o instanceof String s && s.length() > 0) {
+            n += 1
+            o = s.substring(1)
+        }
+        assert n == 2
+        assert o == ''
+    }
+
+    // GROOVY-12242: reuse the same pattern variable name in successive 
statements
+    @Test
+    void testVariableNameReuse() {
+        Object a = 'x', b = 1
+        def r = []
+        if (a instanceof String s) r << s
+        if (b instanceof Integer s) r << s
+        assert r == ['x', 1]
+    }
+
+    // GROOVY-12242: type-checked flow scoping for early return
+    @Test
+    void testVariableScopeEarlyReturnTypeChecked() {
+        def shell = GroovyShell.withConfig {
+            ast groovy.transform.TypeChecked
+        }
+        assert shell.evaluate('''
+            @groovy.transform.TypeChecked
+            class C {
+                static String m(Object o) {
+                    if (!(o instanceof String s)) return 'early'
+                    return s.toUpperCase()
+                }
+            }
+            assert C.m('hi') == 'HI'
+            assert C.m(1) == 'early'
+            true
+        ''')
+    }
+
+    // GROOVY-12242: type-checked — positive instanceof still not in else
+    @Test
+    void testVariableScopePositiveNotInElseTypeChecked() {
+        def shell = GroovyShell.withConfig {
+            ast groovy.transform.TypeChecked
+        }
+        def err = shouldFail shell, '''
+            Number n = 12345
+            if (n instanceof Integer i) {
+            } else {
+                i.toString()
+            }
+        '''
+        assert err.message =~ /The variable .i. is undeclared/
+    }
+
+    // GROOVY-12242: type-checked — negated instanceof is in else
+    @Test
+    void testVariableScopeNegatedInElseTypeChecked() {
+        def shell = GroovyShell.withConfig {
+            ast groovy.transform.TypeChecked
+        }
+        assert shell.evaluate('''
+            @groovy.transform.TypeChecked
+            class C {
+                static String m(Object o) {
+                    if (!(o instanceof String s)) {
+                        return 'not'
+                    } else {
+                        return s.toUpperCase()
+                    }
+                }
+            }
+            assert C.m('hi') == 'HI'
+            assert C.m(1) == 'not'
+            true
+        ''')
+    }

Review Comment:
   I wonder if the tests are complete. I think not. Basically we have to 
consider the following case:
   
   - simple instanceof x
   - simple !instanceof x
   - simple !instanceof x, plus return in else block 
   - instanceof x && cond
   - instanceof x || cond
   - !instanceof x && cond 
   - !instanceof x && cond plus return in else block
   - !instanceof x || cond 
   - !instanceof x || cond plus return in else block
   
   for each case we have to ask where x is visible. in the if-block, 
else-block, after the if-else condition? De Morgan would help to simplify 
maybe. My feeling is also that we do not need to traverse the complete 
expression tree in all cases to decide for x. A complex expression with an 
instanceof deep inside makes things more difficult, but would also be solvable 
with De Morgan. Just not seeing the test cases reflecting all of this.
   



##########
src/main/java/org/codehaus/groovy/classgen/InstanceofFlowBindings.java:
##########
@@ -0,0 +1,249 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.codehaus.groovy.classgen;
+
+import org.codehaus.groovy.ast.CodeVisitorSupport;
+import org.codehaus.groovy.ast.expr.BinaryExpression;
+import org.codehaus.groovy.ast.expr.BooleanExpression;
+import org.codehaus.groovy.ast.expr.DeclarationExpression;
+import org.codehaus.groovy.ast.expr.Expression;
+import org.codehaus.groovy.ast.expr.NotExpression;
+import org.codehaus.groovy.ast.expr.VariableExpression;
+import org.codehaus.groovy.syntax.Types;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * Flow-sensitive analysis of JEP&nbsp;394 {@code instanceof} pattern bindings
+ * (GROOVY-12242).
+ * <p>
+ * This is pure <em>semantic</em> analysis: given a boolean expression, which
+ * pattern variables are <em>definitely bound</em> when the expression is
+ * {@code true} versus {@code false}? (Same idea as compiler “flow info” /
+ * JEP 394 flow scoping — not a bytecode construct.)
+ * <ul>
+ *   <li>{@link #of(Expression)} — true/false binding sets for a condition</li>
+ *   <li>{@link #containsPattern(Expression)} — nested type-pattern presence
+ *       (e.g. whether an expression statement needs CompileStack 
isolation)</li>
+ * </ul>
+ * Covered shapes: {@code e instanceof T t}, negation / {@code !instanceof},
+ * {@code &&} (union of true bindings), {@code ||} (union of false bindings).
+ * Other shapes contribute nothing (conservative).
+ * <p>
+ * Consumers:
+ * <ul>
+ *   <li>{@link VariableScopeVisitor} — declare names on the live path</li>
+ *   <li>{@link org.codehaus.groovy.classgen.asm.InstanceofFlowSlotPublisher} —
+ *       publish/hide CompileStack slots from these bindings</li>
+ * </ul>
+ *
+ * @see org.codehaus.groovy.classgen.asm.InstanceofFlowSlotPublisher
+ * @since 6.0.0
+ */
+public final class InstanceofFlowBindings {
+
+    private static final InstanceofFlowBindings EMPTY =
+            new InstanceofFlowBindings(List.of(), List.of());
+
+    private final List<VariableExpression> whenTrue;
+    private final List<VariableExpression> whenFalse;
+
+    private InstanceofFlowBindings(final List<VariableExpression> whenTrue,
+                                      final List<VariableExpression> 
whenFalse) {
+        this.whenTrue = whenTrue;
+        this.whenFalse = whenFalse;
+    }
+
+    /**
+     * Pattern variables that are definitely assigned when the analysed 
expression
+     * evaluates to {@code true}.
+     */
+    public List<VariableExpression> whenTrue() {
+        return whenTrue;
+    }
+
+    /**
+     * Pattern variables that are definitely assigned when the analysed 
expression
+     * evaluates to {@code false}.
+     */
+    public List<VariableExpression> whenFalse() {
+        return whenFalse;
+    }
+
+    /** Whether any pattern variable is bound on either path. */
+    public boolean isEmpty() {
+        return whenTrue.isEmpty() && whenFalse.isEmpty();
+    }
+
+    /**
+     * Names of pattern variables bound when the expression is {@code true}.
+     */
+    public Set<String> whenTrueNames() {
+        return names(whenTrue);
+    }
+
+    /**
+     * Names of pattern variables bound when the expression is {@code false}.
+     */
+    public Set<String> whenFalseNames() {
+        return names(whenFalse);
+    }
+
+    /**
+     * All pattern-variable names appearing in either path (stable encounter 
order).
+     */
+    public Set<String> allNames() {
+        if (isEmpty()) return Collections.emptySet();
+        Set<String> names = new LinkedHashSet<>(whenTrue.size() + 
whenFalse.size());
+        for (VariableExpression ve : whenTrue) names.add(ve.getName());
+        for (VariableExpression ve : whenFalse) names.add(ve.getName());
+        return names;
+    }
+
+    private static Set<String> names(final List<VariableExpression> vars) {
+        if (vars.isEmpty()) return Collections.emptySet();
+        Set<String> result = new LinkedHashSet<>(vars.size());
+        for (VariableExpression ve : vars) {
+            result.add(ve.getName());
+        }
+        return result;
+    }
+
+    /**
+     * Analyses {@code expression} for definite {@code instanceof} pattern 
bindings.
+     *
+     * @param expression a boolean condition (may be a {@link 
BooleanExpression} wrapper)
+     * @return the true/false binding sets; never {@code null}
+     */
+    public static InstanceofFlowBindings of(final Expression expression) {
+        if (expression == null) {
+            return EMPTY;
+        }
+        return analyse(expression);
+    }
+
+    /**
+     * Returns {@code true} if {@code expression} contains any JEP&nbsp;394 
type
+     * pattern ({@code e instanceof T t} or {@code e !instanceof T t}), 
including
+     * nested subexpressions. Used to decide whether expression-statement
+     * CompileStack isolation is required.
+     *
+     * @param expression any expression; {@code null} yields {@code false}
+     */
+    public static boolean containsPattern(final Expression expression) {
+        if (expression == null) return false;
+        boolean[] found = {false};
+        expression.visit(new CodeVisitorSupport() {
+            @Override
+            public void visitBinaryExpression(final BinaryExpression be) {
+                if (found[0]) return;
+                int op = be.getOperation().getType();
+                if ((op == Types.KEYWORD_INSTANCEOF || op == 
Types.COMPARE_NOT_INSTANCEOF)
+                        && isTypePattern(be.getRightExpression())) {
+                    found[0] = true;
+                    return;
+                }
+                super.visitBinaryExpression(be);
+            }
+        });
+        return found[0];
+    }
+
+    private static boolean isTypePattern(final Expression right) {
+        return right instanceof DeclarationExpression decl
+                && !decl.isMultipleAssignmentDeclaration()
+                && decl.getVariableExpression() != null;
+    }
+
+    private static InstanceofFlowBindings analyse(final Expression expression) 
{
+        Expression expr = expression;
+
+        // Unwrap BooleanExpression wrappers; NotExpression is handled below 
so that
+        // nested negations compose correctly.
+        while (expr instanceof BooleanExpression && !(expr instanceof 
NotExpression)) {
+            expr = ((BooleanExpression) expr).getExpression();
+        }
+
+        if (expr instanceof NotExpression not) {
+            return analyse(not.getExpression()).negated();
+        }
+
+        if (expr instanceof BinaryExpression binary) {
+            int op = binary.getOperation().getType();
+            if (op == Types.KEYWORD_INSTANCEOF) {
+                return ofInstanceof(binary);
+            }
+            if (op == Types.COMPARE_NOT_INSTANCEOF) {
+                // AST may still carry !instanceof before codegen rewrites it 
to !(… instanceof …).
+                return ofInstanceof(binary).negated();
+            }
+            if (op == Types.LOGICAL_AND) {
+                InstanceofFlowBindings left = 
analyse(binary.getLeftExpression());
+                InstanceofFlowBindings right = 
analyse(binary.getRightExpression());
+                // True path evaluates both; false path is not definite for 
either side alone.
+                return new InstanceofFlowBindings(
+                        union(left.whenTrue, right.whenTrue),
+                        List.of());
+            }
+            if (op == Types.LOGICAL_OR) {
+                InstanceofFlowBindings left = 
analyse(binary.getLeftExpression());
+                InstanceofFlowBindings right = 
analyse(binary.getRightExpression());
+                // False path evaluates both; true path is not definite for 
either side alone.
+                return new InstanceofFlowBindings(
+                        List.of(),
+                        union(left.whenFalse, right.whenFalse));
+            }
+        }

Review Comment:
   I wonder if we really have to travel the complete tree of expressions.



##########
src/main/java/org/codehaus/groovy/classgen/InstanceofFlowBindings.java:
##########
@@ -0,0 +1,249 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.codehaus.groovy.classgen;
+
+import org.codehaus.groovy.ast.CodeVisitorSupport;
+import org.codehaus.groovy.ast.expr.BinaryExpression;
+import org.codehaus.groovy.ast.expr.BooleanExpression;
+import org.codehaus.groovy.ast.expr.DeclarationExpression;
+import org.codehaus.groovy.ast.expr.Expression;
+import org.codehaus.groovy.ast.expr.NotExpression;
+import org.codehaus.groovy.ast.expr.VariableExpression;
+import org.codehaus.groovy.syntax.Types;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * Flow-sensitive analysis of JEP&nbsp;394 {@code instanceof} pattern bindings
+ * (GROOVY-12242).
+ * <p>
+ * This is pure <em>semantic</em> analysis: given a boolean expression, which
+ * pattern variables are <em>definitely bound</em> when the expression is
+ * {@code true} versus {@code false}? (Same idea as compiler “flow info” /
+ * JEP 394 flow scoping — not a bytecode construct.)
+ * <ul>
+ *   <li>{@link #of(Expression)} — true/false binding sets for a condition</li>
+ *   <li>{@link #containsPattern(Expression)} — nested type-pattern presence
+ *       (e.g. whether an expression statement needs CompileStack 
isolation)</li>
+ * </ul>
+ * Covered shapes: {@code e instanceof T t}, negation / {@code !instanceof},
+ * {@code &&} (union of true bindings), {@code ||} (union of false bindings).
+ * Other shapes contribute nothing (conservative).
+ * <p>
+ * Consumers:
+ * <ul>
+ *   <li>{@link VariableScopeVisitor} — declare names on the live path</li>
+ *   <li>{@link org.codehaus.groovy.classgen.asm.InstanceofFlowSlotPublisher} —
+ *       publish/hide CompileStack slots from these bindings</li>
+ * </ul>
+ *
+ * @see org.codehaus.groovy.classgen.asm.InstanceofFlowSlotPublisher
+ * @since 6.0.0
+ */
+public final class InstanceofFlowBindings {
+
+    private static final InstanceofFlowBindings EMPTY =
+            new InstanceofFlowBindings(List.of(), List.of());
+
+    private final List<VariableExpression> whenTrue;
+    private final List<VariableExpression> whenFalse;
+
+    private InstanceofFlowBindings(final List<VariableExpression> whenTrue,
+                                      final List<VariableExpression> 
whenFalse) {
+        this.whenTrue = whenTrue;
+        this.whenFalse = whenFalse;
+    }
+
+    /**
+     * Pattern variables that are definitely assigned when the analysed 
expression
+     * evaluates to {@code true}.
+     */
+    public List<VariableExpression> whenTrue() {
+        return whenTrue;
+    }
+
+    /**
+     * Pattern variables that are definitely assigned when the analysed 
expression
+     * evaluates to {@code false}.
+     */
+    public List<VariableExpression> whenFalse() {
+        return whenFalse;
+    }
+
+    /** Whether any pattern variable is bound on either path. */
+    public boolean isEmpty() {
+        return whenTrue.isEmpty() && whenFalse.isEmpty();
+    }
+
+    /**
+     * Names of pattern variables bound when the expression is {@code true}.
+     */
+    public Set<String> whenTrueNames() {
+        return names(whenTrue);
+    }
+
+    /**
+     * Names of pattern variables bound when the expression is {@code false}.
+     */
+    public Set<String> whenFalseNames() {
+        return names(whenFalse);
+    }
+
+    /**
+     * All pattern-variable names appearing in either path (stable encounter 
order).
+     */
+    public Set<String> allNames() {
+        if (isEmpty()) return Collections.emptySet();
+        Set<String> names = new LinkedHashSet<>(whenTrue.size() + 
whenFalse.size());
+        for (VariableExpression ve : whenTrue) names.add(ve.getName());
+        for (VariableExpression ve : whenFalse) names.add(ve.getName());
+        return names;
+    }

Review Comment:
   Did you not use `names.append(names(whenTrue)); 
names.append(names(whenFalse))` because of the additional collection creation?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to