diff --git a/compiler/testdata/generics.go b/compiler/testdata/generics.go index fc35538fe..76169ce5f 100644 --- a/compiler/testdata/generics.go +++ b/compiler/testdata/generics.go @@ -19,12 +19,49 @@ func Add[T Coord](a, b Point[T]) Point[T] { } } +func aliasSize[F float32 | float64]() uintptr { + return unsafe.Sizeof(F(0)) +} + +func aliasSize32() uintptr { + type F = float32 + return aliasSize[F]() +} + +func aliasSize64() uintptr { + type F = float64 + return aliasSize[F]() +} + +type aliasMethodResult[F float32 | float64] struct { + Value F +} + +type aliasMethodValue[F float32 | float64] struct{} + +func (aliasMethodValue[F]) Get() aliasMethodResult[F] { + return aliasMethodResult[F]{} +} + func main() { var af, bf Point[float32] Add(af, bf) var ai, bi Point[int] Add(ai, bi) + + checkSize(aliasSize32()) + checkSize(aliasSize64()) } func checkSize(uintptr) + +func checkBool(bool) + +func aliasMethod32(x any) { + type F = float32 + _, ok := x.(interface { + Get() aliasMethodResult[F] + }) + checkBool(ok) +} diff --git a/compiler/testdata/generics.ll b/compiler/testdata/generics.ll index 2a3fcd0ab..abe80e2ed 100644 --- a/compiler/testdata/generics.ll +++ b/compiler/testdata/generics.ll @@ -14,11 +14,35 @@ entry: ret void } +; Function Attrs: nounwind +define hidden i32 @main.aliasSize32(ptr %context) unnamed_addr #1 { +entry: + %0 = call i32 @"main.aliasSize[main.F]"(ptr undef) + ret i32 %0 +} + +; Function Attrs: nounwind +define linkonce_odr hidden i32 @"main.aliasSize[main.F]"(ptr %context) unnamed_addr #1 { +entry: + ret i32 4 +} + +; Function Attrs: nounwind +define hidden i32 @main.aliasSize64(ptr %context) unnamed_addr #1 { +entry: + %0 = call i32 @"main.aliasSize[main.F]"(ptr undef) + ret i32 %0 +} + ; Function Attrs: nounwind define hidden void @main.main(ptr %context) unnamed_addr #1 { entry: %0 = call %"main.Point[float32]" @"main.Add[float32]"(float 0.000000e+00, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00, ptr undef) %1 = call %"main.Point[int]" @"main.Add[int]"(i32 0, i32 0, i32 0, i32 0, ptr undef) + %2 = call i32 @main.aliasSize32(ptr undef) + call void @main.checkSize(i32 %2, ptr undef) #4 + %3 = call i32 @main.aliasSize64(ptr undef) + call void @main.checkSize(i32 %3, ptr undef) #4 ret void } @@ -26,20 +50,20 @@ entry: define linkonce_odr hidden %"main.Point[float32]" @"main.Add[float32]"(float %a.X, float %a.Y, float %b.X, float %b.Y, ptr %context) unnamed_addr #1 { entry: %stackalloc = alloca i8, align 1 - %a = call align 4 dereferenceable(8) ptr @runtime.alloc(i32 8, ptr nonnull inttoptr (i32 3 to ptr), ptr undef) #3 - call void @runtime.trackPointer(ptr nonnull %a, ptr nonnull %stackalloc, ptr undef) #3 + %a = call align 4 dereferenceable(8) ptr @runtime.alloc(i32 8, ptr nonnull inttoptr (i32 3 to ptr), ptr undef) #4 + call void @runtime.trackPointer(ptr nonnull %a, ptr nonnull %stackalloc, ptr undef) #4 store float %a.X, ptr %a, align 4 %a.repack5 = getelementptr inbounds nuw i8, ptr %a, i32 4 store float %a.Y, ptr %a.repack5, align 4 - %b = call align 4 dereferenceable(8) ptr @runtime.alloc(i32 8, ptr nonnull inttoptr (i32 3 to ptr), ptr undef) #3 - call void @runtime.trackPointer(ptr nonnull %b, ptr nonnull %stackalloc, ptr undef) #3 + %b = call align 4 dereferenceable(8) ptr @runtime.alloc(i32 8, ptr nonnull inttoptr (i32 3 to ptr), ptr undef) #4 + call void @runtime.trackPointer(ptr nonnull %b, ptr nonnull %stackalloc, ptr undef) #4 store float %b.X, ptr %b, align 4 %b.repack7 = getelementptr inbounds nuw i8, ptr %b, i32 4 store float %b.Y, ptr %b.repack7, align 4 - call void @main.checkSize(i32 4, ptr undef) #3 - call void @main.checkSize(i32 8, ptr undef) #3 - %complit = call align 4 dereferenceable(8) ptr @runtime.alloc(i32 8, ptr nonnull inttoptr (i32 3 to ptr), ptr undef) #3 - call void @runtime.trackPointer(ptr nonnull %complit, ptr nonnull %stackalloc, ptr undef) #3 + call void @main.checkSize(i32 4, ptr undef) #4 + call void @main.checkSize(i32 8, ptr undef) #4 + %complit = call align 4 dereferenceable(8) ptr @runtime.alloc(i32 8, ptr nonnull inttoptr (i32 3 to ptr), ptr undef) #4 + call void @runtime.trackPointer(ptr nonnull %complit, ptr nonnull %stackalloc, ptr undef) #4 br i1 false, label %deref.throw, label %deref.next deref.next: ; preds = %entry @@ -89,20 +113,20 @@ declare void @runtime.nilPanic(ptr) #0 define linkonce_odr hidden %"main.Point[int]" @"main.Add[int]"(i32 %a.X, i32 %a.Y, i32 %b.X, i32 %b.Y, ptr %context) unnamed_addr #1 { entry: %stackalloc = alloca i8, align 1 - %a = call align 4 dereferenceable(8) ptr @runtime.alloc(i32 8, ptr nonnull inttoptr (i32 3 to ptr), ptr undef) #3 - call void @runtime.trackPointer(ptr nonnull %a, ptr nonnull %stackalloc, ptr undef) #3 + %a = call align 4 dereferenceable(8) ptr @runtime.alloc(i32 8, ptr nonnull inttoptr (i32 3 to ptr), ptr undef) #4 + call void @runtime.trackPointer(ptr nonnull %a, ptr nonnull %stackalloc, ptr undef) #4 store i32 %a.X, ptr %a, align 4 %a.repack5 = getelementptr inbounds nuw i8, ptr %a, i32 4 store i32 %a.Y, ptr %a.repack5, align 4 - %b = call align 4 dereferenceable(8) ptr @runtime.alloc(i32 8, ptr nonnull inttoptr (i32 3 to ptr), ptr undef) #3 - call void @runtime.trackPointer(ptr nonnull %b, ptr nonnull %stackalloc, ptr undef) #3 + %b = call align 4 dereferenceable(8) ptr @runtime.alloc(i32 8, ptr nonnull inttoptr (i32 3 to ptr), ptr undef) #4 + call void @runtime.trackPointer(ptr nonnull %b, ptr nonnull %stackalloc, ptr undef) #4 store i32 %b.X, ptr %b, align 4 %b.repack7 = getelementptr inbounds nuw i8, ptr %b, i32 4 store i32 %b.Y, ptr %b.repack7, align 4 - call void @main.checkSize(i32 4, ptr undef) #3 - call void @main.checkSize(i32 8, ptr undef) #3 - %complit = call align 4 dereferenceable(8) ptr @runtime.alloc(i32 8, ptr nonnull inttoptr (i32 3 to ptr), ptr undef) #3 - call void @runtime.trackPointer(ptr nonnull %complit, ptr nonnull %stackalloc, ptr undef) #3 + call void @main.checkSize(i32 4, ptr undef) #4 + call void @main.checkSize(i32 8, ptr undef) #4 + %complit = call align 4 dereferenceable(8) ptr @runtime.alloc(i32 8, ptr nonnull inttoptr (i32 3 to ptr), ptr undef) #4 + call void @runtime.trackPointer(ptr nonnull %complit, ptr nonnull %stackalloc, ptr undef) #4 br i1 false, label %deref.throw, label %deref.next deref.next: ; preds = %entry @@ -141,7 +165,26 @@ deref.throw: ; preds = %store.next, %deref. unreachable } +declare void @main.checkBool(i1, ptr) #0 + +; Function Attrs: nounwind +define hidden void @main.aliasMethod32(ptr %x.typecode, ptr %x.value, ptr %context) unnamed_addr #1 { +entry: + %0 = call i1 @"interface:{Get:func:{}{named:main.aliasMethodResult[main.F]}}.$typeassert"(ptr %x.typecode) #4 + br i1 %0, label %typeassert.ok, label %typeassert.next + +typeassert.next: ; preds = %typeassert.ok, %entry + call void @main.checkBool(i1 %0, ptr undef) #4 + ret void + +typeassert.ok: ; preds = %entry + br label %typeassert.next +} + +declare i1 @"interface:{Get:func:{}{named:main.aliasMethodResult[main.F]}}.$typeassert"(ptr) #3 + attributes #0 = { "target-features"="+bulk-memory,+bulk-memory-opt,+call-indirect-overlong,+mutable-globals,+nontrapping-fptoint,+sign-ext,-multivalue,-reference-types" } attributes #1 = { nounwind "target-features"="+bulk-memory,+bulk-memory-opt,+call-indirect-overlong,+mutable-globals,+nontrapping-fptoint,+sign-ext,-multivalue,-reference-types" } attributes #2 = { allockind("alloc,zeroed") allocsize(0) "alloc-family"="runtime.alloc" "target-features"="+bulk-memory,+bulk-memory-opt,+call-indirect-overlong,+mutable-globals,+nontrapping-fptoint,+sign-ext,-multivalue,-reference-types" } -attributes #3 = { nounwind } +attributes #3 = { "target-features"="+bulk-memory,+bulk-memory-opt,+call-indirect-overlong,+mutable-globals,+nontrapping-fptoint,+sign-ext,-multivalue,-reference-types" "tinygo-methods"="reflect/methods.Get() main.aliasMethodResult[main.F]" } +attributes #4 = { nounwind } diff --git a/testdata/localtypes.go b/testdata/localtypes.go index f9081d00b..8960ca713 100644 --- a/testdata/localtypes.go +++ b/testdata/localtypes.go @@ -1,6 +1,9 @@ package main -import "reflect" +import ( + "reflect" + "unsafe" +) type checker = func(any) bool @@ -216,6 +219,92 @@ func issue5180CopyIgnoreNilMembers() (ok bool) { return ok } +func aliasSize[F float32 | float64]() int { + return 8 * int(unsafe.Sizeof(F(0))) +} + +func aliasSizeCaller32() int { + type F = float32 + return aliasSize[F]() +} + +func aliasSizeCaller64() int { + type F = float64 + return aliasSize[F]() +} + +func aliasLocal[F float32 | float64]() (any, checker) { + type Local struct{ Value F } + return Local{}, func(x any) bool { + _, ok := x.(Local) + return ok + } +} + +func aliasLocalCaller32() (any, checker) { + type F = float32 + return aliasLocal[F]() +} + +func aliasLocalCaller64() (any, checker) { + type F = float64 + return aliasLocal[F]() +} + +type aliasBox[F float32 | float64] struct { + Value F +} + +func (b aliasBox[F]) Get() F { + return b.Value +} + +func aliasBoxCaller32() bool { + type F = float32 + var box any = aliasBox[F]{Value: 32} + _, ok := box.(interface{ Get() float32 }) + return ok +} + +func aliasBoxCaller64() bool { + type F = float64 + var box any = aliasBox[F]{Value: 64} + _, ok := box.(interface{ Get() float64 }) + return ok +} + +type aliasMethodResult[F float32 | float64] struct { + Value F +} + +type aliasMethodBox[F float32 | float64] struct { + Value F +} + +func (b aliasMethodBox[F]) Get() aliasMethodResult[F] { + return aliasMethodResult[F]{Value: b.Value} +} + +func aliasMethodCaller32() (any, checker) { + type F = float32 + return aliasMethodBox[F]{Value: 32}, func(x any) bool { + _, ok := x.(interface { + Get() aliasMethodResult[F] + }) + return ok + } +} + +func aliasMethodCaller64() (any, checker) { + type F = float64 + return aliasMethodBox[F]{Value: 64}, func(x any) bool { + _, ok := x.(interface { + Get() aliasMethodResult[F] + }) + return ok + } +} + func main() { expect("issue5180 TestCopy1", issue5180Copy1()) expect("issue5180 TestCopyIgnoreNilMembers", issue5180CopyIgnoreNilMembers()) @@ -300,4 +389,20 @@ func main() { println("issue4931PairB labels:", issue4931PairB()) println("issue4931MethodA labels:", issue4931MethodA()) println("issue4931MethodB labels:", issue4931MethodB()) + expect("aliasSize[float32]==32", aliasSizeCaller32() == 32) + expect("aliasSize[float64]==64", aliasSizeCaller64() == 64) + aliasLocal32, aliasLocalCheck32 := aliasLocalCaller32() + aliasLocal64, aliasLocalCheck64 := aliasLocalCaller64() + expect("aliasLocal[float32] accepts own", aliasLocalCheck32(aliasLocal32)) + expect("aliasLocal[float64] accepts own", aliasLocalCheck64(aliasLocal64)) + expect("aliasLocal[float32] rejects [float64]", !aliasLocalCheck32(aliasLocal64)) + expect("aliasLocal[float64] rejects [float32]", !aliasLocalCheck64(aliasLocal32)) + expect("aliasBox[float32] implements Get() float32", aliasBoxCaller32()) + expect("aliasBox[float64] implements Get() float64", aliasBoxCaller64()) + aliasMethod32, aliasMethodCheck32 := aliasMethodCaller32() + aliasMethod64, aliasMethodCheck64 := aliasMethodCaller64() + expect("aliasMethod[float32] accepts own", aliasMethodCheck32(aliasMethod32)) + expect("aliasMethod[float64] accepts own", aliasMethodCheck64(aliasMethod64)) + expect("aliasMethod[float32] rejects [float64]", !aliasMethodCheck32(aliasMethod64)) + expect("aliasMethod[float64] rejects [float32]", !aliasMethodCheck64(aliasMethod32)) } diff --git a/testdata/localtypes.txt b/testdata/localtypes.txt index dd5d72ddd..85a851741 100644 --- a/testdata/localtypes.txt +++ b/testdata/localtypes.txt @@ -47,3 +47,15 @@ issue4931PairA labels: 0 issue4931PairB labels: 0 issue4931MethodA labels: 0 issue4931MethodB labels: 0 +ok: aliasSize[float32]==32 +BUG: aliasSize[float64]==64 +ok: aliasLocal[float32] accepts own +ok: aliasLocal[float64] accepts own +BUG: aliasLocal[float32] rejects [float64] +BUG: aliasLocal[float64] rejects [float32] +ok: aliasBox[float32] implements Get() float32 +BUG: aliasBox[float64] implements Get() float64 +ok: aliasMethod[float32] accepts own +ok: aliasMethod[float64] accepts own +BUG: aliasMethod[float32] rejects [float64] +BUG: aliasMethod[float64] rejects [float32]