compiler: implement method-set based AssignableTo and Implements (#5304)

* 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>
This commit is contained in:
Jake Bailey
2026-04-17 12:57:03 -07:00
committed by GitHub
parent b792c10680
commit 5ba8766cbc
16 changed files with 853 additions and 438 deletions
+2 -65
View File
@@ -12,14 +12,8 @@ declare void @runtime.printuint8(i8)
declare void @runtime.printint16(i16)
declare void @runtime.printint32(i32)
declare void @runtime.printptr(i32)
declare void @runtime.printnl()
declare void @runtime.nilPanic(ptr)
define void @printInterfaces() {
call void @printInterface(ptr @"reflect/types.type:basic:int", ptr inttoptr (i32 5 to ptr))
call void @printInterface(ptr @"reflect/types.type:basic:uint8", ptr inttoptr (i8 120 to ptr))
@@ -28,35 +22,16 @@ define void @printInterfaces() {
}
define void @printInterface(ptr %typecode, ptr %value) {
%isUnmatched = call i1 @"Unmatched$typeassert"(ptr %typecode)
br i1 %isUnmatched, label %typeswitch.Unmatched, label %typeswitch.notUnmatched
typeswitch.Unmatched: ; preds = %0
%unmatched = ptrtoint ptr %value to i32
call void @runtime.printptr(i32 %unmatched)
call void @runtime.printnl()
ret void
typeswitch.notUnmatched: ; preds = %0
%isDoubler = call i1 @"Doubler$typeassert"(ptr %typecode)
br i1 %isDoubler, label %typeswitch.Doubler, label %typeswitch.notDoubler
typeswitch.Doubler: ; preds = %typeswitch.notUnmatched
%doubler.result = call i32 @"Doubler.Double$invoke"(ptr %value, ptr %typecode, ptr undef)
call void @runtime.printint32(i32 %doubler.result)
ret void
typeswitch.notDoubler: ; preds = %typeswitch.notUnmatched
%typeassert.ok = icmp eq ptr @"reflect/types.type:basic:uint8", %typecode
br i1 %typeassert.ok, label %typeswitch.byte, label %typeswitch.notByte
typeswitch.byte: ; preds = %typeswitch.notDoubler
typeswitch.byte: ; preds = %0
%byte = ptrtoint ptr %value to i8
call void @runtime.printuint8(i8 %byte)
call void @runtime.printnl()
ret void
typeswitch.notByte: ; preds = %typeswitch.notDoubler
typeswitch.notByte: ; preds = %0
br i1 false, label %typeswitch.int16, label %typeswitch.notInt16
typeswitch.int16: ; preds = %typeswitch.notByte
@@ -79,41 +54,3 @@ define i32 @"(Number).Double$invoke"(ptr %receiverPtr, ptr %context) {
%ret = call i32 @"(Number).Double"(i32 %receiver, ptr undef)
ret i32 %ret
}
define internal i32 @"Doubler.Double$invoke"(ptr %receiver, ptr %actualType, ptr %context) unnamed_addr #0 {
entry:
%"named:Number.icmp" = icmp eq ptr %actualType, @"reflect/types.type:named:Number"
br i1 %"named:Number.icmp", label %"named:Number", label %"named:Number.next"
"named:Number": ; preds = %entry
%0 = call i32 @"(Number).Double$invoke"(ptr %receiver, ptr undef)
ret i32 %0
"named:Number.next": ; preds = %entry
call void @runtime.nilPanic(ptr undef)
unreachable
}
define internal i1 @"Doubler$typeassert"(ptr %actualType) unnamed_addr #1 {
entry:
%"named:Number.icmp" = icmp eq ptr %actualType, @"reflect/types.type:named:Number"
br i1 %"named:Number.icmp", label %then, label %"named:Number.next"
then: ; preds = %entry
ret i1 true
"named:Number.next": ; preds = %entry
ret i1 false
}
define internal i1 @"Unmatched$typeassert"(ptr %actualType) unnamed_addr #2 {
entry:
ret i1 false
then: ; No predecessors!
ret i1 true
}
attributes #0 = { "tinygo-invoke"="reflect/methods.Double() int" "tinygo-methods"="reflect/methods.Double() int" }
attributes #1 = { "tinygo-methods"="reflect/methods.Double() int" }
attributes #2 = { "tinygo-methods"="reflect/methods.NeverImplementedMethod()" }