interp: apply type conversion on untyped variable at run

Fixes #938.
This commit is contained in:
Marc Vertes
2020-11-04 18:16:04 +01:00
committed by GitHub
parent 0ed4b362dc
commit b1ccfbf47f
2 changed files with 7 additions and 9 deletions

View File

@@ -77,6 +77,7 @@ func TestEvalShift(t *testing.T) {
{src: "a, b, m := uint32(1), uint32(2), uint32(0); m = a + (1 << b)", res: "5"},
{src: "c := uint(1); d := uint(+(-(1 << c)))", res: "18446744073709551614"},
{src: "e, f := uint32(0), uint32(0); f = 1 << -(e * 2)", res: "1"},
{src: "p := uint(0xdead); byte((1 << (p & 7)) - 1)", res: "31"},
{pre: func() { eval(t, i, "const k uint = 1 << 17") }, src: "int(k)", res: "131072"},
})
}

View File

@@ -634,16 +634,13 @@ func (check typecheck) conversion(n *node, typ *itype) error {
if !ok {
return n.cfgErrorf("cannot convert expression of type %s to type %s", n.typ.id(), typ.id())
}
if n.typ.untyped {
if isInterface(typ) || c != nil && !isConstType(typ) {
typ = n.typ.defaultType()
}
if err := check.convertUntyped(n, typ); err != nil {
return err
}
if !n.typ.untyped || c == nil {
return nil
}
return nil
if isInterface(typ) || !isConstType(typ) {
typ = n.typ.defaultType()
}
return check.convertUntyped(n, typ)
}
type param struct {