@@ -1015,32 +1015,38 @@ func (check *Checker) exprInternal(x *operand, e ast.Expr, hint Type) exprKind {
1015
1015
}
1016
1016
1017
1017
case * ast.CompositeLit :
1018
- typ := hint
1019
- openArray := false
1020
- if e .Type != nil {
1018
+ var typ , base Type
1019
+
1020
+ switch {
1021
+ case e .Type != nil :
1022
+ // composite literal type present - use it
1021
1023
// [...]T array types may only appear with composite literals.
1022
1024
// Check for them here so we don't have to handle ... in general.
1023
- typ = nil
1024
1025
if atyp , _ := e .Type .(* ast.ArrayType ); atyp != nil && atyp .Len != nil {
1025
1026
if ellip , _ := atyp .Len .(* ast.Ellipsis ); ellip != nil && ellip .Elt == nil {
1026
1027
// We have an "open" [...]T array type.
1027
1028
// Create a new ArrayType with unknown length (-1)
1028
1029
// and finish setting it up after analyzing the literal.
1029
1030
typ = & Array {len : - 1 , elem : check .typ (atyp .Elt )}
1030
- openArray = true
1031
+ base = typ
1032
+ break
1031
1033
}
1032
1034
}
1033
- if typ == nil {
1034
- typ = check .typ (e .Type )
1035
- }
1036
- }
1037
- if typ == nil {
1035
+ typ = check .typ (e .Type )
1036
+ base = typ
1037
+
1038
+ case hint != nil :
1039
+ // no composite literal type present - use hint (element type of enclosing type)
1040
+ typ = hint
1041
+ base , _ = deref (typ .Underlying ()) // *T implies &T{}
1042
+
1043
+ default :
1038
1044
// TODO(gri) provide better error messages depending on context
1039
1045
check .error (e .Pos (), "missing type in composite literal" )
1040
1046
goto Error
1041
1047
}
1042
1048
1043
- switch typ , _ := deref ( typ ); utyp := typ .Underlying ().(type ) {
1049
+ switch utyp := base .Underlying ().(type ) {
1044
1050
case * Struct :
1045
1051
if len (e .Elts ) == 0 {
1046
1052
break
@@ -1109,7 +1115,7 @@ func (check *Checker) exprInternal(x *operand, e ast.Expr, hint Type) exprKind {
1109
1115
// If we have an "open" [...]T array, set the length now that we know it
1110
1116
// and record the type for [...] (usually done by check.typExpr which is
1111
1117
// not called for [...]).
1112
- if openArray {
1118
+ if utyp . len < 0 {
1113
1119
utyp .len = n
1114
1120
check .recordTypeAndValue (e .Type , typexpr , utyp , nil )
1115
1121
}
0 commit comments