From e2fef50def98b87107ab963f657d43d7869b8463 Mon Sep 17 00:00:00 2001 From: Michael Matloob Date: Mon, 12 Jan 2026 14:37:39 -0500 Subject: runtime: rename mallocTiny* to mallocgcTinySize* This makes it easier to identify which functions are used for memory allocation by looking for functions that start with mallocgc. The Size suffix is added so that the isSpecializedMalloc function in cmd/compile/internal/ssa can distinguish between the generated functions and the mallocgcTiny function called by mallocgc, similar to the SC suffixes for the mallocgcSmallNoScanSC* and mallocgcSmallScanNoHeaderSC* functons. Change-Id: I6ad7f15617bf6f18ae5d1bfa2a0b94e86a6a6964 Reviewed-on: https://go-review.googlesource.com/c/go/+/735780 Reviewed-by: Michael Matloob Reviewed-by: Michael Knyszek LUCI-TryBot-Result: Go LUCI --- test/codegen/strings.go | 2 +- test/live.go | 4 ++-- test/live_regabi.go | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) (limited to 'test') diff --git a/test/codegen/strings.go b/test/codegen/strings.go index 0c5ee2f8f5..1c14021150 100644 --- a/test/codegen/strings.go +++ b/test/codegen/strings.go @@ -23,7 +23,7 @@ func CountBytes(s []byte) int { func ToByteSlice() []byte { // Issue #24698 // amd64:`LEAQ type:\[3\]uint8` - // amd64:`CALL runtime\.(newobject|mallocTiny3)` + // amd64:`CALL runtime\.(newobject|mallocgcTinySize3)` // amd64:-`.*runtime.stringtoslicebyte` return []byte("foo") } diff --git a/test/live.go b/test/live.go index f8ad8df1ca..6561b20925 100644 --- a/test/live.go +++ b/test/live.go @@ -665,14 +665,14 @@ func f39a() (x []int) { func f39b() (x [10]*int) { x = [10]*int{} - x[0] = new(int) // ERROR "live at call to (newobject|mallocTiny[48]): x$" + x[0] = new(int) // ERROR "live at call to (newobject|mallocgcTinySize[48]): x$" printnl() // ERROR "live at call to printnl: x$" return x } func f39c() (x [10]*int) { x = [10]*int{} - x[0] = new(int) // ERROR "live at call to (newobject|mallocTiny[48]): x$" + x[0] = new(int) // ERROR "live at call to (newobject|mallocgcTinySize[48]): x$" printnl() // ERROR "live at call to printnl: x$" return } diff --git a/test/live_regabi.go b/test/live_regabi.go index 9809353f1b..75b842d732 100644 --- a/test/live_regabi.go +++ b/test/live_regabi.go @@ -663,14 +663,14 @@ func f39a() (x []int) { func f39b() (x [10]*int) { x = [10]*int{} - x[0] = new(int) // ERROR "live at call to (newobject|mallocTiny[48]): x$" + x[0] = new(int) // ERROR "live at call to (newobject|mallocgcTinySize[48]): x$" printnl() // ERROR "live at call to printnl: x$" return x } func f39c() (x [10]*int) { x = [10]*int{} - x[0] = new(int) // ERROR "live at call to (newobject|mallocTiny[48]): x$" + x[0] = new(int) // ERROR "live at call to (newobject|mallocgcTinySize[48]): x$" printnl() // ERROR "live at call to printnl: x$" return } -- cgit v1.3 From 532e3203492ebcac67b2f3aa2a52115f49d51997 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Thu, 18 Dec 2025 12:27:11 -0500 Subject: go/types,cmd/compile/internal/types2: better diagnostic for type shadowing This change causes the "x is not a type" diagnostic to describe x's actual kind, helping to reveal when shadowing is at work. (The kind description could improve other errors too.) Fixes #76877 Change-Id: Ia3484998bb384ff570c20b6792cf8461c60aa38c Reviewed-on: https://go-review.googlesource.com/c/go/+/731180 Reviewed-by: Robert Griesemer Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI Auto-Submit: Robert Griesemer --- src/cmd/compile/internal/types2/object.go | 49 ++++++++++++++++++++++ src/cmd/compile/internal/types2/typexpr.go | 3 +- src/go/types/object.go | 49 ++++++++++++++++++++++ src/go/types/typexpr.go | 3 +- src/internal/types/testdata/check/cycles5.go | 2 +- src/internal/types/testdata/check/cycles5a.go | 2 +- src/internal/types/testdata/check/decls1.go | 9 +++- src/internal/types/testdata/check/issues0.go | 2 +- src/internal/types/testdata/check/issues1.go | 2 +- .../types/testdata/fixedbugs/issue39634.go | 2 +- .../types/testdata/fixedbugs/issue65344.go | 4 +- test/fixedbugs/issue4610.go | 1 - 12 files changed, 117 insertions(+), 11 deletions(-) (limited to 'test') diff --git a/src/cmd/compile/internal/types2/object.go b/src/cmd/compile/internal/types2/object.go index dd2d415790..5d284ee61b 100644 --- a/src/cmd/compile/internal/types2/object.go +++ b/src/cmd/compile/internal/types2/object.go @@ -671,3 +671,52 @@ func writeFuncName(buf *bytes.Buffer, f *Func, qf Qualifier) { } buf.WriteString(f.name) } + +// objectKind returns a description of the object's kind. +func objectKind(obj Object) string { + switch obj := obj.(type) { + case *PkgName: + return "package name" + case *Const: + return "constant" + case *TypeName: + if obj.IsAlias() { + return "type alias" + } else if _, ok := obj.Type().(*TypeParam); ok { + return "type parameter" + } else { + return "defined type" + } + case *Var: + switch obj.Kind() { + case PackageVar: + return "package-level variable" + case LocalVar: + return "local variable" + case RecvVar: + return "receiver" + case ParamVar: + return "parameter" + case ResultVar: + return "result variable" + case FieldVar: + return "struct field" + } + case *Func: + if obj.Signature().Recv() != nil { + return "method" + } else { + return "function" + } + case *Label: + return "label" + case *Builtin: + return "built-in function" + case *Nil: + return "untyped nil" + } + if debug { + panic(fmt.Sprintf("unknown symbol (%T)", obj)) + } + return "unknown symbol" +} diff --git a/src/cmd/compile/internal/types2/typexpr.go b/src/cmd/compile/internal/types2/typexpr.go index a79b54eacc..7d5932eec1 100644 --- a/src/cmd/compile/internal/types2/typexpr.go +++ b/src/cmd/compile/internal/types2/typexpr.go @@ -50,7 +50,8 @@ func (check *Checker) ident(x *operand, e *syntax.Name, wantType bool) { // (see go.dev/issue/65344). _, gotType := obj.(*TypeName) if !gotType && wantType { - check.errorf(e, NotAType, "%s is not a type", obj.Name()) + check.errorf(e, NotAType, "%s (%s) is not a type", obj.Name(), objectKind(obj)) + // avoid "declared but not used" errors // (don't use Checker.use - we don't want to evaluate too much) if v, _ := obj.(*Var); v != nil && v.pkg == check.pkg /* see Checker.use1 */ { diff --git a/src/go/types/object.go b/src/go/types/object.go index 57158c1595..e12bf7285a 100644 --- a/src/go/types/object.go +++ b/src/go/types/object.go @@ -674,3 +674,52 @@ func writeFuncName(buf *bytes.Buffer, f *Func, qf Qualifier) { } buf.WriteString(f.name) } + +// objectKind returns a description of the object's kind. +func objectKind(obj Object) string { + switch obj := obj.(type) { + case *PkgName: + return "package name" + case *Const: + return "constant" + case *TypeName: + if obj.IsAlias() { + return "type alias" + } else if _, ok := obj.Type().(*TypeParam); ok { + return "type parameter" + } else { + return "defined type" + } + case *Var: + switch obj.Kind() { + case PackageVar: + return "package-level variable" + case LocalVar: + return "local variable" + case RecvVar: + return "receiver" + case ParamVar: + return "parameter" + case ResultVar: + return "result variable" + case FieldVar: + return "struct field" + } + case *Func: + if obj.Signature().Recv() != nil { + return "method" + } else { + return "function" + } + case *Label: + return "label" + case *Builtin: + return "built-in function" + case *Nil: + return "untyped nil" + } + if debug { + panic(fmt.Sprintf("unknown symbol (%T)", obj)) + } + return "unknown symbol" +} diff --git a/src/go/types/typexpr.go b/src/go/types/typexpr.go index 346ff18e9a..549c307487 100644 --- a/src/go/types/typexpr.go +++ b/src/go/types/typexpr.go @@ -49,7 +49,8 @@ func (check *Checker) ident(x *operand, e *ast.Ident, wantType bool) { // (see go.dev/issue/65344). _, gotType := obj.(*TypeName) if !gotType && wantType { - check.errorf(e, NotAType, "%s is not a type", obj.Name()) + check.errorf(e, NotAType, "%s (%s) is not a type", obj.Name(), objectKind(obj)) + // avoid "declared but not used" errors // (don't use Checker.use - we don't want to evaluate too much) if v, _ := obj.(*Var); v != nil && v.pkg == check.pkg /* see Checker.use1 */ { diff --git a/src/internal/types/testdata/check/cycles5.go b/src/internal/types/testdata/check/cycles5.go index de85c03d8a..9605f8ded4 100644 --- a/src/internal/types/testdata/check/cycles5.go +++ b/src/internal/types/testdata/check/cycles5.go @@ -162,7 +162,7 @@ func makeArray() (res T12) { return } // issue #20770 var r = newReader() -func newReader() r // ERROR "r is not a type" +func newReader() r // ERROR "r (package-level variable) is not a type" // variations of the theme of #8699 and #20770 var arr /* ERROR "cycle" */ = f() diff --git a/src/internal/types/testdata/check/cycles5a.go b/src/internal/types/testdata/check/cycles5a.go index e10f554e5c..a8cad50243 100644 --- a/src/internal/types/testdata/check/cycles5a.go +++ b/src/internal/types/testdata/check/cycles5a.go @@ -162,7 +162,7 @@ func makeArray() (res T12) { return } // issue #20770 var r = newReader() -func newReader() r // ERROR "r is not a type" +func newReader() r // ERROR "r (package-level variable) is not a type" // variations of the theme of #8699 and #20770 var arr /* ERROR "cycle" */ = f() diff --git a/src/internal/types/testdata/check/decls1.go b/src/internal/types/testdata/check/decls1.go index 6cdbf27f4c..5e4ba86cb0 100644 --- a/src/internal/types/testdata/check/decls1.go +++ b/src/internal/types/testdata/check/decls1.go @@ -63,7 +63,7 @@ var ( t12 complex64 = -(u + *t11) / *&v t13 int = a /* ERROR "shifted operand" */ << d t14 int = i << j - t15 math /* ERROR "math is not a type" */ + t15 math /* ERROR "math (package name) is not a type" */ t16 math.xxx /* ERROR "undefined" */ t17 math /* ERROR "not a type" */ .Pi t18 float64 = math.Pi * 10.0 @@ -144,3 +144,10 @@ func init /* ERROR "no arguments and no return values" */ (int) {} func init /* ERROR "no arguments and no return values" */ () int { return 0 } func init /* ERROR "no arguments and no return values" */ (int) int { return 0 } func (T) init(int) int { return 0 } + +func _() { + var error error + var _ error /* ERROR "error (local variable) is not a type" */ + _ = error +} + diff --git a/src/internal/types/testdata/check/issues0.go b/src/internal/types/testdata/check/issues0.go index 6117f7a8b9..fb4e1282f9 100644 --- a/src/internal/types/testdata/check/issues0.go +++ b/src/internal/types/testdata/check/issues0.go @@ -104,7 +104,7 @@ func issue10979() { // issue11347 // These should not crash. -var a1, b1, c1 /* ERROR "cycle" */ b1 /* ERROR "b1 is not a type" */ = 0 > 0<<""[""[c1]]>c1 +var a1, b1, c1 /* ERROR "cycle" */ b1 /* ERROR "b1 (package-level variable) is not a type" */ = 0 > 0<<""[""[c1]]>c1 var a2, b2 /* ERROR "cycle" */ = 0 /* ERROR "assignment mismatch" */ /* ERROR "assignment mismatch" */ > 0<<""[b2] var a3, b3 /* ERROR "cycle" */ = int /* ERROR "assignment mismatch" */ /* ERROR "assignment mismatch" */ (1<<""[b3]) diff --git a/src/internal/types/testdata/check/issues1.go b/src/internal/types/testdata/check/issues1.go index 72c6cf7757..482bdb08e1 100644 --- a/src/internal/types/testdata/check/issues1.go +++ b/src/internal/types/testdata/check/issues1.go @@ -246,5 +246,5 @@ var _ = append[context.CancelFunc, []context.CancelFunc, context.CancelFunc](can func g[T any](T) T { panic(0) } var _ = g[int] -var _ = g[nil /* ERROR "is not a type" */ ] +var _ = g[nil /* ERROR "nil (untyped nil) is not a type" */ ] var _ = g(0) diff --git a/src/internal/types/testdata/fixedbugs/issue39634.go b/src/internal/types/testdata/fixedbugs/issue39634.go index 58fc43eea6..5392f903c2 100644 --- a/src/internal/types/testdata/fixedbugs/issue39634.go +++ b/src/internal/types/testdata/fixedbugs/issue39634.go @@ -43,7 +43,7 @@ type foo9[A any] interface { foo9 /* ERROR "invalid recursive type" */ [A] } func _() { var _ = new(foo9[int]) } // crash 12 -var u, i [func /* ERROR "used as value" */ /* ERROR "used as value" */ (u /* ERROR "u is not a type" */ /* ERROR "u is not a type" */ , c /* ERROR "undefined" */ /* ERROR "undefined" */ ) {}(0, len /* ERROR "must be called" */ /* ERROR "must be called" */ )]c /* ERROR "undefined" */ /* ERROR "undefined" */ +var u, i [func /* ERROR "used as value" */ /* ERROR "used as value" */ (u /* ERROR "u (package-level variable) is not a type" */ /* ERROR "u (package-level variable) is not a type" */ , c /* ERROR "undefined" */ /* ERROR "undefined" */ ) {}(0, len /* ERROR "must be called" */ /* ERROR "must be called" */ )]c /* ERROR "undefined" */ /* ERROR "undefined" */ // crash 15 func y15() { var a /* ERROR "declared and not used" */ interface{ p() } = G15[string]{} } diff --git a/src/internal/types/testdata/fixedbugs/issue65344.go b/src/internal/types/testdata/fixedbugs/issue65344.go index 9f8337cf2b..4db97c04ff 100644 --- a/src/internal/types/testdata/fixedbugs/issue65344.go +++ b/src/internal/types/testdata/fixedbugs/issue65344.go @@ -4,12 +4,12 @@ package p -type T1 C /* ERROR "C is not a type" */ +type T1 C /* ERROR "C (constant) is not a type" */ // TODO(gri) try to avoid this follow-on error const C = T1(0 /* ERROR "cannot convert 0 (untyped int constant) to type T1" */) -type T2 V /* ERROR "V is not a type" */ +type T2 V /* ERROR "V (package-level variable) is not a type" */ var V T2 diff --git a/test/fixedbugs/issue4610.go b/test/fixedbugs/issue4610.go index d56c6d3e8c..8993f9882b 100644 --- a/test/fixedbugs/issue4610.go +++ b/test/fixedbugs/issue4610.go @@ -14,4 +14,3 @@ func main() { var foo bar _ = &foo{} // ERROR "is not a type|expected .;." } // GCCGO_ERROR "expected declaration" - -- cgit v1.3 From a8291eb61402d4549b69803fc8f834ded45b1f6c Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Tue, 20 Jan 2026 16:18:59 -0500 Subject: cmd/compile/internal/staticinit: fix bug in global new(expr) The StaticInit pass asserts that the operand of &v is a global, but this is not so for the &autotemp desugaring of new(expr). (The variable has by that point escaped to the heap, so the object code calls runtime.newobject. A future optimization would be to statically allocate the variable when it is safe and advantageous to do so.) Thanks to khr for suggesting the fix. + static test Fixes #77237 Change-Id: I71b34a1353fe0f3e297beab9851f8f87d765d8f1 Reviewed-on: https://go-review.googlesource.com/c/go/+/737680 Reviewed-by: Keith Randall Reviewed-by: Keith Randall LUCI-TryBot-Result: Go LUCI --- src/cmd/compile/internal/staticinit/sched.go | 3 +++ test/newexpr.go | 7 +++++++ 2 files changed, 10 insertions(+) (limited to 'test') diff --git a/src/cmd/compile/internal/staticinit/sched.go b/src/cmd/compile/internal/staticinit/sched.go index 5e39bb512f..c79715be46 100644 --- a/src/cmd/compile/internal/staticinit/sched.go +++ b/src/cmd/compile/internal/staticinit/sched.go @@ -228,6 +228,9 @@ func (s *Schedule) staticcopy(l *ir.Name, loff int64, rn *ir.Name, typ *types.Ty case ir.OADDR: r := r.(*ir.AddrExpr) if a, ok := r.X.(*ir.Name); ok && a.Op() == ir.ONAME { + if a.Class != ir.PEXTERN { + return false // e.g. local from new(expr) + } staticdata.InitAddr(l, loff, staticdata.GlobalLinksym(a)) return true } diff --git a/test/newexpr.go b/test/newexpr.go index c9a8804d4e..00d753c572 100644 --- a/test/newexpr.go +++ b/test/newexpr.go @@ -37,3 +37,10 @@ func main() { } } } + +// Regression test for ICE in staticdata.GlobalLinksym from +// use of autotemp outside a function (go.dev/issue/77237). +var ( + x = new(0) + y = x +) -- cgit v1.3