Skip to content

cl: support new(expr) for Go 1.26 - #2712

Merged
xushiwei merged 1 commit into
mainfrom
xgopilot/issue-2702-1775955078
Apr 12, 2026
Merged

xushiwei merged 1 commit into
mainfrom
xgopilot/issue-2702-1775955078

Conversation

@fennoai

@fennoai fennoai Bot commented Apr 12, 2026

Copy link
Copy Markdown
Contributor

Requested by @xushiwei

Implements new(expr) support as proposed in #2702.

Summary

  • Updated github.com/goplus/gogen dependency (builtin: support new(expr) for Go 1.26 gogen#615) to handle expression arguments in new() builtin
  • Added test cases in cl/_testgop/newexpr/ and cl/compile_test.go
  • new(42) → *int pointing to 42, new("hello") → *string pointing to "hello"

Changes

  • gogen (builtin_gengo.go): Modified newInstr.Call to accept expressions, not just types. Uses DefaultConv for proper untyped-to-typed conversion.
  • cl (compile_test.go): Added TestNewExpr inline test
  • cl (_testgop/newexpr/): Added test directory with in.xgo and out.go
  • go.mod/go.sum: Updated gogen dependency

Test plan

  • gogen unit tests pass (TestNew, TestNewExpr, TestNewExprString)
  • cl unit tests pass (TestNewExpr, TestMakeAndNew)
  • _testgop/newexpr directory test passes
  • Full cl test suite passes

See https://antonz.org/accepted/new-expr/

Closes #2702

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>

@fennoai fennoai Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread cl/compile_test.go
Comment on lines +1455 to +1470
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)
}
`)
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread cl/_testgop/newexpr/out.go
Comment thread go.mod
@xushiwei
xushiwei merged commit 1f3cc33 into main Apr 12, 2026
3 of 9 checks passed
@xushiwei
xushiwei deleted the xgopilot/issue-2702-1775955078 branch April 12, 2026 02:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Proposal: support new(expr) in Go 1.26

2 participants