go.tools/go/types: const decls with invalid init expr must not panic
Make sure const objects always have a valid (possibly unknown) value. LGTM=adonovan R=adonovan CC=golang-codereviews https://golang.org/cl/106530044
This commit is contained in:
parent
79e0c7b71e
commit
32c9809768
|
|
@ -9,8 +9,6 @@ package types
|
|||
import (
|
||||
"go/ast"
|
||||
"go/token"
|
||||
|
||||
"code.google.com/p/go.tools/go/exact"
|
||||
)
|
||||
|
||||
// assignment reports whether x can be assigned to a variable of type T,
|
||||
|
|
@ -94,7 +92,6 @@ func (check *Checker) initConst(lhs *Const, x *operand) {
|
|||
if x.mode != invalid {
|
||||
check.errorf(x.pos(), "cannot define constant %s (type %s) as %s", lhs.Name(), lhs.typ, x)
|
||||
}
|
||||
lhs.val = exact.MakeUnknown()
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -99,6 +99,9 @@ func (check *Checker) constDecl(obj *Const, typ, init ast.Expr) {
|
|||
check.iota = obj.val
|
||||
defer func() { check.iota = nil }()
|
||||
|
||||
// provide valid constant value under all circumstances
|
||||
obj.val = exact.MakeUnknown()
|
||||
|
||||
// determine type, if any
|
||||
if typ != nil {
|
||||
t := check.typ(typ)
|
||||
|
|
|
|||
|
|
@ -82,4 +82,13 @@ func _() {
|
|||
)
|
||||
}
|
||||
|
||||
// TODO(gri) move extra tests from testdata/const0.src into here
|
||||
// Test case for constant with invalid initialization.
|
||||
// Caused panic because the constant value was not set up (gri - 7/8/2014).
|
||||
func _() {
|
||||
const (
|
||||
x string = missing /* ERROR "undeclared name" */
|
||||
y = x + ""
|
||||
)
|
||||
}
|
||||
|
||||
// TODO(gri) move extra tests from testdata/const0.src into here
|
||||
|
|
|
|||
Loading…
Reference in New Issue