Commit 5f56279
committed
GROOVY-12166: STC: scope statement-level instanceof narrowing to the member being visited
Temporary type information from instanceof is keyed by the target
variable — for a field access, the shared FieldNode. if/loops/ternaries
and expression statements bracket their own frames, but a narrowing
recorded at statement level (an explicit `return field instanceof Sub`,
or an `assert field instanceof Sub`, whose propagation to subsequent
statements is intentional) lands in the enclosing frame, which was the
frame pushed for the whole class visit. Keyed by the shared FieldNode,
it then applied to every member visited afterwards: the checker stamped
the narrowed type on unrelated field reads and the static compiler
emitted a spurious checkcast, throwing ClassCastException at runtime
whenever the field held a different subtype.
Members now push their own frame (methods and constructors, property
and field initializers, object initializer blocks), so statement-level
narrowing still flows within a member but can never survive into
another; narrowing has no meaning past the member boundary. Intra-member
flow typing (assert, if-branch) is unchanged.
The checker-side leak predates Groovy 5 but was masked by codegen:
StaticTypesTypeChooser resolved types from the declared target first
until GROOVY-11375 (5.0.0-alpha-9) gave the expression-stamped inferred
type priority, exposing the stale narrowing as a checkcast. That change
is correct; the fix belongs here.1 parent e5f9c92 commit 5f56279
2 files changed
Lines changed: 145 additions & 0 deletions
File tree
- src
- main/java/org/codehaus/groovy/transform/stc
- test/groovy/bugs
Lines changed: 12 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2610 | 2610 | | |
2611 | 2611 | | |
2612 | 2612 | | |
| 2613 | + | |
2613 | 2614 | | |
2614 | 2615 | | |
2615 | 2616 | | |
| |||
2618 | 2619 | | |
2619 | 2620 | | |
2620 | 2621 | | |
| 2622 | + | |
2621 | 2623 | | |
2622 | 2624 | | |
2623 | 2625 | | |
| |||
2626 | 2628 | | |
2627 | 2629 | | |
2628 | 2630 | | |
| 2631 | + | |
2629 | 2632 | | |
2630 | 2633 | | |
2631 | 2634 | | |
2632 | 2635 | | |
2633 | 2636 | | |
2634 | 2637 | | |
2635 | 2638 | | |
| 2639 | + | |
2636 | 2640 | | |
2637 | 2641 | | |
2638 | 2642 | | |
| |||
3467 | 3471 | | |
3468 | 3472 | | |
3469 | 3473 | | |
| 3474 | + | |
| 3475 | + | |
| 3476 | + | |
| 3477 | + | |
| 3478 | + | |
3470 | 3479 | | |
3471 | 3480 | | |
3472 | 3481 | | |
| |||
3486 | 3495 | | |
3487 | 3496 | | |
3488 | 3497 | | |
| 3498 | + | |
3489 | 3499 | | |
3490 | 3500 | | |
3491 | 3501 | | |
| |||
3527 | 3537 | | |
3528 | 3538 | | |
3529 | 3539 | | |
| 3540 | + | |
3530 | 3541 | | |
| 3542 | + | |
3531 | 3543 | | |
3532 | 3544 | | |
3533 | 3545 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
0 commit comments