[go] cmd/compile: adjust compiler to allow for embedded field selectors

2 views
Skip to first unread message

Robert Griesemer (Gerrit)

unread,
Mar 16, 2026, 5:03:00 PM (3 days ago) Mar 16
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Robert Griesemer has uploaded the change for review

Commit message

cmd/compile: adjust compiler to allow for embedded field selectors

WIP.
Change-Id: Ie314e28567cfa6cf4c9e962a07b32dd05b06bf5e

Change diff

diff --git a/src/cmd/compile/internal/typecheck/expr.go b/src/cmd/compile/internal/typecheck/expr.go
index 53c2912..10b7c82 100644
--- a/src/cmd/compile/internal/typecheck/expr.go
+++ b/src/cmd/compile/internal/typecheck/expr.go
@@ -312,6 +312,19 @@
return ir.NewStructKeyExpr(kv.Pos(), f, kv.Value)
}

+ var f *types.Field
+ if p, ambig := dotpath(sym, typ, &f, false); p != nil {
+ if ambig {
+ base.Errorf("ambiguous promoted field '%v' in struct literal of type %v", sym, typ)
+ return nil
+ }
+ if f.IsMethod() {
+ base.Errorf("cannot use method '%v' in struct literal of type %v", sym, typ)
+ return nil
+ }
+ return ir.NewStructKeyExpr(kv.Pos(), f, kv.Value)
+ }
+
if ci := Lookdot1(nil, sym, typ, typ.Fields(), 2); ci != nil { // Case-insensitive lookup.
if visible(ci.Sym) {
base.Errorf("unknown field '%v' in struct literal of type %v (but does have %v)", sym, typ, ci.Sym)
@@ -323,7 +336,6 @@
return nil
}

- var f *types.Field
p, _ := dotpath(sym, typ, &f, true)
if p == nil || f.IsMethod() {
base.Errorf("unknown field '%v' in struct literal of type %v", sym, typ)
@@ -335,8 +347,8 @@
for ei := len(p) - 1; ei >= 0; ei-- {
ep = append(ep, p[ei].field.Sym.Name)
}
- ep = append(ep, sym.Name)
- base.Errorf("cannot use promoted field %v in struct literal of type %v", strings.Join(ep, "."), typ)
+ ep = append(ep, f.Sym.Name)
+ base.Errorf("unknown field '%v' in struct literal of type %v (but does have %v)", sym, typ, strings.Join(ep, "."))
return nil
}

diff --git a/src/cmd/compile/internal/walk/complit.go b/src/cmd/compile/internal/walk/complit.go
index 1bc4d42..6f04a37 100644
--- a/src/cmd/compile/internal/walk/complit.go
+++ b/src/cmd/compile/internal/walk/complit.go
@@ -220,7 +220,7 @@
return ir.BlankNode, r.Value
}
ir.SetPos(r)
- return ir.NewSelectorExpr(base.Pos, ir.ODOT, var_, r.Sym()), r.Value
+ return ir.NewSelectorExpr(base.Pos, ir.OXDOT, var_, r.Sym()), r.Value
}
default:
base.Fatalf("fixedlit bad op: %v", n.Op())
diff --git a/test/complit2.go b/test/complit2.go
new file mode 100644
index 0000000..43b23c0
--- /dev/null
+++ b/test/complit2.go
@@ -0,0 +1,37 @@
+// run
+
+// Copyright 2026 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Verify that composite literals using selectors for
+// embedded fields are assembled correctly.
+
+package main
+
+import "fmt"
+
+type A struct {
+ a int
+ B
+}
+
+type B struct {
+ b string
+ C
+}
+
+type C struct {
+ c any
+}
+
+func main() {
+ eq(A{1, B{b: "foo"}}, A{a: 1, b: "foo"})
+ eq(A{B: B{C: C{c: "foo"}}}, A{c: "foo"})
+}
+
+func eq(x, y any) {
+ if x != y {
+ panic(fmt.Sprintf("%v != %v", x, y))
+ }
+}

Change information

Files:
  • M src/cmd/compile/internal/typecheck/expr.go
  • M src/cmd/compile/internal/walk/complit.go
  • A test/complit2.go
