Commit 906e87d5bc for golang

commit 906e87d5bc1ad170fd38f37c0399b650b3645ce6
Author: David Chase <drchase@google.com>
Date:   Fri Nov 27 06:13:13 2026 -0800

    Revert "cmd/cgo: rewrite C.type composite literals to use keywords"

    This reverts CL 647399 (commit 94b6b270c6a6d6b6f74650ef13b440ab06697389).

    Reason for revert: breaks internal testing, and not needed for 1.28.

    Change-Id: Idfe29d70f951f5fdedcb0a103cfdb28218357f22
    Reviewed-on: https://go-review.googlesource.com/c/go/+/866034
    LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
    Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
    Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>

diff --git a/src/cmd/cgo/ast.go b/src/cmd/cgo/ast.go
index db08fdac7f..df0552f525 100644
--- a/src/cmd/cgo/ast.go
+++ b/src/cmd/cgo/ast.go
@@ -217,8 +217,6 @@ func (f *File) saveExprs(x any, context astContext) {
 		}
 	case *ast.CallExpr:
 		f.saveCall(x, context)
-	case *ast.CompositeLit:
-		f.saveLiteral(x, context)
 	}
 }

@@ -266,7 +264,7 @@ func (f *File) saveRef(n *ast.Expr, context astContext) {
 	})
 }

-// saveCall saves calls to C.xxx for later processing.
+// Save calls to C.xxx for later processing.
 func (f *File) saveCall(call *ast.CallExpr, context astContext) {
 	sel, ok := call.Fun.(*ast.SelectorExpr)
 	if !ok {
@@ -279,38 +277,6 @@ func (f *File) saveCall(call *ast.CallExpr, context astContext) {
 	f.Calls = append(f.Calls, c)
 }

-// Save composite literals for later processing.
-func (f *File) saveLiteral(lit *ast.CompositeLit, context astContext) {
-	sel, ok := lit.Type.(*ast.SelectorExpr)
-	if !ok {
-		return
-	}
-	if l, ok := sel.X.(*ast.Ident); !ok || l.Name != "C" || len(lit.Elts) == 0 {
-		return
-	}
-	// if it's already in field:value form, no need to edit
-	for _, e := range lit.Elts {
-		if _, ok := e.(*ast.KeyValueExpr); ok {
-			return
-		}
-	}
-	c := &Lit{Lit: lit}
-	f.Lits = append(f.Lits, c)
-	f.LitMap[lit] = c
-}
-
-// markLiteralDone marks composite literals in an AST (fragment) as done.
-// This is used when a call has been rewritten, which will also cause
-// the literal to be processed (and it has to be processed as part of the
-// call, otherwise it will cause an "overlapping rewrite" error).
-func (f *File) markLiteralDone(x any, context astContext) {
-	if lit, ok := x.(*ast.CompositeLit); ok {
-		if c := f.LitMap[lit]; c != nil {
-			c.Done = true
-		}
-	}
-}
-
 // If a function should be exported add it to ExpFunc.
 func (f *File) saveExport(x any, context astContext) {
 	n, ok := x.(*ast.FuncDecl)
diff --git a/src/cmd/cgo/gcc.go b/src/cmd/cgo/gcc.go
index 4b60de31de..6f3012aaca 100644
--- a/src/cmd/cgo/gcc.go
+++ b/src/cmd/cgo/gcc.go
@@ -270,12 +270,10 @@ func (p *Package) Translate(f *File) {
 		p.recordTypes(f, d, &conv)
 	}
 	p.prepareNames(f)
-	p.prewriteLits(f)
 	if p.rewriteCalls(f) {
 		// Add `import _cgo_unsafe "unsafe"` after the package statement.
 		f.Edit.Insert(f.offset(f.AST.Name.End()), "; import _cgo_unsafe \"unsafe\"")
 	}
-	p.rewriteLits(f)
 	p.rewriteRef(f)
 }

@@ -861,92 +859,6 @@ func (f *File) isMangledName(s string) bool {
 	})
 }

