Skip to content

Commit

Permalink
recursive types: fix gophernotes issue #208
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmos72 committed Jun 7, 2020
1 parent ab9b5f0 commit 647446f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
15 changes: 14 additions & 1 deletion all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -634,12 +634,25 @@ var testcases = []TestCase{
TestCase{F, "self_embedded_2", "var x X; x.X = &x; x.X.X.X.X.X.X.X.X == &x", true, nil},
TestCase{F, "self_embedded_3", "x.X.X.X == x.X.X.X.X.X", true, nil},

TestCase{F, "recursive_type_issue_44", `
TestCase{F, "recursive_type_gomacro_issue_44", `
type FS struct { slice []FS }
fs := make([]FS, 8)
fs[0].slice = fs[1:8]
`, nil, none},

TestCase{F, "recursive_type_gophernotes_issue_208", `
type Item struct {
Name string
Children []Item
}
graph := Item{
Name: "my-name",
Children: []Item{
{Name: "other-name"},
},
}
`, nil, none},

TestCase{A, "address_0", "var vf = 1.25; *&vf == vf", true, nil},
TestCase{A, "address_1", "var pvf = &vf; *pvf", 1.25, nil},
TestCase{A, "address_2", "&*pvf == *&pvf", true, nil},
Expand Down
8 changes: 4 additions & 4 deletions fast/compositelit.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,17 @@ func (c *Comp) CompositeLit(node *ast.CompositeLit, t xr.Type) *Expr {
}
switch t.Kind() {
case xr.Array:
return c.compositeLitArray(t, ellipsis, node)
return c.compositeLitArray(t.Resolve(), ellipsis, node)
case xr.Map:
return c.compositeLitMap(t, node)
case xr.Slice:
return c.compositeLitSlice(t, node)
return c.compositeLitSlice(t.Resolve(), node)
case xr.Struct:
return c.compositeLitStruct(t, node)
return c.compositeLitStruct(t.Resolve(), node)
case xr.Ptr:
switch t.Elem().Kind() {
case xr.Array, r.Map, r.Slice, r.Struct:
return c.addressOf(node, t)
return c.addressOf(node, t.Resolve())
}
}
c.Errorf("invalid type for composite literal: <%v> %v", t, node.Type)
Expand Down
14 changes: 13 additions & 1 deletion xreflect/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,19 @@ func (t Type) PkgPath() string {
// i.e. the type name will be missing due to limitation 1 above,
// and the field 'Rest' will have type interface{} instead of *List due to limitation 5.
func (t Type) ReflectType() r.Type {
return t(z{}).ReflectType()
return t(z{}).rtype
}

// if t.ReflectType() is Forward i.e. unknown/approximated due to recursive types,
// try to resolve it
func (t Type) Resolve() Type {
xt := t(z{})
if xt.rtype == rtypeOfForward {
if rtype := resolveFwdR(xt); rtype != xt.rtype {
t = xt.universe.MakeType(xt.gtype, rtype, OptRecursive)
}
}
return t
}

func (t Type) approxReflectType() r.Type {
Expand Down

0 comments on commit 647446f

Please sign in to comment.