Change size: M
Delta: 3 files changed, 53 insertions(+), 4 deletions(-)
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Ie314e28567cfa6cf4c9e962a07b32dd05b06bf5e
Gerrit-Change-Number: 755740
Gerrit-PatchSet: 1
Gerrit-Owner: Robert Griesemer <g...@google.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

Robert Griesemer (Gerrit)

unread,
Mar 16, 2026, 7:00:55 PM (3 days ago) Mar 16
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
Attention needed from Keith Randall, Matthew Dempsky and Robert Griesemer

Robert Griesemer uploaded new patchset

Robert Griesemer uploaded patch set #2 to this change.
Open in Gerrit

Related details

Attention is currently required from:
  • Keith Randall
  • Matthew Dempsky
  • Robert Griesemer
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newpatchset
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Ie314e28567cfa6cf4c9e962a07b32dd05b06bf5e
Gerrit-Change-Number: 755740
Gerrit-PatchSet: 2
Gerrit-Owner: Robert Griesemer <g...@google.com>
Gerrit-Reviewer: Keith Randall <k...@golang.org>
Gerrit-Reviewer: Matthew Dempsky <mat...@go.dev>
Gerrit-Reviewer: Robert Griesemer <g...@golang.org>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-Attention: Keith Randall <k...@golang.org>
Gerrit-Attention: Matthew Dempsky <mat...@go.dev>
Gerrit-Attention: Robert Griesemer <g...@golang.org>
unsatisfied_requirement
satisfied_requirement
open
diffy

Robert Griesemer (Gerrit)

unread,
Mar 16, 2026, 7:06:51 PM (3 days ago) Mar 16
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
Attention needed from Keith Randall, Matthew Dempsky and Robert Griesemer

Robert Griesemer uploaded new patchset

Robert Griesemer uploaded patch set #3 to this change.
Open in Gerrit

Related details

Attention is currently required from:
  • Keith Randall
  • Matthew Dempsky
  • Robert Griesemer
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newpatchset
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Ie314e28567cfa6cf4c9e962a07b32dd05b06bf5e
Gerrit-Change-Number: 755740
Gerrit-PatchSet: 3
unsatisfied_requirement
satisfied_requirement
open
diffy

Robert Griesemer (Gerrit)

unread,
Mar 16, 2026, 7:07:26 PM (3 days ago) Mar 16
to goph...@pubsubhelper.golang.org, Mark Freeman, Robert Griesemer, Keith Randall, Gopher Robot, golang-co...@googlegroups.com
Attention needed from Keith Randall, Mark Freeman and Robert Griesemer

Robert Griesemer voted Commit-Queue+1

Commit-Queue+1
Open in Gerrit

Related details

Attention is currently required from:
  • Keith Randall
  • Mark Freeman
  • Robert Griesemer
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Ie314e28567cfa6cf4c9e962a07b32dd05b06bf5e
Gerrit-Change-Number: 755740
Gerrit-PatchSet: 3
Gerrit-Owner: Robert Griesemer <g...@google.com>
Gerrit-Reviewer: Keith Randall <k...@golang.org>
Gerrit-Reviewer: Mark Freeman <markf...@google.com>
Gerrit-Reviewer: Robert Griesemer <g...@golang.org>
Gerrit-Reviewer: Robert Griesemer <g...@google.com>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-Attention: Keith Randall <k...@golang.org>
Gerrit-Attention: Robert Griesemer <g...@golang.org>
Gerrit-Attention: Mark Freeman <markf...@google.com>
Gerrit-Comment-Date: Mon, 16 Mar 2026 23:07:22 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
unsatisfied_requirement
satisfied_requirement
open
diffy

Keith Randall (Gerrit)

unread,
Mar 17, 2026, 5:23:34 PM (2 days ago) Mar 17
to Robert Griesemer, goph...@pubsubhelper.golang.org, Keith Randall, Go LUCI, Mark Freeman, Robert Griesemer, Gopher Robot, golang-co...@googlegroups.com
Attention needed from Mark Freeman, Robert Griesemer and Robert Griesemer

Keith Randall voted and added 1 comment

Votes added by Keith Randall

