Skip to content

Commit

Permalink
fix: composite map assign (#683)
Browse files Browse the repository at this point in the history
* fix: composite map assign

* reword comment

Co-authored-by: Marc Vertes <mvertes@free.fr>
  • Loading branch information
nrwiersma and mvertes committed Jun 10, 2020
1 parent 36836cd commit 8365f68
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
26 changes: 26 additions & 0 deletions _test/map29.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"fmt"
"time"
)

type Item struct {
Object interface{}
Expiry time.Duration
}

func main() {
items := map[string]Item{}

items["test"] = Item{
Object: "test",
Expiry: time.Second,
}

item := items["test"]
fmt.Println(item)
}

// Output:
// {test 1s}
5 changes: 3 additions & 2 deletions interp/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,9 +523,10 @@ func (interp *Interpreter) cfg(root *node, pkgID string) ([]*node, error) {
n.gen = nop
src.findex = dest.findex // Set recv address to LHS
dest.typ = src.typ
case n.action == aAssign && src.action == aCompositeLit:
case n.action == aAssign && src.action == aCompositeLit && !isMapEntry(dest):
if dest.typ.cat == valueT && dest.typ.rtype.Kind() == reflect.Interface {
// No optimisation attempt for assigned binary interface
// Skip optimisation for assigned binary interface or map entry
// which require and additional operation to set the value
break
}
// Skip the assign operation entirely, the source frame index is set
Expand Down

0 comments on commit 8365f68

Please sign in to comment.