Skip to content

Commit dc23a5d

Browse files
luoliwoshangclaude
andcommitted
test: add regression test for embedunexport-1598 (#1597)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent e63221e commit dc23a5d

File tree

5 files changed

+310
-0
lines changed

5 files changed

+310
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import (
4+
"go/token"
5+
"go/types"
6+
)
7+
8+
// wrappedFunc embeds *types.Func to implement types.Object
9+
type wrappedFunc struct {
10+
*types.Func
11+
}
12+
13+
func main() {
14+
pkg := types.NewPackage("test", "test")
15+
scope := pkg.Scope()
16+
17+
sig := types.NewSignatureType(nil, nil, nil, nil, nil, false)
18+
fn := types.NewFunc(token.NoPos, pkg, "testFunc", sig)
19+
20+
wrapped := &wrappedFunc{Func: fn}
21+
var obj types.Object = wrapped
22+
23+
scope.Insert(obj)
24+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package embedunexport
2+
3+
// Object is an interface with both exported and unexported methods
4+
type Object interface {
5+
Name() string
6+
setName(string)
7+
}
8+
9+
// Base implements Object
10+
type Base struct {
11+
name string
12+
}
13+
14+
func (b *Base) Name() string {
15+
return b.name
16+
}
17+
18+
func (b *Base) setName(name string) {
19+
b.name = name
20+
}
21+
22+
func NewBase(name string) *Base {
23+
return &Base{name: name}
24+
}
25+
26+
// Use calls the unexported method through interface
27+
func Use(obj Object) {
28+
obj.setName("modified")
29+
}

cl/_testdata/embedunexport/out.ll

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
; ModuleID = 'github.com/goplus/llgo/cl/_testdata/embedunexport'
2+
source_filename = "github.com/goplus/llgo/cl/_testdata/embedunexport"
3+
4+
%"github.com/goplus/llgo/runtime/internal/runtime.String" = type { ptr, i64 }
5+
%"github.com/goplus/llgo/cl/_testdata/embedunexport.Base" = type { %"github.com/goplus/llgo/runtime/internal/runtime.String" }
6+
%"github.com/goplus/llgo/runtime/internal/runtime.iface" = type { ptr, ptr }
7+
8+
@"github.com/goplus/llgo/cl/_testdata/embedunexport.init$guard" = global i1 false, align 1
9+
@0 = private unnamed_addr constant [8 x i8] c"modified", align 1
10+
11+
define %"github.com/goplus/llgo/runtime/internal/runtime.String" @"github.com/goplus/llgo/cl/_testdata/embedunexport.(*Base).Name"(ptr %0) {
12+
_llgo_0:
13+
%1 = getelementptr inbounds %"github.com/goplus/llgo/cl/_testdata/embedunexport.Base", ptr %0, i32 0, i32 0
14+
%2 = load %"github.com/goplus/llgo/runtime/internal/runtime.String", ptr %1, align 8
15+
ret %"github.com/goplus/llgo/runtime/internal/runtime.String" %2
16+
}
17+
18+
define void @"github.com/goplus/llgo/cl/_testdata/embedunexport.(*Base).setName"(ptr %0, %"github.com/goplus/llgo/runtime/internal/runtime.String" %1) {
19+
_llgo_0:
20+
%2 = getelementptr inbounds %"github.com/goplus/llgo/cl/_testdata/embedunexport.Base", ptr %0, i32 0, i32 0
21+
store %"github.com/goplus/llgo/runtime/internal/runtime.String" %1, ptr %2, align 8
22+
ret void
23+
}
24+
25+
define ptr @"github.com/goplus/llgo/cl/_testdata/embedunexport.NewBase"(%"github.com/goplus/llgo/runtime/internal/runtime.String" %0) {
26+
_llgo_0:
27+
%1 = call ptr @"github.com/goplus/llgo/runtime/internal/runtime.AllocZ"(i64 16)
28+
%2 = getelementptr inbounds %"github.com/goplus/llgo/cl/_testdata/embedunexport.Base", ptr %1, i32 0, i32 0
29+
store %"github.com/goplus/llgo/runtime/internal/runtime.String" %0, ptr %2, align 8
30+
ret ptr %1
31+
}
32+
33+
define void @"github.com/goplus/llgo/cl/_testdata/embedunexport.Use"(%"github.com/goplus/llgo/runtime/internal/runtime.iface" %0) {
34+
_llgo_0:
35+
%1 = call ptr @"github.com/goplus/llgo/runtime/internal/runtime.IfacePtrData"(%"github.com/goplus/llgo/runtime/internal/runtime.iface" %0)
36+
%2 = extractvalue %"github.com/goplus/llgo/runtime/internal/runtime.iface" %0, 0
37+
%3 = getelementptr ptr, ptr %2, i64 4
38+
%4 = load ptr, ptr %3, align 8
39+
%5 = insertvalue { ptr, ptr } undef, ptr %4, 0
40+
%6 = insertvalue { ptr, ptr } %5, ptr %1, 1
41+
%7 = extractvalue { ptr, ptr } %6, 1
42+
%8 = extractvalue { ptr, ptr } %6, 0
43+
call void %8(ptr %7, %"github.com/goplus/llgo/runtime/internal/runtime.String" { ptr @0, i64 8 })
44+
ret void
45+
}
46+
47+
define void @"github.com/goplus/llgo/cl/_testdata/embedunexport.init"() {
48+
_llgo_0:
49+
%0 = load i1, ptr @"github.com/goplus/llgo/cl/_testdata/embedunexport.init$guard", align 1
50+
br i1 %0, label %_llgo_2, label %_llgo_1
51+
52+
_llgo_1: ; preds = %_llgo_0
53+
store i1 true, ptr @"github.com/goplus/llgo/cl/_testdata/embedunexport.init$guard", align 1
54+
br label %_llgo_2
55+
56+
_llgo_2: ; preds = %_llgo_1, %_llgo_0
57+
ret void
58+
}
59+
60+
declare ptr @"github.com/goplus/llgo/runtime/internal/runtime.AllocZ"(i64)
61+
62+
declare ptr @"github.com/goplus/llgo/runtime/internal/runtime.IfacePtrData"(%"github.com/goplus/llgo/runtime/internal/runtime.iface")
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import "github.com/goplus/llgo/cl/_testdata/embedunexport"
4+
5+
// Wrapped embeds *embedunexport.Base to implement embedunexport.Object
6+
type Wrapped struct {
7+
*embedunexport.Base
8+
}
9+
10+
func main() {
11+
base := embedunexport.NewBase("test")
12+
wrapped := &Wrapped{Base: base}
13+
14+
// This should work: calling unexported method through interface
15+
var obj embedunexport.Object = wrapped
16+
embedunexport.Use(obj)
17+
18+
println(obj.Name())
19+
}

0 commit comments

Comments
 (0)