-
Notifications
You must be signed in to change notification settings - Fork 397
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
Closed
Changes from 12 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
8506968
explicit conversion while preprocess for declared types
ltzmaxwell 1bad26d
more test
ltzmaxwell fe71a3f
simplify, fix copy spec
ltzmaxwell 8696469
rename arg
ltzmaxwell bd9710e
fix copy check; more test
ltzmaxwell ced0f31
clean
ltzmaxwell b4c483c
chore: fixup
ltzmaxwell 0069063
chore: tune panic format
ltzmaxwell 51933bd
restore
ltzmaxwell 8415035
chore: fixup
ltzmaxwell c04ab08
chore: fix lint
ltzmaxwell 42aec0f
more test
ltzmaxwell 9382c09
simplify checkType, return bool value
ltzmaxwell 9a37443
fixup
ltzmaxwell e54e5fe
more test
ltzmaxwell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1008,10 +1008,16 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node { | |
} | ||
} else if fv.PkgPath == uversePkgPath && fv.Name == "copy" { | ||
if len(n.Args) == 2 { | ||
args0T := evalStaticTypeOf(store, last, n.Args[0]) | ||
args1T := evalStaticTypeOf(store, last, n.Args[1]) | ||
if args0T.Elem().TypeID() != args1T.Elem().TypeID() { | ||
panic(fmt.Sprintf( | ||
"arguments to copy have different element types, want %s, got %s", args0T.Elem().TypeID(), args1T.Elem().TypeID())) | ||
} | ||
// If the second argument is a string, | ||
// convert to byteslice. | ||
args1 := n.Args[1] | ||
if evalStaticTypeOf(store, last, args1).Kind() == StringKind { | ||
if args1T.Kind() == StringKind { | ||
bsx := constType(nil, gByteSliceType) | ||
args1 = Call(bsx, args1) | ||
args1 = Preprocess(nil, last, args1).(Expr) | ||
|
@@ -1119,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) | ||
checkType(tv.T, spts[len(spts)-1].Type.Elem(), true, nil) | ||
} else { | ||
checkType(tv.T, spts[i].Type, true) | ||
checkType(tv.T, spts[i].Type, true, nil) | ||
} | ||
} else { | ||
checkType(tv.T, spts[i].Type, true) | ||
checkType(tv.T, spts[i].Type, true, nil) | ||
} | ||
} | ||
} else { | ||
|
@@ -2309,9 +2315,12 @@ func checkOrConvertType(store Store, last BlockNode, x *Expr, t Type, autoNative | |
checkOrConvertType(store, last, &bx.Left, t, autoNative) | ||
} else if *x != nil { // XXX if x != nil && t != nil { | ||
xt := evalStaticTypeOf(store, last, *x) | ||
var conversionNeeded bool | ||
if t != nil { | ||
checkType(xt, t, autoNative) | ||
// TODO: shall convert here? | ||
checkType(xt, t, autoNative, &conversionNeeded) | ||
} | ||
|
||
if isUntyped(xt) { | ||
if t == nil { | ||
t = defaultTypeOf(xt) | ||
|
@@ -2337,6 +2346,12 @@ func checkOrConvertType(store Store, last BlockNode, x *Expr, t Type, autoNative | |
cx = Preprocess(store, last, cx).(Expr) | ||
*x = cx | ||
} | ||
// cover all declared type case | ||
if conversionNeeded { | ||
cx := Expr(Call(constType(nil, t), *x)) | ||
cx = Preprocess(store, last, cx).(Expr) | ||
*x = cx | ||
} | ||
} | ||
} | ||
|
||
|
@@ -2364,7 +2379,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) { | ||
func checkType(xt Type, dt Type, autoNative bool, conversionNeeded *bool) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. right, right. this would make sense (or it could just return the value as a bool, i think is simpler) to get the information out of the function. we will need this later for the full unroll solution (e.g. the second pr after ray's), but I suggest a return bool value instead. |
||
// Special case if dt is interface kind: | ||
if dt.Kind() == InterfaceKind { | ||
if idt, ok := baseOf(dt).(*InterfaceType); ok { | ||
|
@@ -2480,9 +2495,12 @@ func checkType(xt Type, dt Type, autoNative bool) { | |
"cannot use %s as %s without explicit conversion", | ||
xt.String(), | ||
ddt.String())) | ||
} else { | ||
} else { // implies unnamed composite | ||
// carry on with baseOf(ddt) | ||
dt = ddt.Base | ||
if conversionNeeded != nil { | ||
*conversionNeeded = true | ||
} | ||
} | ||
} | ||
// General cases. | ||
|
@@ -2527,23 +2545,23 @@ func checkType(xt Type, dt Type, autoNative bool) { | |
} | ||
case *PointerType: | ||
if pt, ok := xt.(*PointerType); ok { | ||
checkType(pt.Elt, cdt.Elt, false) | ||
checkType(pt.Elt, cdt.Elt, false, conversionNeeded) | ||
return // ok | ||
} | ||
case *ArrayType: | ||
if at, ok := xt.(*ArrayType); ok { | ||
checkType(at.Elt, cdt.Elt, false) | ||
checkType(at.Elt, cdt.Elt, false, conversionNeeded) | ||
return // ok | ||
} | ||
case *SliceType: | ||
if st, ok := xt.(*SliceType); ok { | ||
checkType(st.Elt, cdt.Elt, false) | ||
checkType(st.Elt, cdt.Elt, false, conversionNeeded) | ||
return // ok | ||
} | ||
case *MapType: | ||
if mt, ok := xt.(*MapType); ok { | ||
checkType(mt.Key, cdt.Key, false) | ||
checkType(mt.Value, cdt.Value, false) | ||
checkType(mt.Key, cdt.Key, false, conversionNeeded) | ||
checkType(mt.Value, cdt.Value, false, conversionNeeded) | ||
return // ok | ||
} | ||
case *FuncType: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package main | ||
|
||
type word uint | ||
type nat []word | ||
|
||
func (n nat) add() bool { | ||
return true | ||
} | ||
|
||
func main() { | ||
var abs nat | ||
abs = []word{0} // unname-Composit to named DeclaredTypes | ||
println(abs.add()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package main | ||
|
||
type word uint | ||
type nat []word | ||
|
||
// structLit | ||
type Int struct { | ||
neg bool | ||
abs nat | ||
} | ||
|
||
func (n nat) add() bool { | ||
return true | ||
} | ||
|
||
func main() { | ||
z := &Int{ | ||
neg: true, | ||
abs: []word{0}, | ||
} | ||
println(z.abs.add()) | ||
} | ||
|
||
// Output: | ||
// true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package main | ||
|
||
type word uint | ||
type nat []word | ||
|
||
type Int struct { | ||
neg bool | ||
abs nat | ||
} | ||
|
||
func (n nat) add() bool { | ||
return true | ||
} | ||
|
||
func main() { | ||
z := &Int{ | ||
neg: true, | ||
} | ||
|
||
z.abs = []word{0} | ||
println(z.abs.add()) | ||
} | ||
|
||
// Output: | ||
// true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package main | ||
|
||
type word uint | ||
type nat []word | ||
|
||
func (n nat) add() bool { | ||
return true | ||
} | ||
|
||
// map | ||
func main() { | ||
items := map[string]nat{} | ||
|
||
n := []word{0} | ||
|
||
items["test"] = n | ||
|
||
r := items["test"] | ||
|
||
println(r.add()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package main | ||
|
||
type word uint | ||
type nat []word | ||
|
||
func (n nat) add() bool { | ||
return true | ||
} | ||
|
||
// mapLit | ||
func main() { | ||
n := []word{0} | ||
|
||
items := map[string]nat{ | ||
"test": n, | ||
} | ||
|
||
r := items["test"] | ||
|
||
println(r.add()) | ||
} | ||
|
||
// Output: | ||
// true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package main | ||
|
||
type word uint | ||
type nat []word | ||
|
||
func (n nat) add() bool { | ||
return true | ||
} | ||
|
||
// sliceLit | ||
func main() { | ||
items := []nat{[]word{0}, []word{1}} | ||
|
||
r := items[0] | ||
|
||
println(r.add()) | ||
} | ||
|
||
// Output: | ||
// true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package main | ||
|
||
type word uint | ||
type nat []word | ||
|
||
func (n nat) add() bool { | ||
return true | ||
} | ||
|
||
// sliceLit2 | ||
func main() { | ||
items := []nat{1:[]word{0}, 2:[]word{1}} | ||
|
||
r := items[1] | ||
|
||
println(r.add()) | ||
} | ||
|
||
// Output: | ||
// true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package main | ||
|
||
type word uint | ||
type nat []word | ||
|
||
func (n nat) add() bool { | ||
return true | ||
} | ||
|
||
// slice append | ||
func main() { | ||
var items []nat | ||
|
||
n := []word{0} | ||
|
||
items = append(items, n) | ||
|
||
r := items[0] | ||
|
||
println(r.add()) | ||
} | ||
|
||
// Output: | ||
// true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package main | ||
|
||
type word uint | ||
type nat []word | ||
|
||
func (n nat) add() bool { | ||
return true | ||
} | ||
|
||
// ArrayLit | ||
func main() { | ||
items := [3]nat{[]word{0}, []word{1}} | ||
|
||
r := items[0] | ||
|
||
println(r.add()) | ||
} | ||
|
||
// Output: | ||
// true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package main | ||
|
||
|
||
type word uint | ||
type nat []word | ||
|
||
func (n nat) add(x, y nat) bool { | ||
return true | ||
} | ||
|
||
// parameter | ||
func main() { | ||
var abs nat | ||
abs = []word{0} | ||
x := []word{1} | ||
y := []word{2} | ||
|
||
println(abs.add(x, y)) | ||
} | ||
|
||
// Output: | ||
// true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package main | ||
|
||
type BasicFunc func(int, int) int | ||
type MyFunc BasicFunc | ||
|
||
func (f MyFunc) Apply(a, b int) int { | ||
return f(a, b) | ||
} | ||
|
||
func main() { | ||
basicAdd := func(a, b int) int { | ||
return a + b | ||
} | ||
var myAdd MyFunc | ||
myAdd = basicAdd | ||
|
||
result := myAdd.Apply(2, 3) | ||
println(result) | ||
} | ||
|
||
// Output: | ||
// 5 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what prompted this? seems like this could/should be a separate PR for complete discussion. seems correct though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a fix of
copy
function. According to the Go specification, "The core types of both arguments must be slices with identical element types". Our previous implementation lacked this necessary check.I'll put it into another PR. Thank you~