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: solve problem that variable (of declaredType) is not assigned with correct type #674

Closed
wants to merge 15 commits into from
Prev Previous commit
Next Next commit
simplify checkType, return bool value
ltzmaxwell committed Jun 2, 2023

Verified

This commit was signed with the committer’s verified signature. The key has expired.
commit 9382c09787cef38aaeecdee2898dd2f5a58f5811
35 changes: 16 additions & 19 deletions gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
@@ -1125,12 +1125,12 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node {
for i, tv := range argTVs {
if hasVarg {
if (len(spts) - 1) <= i {
checkType(tv.T, spts[len(spts)-1].Type.Elem(), true, nil)
checkType(tv.T, spts[len(spts)-1].Type.Elem(), true)
} else {
checkType(tv.T, spts[i].Type, true, nil)
checkType(tv.T, spts[i].Type, true)
}
} else {
checkType(tv.T, spts[i].Type, true, nil)
checkType(tv.T, spts[i].Type, true)
}
}
} else {
@@ -2317,8 +2317,7 @@ func checkOrConvertType(store Store, last BlockNode, x *Expr, t Type, autoNative
xt := evalStaticTypeOf(store, last, *x)
var conversionNeeded bool
if t != nil {
// TODO: shall convert here?
checkType(xt, t, autoNative, &conversionNeeded)
conversionNeeded = checkType(xt, t, autoNative)
}

if isUntyped(xt) {
@@ -2379,7 +2378,7 @@ func convertConst(store Store, last BlockNode, cx *ConstExpr, t Type) {
// Assert that xt can be assigned as dt (dest type).
// If autoNative is true, a broad range of xt can match against
// a target native dt type, if and only if dt is a native type.
func checkType(xt Type, dt Type, autoNative bool, conversionNeeded *bool) {
func checkType(xt Type, dt Type, autoNative bool) (conversionNeeded bool) {
// Special case if dt is interface kind:
if dt.Kind() == InterfaceKind {
if idt, ok := baseOf(dt).(*InterfaceType); ok {
@@ -2495,12 +2494,10 @@ func checkType(xt Type, dt Type, autoNative bool, conversionNeeded *bool) {
"cannot use %s as %s without explicit conversion",
xt.String(),
ddt.String()))
} else { // implies unnamed composite
} else { // implies unnamed composite? XXX: Is this complete? any Boudary condetions?
// carry on with baseOf(ddt)
dt = ddt.Base
if conversionNeeded != nil {
*conversionNeeded = true
}
conversionNeeded = true
}
}
// General cases.
@@ -2545,24 +2542,24 @@ func checkType(xt Type, dt Type, autoNative bool, conversionNeeded *bool) {
}
case *PointerType:
if pt, ok := xt.(*PointerType); ok {
checkType(pt.Elt, cdt.Elt, false, conversionNeeded)
return // ok
cdt := checkType(pt.Elt, cdt.Elt, false)
return cdt || conversionNeeded
}
case *ArrayType:
if at, ok := xt.(*ArrayType); ok {
checkType(at.Elt, cdt.Elt, false, conversionNeeded)
return // ok
cdt := checkType(at.Elt, cdt.Elt, false)
return cdt || conversionNeeded
}
case *SliceType:
if st, ok := xt.(*SliceType); ok {
checkType(st.Elt, cdt.Elt, false, conversionNeeded)
return // ok
cdt := checkType(st.Elt, cdt.Elt, false)
return cdt || conversionNeeded
}
case *MapType:
if mt, ok := xt.(*MapType); ok {
checkType(mt.Key, cdt.Key, false, conversionNeeded)
checkType(mt.Value, cdt.Value, false, conversionNeeded)
return // ok
cn1 := checkType(mt.Key, cdt.Key, false)
cn2 := checkType(mt.Value, cdt.Value, false)
return cn1 || cn2 || conversionNeeded
}
case *FuncType:
if xt.TypeID() == cdt.TypeID() {
4 changes: 2 additions & 2 deletions gnovm/pkg/gnolang/types.go
Original file line number Diff line number Diff line change
@@ -2363,7 +2363,7 @@ func specifyType(store Store, lookup map[Name]Type, tmpl Type, spec Type, specTy
generic := ct.Generic[:len(ct.Generic)-len(".Elem()")]
match, ok := lookup[generic]
if ok {
checkType(spec, match.Elem(), false, nil)
checkType(spec, match.Elem(), false)
return // ok
} else {
// Panic here, because we don't know whether T
@@ -2377,7 +2377,7 @@ func specifyType(store Store, lookup map[Name]Type, tmpl Type, spec Type, specTy
} else {
match, ok := lookup[ct.Generic]
if ok {
checkType(spec, match, false, nil)
checkType(spec, match, false)
return // ok
} else {
if isUntyped(spec) {