Code-Review+2

1 comment

Patchset-level comments
File-level comment, Patchset 3 (Latest):
Keith Randall . resolved

Seems reasonable.

Open in Gerrit

Related details

Attention is currently required from:
  • Mark Freeman
  • Robert Griesemer
  • Robert Griesemer
Submit Requirements:
  • requirement satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Ie314e28567cfa6cf4c9e962a07b32dd05b06bf5e
Gerrit-Change-Number: 755740
Gerrit-PatchSet: 3
Gerrit-Owner: Robert Griesemer <g...@google.com>
Gerrit-Reviewer: Keith Randall <k...@golang.org>
Gerrit-Reviewer: Mark Freeman <markf...@google.com>
Gerrit-Reviewer: Robert Griesemer <g...@golang.org>
Gerrit-Reviewer: Robert Griesemer <g...@google.com>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-Attention: Robert Griesemer <g...@google.com>
Gerrit-Attention: Robert Griesemer <g...@golang.org>
Gerrit-Attention: Mark Freeman <markf...@google.com>
Gerrit-Comment-Date: Tue, 17 Mar 2026 21:23:30 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
satisfied_requirement
unsatisfied_requirement
open
diffy

Mark Freeman (Gerrit)

unread,
Mar 18, 2026, 3:06:57 PM (2 days ago) Mar 18
to Robert Griesemer, goph...@pubsubhelper.golang.org, Keith Randall, Go LUCI, Robert Griesemer, Gopher Robot, golang-co...@googlegroups.com
Attention needed from Robert Griesemer and Robert Griesemer

Mark Freeman voted Code-Review+1

Code-Review+1
Open in Gerrit

Related details

Attention is currently required from:
  • Robert Griesemer
  • Robert Griesemer
