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

Commit 961cca9

Browse filesBrowse files
committed
Fix compiler panic when handling composite literals representing names pointer types.
Prior to this change, the following code would panic the compiler: ```go type ( S struct{ f int } PS *[]S ) var _ = []*S{{}} // This compiles, but potentially incorrectly. var _ = []PS{{}} // Compiler panics here. ``` Prior to this change, GopherJS compiler didn't expect that a pointer type could be named in this context, and would panic. This is addressed by checking the underlying type, rather than the type itself. However, there was a bigger correctness problem here too. According to the Go spec: > Within a composite literal of array, slice, or map type T, elements or map keys that are themselves composite literals may elide the respective literal type if it is identical to the element or key type of T. Similarly, elements or keys that are addresses of composite literals may elide the &T when the element or key type is *T. So in the example above, `[]PS{{}}` expression is equivalent to `[]PS{PS(*S{})}`. However, even with the first part of the fix, the code emitted by the compiler would have been equivalent to `[]PS{S{}}`. This mostly works because at runtime GopherJS represents a pointer to the struct and the struct type as the same JS object, but it would break down when it comes to methods and non-struct types. So the second part of the fix is to generate the explicit AST for taking an address of the value type and type conversion, and compiling that.
1 parent c3dcfaf commit 961cca9
Copy full SHA for 961cca9

File tree

3 files changed

+50
-3
lines changed
Filter options

3 files changed

+50
-3
lines changed

‎compiler/expressions.go

Copy file name to clipboardExpand all lines: compiler/expressions.go
+29-3Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,34 @@ func (fc *funcContext) translateExpr(expr ast.Expr) *expression {
100100

101101
switch e := expr.(type) {
102102
case *ast.CompositeLit:
103-
if ptrType, isPointer := exprType.(*types.Pointer); isPointer {
104-
exprType = ptrType.Elem()
103+
if ptrType, isPointer := exprType.Underlying().(*types.Pointer); isPointer {
104+
// Go automatically treats `[]*T{{}}` as `[]*T{&T{}}`, in which case the
105+
// inner composite literal `{}` would has a pointer type. To make sure the
106+
// type conversion is handled correctly, we generate the explicit AST for
107+
// this.
108+
var rewritten ast.Expr = fc.setType(&ast.UnaryExpr{
109+
OpPos: e.Pos(),
110+
Op: token.AND,
111+
X: fc.setType(&ast.CompositeLit{
112+
Elts: e.Elts,
113+
}, ptrType.Elem()),
114+
}, ptrType)
115+
116+
if exprType, ok := exprType.(*types.Named); ok {
117+
// Handle a special case when the pointer type is named, e.g.:
118+
// type PS *S
119+
// _ = []PS{{}}
120+
// In that case the value corresponding to the inner literal `{}` is
121+
// initialized as `&S{}` and then converted to `PS`: `[]PS{PS(&S{})}`.
122+
typeCast := fc.setType(&ast.CallExpr{
123+
Fun: fc.newTypeIdent(exprType.String(), exprType.Obj()),
124+
Lparen: e.Lbrace,
125+
Args: []ast.Expr{rewritten},
126+
Rparen: e.Rbrace,
127+
}, exprType)
128+
rewritten = typeCast
129+
}
130+
return fc.translateExpr(rewritten)
105131
}
106132

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

179205
case *ast.FuncLit:

‎compiler/utils.go

Copy file name to clipboardExpand all lines: compiler/utils.go
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,12 @@ func (fc *funcContext) newIdent(name string, t types.Type) *ast.Ident {
282282
return ident
283283
}
284284

285+
func (fc *funcContext) newTypeIdent(name string, obj types.Object) *ast.Ident {
286+
ident := ast.NewIdent(name)
287+
fc.pkgCtx.Info.Uses[ident] = obj
288+
return ident
289+
}
290+
285291
func (fc *funcContext) setType(e ast.Expr, t types.Type) ast.Expr {
286292
fc.pkgCtx.Types[e] = types.TypeAndValue{Type: t}
287293
return e

‎tests/misc_test.go

Copy file name to clipboardExpand all lines: tests/misc_test.go
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,3 +920,18 @@ func TestAssignImplicitConversion(t *testing.T) {
920920
}
921921
})
922922
}
923+
924+
func TestCompositeLiterals(t *testing.T) {
925+
type S struct{}
926+
type SP *S
927+
928+
s1 := []*S{{}}
929+
if got := reflect.TypeOf(s1[0]); got.String() != "*tests.S" {
930+
t.Errorf("Got: reflect.TypeOf(s1[0]) = %v. Want: *tests.S", got)
931+
}
932+
933+
s2 := []SP{{}}
934+
if got := reflect.TypeOf(s2[0]); got.String() != "tests.SP" {
935+
t.Errorf("Got: reflect.TypeOf(s2[0]) = %v. Want: tests.SP", got)
936+
}
937+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.