Skip to content

BUG: var() group fails to parse with composite literal #63

Description

@Capt0rr

BUG: var() group fails to parse when any declaration contains a composite literal with 2+ elements

Version: v0.14.0
Go: 1.26.1
OS: Linux x86_64

Description

A grouped var ( ... ) block fails to parse when any variable is initialised
with a composite literal (slice, array, map, struct) that contains a comma.

The error always points to column 18 of the var ( line:

transpilation error: parse error: file.dingo:N:18: expected ')', found '='

The preprocessor appears to treat commas inside composite literals as
declaration separators, then chokes on the = of the next real declaration.
A brace-depth counter on the comma scan would fix it.

Minimal reproducers

All of the following fail with the error above:

// slice literal
var (
    a = []string{"hello", "world"}
)

// int slice
var (
    a = []int{1, 2}
)

// map literal
var (
    x = map[string]int{"a": 1, "b": 2}
)

// struct literal
var (
    x = T{1, 2}
)

Workaround

Expand the group into individual var declarations:

var a = []string{"hello", "world"}
var x = map[string]int{"a": 1, "b": 2}

What does NOT reproduce the bug

// single-element composite literal in var() — ok
var (
    a = []string{"hello"}
)

// same literals as top-level var — ok
var a = []string{"hello", "world"}

// const() group with any content — ok
const (
    x = "10.0.0.1"
    y = 42
)

// var() group with only scalar values — ok
var (
    a = "hello"
    b = 42
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions