Skip to content

reflect: fully implement Type.AssignableTo#4376

Closed
aykevl wants to merge 2 commits intodevfrom
reflect-AssignableTo
Closed

reflect: fully implement Type.AssignableTo#4376
aykevl wants to merge 2 commits intodevfrom
reflect-AssignableTo

Conversation

@aykevl
Copy link
Copy Markdown
Member

@aykevl aykevl commented Jul 31, 2024

This fully implements AssignableTo, fixing a number of bugs in the previous implementation. In particular, it now supports AssignableTo for interface types.

Depends on #4375

aykevl added 2 commits July 31, 2024 19:55
This is a big reimplementation that simplifies the compiler a lot.
Instead of storing the method set in metadata and lowering the type
asserts as a whole program pass, this change just puts the list of
methods in the type code (and a separate global for the interface type).
This fully implements AssignableTo, fixing a number of bugs in the
previous implementation. In particular, it now supports AssignableTo for
interface types.
Comment thread transform/optimizer.go

// Run TinyGo-specific optimization passes.
OptimizeStringToBytes(mod)
OptimizeReflectImplements(mod)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

So what happens with type asserts that are statically known to be true or false? Optimization pushed for future work?

Copy link
Copy Markdown
Member Author

@aykevl aykevl Aug 2, 2024

Choose a reason for hiding this comment

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

This is specifically for the reflect Implements method, which is used in some important code but not really used that much in total (so I don't think optimizing it will provide much benefit). The main reason this "optimization" existed was to get the encoding/json package to work.

But yeah, good point about other (normal Go style) interface type asserts. I can do a check how common that is and whether optimizing that case is beneficial. That is something that should work well as a package level optimization though.

@reteps
Copy link
Copy Markdown

reteps commented Apr 18, 2025

@dgryski any progress on this? This is causing issues for me. I would like to use tinygo to generate WASM bindings, but the lack of a JSON encoder isn't great. See reteps/dockerfmt#25.

@progrium
Copy link
Copy Markdown

Is the dependency on #4375 a hard one? Could this be implemented without it?

jakebailey added a commit to jakebailey/tinygo that referenced this pull request Apr 14, 2026
Based on the design from tinygo-org#4376 by aykevl.
Fixes tinygo-org#4277, fixes tinygo-org#3580.

Co-authored-by: Ayke van Laethem <aykevanlaethem@gmail.com>
jakebailey added a commit to jakebailey/tinygo that referenced this pull request Apr 14, 2026
Based on the design from tinygo-org#4376 by aykevl.
Fixes tinygo-org#4277, fixes tinygo-org#3580.

Co-authored-by: Ayke van Laethem <aykevanlaethem@gmail.com>
@jakebailey
Copy link
Copy Markdown
Member

FWIW I took a crack at this and #4375 in #5304, since this unblocks my own usage.

@aykevl
Copy link
Copy Markdown
Member Author

aykevl commented Apr 17, 2026

Closing since #5304 is just a better version of this PR.

@aykevl aykevl closed this Apr 17, 2026
deadprogram pushed a commit that referenced this pull request Apr 17, 2026
* reflect: implement method-set based AssignableTo and Implements

Based on the design from #4376 by aykevl.
Fixes #4277, fixes #3580.

Co-authored-by: Ayke van Laethem <aykevanlaethem@gmail.com>

* builder: update expected binary sizes for reflect changes

* Make interface checks similar to invoke, allowing typeImplementsMethodSet and method info to be dropped when reflect is not present

* Add more tests that BigGo reflect tests

* Even more pruning

* Add go/token and net/url to passing tests

* Prune even further, I am less happy with this, though

* Update size test now that we are smaller

* Skip some tests

* elide method lists

* format, oops

* fix tests

* Add a panic, pull out constant to keep in sync

* Add debug info

* Remove code that was leftover from a previous refactor

---------

Co-authored-by: Ayke van Laethem <aykevanlaethem@gmail.com>
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.

5 participants