aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal
diff options
context:
space:
mode:
authorRobert Griesemer <gri@google.com>2025-10-20 15:02:23 -0700
committerGopher Robot <gobot@golang.org>2025-10-21 14:41:38 -0700
commit06e57e60a7ff6cbc9c608278cbac4db514c7161a (patch)
treeb8add1cba93a0dd9b05ceaad8975517fd0c880cd /src/cmd/compile/internal
parent6c3d0d259f2d83a338a3a7da268e2dd5834aa35f (diff)
downloadgo-06e57e60a7ff6cbc9c608278cbac4db514c7161a.tar.xz
go/types, types2: only report version errors if new(expr) is ok otherwise
If new(expr) is used before Go 1.26, don't report version errors if there are other problems with the expression. While at it, implement multiple missing type checks for new(expr) and add corresponding test cases that were missed in CL 704935 (tests for no value expressions, generic types, untyped nil). Reorganize/rename builtins0.go tests for new to match existing test case patterns again. Fixes #75986. For #45624. Change-Id: I39e5516d3f8d191cc390a4d8b9911c312bbb177c Reviewed-on: https://go-review.googlesource.com/c/go/+/713241 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Mark Freeman <markfreeman@google.com> Reviewed-by: Robert Griesemer <gri@google.com> Auto-Submit: Robert Griesemer <gri@google.com>
Diffstat (limited to 'src/cmd/compile/internal')
-rw-r--r--src/cmd/compile/internal/types2/builtins.go32
1 files changed, 16 insertions, 16 deletions
diff --git a/src/cmd/compile/internal/types2/builtins.go b/src/cmd/compile/internal/types2/builtins.go
index 84acb4ca48..d4463bc4b0 100644
--- a/src/cmd/compile/internal/types2/builtins.go
+++ b/src/cmd/compile/internal/types2/builtins.go
@@ -639,31 +639,31 @@ func (check *Checker) builtin(x *operand, call *syntax.CallExpr, id builtinId) (
// new(T) or new(expr)
// (no argument evaluated yet)
arg := argList[0]
- check.exprOrType(x, arg, true)
- var T Type
+ check.exprOrType(x, arg, false)
+ check.exclude(x, 1<<novalue|1<<builtin)
switch x.mode {
- case builtin:
- check.errorf(x, UncalledBuiltin, "%s must be called", x)
- x.mode = invalid
+ case invalid:
+ return
case typexpr:
// new(T)
- T = x.typ
- if !isValid(T) {
- return
- }
+ check.validVarType(arg, x.typ)
default:
// new(expr)
- check.verifyVersionf(call.Fun, go1_26, "new(expr)")
- T = Default(x.typ)
- if T != x.typ {
- // untyped constant: check for overflow.
- check.assignment(x, T, "argument to new")
+ if isUntyped(x.typ) {
+ // check for overflow and untyped nil
+ check.assignment(x, nil, "argument to new")
+ if x.mode == invalid {
+ return
+ }
+ assert(isTyped(x.typ))
}
- check.validVarType(arg, T)
+ // report version error only if there are no other errors
+ check.verifyVersionf(call.Fun, go1_26, "new(%s)", arg)
}
+ T := x.typ
x.mode = value
- x.typ = &Pointer{base: T}
+ x.typ = NewPointer(T)
if check.recordTypes() {
check.recordBuiltinType(call.Fun, makeSig(x.typ, T))
}