Submit Requirements:
    • requirement satisfiedCode-Review
    • requirement satisfiedNo-Unresolved-Comments
    • requirement satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Ie314e28567cfa6cf4c9e962a07b32dd05b06bf5e
    Gerrit-Change-Number: 755740
    Gerrit-PatchSet: 3
    Gerrit-Owner: Robert Griesemer <g...@google.com>
    Gerrit-Reviewer: Keith Randall <k...@golang.org>
    Gerrit-Reviewer: Mark Freeman <markf...@google.com>
    Gerrit-Reviewer: Robert Griesemer <g...@golang.org>
    Gerrit-Reviewer: Robert Griesemer <g...@google.com>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-Attention: Robert Griesemer <g...@google.com>
    Gerrit-Attention: Robert Griesemer <g...@golang.org>
    Gerrit-Comment-Date: Wed, 18 Mar 2026 19:06:53 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: Yes
    satisfied_requirement
    unsatisfied_requirement
    open
    diffy

    Robert Griesemer (Gerrit)

    unread,
    Mar 19, 2026, 5:44:23 PM (9 hours ago) Mar 19
    to goph...@pubsubhelper.golang.org, Mark Freeman, Keith Randall, Go LUCI, Robert Griesemer, Gopher Robot, golang-co...@googlegroups.com
    Attention needed from Robert Griesemer

    Robert Griesemer voted Commit-Queue+1

    Commit-Queue+1
    Open in Gerrit

    Related details

    Attention is currently required from:
    • Robert Griesemer
    Submit Requirements:
    • requirement satisfiedCode-Review
    • requirement satisfiedNo-Unresolved-Comments
    • requirement satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Ie314e28567cfa6cf4c9e962a07b32dd05b06bf5e
    Gerrit-Change-Number: 755740
    Gerrit-PatchSet: 5
    Gerrit-Owner: Robert Griesemer <g...@google.com>
    Gerrit-Reviewer: Keith Randall <k...@golang.org>
    Gerrit-Reviewer: Mark Freeman <markf...@google.com>
    Gerrit-Reviewer: Robert Griesemer <g...@golang.org>
    Gerrit-Reviewer: Robert Griesemer <g...@google.com>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-Attention: Robert Griesemer <g...@golang.org>
    Gerrit-Comment-Date: Thu, 19 Mar 2026 21:44:19 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: Yes
    satisfied_requirement
    unsatisfied_requirement
    open
    diffy

    Robert Griesemer (Gerrit)

    unread,
    Mar 19, 2026, 6:14:19 PM (9 hours ago) Mar 19
    to goph...@pubsubhelper.golang.org, Go LUCI, Mark Freeman, Keith Randall, Robert Griesemer, Gopher Robot, golang-co...@googlegroups.com
    Attention needed from Robert Griesemer

    Robert Griesemer voted and added 1 comment

    Votes added by Robert Griesemer

    Commit-Queue+1

    1 comment

    Patchset-level comments
    File-level comment, Patchset 5 (Latest):
    Robert Griesemer . resolved

    Failure seems unrelated.

    Gerrit-Comment-Date: Thu, 19 Mar 2026 22:14:14 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: Yes
    satisfied_requirement
    unsatisfied_requirement
    open
    diffy

    Robert Griesemer (Gerrit)

    unread,
    Mar 19, 2026, 7:59:07 PM (7 hours ago) Mar 19
    to goph...@pubsubhelper.golang.org, Go LUCI, Mark Freeman, Keith Randall, Robert Griesemer, Gopher Robot, golang-co...@googlegroups.com
    Attention needed from Robert Griesemer

    Robert Griesemer added 1 comment

    Patchset-level comments
    Robert Griesemer . resolved

    I believe these failures are unrelated.

    Gerrit-Comment-Date: Thu, 19 Mar 2026 23:59:01 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    satisfied_requirement
    unsatisfied_requirement
    open
    diffy

    Robert Griesemer (Gerrit)

    unread,
    Mar 19, 2026, 7:59:24 PM (7 hours ago) Mar 19
    to goph...@pubsubhelper.golang.org, Go LUCI, Mark Freeman, Keith Randall, Robert Griesemer, Gopher Robot, golang-co...@googlegroups.com
    Attention needed from Robert Griesemer

    Robert Griesemer voted

    Auto-Submit+1
    Commit-Queue+1
    Gerrit-Comment-Date: Thu, 19 Mar 2026 23:59:20 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: Yes
    satisfied_requirement
    unsatisfied_requirement
    open
    diffy

    Gopher Robot (Gerrit)

    unread,
    Mar 19, 2026, 8:16:44 PM (7 hours ago) Mar 19
    to Robert Griesemer, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Go LUCI, Mark Freeman, Keith Randall, Robert Griesemer, golang-co...@googlegroups.com

    Gopher Robot submitted the change

    Unreviewed changes

    3 is the latest approved patch-set.
    No files were changed between the latest approved patch-set and the submitted one.

    Change information

    Commit message:
    cmd/compile: enable promoted field selectors as keys in struct literals

    Switch the generated UIR version from V2 to V3.
    Adjust cmd/compile/internal/types to accept promoted field selectors
    in composite literals.

    Fixes #9859.
    Change-Id: Ie314e28567cfa6cf4c9e962a07b32dd05b06bf5e
    Auto-Submit: Robert Griesemer <g...@google.com>
    Reviewed-by: Mark Freeman <markf...@google.com>
    Reviewed-by: Keith Randall <k...@golang.org>
    Files:
    • M src/cmd/compile/internal/noder/unified.go
    • M src/cmd/compile/internal/typecheck/expr.go
    • M src/cmd/compile/internal/walk/complit.go
    • A test/complit2.go
      Change size: M
      Delta: 4 files changed, 55 insertions(+), 6 deletions(-)
      Branch: refs/heads/master
      Submit Requirements:
      • requirement satisfiedCode-Review: +2 by Keith Randall, +1 by Mark Freeman
      • requirement satisfiedTryBots-Pass: LUCI-TryBot-Result+1 by Go LUCI
      Open in Gerrit
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: merged
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: Ie314e28567cfa6cf4c9e962a07b32dd05b06bf5e
      Gerrit-Change-Number: 755740
      Gerrit-PatchSet: 6
      Gerrit-Owner: Robert Griesemer <g...@google.com>
      Gerrit-Reviewer: Gopher Robot <go...@golang.org>
      open
      diffy
      satisfied_requirement
      Reply all
      Reply to author
      Forward
      0 new messages