-// prewriteLits modifies the AST of the composite literal in case it is
-// emitted as part of a gofmt in call rewriting.
-func (p *Package) prewriteLits(f *File) {
-	for _, lit := range f.Lits {
-		p.prewriteLit(lit)
-	}
-}
-
-func typeFor(cType string) *Type {
-	ty := typedef["_Ctype_struct_"+cType]
-	if ty != nil {
-		return ty
-	}
-	ty = typedef["_Ctype_"+cType]
-	return ty
-}
-
-func (p *Package) prewriteLit(lit *Lit) {
-	sel := lit.Lit.Type.(*ast.SelectorExpr)
-	ty := typeFor(sel.Sel.Name)
-	goType, ok := (ty.Go).(*ast.StructType)
-	if !ok {
-		lit.Done = true
-		return
-	}
-
-	fields := goType.Fields.List
-	j := 0
-	// Before modifying fields, attempt to match non-underscore struct field names
-	// with fields in the literal.  If their lengths do not agree, do not modify
-	// the literal.
-	for _, fs := range fields {
-		for _, fieldName := range fs.Names {
-			if fieldName.Name == "_" {
-				continue
-			}
-			j++
-		}
-	}
-	if len(lit.Lit.Elts) != j {
-		return // lengths do not agree, do nothing.
-	}
-	j = 0
-	for _, fs := range fields {
-		for _, fieldName := range fs.Names {
-			if fieldName.Name == "_" {
-				continue
-			}
-			init := lit.Lit.Elts[j]
-			// prepend fieldName.Name+":"
-			lit.Lit.Elts[j] = &ast.KeyValueExpr{Key: ast.NewIdent(fieldName.Name), Colon: init.Pos(), Value: init}
-			j++
-		}
-	}
-}
-
-func (p *Package) rewriteLits(f *File) {
-	for _, lit := range f.Lits {
-		if lit.Done {
-			continue
-		}
-		p.rewriteLit(f, lit)
-		lit.Done = true
-	}
-}
-
-func (p *Package) rewriteLit(f *File, lit *Lit) {
-	sel := lit.Lit.Type.(*ast.SelectorExpr)
-	ty := typeFor(sel.Sel.Name)
-	fields := ty.Go.(*ast.StructType).Fields.List
-	j := 0
-	for _, fs := range fields {
-		for _, fieldName := range fs.Names {
-			if fieldName.Name == "_" {
-				continue
-			}
-
-			init := lit.Lit.Elts[j].(*ast.KeyValueExpr).Value // it got rewritten in place, in prewriteLit
-			j++
-			pos := f.offset(init.Pos())
-			// just splice in the tag.
-			f.Edit.Replace(pos, pos, fieldName.Name+":")
-		}
-	}
-}
-
 // rewriteCalls rewrites all calls that pass pointers to check that
 // they follow the rules for passing pointers between Go and C.
 // This reports whether the package needs to import unsafe as _cgo_unsafe.
@@ -965,7 +877,6 @@ func (p *Package) rewriteCalls(f *File) bool {
 			if nu {
 				needsUnsafe = true
 			}
-			f.walk(call.Call, ctxExpr, (*File).markLiteralDone)
 		}
 	}
 	return needsUnsafe
diff --git a/src/cmd/cgo/internal/testerrors/ptr_test.go b/src/cmd/cgo/internal/testerrors/ptr_test.go
index 3ff681daed..bc1cc1c6e0 100644
--- a/src/cmd/cgo/internal/testerrors/ptr_test.go
+++ b/src/cmd/cgo/internal/testerrors/ptr_test.go
@@ -62,7 +62,7 @@ var ptrTests = []ptrTest{
 		// that (irrelevantly) contains a Go pointer.
 		name: "ok1",
 		c:    `struct s3 { int i; int *p; }; void f3(int *p) {}`,
-		body: `p := &C.struct_s3{0, new(C.int)}; C.f3(&p.i)`,
+		body: `p := &C.struct_s3{i: 0, p: new(C.int)}; C.f3(&p.i)`,
 		fail: false,
 	},
 	{
diff --git a/src/cmd/cgo/main.go b/src/cmd/cgo/main.go
index 27313e84ef..26609368ae 100644
--- a/src/cmd/cgo/main.go
+++ b/src/cmd/cgo/main.go
@@ -66,19 +66,17 @@ type typedefInfo struct {

 // A File collects information about a single Go input file.
 type File struct {
-	AST         *ast.File                  // parsed AST
-	Comments    []*ast.CommentGroup        // comments from file
-	Package     string                     // Package name
-	Preamble    string                     // C preamble (doc comment on import "C")
-	Ref         []*Ref                     // all references to C.xxx in AST
-	Calls       []*Call                    // all calls to C.xxx in AST
-	Lits        []*Lit                     // all  C.xxx{...} literals in AST
-	LitMap      map[*ast.CompositeLit]*Lit // composite literals can be rewritten in TWO WAYS, use this to find and mark done.
-	ExpFunc     []*ExpFunc                 // exported functions for this file
-	Name        map[string]*Name           // map from Go name to Name
-	NamePos     map[*Name]token.Pos        // map from Name to position of the first reference
-	NoCallbacks map[string]bool            // C function names with #cgo nocallback directive
-	NoEscapes   map[string]bool            // C function names with #cgo noescape directive
+	AST         *ast.File           // parsed AST
+	Comments    []*ast.CommentGroup // comments from file
+	Package     string              // Package name
+	Preamble    string              // C preamble (doc comment on import "C")
+	Ref         []*Ref              // all references to C.xxx in AST
+	Calls       []*Call             // all calls to C.xxx in AST
+	ExpFunc     []*ExpFunc          // exported functions for this file
+	Name        map[string]*Name    // map from Go name to Name
+	NamePos     map[*Name]token.Pos // map from Name to position of the first reference
+	NoCallbacks map[string]bool     // C function names with #cgo nocallback directive
+	NoEscapes   map[string]bool     // C function names with #cgo noescape directive
 	Edit        *edit.Buffer

 	debugs []*debug // debug data from iterations of gccDebug. Initialized by File.loadDebug.
@@ -106,12 +104,6 @@ type Call struct {
 	Done     bool
 }

-// A Lit refers to a composite literal of a C.xxx type in the AST.
-type Lit struct {
-	Lit  *ast.CompositeLit
-	Done bool
-}
-
 // A Ref refers to an expression of the form C.xxx in the AST.
 type Ref struct {
 	Name    *Name
@@ -485,7 +477,6 @@ func main() {
 				return
 			}
 			f.Edit = edit.NewBuffer(b)
-			f.LitMap = make(map[*ast.CompositeLit]*Lit)
 			f.ParseGo(input, b)
 			if *genBinding {
 				f.useGenBindingPragmas(input, origDir, b)