Skip to content

Commit

Permalink
Merge pull request #1242 from xushiwei/1240
Browse files Browse the repository at this point in the history
compileCallExpr: support auto check SliceLit type (fix #1240)
  • Loading branch information
xushiwei committed Jun 5, 2022
2 parents 422e2be + ab091f5 commit 38292c9
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
24 changes: 24 additions & 0 deletions cl/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,30 @@ var x string = c.Str()
`)
}

func Test_Issue1240(t *testing.T) {
gopClTest(t, `
type fvec []float64
type foo float64
a := []float64([1, 2])
b := fvec([1, 2])
c := foo([1, 2])
println a, b, c
`, `package main
import fmt "fmt"
type fvec []float64
type foo float64
func main() {
a := []float64{1, 2}
b := fvec{1, 2}
c := foo([]int{1, 2})
fmt.Println(a, b, c)
}
`)
}

func TestUnderscoreRedeclared_Issue1197(t *testing.T) {
gopClTest(t, `
func() (_ [2]int) { type _ int; return }()
Expand Down
31 changes: 30 additions & 1 deletion cl/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ type fnType struct {
params *types.Tuple
n1 int
variadic bool
typetype bool
inited bool
}

Expand All @@ -428,12 +429,20 @@ func (p *fnType) init(t *types.Signature) {
}
}

func (p *fnType) initTypeType(t *gox.TypeType) {
param := types.NewParam(0, nil, "", t.Type())
p.params, p.typetype = types.NewTuple(param), true
p.n1 = 1
}

func (p *fnType) initWith(fnt types.Type, idx, nin int) {
if p.inited {
return
}
p.inited = true
if t := gox.CheckSignature(fnt, idx, nin); t != nil {
if t, ok := fnt.(*gox.TypeType); ok {
p.initTypeType(t)
} else if t := gox.CheckSignature(fnt, idx, nin); t != nil {
p.init(t)
}
}
Expand Down Expand Up @@ -470,6 +479,26 @@ func compileCallExpr(ctx *blockCtx, v *ast.CallExpr, inFlags int) {
case *ast.CompositeLit:
fn.initWith(fnt, i, -1)
compileCompositeLit(ctx, expr, fn.arg(i, ellipsis), true)
case *ast.SliceLit:
fn.initWith(fnt, i, -2)
t := fn.arg(i, ellipsis)
switch t.(type) {
case *types.Slice:
case *types.Named:
if _, ok := getUnderlying(ctx, t).(*types.Slice); !ok {
t = nil
}
default:
t = nil
}
typetype := fn.typetype && t != nil && len(v.Args) == 1
if typetype {
ctx.cb.InternalStack().Pop()
}
compileSliceLit(ctx, expr, t)
if typetype {
return
}
default:
compileExpr(ctx, arg)
}
Expand Down

0 comments on commit 38292c9

Please sign in to comment.