Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Fix compiler panic when handling composite literals representing named pointer types. #1208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions 32 compiler/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,34 @@ func (fc *funcContext) translateExpr(expr ast.Expr) *expression {

switch e := expr.(type) {
case *ast.CompositeLit:
if ptrType, isPointer := exprType.(*types.Pointer); isPointer {
exprType = ptrType.Elem()
if ptrType, isPointer := exprType.Underlying().(*types.Pointer); isPointer {
// Go automatically treats `[]*T{{}}` as `[]*T{&T{}}`, in which case the
// inner composite literal `{}` would has a pointer type. To make sure the
// type conversion is handled correctly, we generate the explicit AST for
// this.
var rewritten ast.Expr = fc.setType(&ast.UnaryExpr{
OpPos: e.Pos(),
Op: token.AND,
X: fc.setType(&ast.CompositeLit{
Elts: e.Elts,
}, ptrType.Elem()),
}, ptrType)

if exprType, ok := exprType.(*types.Named); ok {
// Handle a special case when the pointer type is named, e.g.:
// type PS *S
// _ = []PS{{}}
// In that case the value corresponding to the inner literal `{}` is
// initialized as `&S{}` and then converted to `PS`: `[]PS{PS(&S{})}`.
typeCast := fc.setType(&ast.CallExpr{
Fun: fc.newTypeIdent(exprType.String(), exprType.Obj()),
Lparen: e.Lbrace,
Args: []ast.Expr{rewritten},
Rparen: e.Rbrace,
}, exprType)
rewritten = typeCast
}
return fc.translateExpr(rewritten)
}

collectIndexedElements := func(elementType types.Type) []string {
Expand Down Expand Up @@ -173,7 +199,7 @@ func (fc *funcContext) translateExpr(expr ast.Expr) *expression {
}
return fc.formatExpr("new %s.ptr(%s)", fc.typeName(exprType), strings.Join(elements, ", "))
default:
panic(fmt.Sprintf("Unhandled CompositeLit type: %T\n", t))
panic(fmt.Sprintf("Unhandled CompositeLit type: %[1]T %[1]v\n", t))
}

case *ast.FuncLit:
Expand Down
6 changes: 6 additions & 0 deletions 6 compiler/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,12 @@ func (fc *funcContext) newIdent(name string, t types.Type) *ast.Ident {
return ident
}

func (fc *funcContext) newTypeIdent(name string, obj types.Object) *ast.Ident {
ident := ast.NewIdent(name)
fc.pkgCtx.Info.Uses[ident] = obj
return ident
}

func (fc *funcContext) setType(e ast.Expr, t types.Type) ast.Expr {
fc.pkgCtx.Types[e] = types.TypeAndValue{Type: t}
return e
Expand Down
15 changes: 15 additions & 0 deletions 15 tests/misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -920,3 +920,18 @@ func TestAssignImplicitConversion(t *testing.T) {
}
})
}

func TestCompositeLiterals(t *testing.T) {
type S struct{}
type SP *S

s1 := []*S{{}}
if got := reflect.TypeOf(s1[0]); got.String() != "*tests.S" {
t.Errorf("Got: reflect.TypeOf(s1[0]) = %v. Want: *tests.S", got)
}

s2 := []SP{{}}
if got := reflect.TypeOf(s2[0]); got.String() != "tests.SP" {
t.Errorf("Got: reflect.TypeOf(s2[0]) = %v. Want: tests.SP", got)
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.