Repository navigation
cl: support new(expr) for Go 1.26 - #2712
Conversation
Update gogen dependency to support new(expr) syntax where
the argument is an expression instead of a type. This allocates
a variable of the expression's type, initializes it with the
expression value, and returns its address.
Examples: new(42) returns *int, new("hello") returns *string.
See https://antonz.org/accepted/new-expr/
Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: xushiwei <396972+xushiwei@users.noreply.github.com>
There was a problem hiding this comment.
Review Summary
Clean, focused PR that adds new(expr) support for Go 1.26 by bumping the gogen dependency and adding corresponding tests. The implementation correctly follows the Phase 2 convention (only cl/ tests and dependency changes). No performance or security concerns.
Main feedback: Test coverage is thin for a new language feature — only two happy-path literal types are tested. Adding edge cases and error tests would strengthen confidence in the gogen integration.
| func TestNewExpr(t *testing.T) { | ||
| gopClTest(t, ` | ||
| a := new(42) | ||
| b := new("hello") | ||
| echo *a, *b | ||
| `, `package main | ||
|
|
||
| import "fmt" | ||
|
|
||
| func main() { | ||
| a := new(42) | ||
| b := new("hello") | ||
| fmt.Println(*a, *b) | ||
| } | ||
| `) | ||
| } |
There was a problem hiding this comment.
The test only covers new(42) (int literal) and new("hello") (string literal). Consider expanding coverage with additional expression types to better exercise the gogen integration:
- Float/bool literals:
new(3.14),new(true) - Typed expressions:
new(int64(42)) - Variable expressions:
x := 42; p := new(x)(if supported) - Compound expressions:
new(x + 1),new(len(s))(if supported)
Also, no error-case tests are present. It would be valuable to verify proper error messages for invalid usages (e.g., new() with no args, new(a, b) with too many args) — similar to the existing TestErrNewObj pattern in error_msg_test.go.
Requested by @xushiwei
Implements
new(expr)support as proposed in #2702.Summary
github.com/goplus/gogendependency (builtin: support new(expr) for Go 1.26 gogen#615) to handle expression arguments innew()builtincl/_testgop/newexpr/andcl/compile_test.gonew(42)→*intpointing to 42,new("hello")→*stringpointing to "hello"Changes
builtin_gengo.go): ModifiednewInstr.Callto accept expressions, not just types. UsesDefaultConvfor proper untyped-to-typed conversion.compile_test.go): AddedTestNewExprinline test_testgop/newexpr/): Added test directory within.xgoandout.goTest plan
TestNew,TestNewExpr,TestNewExprString)TestNewExpr,TestMakeAndNew)_testgop/newexprdirectory test passescltest suite passesSee https://antonz.org/accepted/new-expr/
Closes #2702