// 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))
}
// 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))
}
)
}
-func newInvalid() {
- f2 := func() (x, y int) { return }
+func new1() {
_ = new() // ERROR "not enough arguments"
_ = new(1, 2) // ERROR "too many arguments"
+ _ = new(unsafe /* ERROR "use of package unsafe not in selector" */ )
+
+ _ = new(struct{ x, y int })
+ p := new(float64)
+ q := new(*float64)
+ _ = *p == **q
+
+ type G[P any] struct{}
+ _ = new(G[int])
+ _ = new(G /* ERROR "cannot use generic type G without instantiation" */ )
+
new /* ERROR "not used" */ (int)
_ = &new /* ERROR "cannot take address" */ (int)
_ = new(int... /* ERROR "invalid use of ..." */)
_ = new(f0 /* ERROR "f0() (no value) used as value or type" */ ())
_ = new(len /* ERROR "len (built-in) must be called" */)
_ = new(1 /* ERROR "argument to new (overflows)" */ << 70)
- _ = new(f2 /* ERRORx "multiple-value.*in single-value context" */ ())
}
-// new(T)
-func newType() {
- _ = new(struct{ x, y int })
-
- p := new(float64)
- q := new(*float64)
- _ = *p == **q
-}
-
-// new(expr), added in go1.26
-func newExpr() {
- f1 := func() (x []int) { return }
+func new2() {
+ // new(expr), added in go1.26
+ f1 := func() []int { panic(0) }
+ f2 := func() (int, int) { panic(0) }
var (
_ *[]int = new(f1())
_ *func() []int = new(f1)
_ *bool = new(false)
+ _ *bool = new(1 < 2)
_ *int = new(123)
_ *float64 = new(1.0)
_ *uint = new(uint(3))
_ *struct{} = new(struct{}{})
_ *any = new(any)
+ _ = new(f2 /* ERRORx "multiple-value.*in single-value context" */ ())
+ _ = new(1 << /* ERROR "constant shift overflow" */ 1000)
+ _ = new(1e10000 /* ERROR "cannot use 1e10000 (untyped float constant 1e+10000) as float64 value in argument to new (overflows)" */ )
+ _ = new(nil /* ERROR "use of untyped nil in argument to new" */ )
+ _ = new(comparable /* ERROR "cannot use type comparable outside a type constraint" */ )
+ _ = new(new /* ERROR "new (built-in) must be called" */ )
+ _ = new(panic /* ERROR "panic(0) (no value) used as value or type" */ (0))
+
// from issue 43125
_ = new(-1)
_ = new(1 + 1)
package p
-var _ = new /* ERROR "new(expr) requires go1.26 or later" */ (123)
+func f(x int) {
+ _ = new /* ERROR "new(123) requires go1.26 or later" */ (123)
+ _ = new /* ERROR "new(x) requires go1.26 or later" */ (x)
+ _ = new /* ERROR "new(f) requires go1.26 or later" */ (f)
+ _ = new /* ERROR "new(1 < 2) requires go1.26 or later" */ (1 < 2)
+}
--- /dev/null
+// -lang=go1.25
+
+// Copyright 2025 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.
+
+package p
+
+import "strings"
+
+type T int
+type G[P any] struct{}
+
+var x T
+
+// Verify that we don't get a version error when there's another error present in new(expr).
+
+func f() {
+ _ = new(U /* ERROR "undefined: U" */)
+ _ = new(strings.BUILDER /* ERROR "undefined: strings.BUILDER (but have Builder)" */)
+ _ = new(T) // ok
+ _ = new(G[int]) // ok
+ _ = new(G /* ERROR "cannot use generic type G without instantiation" */)
+ _ = new(nil /* ERROR "use of untyped nil in argument to new" */)
+ _ = new(comparable /* ERROR "cannot use type comparable outside a type constraint" */)
+ _ = new(new /* ERROR "new (built-in) must be called" */)
+ _ = new(panic /* ERROR "panic(0) (no value) used as value or type" */ (0))
+}