@@ -1091,6 +1091,34 @@ func compileCompositeLitElts(ctx *blockCtx, elts []ast.Expr, kind int, expected
1091
1091
}
1092
1092
}
1093
1093
1094
+ func unparent (x ast.Expr ) ast.Expr {
1095
+ if e , ok := x .(* ast.ParenExpr ); ok {
1096
+ return e .X
1097
+ }
1098
+ return x
1099
+ }
1100
+
1101
+ func compileStructLit (ctx * blockCtx , elts []ast.Expr , t * types.Struct , typ types.Type , src * ast.CompositeLit ) error {
1102
+ for idx , elt := range elts {
1103
+ switch expr := unparent (elt ).(type ) {
1104
+ case * ast.LambdaExpr , * ast.LambdaExpr2 :
1105
+ sig , err := checkLambdaFuncType (ctx , expr , t .Field (idx ).Type (), clLambaField , elt )
1106
+ if err != nil {
1107
+ return err
1108
+ }
1109
+ compileLambda (ctx , expr , sig )
1110
+ case * ast.SliceLit :
1111
+ compileSliceLit (ctx , expr , t .Field (idx ).Type ())
1112
+ case * ast.CompositeLit :
1113
+ compileCompositeLit (ctx , expr , t .Field (idx ).Type (), false )
1114
+ default :
1115
+ compileExpr (ctx , elt )
1116
+ }
1117
+ }
1118
+ ctx .cb .StructLit (typ , len (elts ), false , src )
1119
+ return nil
1120
+ }
1121
+
1094
1122
func compileStructLitInKeyVal (ctx * blockCtx , elts []ast.Expr , t * types.Struct , typ types.Type , src * ast.CompositeLit ) error {
1095
1123
for _ , elt := range elts {
1096
1124
kv := elt .(* ast.KeyValueExpr )
@@ -1105,13 +1133,17 @@ func compileStructLitInKeyVal(ctx *blockCtx, elts []ast.Expr, t *types.Struct, t
1105
1133
if rec := ctx .recorder (); rec != nil {
1106
1134
rec .Use (name , t .Field (idx ))
1107
1135
}
1108
- switch expr := kv .Value .(type ) {
1136
+ switch expr := unparent ( kv .Value ) .(type ) {
1109
1137
case * ast.LambdaExpr , * ast.LambdaExpr2 :
1110
1138
sig , err := checkLambdaFuncType (ctx , expr , t .Field (idx ).Type (), clLambaField , kv .Key )
1111
1139
if err != nil {
1112
1140
return err
1113
1141
}
1114
1142
compileLambda (ctx , expr , sig )
1143
+ case * ast.SliceLit :
1144
+ compileSliceLit (ctx , expr , t .Field (idx ).Type ())
1145
+ case * ast.CompositeLit :
1146
+ compileCompositeLit (ctx , expr , t .Field (idx ).Type (), false )
1115
1147
default :
1116
1148
compileExpr (ctx , kv .Value )
1117
1149
}
@@ -1194,8 +1226,14 @@ func compileCompositeLitEx(ctx *blockCtx, v *ast.CompositeLit, expected types.Ty
1194
1226
typ , underlying = expected , tu
1195
1227
}
1196
1228
}
1197
- if t , ok := underlying .(* types.Struct ); ok && kind == compositeLitKeyVal {
1198
- if err := compileStructLitInKeyVal (ctx , v .Elts , t , typ , v ); err != nil {
1229
+ if t , ok := underlying .(* types.Struct ); ok {
1230
+ var err error
1231
+ if kind == compositeLitKeyVal {
1232
+ err = compileStructLitInKeyVal (ctx , v .Elts , t , typ , v )
1233
+ } else {
1234
+ err = compileStructLit (ctx , v .Elts , t , typ , v )
1235
+ }
1236
+ if err != nil {
1199
1237
return err
1200
1238
}
1201
1239
} else {
@@ -1214,8 +1252,6 @@ func compileCompositeLitEx(ctx *blockCtx, v *ast.CompositeLit, expected types.Ty
1214
1252
ctx .cb .SliceLitEx (typ , n << kind , kind == compositeLitKeyVal , v )
1215
1253
case * types.Array :
1216
1254
ctx .cb .ArrayLitEx (typ , n << kind , kind == compositeLitKeyVal , v )
1217
- case * types.Struct :
1218
- ctx .cb .StructLit (typ , n , false , v ) // key-val mode handled by compileStructLitInKeyVal
1219
1255
default :
1220
1256
return ctx .newCodeErrorf (v .Pos (), "invalid composite literal type %v" , typ )
1221
1257
}
0 commit comments