Commit dbd22f8
authored
Fix typed select pushing Type::Any instead of expected type (WebAssembly#2707)
## Summary
- The typed select instruction's type checker validates operands against
the expected type but forgets to set `result_type`, leaving it as
`Type::Any`.
- Fix adds `result_type = expected[0]` in the typed select branch.
## Details
In `TypeChecker::OnSelect` (type-checker.cc), `result_type` is
initialized to `Type::Any`. The untyped select path (when `expected` is
empty) correctly sets `result_type = type1` on line 1012. However, the
typed select path (when `expected` is non-empty) validates both operands
against `expected[0]` but never assigns `result_type`.
The typed select instruction (`select t`) exists specifically to provide
precise type information for cases where the operand types alone are
insufficient (e.g., reference types). Pushing `Type::Any` as the result
type defeats this purpose and can cause downstream type checking to be
less precise than intended.
The fix adds `result_type = expected[0]` after the CheckType calls in
the typed select branch, mirroring the pattern used in the untyped path.
## Tests
- `typed-select-result-type.txt`: Verifies that typed select with
funcref/externref annotations properly propagates the result type,
allowing the result to be used in contexts expecting that specific
reference type (e.g., `global.set` to a `funcref` global, return from an
`externref` function).
- `bad-typed-select-type-mismatch.txt`: Verifies that using a typed
select `(result funcref)` where `externref` is expected correctly
produces a type mismatch error.1 parent 9a226dc commit dbd22f8
3 files changed
Lines changed: 38 additions & 0 deletions
File tree
- src
- test/typecheck
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1052 | 1052 | | |
1053 | 1053 | | |
1054 | 1054 | | |
| 1055 | + | |
1055 | 1056 | | |
1056 | 1057 | | |
1057 | 1058 | | |
| |||
| 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 | + | |
| 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 | + | |
0 commit comments