Skip to content

Commit

Permalink
fix issue #6 - cannot update fields in elements of map[T1]*T2.
Browse files Browse the repository at this point in the history
The logic to handle this case was already present, but a stray check caused it to fail
  • Loading branch information
cosmos72 committed Dec 6, 2017
1 parent 14a71b2 commit 48c8420
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions fast/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ func (c *Comp) LookupMethod(t xr.Type, name string) (mtd xr.Method, numfound int
return t.MethodByName(name, c.FileComp().Path)
}

// field1 isa variand of reflect.Value.Field, also accepts pointer values
// and dereferences pointer ONLY if index is negative (actually used index will be ^x)
// field0 is a variant of reflect.Value.Field, also accepts pointer values
// and dereferences pointer ONLY if index is negative (actually used index will be ^index)
func field0(v r.Value, index int) r.Value {
if index < 0 {
v = v.Elem()
Expand All @@ -159,7 +159,7 @@ func field0(v r.Value, index int) r.Value {
}

// fieldByIndex is a variant of reflect.Value.FieldByIndex, also accepts pointer values
// and dereferences pointers ONLY if index[i] is negative (actually used index will be ^x)
// and dereferences pointers ONLY if index[i] is negative (actually used index will be ^index[i])
func fieldByIndex(v r.Value, index []int) r.Value {
for _, x := range index {
if x < 0 {
Expand Down Expand Up @@ -780,13 +780,15 @@ func (c *Comp) compileMethodAsFunc(t xr.Type, mtd xr.Method) *Expr {
return c.exprValue(tfunc, ret.Interface())
}

// SelectorPlace compiles a.b returning a settable and addressable Place
// SelectorPlace compiles a.b returning a settable and/or addressable Place
func (c *Comp) SelectorPlace(node *ast.SelectorExpr, opt PlaceOption) *Place {
obje := c.Expr1(node.X)
te := obje.Type
name := node.Sel.Name
ispointer := false
switch te.Kind() {
case r.Ptr:
ispointer = true
te = te.Elem()
if te.Kind() != r.Struct {
break
Expand All @@ -799,7 +801,7 @@ func (c *Comp) SelectorPlace(node *ast.SelectorExpr, opt PlaceOption) *Place {
})
fallthrough
case r.Struct:
if te.ReflectType() == rtypeOfImport && obje.Const() {
if !ispointer && te.ReflectType() == rtypeOfImport && obje.Const() {
// access symbol from imported package, for example fmt.Printf
imp := obje.Value.(Import)
return c.selectorPlaceImport(&imp, name, opt)
Expand All @@ -811,9 +813,9 @@ func (c *Comp) SelectorPlace(node *ast.SelectorExpr, opt PlaceOption) *Place {
c.Errorf("type %v has %d fields named %q, all at depth %d", te, fieldn, name, len(field.Index))
return nil
}
// if te.Kind() == r.Ptr, field is automatically settable and addressable
// if ispointer, field is automatically settable and addressable
// because the 'a' in 'a.b' is actually a pointer
if te.Kind() == r.Struct {
if !ispointer {
c.checkAddressableField(node)
}
return c.compileFieldPlace(obje, field)
Expand Down

0 comments on commit 48c8420

Please sign in to comment.