Commit 2c58f5b
authored
[jvm] Fix SAM (#12903)
* [jvm] restore functional_interfaces_used; AST scan misses argument-position SAMs
Revert the AbstractCast/Common/Gctx parts of a134fd8. That commit
dropped the functional_interfaces_used hashtbl on the claim that the
genjvm AST scan in collect_used_functional_interfaces "strictly subsumes"
the AbstractCast writeback. It does not.
AbstractCast.do_check_cast's SAM branch unifies eright.etype against the
SAM signature and returns eright **unchanged** — no TCast wrapper. So
when a function expression is passed where a Java SAM is expected, the
SAM TInst never appears in the typed AST. The only place it exists is on
the extern callee's signature, which the scan deliberately skips
(`if not (has_class_flag c CExtern)`) to keep classpath noise out.
For TCall sites this is masked: the callee TField has a TFun etype whose
parameter types are visited, so the SAM TInst is reached transitively.
But TNew has no callee subexpression — the constructor's parameter types
live on c.cl_constructor.cf_type, which Type.iter never visits. A bound
instance-method reference passed to a Java extern's constructor (e.g.
`new TextToSpeech(this, onTtsInit)` against android.jar) therefore
produces a closure that does not implement the SAM interface, hitting
ClassCastException at runtime.
Restore the field on Common.context + Gctx.t (shared in Common.clone),
the AbstractCast writeback keyed by @:native path, and seed
collect_used_functional_interfaces's `used` hashtbl from it before the
AST scan runs.
* [tests] StructuralSam: cover constructor-position SAM with method reference
The existing cases either bind to a typed local (SAM TInst lands in the
AST directly) or call a static method (TCall callee's TField has a TFun
etype that exposes parameter types to the scan). Neither exercises the
path that AbstractCast's functional_interfaces_used writeback is needed
for: a bound instance-method reference passed to a Java extern's
constructor, with the SAM type never named anywhere else in user code.
Add CtorOnly + CtorSam to test.Listeners and a Holder class in Main.hx
that does `new Listeners_CtorSam(onCtor, 42)`. Without the writeback the
emitted closure does not implement CtorOnly and the JVM throws
ClassCastException at runtime — same shape as RideAssist's
`new TextToSpeech(this, onTtsInit)` crash against android.jar.
* [tests] update traces positions
* [jvm] AbstractCast: wrap SAM conversion with TCast to surface SAM type in AST
Replace the functional_interfaces_used hashtbl plumbing (Common.context
+ Gctx.t + AbstractCast writeback + genjvm seeding) with a one-line
change in AbstractCast: wrap the function expression in mk_cast — a
TCast(eright, None) with the SAM type as the cast's etype.
The SAM TInst now lives in the typed AST at every conversion site, so
genjvm's existing collect_used_functional_interfaces scan finds it
naturally via note_fi_in_type on the TCast's etype. No cross-module
mutable state, no typing↔codegen back-channel, no @:native path-rewrite
bridging.
mk_cast is already the established pattern in this file — it's what the
MultiType abstract path uses a few lines up.1 parent dce7adf commit 2c58f5b
5 files changed
Lines changed: 80 additions & 11 deletions
File tree
- src/context
- tests/misc/jvm/projects/StructuralSam
- project/test
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
96 | 96 | | |
97 | 97 | | |
98 | 98 | | |
99 | | - | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
100 | 106 | | |
101 | 107 | | |
102 | 108 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| |||
35 | 36 | | |
36 | 37 | | |
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 | + | |
38 | 63 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
18 | 21 | | |
Lines changed: 10 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
3 | | - | |
4 | | - | |
5 | | - | |
6 | | - | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
7 | 7 | | |
8 | | - | |
9 | | - | |
| 8 | + | |
| 9 | + | |
10 | 10 | | |
11 | | - | |
12 | | - | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
Lines changed: 34 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
52 | 52 | | |
53 | 53 | | |
54 | 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 | + | |
55 | 89 | | |
56 | 90 | | |
57 | 91 | | |
| |||
0 commit comments