mirror of https://github.com/golang/go.git
cmd/compile: untyped arrays bounds representable as integers are valid
Fixes #13485. Change-Id: I11dd15c7d14fc19d42a3b48427a4cc1208b18e6a Reviewed-on: https://go-review.googlesource.com/30456 Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
parent
f6b4c88941
commit
936ae27b9c
|
|
@ -340,18 +340,16 @@ OpSwitch:
|
||||||
|
|
||||||
case OTARRAY:
|
case OTARRAY:
|
||||||
ok |= Etype
|
ok |= Etype
|
||||||
var t *Type
|
r := typecheck(n.Right, Etype)
|
||||||
l := n.Left
|
|
||||||
r := n.Right
|
|
||||||
r = typecheck(r, Etype)
|
|
||||||
if r.Type == nil {
|
if r.Type == nil {
|
||||||
n.Type = nil
|
n.Type = nil
|
||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
if l == nil {
|
var t *Type
|
||||||
|
if n.Left == nil {
|
||||||
t = typSlice(r.Type)
|
t = typSlice(r.Type)
|
||||||
} else if l.Op == ODDD {
|
} else if n.Left.Op == ODDD {
|
||||||
t = typDDDArray(r.Type)
|
t = typDDDArray(r.Type)
|
||||||
if top&Ecomplit == 0 && n.Diag == 0 {
|
if top&Ecomplit == 0 && n.Diag == 0 {
|
||||||
t.Broke = true
|
t.Broke = true
|
||||||
|
|
@ -359,17 +357,9 @@ OpSwitch:
|
||||||
yyerror("use of [...] array outside of array literal")
|
yyerror("use of [...] array outside of array literal")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
n.Left = typecheck(n.Left, Erv)
|
n.Left = indexlit(typecheck(n.Left, Erv))
|
||||||
l := n.Left
|
l := n.Left
|
||||||
var v Val
|
if consttype(l) != CTINT {
|
||||||
switch consttype(l) {
|
|
||||||
case CTINT, CTRUNE:
|
|
||||||
v = l.Val()
|
|
||||||
|
|
||||||
case CTFLT:
|
|
||||||
v = toint(l.Val())
|
|
||||||
|
|
||||||
default:
|
|
||||||
if l.Type != nil && l.Type.IsInteger() && l.Op != OLITERAL {
|
if l.Type != nil && l.Type.IsInteger() && l.Op != OLITERAL {
|
||||||
yyerror("non-constant array bound %v", l)
|
yyerror("non-constant array bound %v", l)
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -379,11 +369,13 @@ OpSwitch:
|
||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
|
v := l.Val()
|
||||||
if doesoverflow(v, Types[TINT]) {
|
if doesoverflow(v, Types[TINT]) {
|
||||||
yyerror("array bound is too large")
|
yyerror("array bound is too large")
|
||||||
n.Type = nil
|
n.Type = nil
|
||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
bound := v.U.(*Mpint).Int64()
|
bound := v.U.(*Mpint).Int64()
|
||||||
if bound < 0 {
|
if bound < 0 {
|
||||||
yyerror("array bound must be non-negative")
|
yyerror("array bound must be non-negative")
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ var a [10]int // ok
|
||||||
var b [1e1]int // ok
|
var b [1e1]int // ok
|
||||||
var c [1.5]int // ERROR "truncated"
|
var c [1.5]int // ERROR "truncated"
|
||||||
var d ["abc"]int // ERROR "invalid array bound|not numeric"
|
var d ["abc"]int // ERROR "invalid array bound|not numeric"
|
||||||
var e [nil]int // ERROR "invalid array bound|not numeric"
|
var e [nil]int // ERROR "use of untyped nil|invalid array bound|not numeric"
|
||||||
var f [e]int // ERROR "invalid array bound|not constant"
|
var f [e]int // ERROR "invalid array bound|not constant"
|
||||||
var g [1 << 65]int // ERROR "array bound is too large|overflows"
|
var g [1 << 65]int // ERROR "array bound is too large|overflows"
|
||||||
var h [len(a)]int // ok
|
var h [len(a)]int // ok
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
// errorcheck
|
||||||
|
|
||||||
|
// Copyright 2010 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package p
|
||||||
|
|
||||||
|
var (
|
||||||
|
_ [10]int
|
||||||
|
_ [10.0]int
|
||||||
|
_ [float64(10)]int // ERROR "invalid array bound"
|
||||||
|
_ [10 + 0i]int
|
||||||
|
_ [complex(10, 0)]int
|
||||||
|
_ [complex128(complex(10, 0))]int // ERROR "invalid array bound"
|
||||||
|
_ ['a']int
|
||||||
|
_ [rune(65)]int
|
||||||
|
)
|
||||||
Loading…
Reference in New Issue