Skip to content

Commit

Permalink
backport commit 0eb51bf from master:
Browse files Browse the repository at this point in the history
fix #36: do not crash on a := [...]int{1,2,3}; len(a)
  • Loading branch information
cosmos72 committed Aug 15, 2018
1 parent 2f76d63 commit 162c509
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
3 changes: 2 additions & 1 deletion all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,8 @@ var testcases = []TestCase{
TestCase{A, "builtin_append_1", "append(vs,0,1,2)", []byte{0, 1, 2}, nil},
TestCase{A, "builtin_append_2", "append(vn,3,4)", []byte{3, 4}, nil},
TestCase{A, "builtin_cap", "cap(va)", 2, nil},
TestCase{A, "builtin_len", "len(v5)", len("8y57riuh@#$"), nil},
TestCase{A, "builtin_len_1", "len(v5)", len("8y57riuh@#$"), nil},
TestCase{A, "builtin_len_2", "{ a := [...]int{1,2,3}; len(a) }", nil, none},
TestCase{A, "builtin_new", "new(int)", new(int), nil},
TestCase{A, "builtin_make_1", "make(map[int]int)", make(map[int]int), nil},
TestCase{A, "builtin_make_2", "make(map[int]int, 10)", make(map[int]int), nil}, // capacity is ignored
Expand Down
3 changes: 3 additions & 0 deletions fast/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,9 @@ func compileLen(c *Comp, sym Symbol, node *ast.CallExpr) *Call {
fun.Value = func(_ r.Value) int {
return n
}
// since we currently optimize len() by evaluating it at compile time,
// actual arg may not exist yet. optimize it away.
arg = exprLit(Lit{Type: tin, Value: xr.Zero(tin).Interface()}, nil)
}
return newCall1(fun, arg, isarray || arg.Const(), tout)
}
Expand Down

0 comments on commit 162c509

Please sign in to comment.