-
Notifications
You must be signed in to change notification settings - Fork 911
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: use int in StringHeader and SliceHeader on non-AVR platforms #4156
Conversation
This has been discussed before, and the conclusion was that it wasn't the right solution. I still hold that opinion. Also, I'm not sure x/tools will compile even with this patch: there are still a lot of incompatibilities in the parsing libraries. I hope that in about half a year or so x/tools will have a minimum version of Go 1.20 so that it can use |
Confirmed that x/tools does compile with this patch (and #4157). |
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.
On second thought, maybe we should just treat reflect.*Header
as a deprecated non-portable API. So we'll just aim for the most portable code, not the most correct/clean code. Code that wants to be compatible with all Go compilers should use unsafe.String
and the like instead.
Regarding the actual PR, I appreciate that you're adding tests but they don't really test anything useful:
- They really just repeat the type definition.
- The
reflect
package doesn't even pass tests on AVR (it's too big) so adding a test for AVR specifically is useless.
A PR like this is difficult to test, but if you want to add tests you could instead check that the sizes match. You can in fact even do this without a test, with the following code:
// Check that StringHeader is the same size as a string value.
var _ [unsafe.Sizeof("")]byte = [unsafe.Sizeof(StringHeader{})]byte{}
This will be a no-op when the string value is the same size as reflect.StringHeader
, and is a compile error otherwise. You can do the same thing for slices (using unsafe.Sizeof([]byte(nil)
for example).
Thanks for the feedback! I'll update the PR and tests. |
b967c47
to
2ef1270
Compare
…uintptr on AVR platforms
…size as [slice,string]
2ef1270
to
f6cb6af
Compare
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.
Looks good to me!
Thank you for the contribution and for the fixes afterwards! |
In #1284, the recommended mechanism to use
SliceHeader
orStringHeader
on TinyGo is with build tags. Unfortunately this isn't an option if your code imports golang.org/x/tools, which usesSliceHeader
andStringHeader
extensively, assuming thatLen
andCap
fields are typeint
.This PR adds a single un-exported type to the TinyGo
reflect
package,intw
, which is a word-sized integer type. It is aliased toint
on most platforms, anduintptr
when compiled with theavr
build tag:Fixes #1284.