Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove untyped bools at runtime #3728

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions gnovm/pkg/gnolang/op_assign.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package gnolang

import "fmt"

func (m *Machine) doOpDefine() {
s := m.PopStmt().(*AssignStmt)
// Define each value evaluated for Lhs.
Expand All @@ -20,6 +22,10 @@ func (m *Machine) doOpDefine() {
}
}
}
if !m.PreprocessorMode && isUntyped(rvs[i].T) {
fmt.Println(s)
panic("untyped conversion should not happen at runtime")
}
ptr.Assign2(m.Alloc, m.Store, m.Realm, rvs[i], true)
}
}
Expand All @@ -41,6 +47,9 @@ func (m *Machine) doOpAssign() {
}
}
}
if !m.PreprocessorMode && isUntyped(rvs[i].T) {
panic("untyped conversion should not happen at runtime")
}
lv.Assign2(m.Alloc, m.Store, m.Realm, rvs[i], true)
}
}
Expand Down
44 changes: 20 additions & 24 deletions gnovm/pkg/gnolang/op_decl.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,31 @@ func (m *Machine) doOpValueDecl() {
} else {
tv = rvs[i]
}
if nt != nil {
if nt.Kind() == InterfaceKind {
if isUntyped(tv.T) {
ConvertUntypedTo(&tv, nil)
} else {
// keep type as is.

if isUntyped(tv.T) {
if !s.Const {
if !m.PreprocessorMode {
panic("untyped conversion should not happen at runtime")
}
} else {
if isUntyped(tv.T) {
ConvertUntypedTo(&tv, nt)
} else {
if debug {
if nt.TypeID() != tv.T.TypeID() &&
baseOf(nt).TypeID() != tv.T.TypeID() {
panic(fmt.Sprintf(
"type mismatch: %s vs %s",
nt.TypeID(),
tv.T.TypeID(),
))
}
ConvertUntypedTo(&tv, nil)
}
} else if nt != nil {
// if nt.T is an interface, maintain tv.T as-is.
if nt.Kind() != InterfaceKind {
if debug {
if nt.TypeID() != tv.T.TypeID() &&
baseOf(nt).TypeID() != tv.T.TypeID() {
panic(fmt.Sprintf(
"type mismatch: %s vs %s",
nt.TypeID(),
tv.T.TypeID(),
))
}
tv.T = nt
}
tv.T = nt
}
} else if s.Const {
// leave untyped as is.
} else if isUntyped(tv.T) {
ConvertUntypedTo(&tv, nil)
}

nx := &s.NameExprs[i]
ptr := lb.GetPointerToMaybeHeapDefine(m.Store, nx)
ptr.Assign2(m.Alloc, m.Store, m.Realm, tv, false)
Expand Down
32 changes: 19 additions & 13 deletions gnovm/pkg/gnolang/op_expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,19 @@ func (m *Machine) doOpIndex2() {
T: vt,
V: defaultValue(m.Alloc, vt),
}
*iv = untypedBool(false) // reuse as result
*iv = boolResult(false) // reuse as result
} else {
mv := xv.V.(*MapValue)
vv, exists := mv.GetValueForKey(m.Store, iv)
if exists {
*xv = vv // reuse as result
*iv = untypedBool(true) // reuse as result
*xv = vv // reuse as result
*iv = boolResult(true) // reuse as result
} else {
*xv = TypedValue{ // reuse as result
T: vt,
V: defaultValue(m.Alloc, vt),
}
*iv = untypedBool(false) // reuse as result
*iv = boolResult(false) // reuse as result
}
}
case *NativeType:
Expand All @@ -75,6 +75,12 @@ func (m *Machine) doOpIndex2() {
}
}

func boolResult(b bool) TypedValue {
tv := TypedValue{T: BoolType}
tv.SetBool(b)
return tv
}

func (m *Machine) doOpSelector() {
sx := m.PopExpr().(*SelectorExpr)
xv := m.PeekValue(1)
Expand Down Expand Up @@ -334,7 +340,7 @@ func (m *Machine) doOpTypeAssert2() {
if t.Kind() == InterfaceKind { // is interface assert
if xt == nil {
*xv = TypedValue{}
*tv = untypedBool(false)
*tv = boolResult(false)
return
}

Expand All @@ -343,7 +349,7 @@ func (m *Machine) doOpTypeAssert2() {
// type should always fail.
if _, ok := baseOf(xt).(*InterfaceType); ok {
*xv = TypedValue{}
*tv = untypedBool(false)
*tv = boolResult(false)
return
}

Expand All @@ -353,12 +359,12 @@ func (m *Machine) doOpTypeAssert2() {
impl = it.IsImplementedBy(xt)
if impl {
// *xv = *xv
*tv = untypedBool(true)
*tv = boolResult(true)
} else {
// NOTE: consider ability to push an
// interface-restricted form
*xv = TypedValue{}
*tv = untypedBool(false)
*tv = boolResult(false)
}
} else if nt, ok := baseOf(t).(*NativeType); ok {
// If the value being asserted on is nil, it can't implement an interface.
Expand All @@ -375,10 +381,10 @@ func (m *Machine) doOpTypeAssert2() {

if impl {
// *xv = *xv
*tv = untypedBool(true)
*tv = boolResult(true)
} else {
*xv = TypedValue{}
*tv = untypedBool(false)
*tv = boolResult(false)
}
} else {
panic("should not happen")
Expand All @@ -389,7 +395,7 @@ func (m *Machine) doOpTypeAssert2() {
T: t,
V: defaultValue(m.Alloc, t),
}
*tv = untypedBool(false)
*tv = boolResult(false)
return
}

Expand All @@ -400,13 +406,13 @@ func (m *Machine) doOpTypeAssert2() {

if same {
// *xv = *xv
*tv = untypedBool(true)
*tv = boolResult(true)
} else {
*xv = TypedValue{
T: t,
V: defaultValue(m.Alloc, t),
}
*tv = untypedBool(false)
*tv = boolResult(false)
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -3679,6 +3679,9 @@ func checkOrConvertType(store Store, last BlockNode, n Node, x *Expr, t Type, au
// always be a BoolType. (in this scenario, we may have some
// UntypedBoolTypes)
t = BoolType
if strings.Contains(bx.Left.String(), "neg<!VPBlock(1,5)>") {
fmt.Println("HEY")
}
default:
// do nothing
}
Expand Down
14 changes: 0 additions & 14 deletions gnovm/pkg/gnolang/values_conversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1312,28 +1312,14 @@ func ConvertUntypedTo(tv *TypedValue, t Type) {
}
switch tv.T {
case UntypedBoolType:
if debug {
if t.Kind() != BoolKind {
panic("untyped bool can only be converted to bool kind")
}
}
tv.T = t
case UntypedRuneType:
ConvertUntypedRuneTo(tv, t)
case UntypedBigintType:
if preprocessing.Load() == 0 {
panic("untyped Bigint conversion should not happen during interpretation")
}
ConvertUntypedBigintTo(tv, tv.V.(BigintValue), t)
case UntypedBigdecType:
if preprocessing.Load() == 0 {
panic("untyped Bigdec conversion should not happen during interpretation")
}
ConvertUntypedBigdecTo(tv, tv.V.(BigdecValue), t)
case UntypedStringType:
if preprocessing.Load() == 0 {
panic("untyped String conversion should not happen during interpretation")
}
if t.Kind() == StringKind {
tv.T = t
return
Expand Down
Loading