Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions cl/_testgo/tpnamed/in.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ func WriteFile(fileName string) IO[error] {
}

// CHECK-LABEL: define void @main.main(){{.*}} {
// CHECK: call [0 x i8] @"main.RunIO{{\[\[0\]byte\]}}"(%"main.IO{{\[\[0\]byte\]}}" { ptr @"main.main$1", ptr null })
// CHECK: call [0 x i8] @"main.RunIO{{\[\[0\]uint8\]}}"(%"main.IO{{\[\[0\]uint8\]}}" { ptr @"main.main$1", ptr null })
// CHECK-NEXT: ret void

func main() {

RunIO[Void](func() Future[Void] {

return func() (ret Void) {
Expand All @@ -42,13 +41,13 @@ func RunIO[T any](call IO[T]) T {
return call()()
}

// CHECK-LABEL: define linkonce [0 x i8] @"main.RunIO{{\[\[0\]byte\]}}"(%"main.IO{{\[\[0\]byte\]}}" %0){{.*}} {
// CHECK: [[IO_ENV:%[0-9]+]] = extractvalue %"main.IO{{\[\[0\]byte\]}}" %0, 1
// CHECK-NEXT: [[IO_CODE:%[0-9]+]] = extractvalue %"main.IO{{\[\[0\]byte\]}}" %0, 0
// DARWIN-ARM64: [[FUTURE:%[0-9]+]] = call %"main.Future{{\[\[0\]byte\]}}" %__llgo_funcval_code(ptr swiftself [[IO_ENV]])
// LINUX-AMD64: [[FUTURE:%[0-9]+]] = call %"main.Future{{\[\[0\]byte\]}}" %__llgo_funcval_code(ptr nest [[IO_ENV]])
// CHECK-NEXT: [[FUTURE_ENV:%[0-9]+]] = extractvalue %"main.Future{{\[\[0\]byte\]}}" [[FUTURE]], 1
// CHECK-NEXT: [[FUTURE_CODE:%[0-9]+]] = extractvalue %"main.Future{{\[\[0\]byte\]}}" [[FUTURE]], 0
// CHECK-LABEL: define linkonce [0 x i8] @"main.RunIO{{\[\[0\]uint8\]}}"(%"main.IO{{\[\[0\]uint8\]}}" %0){{.*}} {
// CHECK: [[IO_ENV:%[0-9]+]] = extractvalue %"main.IO{{\[\[0\]uint8\]}}" %0, 1
// CHECK-NEXT: [[IO_CODE:%[0-9]+]] = extractvalue %"main.IO{{\[\[0\]uint8\]}}" %0, 0
// DARWIN-ARM64: [[FUTURE:%[0-9]+]] = call %"main.Future{{\[\[0\]uint8\]}}" %__llgo_funcval_code(ptr swiftself [[IO_ENV]])
// LINUX-AMD64: [[FUTURE:%[0-9]+]] = call %"main.Future{{\[\[0\]uint8\]}}" %__llgo_funcval_code(ptr nest [[IO_ENV]])
// CHECK-NEXT: [[FUTURE_ENV:%[0-9]+]] = extractvalue %"main.Future{{\[\[0\]uint8\]}}" [[FUTURE]], 1
// CHECK-NEXT: [[FUTURE_CODE:%[0-9]+]] = extractvalue %"main.Future{{\[\[0\]uint8\]}}" [[FUTURE]], 0
// CHECK-NEXT: [[FUTURE_NIL:%[0-9]+]] = icmp eq ptr [[FUTURE_CODE]], null
// CHECK-NEXT: call void @"{{.*}}/runtime/internal/runtime.AssertNilDeref"(i1 [[FUTURE_NIL]])
// DARWIN-ARM64: [[IO_RESULT:%[0-9]+]] = call [0 x i8] %__llgo_funcval_code1(ptr swiftself [[FUTURE_ENV]])
Expand Down
9 changes: 9 additions & 0 deletions ssa/abi/abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,15 @@ func typeArgString(t types.Type) string {
case *types.Alias:
return typeArgString(types.Unalias(t))
case *types.Basic:
// byte and rune are aliases for uint8 and int32. go/types may cache a
// generic instance using either spelling, so ABI symbols must not
// depend on which spelling was instantiated first.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: the switch is keyed on Kind(), and types.Byte/types.Rune are the same constants as types.Uint8/types.Int32. So these cases also match plain uint8/int32 arguments (harmless no-op here), and a future case types.Uint8:/case types.Int32: would be a duplicate-case compile error. A one-line note that the branch is keyed on kind (which can't distinguish the alias from the target) would prevent that foot-gun. Non-blocking.

switch t.Kind() {
case types.Byte:
return types.Typ[types.Uint8].String()
case types.Rune:
return types.Typ[types.Int32].String()
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Minor: this canonicalization now lives in four places in the package (typeArgString here, BasicName below at ~L311, and Str/reflectTypeArgBaseString in type.go), split between Kind()-based and Name()-based mappings. Consider a small shared helper (e.g. canonicalBasicName(*types.Basic) string) to keep the byte->uint8 / rune->int32 rule from drifting. Also, for consistency with BasicName you could return the literals "uint8"/"int32" instead of types.Typ[...].String(). Non-blocking.

return t.String()
case *types.Named:
return namedLikeTypeArgString(t.Obj(), t.TypeArgs())
Expand Down
15 changes: 14 additions & 1 deletion ssa/abi/abi_patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,25 @@ func TestTypeArgs_FormatsCommonKinds(t *testing.T) {
local,
aliasInt,
})
const want = "[byte,*int,[]string,[2]int,map[string]int,chan int,chan<- int,<-chan int,chan (<-chan int),Local,int]"
const want = "[uint8,*int,[]string,[2]int,map[string]int,chan int,chan<- int,<-chan int,chan (<-chan int),Local,int]"
if got != want {
t.Fatalf("TypeArgs = %q, want %q", got, want)
}
}

func TestTypeArgs_CanonicalizesBasicAliases(t *testing.T) {
for _, pair := range [][2]*types.Basic{
{types.Typ[types.Byte], types.Typ[types.Uint8]},
{types.Typ[types.Rune], types.Typ[types.Int32]},
} {
alias := TypeArgs([]types.Type{pair[0]})
underlying := TypeArgs([]types.Type{pair[1]})
if alias != underlying {
t.Fatalf("TypeArgs(%v) = %q, TypeArgs(%v) = %q", pair[0], alias, pair[1], underlying)
}
}
}

func TestNamedName_UsesTypeArgString(t *testing.T) {
pkg := types.NewPackage("example.com/p", "p")
generic := testGenericNamed(pkg, "Box")
Expand Down
Loading