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

reflect: add Value.Clear; support anytype->interface{}, Slice->(*)Array in Value.Convert, fix Copy #4543

Merged
merged 2 commits into from
Nov 13, 2024

Conversation

ben-krieger
Copy link

@ben-krieger ben-krieger commented Oct 24, 2024

Fixes #4554

This PR adds a few things to the reflect package.

  1. Add Value.Clear
  2. Convert anything to interface{}, i.e. reflect.ValueOf(42).Convert(reflect.TypeFor[interface{}]())
  3. Convert slices to arrays, i.e. reflect.ValueOf([]int{1, 2, 3}).Convert(reflect.TypeFor[[3]int]())
  4. Convert slices to array pointers... because after doing 3, I felt like I had to
  5. Fixes reflect.Copy when the source is an array

The motivation was to allow compiling the CBOR library at https://github.com/fido-device-onboard/go-fdo/tree/tinygo-server-support/cbor. However, this PR can be split into smaller ones if desired.

Copy link
Member

@dgryski dgryski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticed an issue, but I'll do a more complete review Thursday.

src/reflect/value.go Outdated Show resolved Hide resolved
@ben-krieger ben-krieger changed the title reflect: add Value.Clear; support anytype->interface{}, Slice->(*)Array in Value.Convert reflect: add Value.Clear; support anytype->interface{}, Slice->(*)Array in Value.Convert Oct 24, 2024
src/reflect/type.go Outdated Show resolved Hide resolved
src/reflect/type.go Outdated Show resolved Hide resolved
src/reflect/value.go Show resolved Hide resolved
src/reflect/value.go Show resolved Hide resolved
src/reflect/value_test.go Show resolved Hide resolved
src/reflect/value_test.go Show resolved Hide resolved
@ben-krieger
Copy link
Author

Thanks for the thorough review @dgryski! I'll spend some time this weekend fixing it up and adding more tests.

@dgryski
Copy link
Member

dgryski commented Oct 31, 2024

testdata/reflect.go is still failing. That's Bad because the desired output is generated by upstream Go, so that indicates incompatibilities.

@ben-krieger
Copy link
Author

testdata/reflect.go is still failing. That's Bad because the desired output is generated by upstream Go, so that indicates incompatibilities.

Got it. So it seems that arrays in some cases have become addrable and settable, when they shouldn't. Let me see if I can figure out why...

@dgryski
Copy link
Member

dgryski commented Oct 31, 2024

It's almost certainly the flag changes for indirect support. You probably need some RO bits too.

@ben-krieger ben-krieger force-pushed the reflect-clear-and-more branch 2 times, most recently from e391914 to 6340647 Compare November 1, 2024 01:07
@ben-krieger
Copy link
Author

It's almost certainly the flag changes for indirect support. You probably need some RO bits too.

Tried RO, but couldn't get it working, so I reverted the change that broke it. (I was also uncomfortable with that code, because I didn't understand the underlying mechanism that distinguished between arrays > 64 bits in size.) However, the test I added for reflect.Copy is still failing.

Any thoughts on the proper way to fix #4554?

@ben-krieger ben-krieger force-pushed the reflect-clear-and-more branch 2 times, most recently from 1e3d12c to 97c6c38 Compare November 1, 2024 23:02
@ben-krieger ben-krieger changed the title reflect: add Value.Clear; support anytype->interface{}, Slice->(*)Array in Value.Convert reflect: add Value.Clear; support anytype->interface{}, Slice->(*)Array in Value.Convert, fix Copy Nov 1, 2024
@ben-krieger
Copy link
Author

It's almost certainly the flag changes for indirect support. You probably need some RO bits too.

@dgryski I think this is ready for (hopefully the last) review. All tests are passing now and the fix for #4554 matches a lot of other code within the reflect package that check either indirect OR size > uintptr.

I don't see any more low-hanging fruit for test coverage, but let me know what you think. Thanks!

src/reflect/value.go Outdated Show resolved Hide resolved
@dgryski
Copy link
Member

dgryski commented Nov 3, 2024

One quick comment. I'll do a full review tomorrow (Monday) and then hopefully we can get this merged. Thanks for all your work on this!

@ben-krieger ben-krieger force-pushed the reflect-clear-and-more branch 4 times, most recently from 1401cad to a80c83b Compare November 4, 2024 02:00
@@ -995,6 +1001,10 @@ func (t *rawType) AssignableTo(u Type) bool {
return true
}

if t.underlying() == u.(*rawType).underlying() && (!t.isNamed() || !u.(*rawType).isNamed()) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See https://go.dev/ref/spec#Assignability

If the underlying types are the same, only one can be named.

Copy link
Member

@dgryski dgryski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@dgryski
Copy link
Member

dgryski commented Nov 6, 2024

@aykevl Any comments?

@dgryski dgryski merged commit 4f96a50 into tinygo-org:dev Nov 13, 2024
17 checks passed
@aykevl
Copy link
Member

aykevl commented Nov 19, 2024

Took a look, looks good to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants