Repository navigation
frontend: type [] from the other initializers of its name - #606
Open
notactuallytreyanastasio wants to merge 1 commit into
Open
notactuallytreyanastasio wants to merge 1 commit into
notactuallytreyanastasio wants to merge 1 commit into
Conversation
`let xs = items ?? []` with `items: List<String>?` typed `xs` as
`List<AnyValue>`. The `??` macro expands to an `if` that assigns `[]`
on one branch and `items` on the other, so `xs` has two initializers.
The typer typed each with no context, `[]` came out `List<AnyValue>`,
and the union of that with `List<String>` is `List<AnyValue>`. That
is a legal Temper type, but java, rust and cpp then declare the local
with it and assign a `List<String>`:
List<Object> xs__3;
...
xs__3 = items__1; // incompatible types
TyperPlan already described this case (`h = []` on one branch,
`h = ["foo"]` on the other) and left a TODO to exclude initializers
that need context. This does that, the way `panic()` initializers
are already handled: when a name has an empty list literal among its
initializers and at least one other that is not `null`, the `[]` is
left out of the initializers, moved to the end of the type order, and
typed with the name's inferred type as its context. The java output
is now
List<String> xs__3;
if (items__1 == null) {
xs__3 = List.of();
} else {
xs__3 = items__1;
}
and the case prints `2` and `0` on js, py, lua, java, rust and cpp.
A `null` initializer does not count as the other one: it only says the
type is nullable, so `if (b) { zs = null } else { zs = [] }` still
types `zs` as `List<AnyValue>?`, as before. A name whose only
initializers are `[]` is unchanged too. This only covers the empty
literal; a non-empty literal of a narrower type on one branch still
widens the union, which is temperlang#548's territory. csharp was not run.
Fixes temperlang#547.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Bobby Grayson <bobbbygrayson+github@gmail.com>
mikesamuel
approved these changes
Oct 12, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
let xs = items ?? []withitems: List<String>?typedxsasList<AnyValue>. The??macro expands to anifthat assigns[]on one branch and
itemson the other, soxshas two initializers.The typer typed each with no context,
[]came outList<AnyValue>,and the union of that with
List<String>isList<AnyValue>. Thatis a legal Temper type, but java, rust and cpp then declare the local
with it and assign a
List<String>:TyperPlan already described this case (
h = []on one branch,h = ["foo"]on the other) and left a TODO to exclude initializersthat need context. This does that, the way
panic()initializersare already handled: when a name has an empty list literal among its
initializers and at least one other that is not
null, the[]isleft out of the initializers, moved to the end of the type order, and
typed with the name's inferred type as its context. The java output
is now
and the case prints
2and0on js, py, lua, java, rust and cpp.A
nullinitializer does not count as the other one: it only says thetype is nullable, so
if (b) { zs = null } else { zs = [] }stilltypes
zsasList<AnyValue>?, as before. A name whose onlyinitializers are
[]is unchanged too. This only covers the emptyliteral; a non-empty literal of a narrower type on one branch still
widens the union, which is #548's territory. csharp was not run.
Fixes #547.
Repro: https://github.com/notactuallytreyanastasio/temper-issue-repros/tree/main/25-coalesce-empty-list-any-value
🤖 Generated with Claude Code