aboutsummaryrefslogtreecommitdiff
path: root/src/cmd
diff options
context:
space:
mode:
authorAlan Donovan <adonovan@google.com>2025-09-17 22:17:41 -0400
committerAlan Donovan <adonovan@google.com>2025-09-23 11:36:46 -0700
commita27261c42fcebf601587725714b9ef53c47b06b3 (patch)
tree2f584ab21e11369633139c3c8f5752779a68bafa /src/cmd
parente93f439ac4160baf9992f059d2bfb511e23f63c9 (diff)
downloadgo-a27261c42fcebf601587725714b9ef53c47b06b3.tar.xz
go/types,types2: allow new(expr)
For #45624 Change-Id: I6d77a2a1d6095cac0edc36060cbf98c72b749404 Reviewed-on: https://go-review.googlesource.com/c/go/+/704935 Auto-Submit: Alan Donovan <adonovan@google.com> Reviewed-by: Robert Findley <rfindley@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Diffstat (limited to 'src/cmd')
-rw-r--r--src/cmd/compile/internal/types2/builtins.go27
-rw-r--r--src/cmd/compile/internal/types2/version.go1
2 files changed, 24 insertions, 4 deletions
diff --git a/src/cmd/compile/internal/types2/builtins.go b/src/cmd/compile/internal/types2/builtins.go
index 4bb2135755..3de2857ed4 100644
--- a/src/cmd/compile/internal/types2/builtins.go
+++ b/src/cmd/compile/internal/types2/builtins.go
@@ -636,11 +636,30 @@ func (check *Checker) builtin(x *operand, call *syntax.CallExpr, id builtinId) (
}
case _New:
- // new(T)
+ // new(T) or new(expr)
// (no argument evaluated yet)
- T := check.varType(argList[0])
- if !isValid(T) {
- return
+ arg := argList[0]
+ check.exprOrType(x, arg, true)
+ var T Type
+ switch x.mode {
+ case builtin:
+ check.errorf(x, UncalledBuiltin, "%s must be called", x)
+ x.mode = invalid
+ case typexpr:
+ // new(T)
+ T = x.typ
+ if !isValid(T) {
+ return
+ }
+ 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")
+ }
+ check.validVarType(arg, T)
}
x.mode = value
diff --git a/src/cmd/compile/internal/types2/version.go b/src/cmd/compile/internal/types2/version.go
index b555f398da..765b0f7e9a 100644
--- a/src/cmd/compile/internal/types2/version.go
+++ b/src/cmd/compile/internal/types2/version.go
@@ -43,6 +43,7 @@ var (
go1_21 = asGoVersion("go1.21")
go1_22 = asGoVersion("go1.22")
go1_23 = asGoVersion("go1.23")
+ go1_26 = asGoVersion("go1.26")
// current (deployed) Go version
go_current = asGoVersion(fmt.Sprintf("go1.%d", goversion.Version))