Skip to content

Commit

Permalink
interp: retry type definition if an array size is undefined
Browse files Browse the repository at this point in the history
In case of forward definition of a constant, a symbol may be undefined when attempting to compute the array size in type analysis. Just mark the type as incomplete instead of aborting directly, to allow resolution at a second pass.

Fixes #1470.
  • Loading branch information
mvertes committed Oct 24, 2022
1 parent c4d1bf5 commit a5242cb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
15 changes: 15 additions & 0 deletions _test/issue-1470.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

type T struct {
num [tnum + 2]int
}

const tnum = 23

func main() {
t := T{}
println(len(t.num))
}

// Output:
// 25
6 changes: 5 additions & 1 deletion interp/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,11 @@ func nodeType2(interp *Interpreter, sc *scope, n *node, seen []*node) (t *itype,
length = int(vInt(sym.rval))
default:
// Size is defined by a numeric constant expression.
if _, err = interp.cfg(c0, sc, sc.pkgID, sc.pkgName); err != nil {
if _, err := interp.cfg(c0, sc, sc.pkgID, sc.pkgName); err != nil {
if strings.Contains(err.Error(), " undefined: ") {
incomplete = true
break
}
return nil, err
}
v, ok := c0.rval.Interface().(constant.Value)
Expand Down

0 comments on commit a5242cb

Please sign in to comment.