compiler: correctly generate code for local named types#3565
Conversation
|
Seems to work. I'll give it a better review in a bit. Even passes this test: |
|
Oddly the failing XML test doesn't pass with this patch. So looks like there's something else we need to hunt down. I'll try to figure out if it's this PR or something else is still broken. |
|
Here's the extracted xml test that is still getting the wrong type info for the structs: Doubt this runs, but I can maybe try to extract the pieces so it's just reflect calls grabbing type info. |
That would certainly be very useful for debugging. |
|
Better test case: Output: |
|
Much smaller reproducer: package main
import "reflect"
func main() {
itf := test2()
t := reflect.TypeOf(itf).Elem()
println("numField:", t.NumField())
}
func test1() interface{} {
type Test struct {
B int
}
return &Test{}
}
func test2() interface{} {
type Test struct {
A int
B int
}
return &Test{}
}Prints 2 in |
|
This makes it very clear that the wrong type is stored in the interface: package main
import "reflect"
func main() {
itf := test2()
kind := reflect.TypeOf(itf).Elem().Kind()
println("kind:", kind.String())
}
func test1() interface{} {
type Test int
return new(Test)
}
func test2() interface{} {
type Test byte
return new(Test)
} |
|
Pretty sure I know what's wrong now, I'll update this PR with a fix. |
42d8adc to
781bf4a
Compare
|
Should be fixed now (with an additional test case). |
|
This PR does not appear to pass CircleCI tests of backward compatibility: |
| switch t := t.(type) { | ||
| case *types.Named: | ||
| return "named:" + t.String() | ||
| if t.Obj().Parent() != t.Obj().Pkg().Scope() { |
There was a problem hiding this comment.
It seems like in Go1.18/LLVM 14 that t.Obj().Pkg() is sometimes nil here, causing
panic: runtime error: invalid memory address or nil pointer dereference
There was a problem hiding this comment.
Probably not LLVM, but it could be Go 1.18. I'd have to take a closer look.
781bf4a to
740e9c5
Compare
|
Indeed, this was an issue with Go 1.18. It should be fixed now. |
|
Looks like a number of other CI fails now, however 😺 |
|
Need to rerun |
|
@aykevl can you do that please? |
It is possible to create function-local named types:
func foo() any {
type named int
return named(0)
}
This patch makes sure they don't alias with named types declared at the
package scope.
Bug originally found by Damian Gryski while working on reflect support.
740e9c5 to
b113396
Compare
No, this was actually a regression after I fixed Go 1.18 support. I've fixed the fix, so that |
|
Only CI fail was the circuitplay-express. |
It is possible to create function-local named types:
This patch makes sure they don't alias with named types declared at the package scope.
Bug originally found by Damian Gryski while working on reflect support.