Skip to content

Commit 39cc89a

Browse files
mrkfrmngopherbot
authored andcommitted
go/types, types2: check for composite literal type target
This change checks for a type target for composite literals and performs the appropriate type inference. Note that U is currently always nil; we still need to pipe in the types for the relevant contexts. Thus, this change is a no-op for now. For #12854 Change-Id: I8517ef72da3b09328be5661021f700b6b00e5f1f Reviewed-on: https://go-review.googlesource.com/c/go/+/794060 Reviewed-by: Robert Griesemer <gri@google.com> Auto-Submit: Mark Freeman <markfreeman@google.com> LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent 17810c6 commit 39cc89a

4 files changed

Lines changed: 44 additions & 10 deletions

File tree

src/cmd/compile/internal/types2/literals.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ func (check *Checker) compositeLit(U Type, x *operand, e *syntax.CompositeLit, h
124124
typ = check.typ(e.Type)
125125
base = typ
126126

127+
// The hint mechanism is kept around to avoid reporting a false "need Go 1.28" error
128+
// for users of a Go 1.27 compiler.
127129
case hint != nil:
128130
// no composite literal type present - use hint (element type of enclosing type)
129131
typ = hint
@@ -136,11 +138,25 @@ func (check *Checker) compositeLit(U Type, x *operand, e *syntax.CompositeLit, h
136138
isElem = true
137139

138140
default:
139-
// TODO(gri) provide better error messages depending on context
140-
check.error(e, UntypedLit, "missing type in composite literal")
141-
// continue with invalid type so that elements are "used" (go.dev/issue/69092)
142-
typ = Typ[Invalid]
143-
base = typ
141+
// no composite literal type or hint present - use assignment context (if available and past Go 1.28)
142+
if U != nil {
143+
// report a version error only if we have an inferred type
144+
check.verifyVersionf(e, go1_28, "missing type in composite literal")
145+
// continue with the inferred type regardless of version
146+
typ = U
147+
base = typ
148+
// *T implies &T{}
149+
u, _ := commonUnder(base, nil)
150+
if b, ok := deref(u); ok {
151+
base = b
152+
}
153+
} else {
154+
// TODO(gri) provide better error messages depending on context
155+
check.error(e, UntypedLit, "missing type in composite literal")
156+
// continue with invalid type so that elements are "used" (go.dev/issue/69092)
157+
typ = Typ[Invalid]
158+
base = typ
159+
}
144160
}
145161

146162
// We cannot create a literal of an incomplete type; make sure it's complete.

src/cmd/compile/internal/types2/version.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ var (
4545
go1_23 = asGoVersion("go1.23")
4646
go1_26 = asGoVersion("go1.26")
4747
go1_27 = asGoVersion("go1.27")
48+
go1_28 = asGoVersion("go1.28")
4849

4950
// current (deployed) Go version
5051
go_current = asGoVersion(fmt.Sprintf("go1.%d", goversion.Version))

src/go/types/literals.go

Lines changed: 21 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/go/types/version.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)