@@ -650,6 +650,8 @@ func getdyn(n *Node, top bool) initGenType {
650
650
// isStaticCompositeLiteral reports whether n is a compile-time constant.
651
651
func isStaticCompositeLiteral (n * Node ) bool {
652
652
switch n .Op {
653
+ case ONAME :
654
+ return n .Class () == PEXTERN && n .Name != nil && n .Name .Readonly ()
653
655
case OSLICELIT :
654
656
return false
655
657
case OARRAYLIT :
@@ -954,26 +956,22 @@ func maplit(n *Node, m *Node, init *Nodes) {
954
956
a .List .Set2 (typenod (n .Type ), nodintconst (int64 (n .List .Len ())))
955
957
litas (m , a , init )
956
958
957
- // Split the initializers into static and dynamic.
958
- var stat , dyn []* Node
959
- for _ , r := range n .List .Slice () {
960
- if r .Op != OKEY {
961
- Fatalf ("maplit: rhs not OKEY: %v" , r )
962
- }
963
- if isStaticCompositeLiteral (r .Left ) && isStaticCompositeLiteral (r .Right ) {
964
- stat = append (stat , r )
965
- } else {
966
- dyn = append (dyn , r )
959
+ entries := n .List .Slice ()
960
+
961
+ // The order pass already removed any dynamic (runtime-computed) entries.
962
+ // All remaining entries are static. Double-check that.
963
+ for _ , r := range entries {
964
+ if ! isStaticCompositeLiteral (r .Left ) || ! isStaticCompositeLiteral (r .Right ) {
965
+ Fatalf ("maplit: entry is not a literal: %v" , r )
967
966
}
968
967
}
969
968
970
- // Add static entries.
971
- if len (stat ) > 25 {
972
- // For a large number of static entries, put them in an array and loop.
969
+ if len (entries ) > 25 {
970
+ // For a large number of entries, put them in an array and loop.
973
971
974
972
// build types [count]Tindex and [count]Tvalue
975
- tk := types .NewArray (n .Type .Key (), int64 (len (stat )))
976
- te := types .NewArray (n .Type .Elem (), int64 (len (stat )))
973
+ tk := types .NewArray (n .Type .Key (), int64 (len (entries )))
974
+ te := types .NewArray (n .Type .Elem (), int64 (len (entries )))
977
975
978
976
// TODO(josharian): suppress alg generation for these types?
979
977
dowidth (tk )
@@ -987,7 +985,7 @@ func maplit(n *Node, m *Node, init *Nodes) {
987
985
988
986
datak := nod (OARRAYLIT , nil , nil )
989
987
datae := nod (OARRAYLIT , nil , nil )
990
- for _ , r := range stat {
988
+ for _ , r := range entries {
991
989
datak .List .Append (r .Left )
992
990
datae .List .Append (r .Right )
993
991
}
@@ -1018,29 +1016,17 @@ func maplit(n *Node, m *Node, init *Nodes) {
1018
1016
loop = typecheck (loop , ctxStmt )
1019
1017
loop = walkstmt (loop )
1020
1018
init .Append (loop )
1021
- } else {
1022
- // For a small number of static entries, just add them directly.
1023
- addMapEntries (m , stat , init )
1024
- }
1025
-
1026
- // Add dynamic entries.
1027
- addMapEntries (m , dyn , init )
1028
- }
1029
-
1030
- func addMapEntries (m * Node , dyn []* Node , init * Nodes ) {
1031
- if len (dyn ) == 0 {
1032
1019
return
1033
1020
}
1034
-
1035
- nerr := nerrors
1021
+ // For a small number of entries, just add them directly.
1036
1022
1037
1023
// Build list of var[c] = expr.
1038
1024
// Use temporaries so that mapassign1 can have addressable key, elem.
1039
1025
// TODO(josharian): avoid map key temporaries for mapfast_* assignments with literal keys.
1040
1026
tmpkey := temp (m .Type .Key ())
1041
1027
tmpelem := temp (m .Type .Elem ())
1042
1028
1043
- for _ , r := range dyn {
1029
+ for _ , r := range entries {
1044
1030
index , elem := r .Left , r .Right
1045
1031
1046
1032
setlineno (index )
@@ -1060,13 +1046,9 @@ func addMapEntries(m *Node, dyn []*Node, init *Nodes) {
1060
1046
a = typecheck (a , ctxStmt )
1061
1047
a = walkstmt (a )
1062
1048
init .Append (a )
1063
-
1064
- if nerr != nerrors {
1065
- break
1066
- }
1067
1049
}
1068
1050
1069
- a : = nod (OVARKILL , tmpkey , nil )
1051
+ a = nod (OVARKILL , tmpkey , nil )
1070
1052
a = typecheck (a , ctxStmt )
1071
1053
init .Append (a )
1072
1054
a = nod (OVARKILL , tmpelem , nil )
0 commit comments