Skip to content

Commit

Permalink
fix: handle type declaration inside function
Browse files Browse the repository at this point in the history
  • Loading branch information
mvertes authored and traefiker committed Sep 25, 2019
1 parent 35e645c commit 0f46cd5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
12 changes: 12 additions & 0 deletions _test/fun9.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

type T uint

func main() {
type myint int
var i = myint(1)
println(i)
}

// Output:
// 1
20 changes: 19 additions & 1 deletion interp/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,25 @@ func (interp *Interpreter) cfg(root *node) ([]*node, error) {
return false

case typeSpec:
// processing already done in GTA pass
// processing already done in GTA pass for global types, only parses inlined types
if sc.def != nil {
typeName := n.child[0].ident
var typ *itype
if typ, err = nodeType(interp, sc, n.child[1]); err != nil {
return false
}
if typ.incomplete {
err = n.cfgErrorf("invalid type declaration")
return false
}
if n.child[1].kind == identExpr {
n.typ = &itype{cat: aliasT, val: typ, name: typeName}
} else {
n.typ = typ
n.typ.name = typeName
}
sc.sym[typeName] = &symbol{kind: typeSym, typ: n.typ}
}
return false

case arrayType, basicLit, chanType, funcType, mapType, structType:
Expand Down
2 changes: 1 addition & 1 deletion interp/gta.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func (interp *Interpreter) gta(root *node, rpath string) ([]*node, error) {
n.typ.name = typeName
n.typ.path = rpath
}
// Type may already be declared for a receiver in a method function
// Type may be already declared for a receiver in a method function
if sc.sym[typeName] == nil {
sc.sym[typeName] = &symbol{kind: typeSym}
} else {
Expand Down

0 comments on commit 0f46cd5

Please sign in to comment.