-
Notifications
You must be signed in to change notification settings - Fork 570
[WIP] compiler: fix handling of struct, array and interface values #669
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
Open
myitcv
wants to merge
3
commits into
gopherjs:master
Choose a base branch
from
myitcv:struct_val_bug
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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,88 @@ | ||
package tests | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
type Struct struct { | ||
Name string | ||
} | ||
|
||
func (s Struct) SetName(n string) { | ||
s.Name = n | ||
} | ||
|
||
type SetName interface { | ||
SetName(n string) | ||
} | ||
|
||
func TestAssignStructValInterface(t *testing.T) { | ||
s := Struct{ | ||
Name: "Rob", | ||
} | ||
|
||
var i1 interface{} = s | ||
var i2 interface{} = i1 | ||
|
||
s.Name = "Pike" | ||
|
||
ss := fmt.Sprintf("%#v", s) | ||
i1s := fmt.Sprintf("%#v", i1) | ||
i2s := fmt.Sprintf("%#v", i2) | ||
|
||
if exp := "tests.Struct{Name:\"Pike\"}"; ss != exp { | ||
t.Fatalf("ss should have been %q; got %q", exp, ss) | ||
} | ||
|
||
iexp := "tests.Struct{Name:\"Rob\"}" | ||
|
||
if i1s != iexp { | ||
t.Fatalf("is should have been %q; got %q", iexp, i1s) | ||
} | ||
|
||
if i2s != iexp { | ||
t.Fatalf("is should have been %q; got %q", iexp, i2s) | ||
} | ||
} | ||
|
||
func TestStructValInterfaceMethodCall(t *testing.T) { | ||
var i SetName = Struct{ | ||
Name: "Rob", | ||
} | ||
|
||
i.SetName("Pike") | ||
|
||
is := fmt.Sprintf("%#v", i) | ||
|
||
if exp := "tests.Struct{Name:\"Rob\"}"; is != exp { | ||
t.Fatalf("is should have been %q; got %q", exp, is) | ||
} | ||
} | ||
|
||
func TestAssignArrayInterface(t *testing.T) { | ||
a := [2]int{1, 2} | ||
|
||
var i1 interface{} = a | ||
var i2 interface{} = i1 | ||
|
||
a[0] = 0 | ||
|
||
as := fmt.Sprintf("%#v", a) | ||
i1s := fmt.Sprintf("%#v", i1) | ||
i2s := fmt.Sprintf("%#v", i2) | ||
|
||
if exp := "[2]int{0, 2}"; as != exp { | ||
t.Fatalf("ss should have been %q; got %q", exp, as) | ||
} | ||
|
||
iexp := "[2]int{1, 2}" | ||
|
||
if i1s != iexp { | ||
t.Fatalf("is should have been %q; got %q", iexp, i1s) | ||
} | ||
|
||
if i2s != iexp { | ||
t.Fatalf("is should have been %q; got %q", iexp, i2s) | ||
} | ||
} |
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.
Address case 1.
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.
Can this maybe be solved instead by making
translateImplicitConversionWithCloning
look atc.p.TypeOf(expr)
instead ofdesiredType
? This would also catch the case of "array/struct to interface".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.
As I understand it, we effectively need some combination of the logic from
translateImplicitConversionWithCloning
andtranslateImplicitConversion
when it comes to array/struct values.As this PR stands:
translateImplicitConversionWithCloning
handles the case where the target is an array/struct type which by definition means the source must be exactly the same type, hence a$clone
is requiredtranslateImplicitConversion
handles the conversion from an array/struct type to an interface type, hence thenew X
is required to wrap the$clone
-ed value (part of Struct values not being cloned when assigned from/called with when variable type is interface #661 concerned the fact that we weren't cloning before wrapping)I'm relatively relaxed on where the code lives because once your first comment is addressed I think this PR looks simpler as far as
translateImplicitConversion
is concerned in any case.Let me know what you think