}
// isStaticCompositeLiteral reports whether n is a compile-time constant.
-// n must be a struct or array literal.
func isStaticCompositeLiteral(n *Node) bool {
+ switch n.Op {
+ case OARRAYLIT:
+ if n.Type.IsSlice() {
+ return false
+ }
+ case OSTRUCTLIT:
+ case OLITERAL:
+ return true
+ default:
+ return false
+ }
for _, r := range n.List.Slice() {
if r.Op != OKEY {
Fatalf("isStaticCompositeLiteral: rhs not OKEY: %v", r)
return false
}
value := r.Right
- switch value.Op {
- case OSTRUCTLIT, OARRAYLIT:
- if !isStaticCompositeLiteral(value) {
- return false
- }
- default:
- if value.Op != OLITERAL {
- return false
- }
+ if !isStaticCompositeLiteral(value) {
+ return false
}
}
return true
t := n.Type
switch n.Op {
default:
- Fatalf("anylit: not lit")
+ Fatalf("anylit: not lit, op=%v node=%v", opnames[n.Op], n)
case OPTRLIT:
if !t.IsPtr() {
n = r
case OARRAYLIT, OMAPLIT, OSTRUCTLIT, OPTRLIT:
- if (n.Op == OSTRUCTLIT || (n.Op == OARRAYLIT && !n.Type.IsSlice())) && isStaticCompositeLiteral(n) {
+ if isStaticCompositeLiteral(n) {
// n can be directly represented in the read-only data section.
// Make direct reference to the static data. See issue 12841.
vstat := staticname(n.Type, 0)