Skip to content

frontend: type [] from the other initializers of its name - #606

Open
notactuallytreyanastasio wants to merge 1 commit into
temperlang:mainfrom
notactuallytreyanastasio:fix/coalesce-empty-list-type
Open

notactuallytreyanastasio wants to merge 1 commit into
temperlang:mainfrom
notactuallytreyanastasio:fix/coalesce-empty-list-type

Conversation

@notactuallytreyanastasio

Copy link
Copy Markdown
Contributor

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 #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

`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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

items ?? [] with items: List<String>? is typed List<AnyValue>, and the java, rust and cpp output does not compile

2 participants