fix(demo): remove unused go/ast import from gotypes demo
Removed the unused "go/ast" import from _demo/go/gotypes/main.go that was causing compilation errors. The demo now compiles and runs successfully with both standard go and llgo. Generated with [codeagent](https://github.com/qbox/codeagent) Co-authored-by: luoliwoshang <51194195+luoliwoshang@users.noreply.github.com>
This commit is contained in:
@@ -2,14 +2,13 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"go/ast"
|
|
||||||
"go/token"
|
"go/token"
|
||||||
"go/types"
|
"go/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println("=== Comprehensive go/types Package Demo ===\n")
|
fmt.Println("=== Comprehensive go/types Package Demo ===\n")
|
||||||
|
|
||||||
testBasicTypes()
|
testBasicTypes()
|
||||||
testObjects()
|
testObjects()
|
||||||
testScope()
|
testScope()
|
||||||
@@ -24,44 +23,44 @@ func main() {
|
|||||||
testPointer()
|
testPointer()
|
||||||
testMap()
|
testMap()
|
||||||
testChan()
|
testChan()
|
||||||
|
|
||||||
fmt.Println("\n=== All go/types tests completed successfully! ===")
|
fmt.Println("\n=== All go/types tests completed successfully! ===")
|
||||||
}
|
}
|
||||||
|
|
||||||
func testBasicTypes() {
|
func testBasicTypes() {
|
||||||
fmt.Println("=== Test Basic Types ===")
|
fmt.Println("=== Test Basic Types ===")
|
||||||
|
|
||||||
intType := types.Typ[types.Int]
|
intType := types.Typ[types.Int]
|
||||||
fmt.Printf("Int type: %v, Kind: %v\n", intType, intType.Kind())
|
fmt.Printf("Int type: %v, Kind: %v\n", intType, intType.Kind())
|
||||||
|
|
||||||
stringType := types.Typ[types.String]
|
stringType := types.Typ[types.String]
|
||||||
fmt.Printf("String type: %v, Kind: %v\n", stringType, stringType.Kind())
|
fmt.Printf("String type: %v, Kind: %v\n", stringType, stringType.Kind())
|
||||||
|
|
||||||
boolType := types.Typ[types.Bool]
|
boolType := types.Typ[types.Bool]
|
||||||
fmt.Printf("Bool type: %v, Kind: %v\n", boolType, boolType.Kind())
|
fmt.Printf("Bool type: %v, Kind: %v\n", boolType, boolType.Kind())
|
||||||
|
|
||||||
float64Type := types.Typ[types.Float64]
|
float64Type := types.Typ[types.Float64]
|
||||||
fmt.Printf("Float64 type: %v, Kind: %v\n", float64Type, float64Type.Kind())
|
fmt.Printf("Float64 type: %v, Kind: %v\n", float64Type, float64Type.Kind())
|
||||||
|
|
||||||
fmt.Println("SUCCESS: Basic types work correctly\n")
|
fmt.Println("SUCCESS: Basic types work correctly\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
func testObjects() {
|
func testObjects() {
|
||||||
fmt.Println("=== Test Objects (Var, Const, Func, TypeName) ===")
|
fmt.Println("=== Test Objects (Var, Const, Func, TypeName) ===")
|
||||||
|
|
||||||
varObj := types.NewVar(token.NoPos, nil, "x", types.Typ[types.Int])
|
varObj := types.NewVar(token.NoPos, nil, "x", types.Typ[types.Int])
|
||||||
fmt.Printf("Var: Name=%s, Type=%v\n", varObj.Name(), varObj.Type())
|
fmt.Printf("Var: Name=%s, Type=%v\n", varObj.Name(), varObj.Type())
|
||||||
|
|
||||||
constObj := types.NewConst(token.NoPos, nil, "pi", types.Typ[types.Float64], nil)
|
constObj := types.NewConst(token.NoPos, nil, "pi", types.Typ[types.Float64], nil)
|
||||||
fmt.Printf("Const: Name=%s, Type=%v\n", constObj.Name(), constObj.Type())
|
fmt.Printf("Const: Name=%s, Type=%v\n", constObj.Name(), constObj.Type())
|
||||||
|
|
||||||
sig := types.NewSignatureType(nil, nil, nil, nil, nil, false)
|
sig := types.NewSignatureType(nil, nil, nil, nil, nil, false)
|
||||||
funcObj := types.NewFunc(token.NoPos, nil, "foo", sig)
|
funcObj := types.NewFunc(token.NoPos, nil, "foo", sig)
|
||||||
fmt.Printf("Func: Name=%s, Type=%v\n", funcObj.Name(), funcObj.Type())
|
fmt.Printf("Func: Name=%s, Type=%v\n", funcObj.Name(), funcObj.Type())
|
||||||
|
|
||||||
typeObj := types.NewTypeName(token.NoPos, nil, "MyInt", types.Typ[types.Int])
|
typeObj := types.NewTypeName(token.NoPos, nil, "MyInt", types.Typ[types.Int])
|
||||||
fmt.Printf("TypeName: Name=%s, Type=%v\n", typeObj.Name(), typeObj.Type())
|
fmt.Printf("TypeName: Name=%s, Type=%v\n", typeObj.Name(), typeObj.Type())
|
||||||
|
|
||||||
var obj types.Object = varObj
|
var obj types.Object = varObj
|
||||||
if obj.Name() != "x" {
|
if obj.Name() != "x" {
|
||||||
panic("Object interface conversion failed")
|
panic("Object interface conversion failed")
|
||||||
@@ -71,190 +70,190 @@ func testObjects() {
|
|||||||
|
|
||||||
func testScope() {
|
func testScope() {
|
||||||
fmt.Println("=== Test Scope ===")
|
fmt.Println("=== Test Scope ===")
|
||||||
|
|
||||||
scope := types.NewScope(nil, 0, 0, "test")
|
scope := types.NewScope(nil, 0, 0, "test")
|
||||||
obj := types.NewVar(0, nil, "x", types.Typ[types.Int])
|
obj := types.NewVar(0, nil, "x", types.Typ[types.Int])
|
||||||
|
|
||||||
scope.Insert(obj)
|
scope.Insert(obj)
|
||||||
result := scope.Lookup("x")
|
result := scope.Lookup("x")
|
||||||
if result != obj {
|
if result != obj {
|
||||||
panic("Scope.Lookup failed")
|
panic("Scope.Lookup failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
names := scope.Names()
|
names := scope.Names()
|
||||||
if len(names) != 1 || names[0] != "x" {
|
if len(names) != 1 || names[0] != "x" {
|
||||||
panic("Scope.Names failed")
|
panic("Scope.Names failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
num := scope.Len()
|
num := scope.Len()
|
||||||
if num != 1 {
|
if num != 1 {
|
||||||
panic("Scope.Len failed")
|
panic("Scope.Len failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Scope contains %d object(s): %v\n", num, names)
|
fmt.Printf("Scope contains %d object(s): %v\n", num, names)
|
||||||
fmt.Println("SUCCESS: Scope operations work correctly\n")
|
fmt.Println("SUCCESS: Scope operations work correctly\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
func testPackage() {
|
func testPackage() {
|
||||||
fmt.Println("=== Test Package ===")
|
fmt.Println("=== Test Package ===")
|
||||||
|
|
||||||
pkg := types.NewPackage("example.com/test", "test")
|
pkg := types.NewPackage("example.com/test", "test")
|
||||||
fmt.Printf("Package: Path=%s, Name=%s\n", pkg.Path(), pkg.Name())
|
fmt.Printf("Package: Path=%s, Name=%s\n", pkg.Path(), pkg.Name())
|
||||||
|
|
||||||
scope := pkg.Scope()
|
scope := pkg.Scope()
|
||||||
if scope == nil {
|
if scope == nil {
|
||||||
panic("Package.Scope returned nil")
|
panic("Package.Scope returned nil")
|
||||||
}
|
}
|
||||||
|
|
||||||
varObj := types.NewVar(token.NoPos, pkg, "x", types.Typ[types.Int])
|
varObj := types.NewVar(token.NoPos, pkg, "x", types.Typ[types.Int])
|
||||||
scope.Insert(varObj)
|
scope.Insert(varObj)
|
||||||
|
|
||||||
result := pkg.Scope().Lookup("x")
|
result := pkg.Scope().Lookup("x")
|
||||||
if result != varObj {
|
if result != varObj {
|
||||||
panic("Package scope lookup failed")
|
panic("Package scope lookup failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("SUCCESS: Package operations work correctly\n")
|
fmt.Println("SUCCESS: Package operations work correctly\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
func testNamed() {
|
func testNamed() {
|
||||||
fmt.Println("=== Test Named Types ===")
|
fmt.Println("=== Test Named Types ===")
|
||||||
|
|
||||||
pkg := types.NewPackage("example.com/test", "test")
|
pkg := types.NewPackage("example.com/test", "test")
|
||||||
typeName := types.NewTypeName(token.NoPos, pkg, "MyInt", nil)
|
typeName := types.NewTypeName(token.NoPos, pkg, "MyInt", nil)
|
||||||
named := types.NewNamed(typeName, types.Typ[types.Int], nil)
|
named := types.NewNamed(typeName, types.Typ[types.Int], nil)
|
||||||
|
|
||||||
fmt.Printf("Named type: %v, Underlying: %v\n", named, named.Underlying())
|
fmt.Printf("Named type: %v, Underlying: %v\n", named, named.Underlying())
|
||||||
|
|
||||||
if named.Obj() != typeName {
|
if named.Obj() != typeName {
|
||||||
panic("Named.Obj failed")
|
panic("Named.Obj failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("SUCCESS: Named type operations work correctly\n")
|
fmt.Println("SUCCESS: Named type operations work correctly\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
func testInterface() {
|
func testInterface() {
|
||||||
fmt.Println("=== Test Interface ===")
|
fmt.Println("=== Test Interface ===")
|
||||||
|
|
||||||
pkg := types.NewPackage("example.com/test", "test")
|
pkg := types.NewPackage("example.com/test", "test")
|
||||||
|
|
||||||
posMethod := types.NewFunc(token.NoPos, pkg, "Pos", types.NewSignatureType(nil, nil, nil, nil, types.NewTuple(types.NewVar(0, pkg, "", types.Typ[types.Int])), false))
|
posMethod := types.NewFunc(token.NoPos, pkg, "Pos", types.NewSignatureType(nil, nil, nil, nil, types.NewTuple(types.NewVar(0, pkg, "", types.Typ[types.Int])), false))
|
||||||
endMethod := types.NewFunc(token.NoPos, pkg, "End", types.NewSignatureType(nil, nil, nil, nil, types.NewTuple(types.NewVar(0, pkg, "", types.Typ[types.Int])), false))
|
endMethod := types.NewFunc(token.NoPos, pkg, "End", types.NewSignatureType(nil, nil, nil, nil, types.NewTuple(types.NewVar(0, pkg, "", types.Typ[types.Int])), false))
|
||||||
|
|
||||||
methods := []*types.Func{posMethod, endMethod}
|
methods := []*types.Func{posMethod, endMethod}
|
||||||
iface := types.NewInterfaceType(methods, nil)
|
iface := types.NewInterfaceType(methods, nil)
|
||||||
iface.Complete()
|
iface.Complete()
|
||||||
|
|
||||||
fmt.Printf("Interface with %d methods\n", iface.NumMethods())
|
fmt.Printf("Interface with %d methods\n", iface.NumMethods())
|
||||||
|
|
||||||
method := iface.Method(0)
|
method := iface.Method(0)
|
||||||
fmt.Printf("Method 0: %s\n", method.Name())
|
fmt.Printf("Method 0: %s\n", method.Name())
|
||||||
|
|
||||||
fmt.Println("SUCCESS: Interface operations work correctly\n")
|
fmt.Println("SUCCESS: Interface operations work correctly\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
func testStruct() {
|
func testStruct() {
|
||||||
fmt.Println("=== Test Struct ===")
|
fmt.Println("=== Test Struct ===")
|
||||||
|
|
||||||
fields := []*types.Var{
|
fields := []*types.Var{
|
||||||
types.NewField(token.NoPos, nil, "X", types.Typ[types.Int], false),
|
types.NewField(token.NoPos, nil, "X", types.Typ[types.Int], false),
|
||||||
types.NewField(token.NoPos, nil, "Y", types.Typ[types.String], false),
|
types.NewField(token.NoPos, nil, "Y", types.Typ[types.String], false),
|
||||||
}
|
}
|
||||||
|
|
||||||
structType := types.NewStruct(fields, nil)
|
structType := types.NewStruct(fields, nil)
|
||||||
fmt.Printf("Struct with %d fields\n", structType.NumFields())
|
fmt.Printf("Struct with %d fields\n", structType.NumFields())
|
||||||
|
|
||||||
field0 := structType.Field(0)
|
field0 := structType.Field(0)
|
||||||
fmt.Printf("Field 0: Name=%s, Type=%v\n", field0.Name(), field0.Type())
|
fmt.Printf("Field 0: Name=%s, Type=%v\n", field0.Name(), field0.Type())
|
||||||
|
|
||||||
fmt.Println("SUCCESS: Struct operations work correctly\n")
|
fmt.Println("SUCCESS: Struct operations work correctly\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
func testSignature() {
|
func testSignature() {
|
||||||
fmt.Println("=== Test Signature ===")
|
fmt.Println("=== Test Signature ===")
|
||||||
|
|
||||||
params := types.NewTuple(
|
params := types.NewTuple(
|
||||||
types.NewVar(token.NoPos, nil, "x", types.Typ[types.Int]),
|
types.NewVar(token.NoPos, nil, "x", types.Typ[types.Int]),
|
||||||
types.NewVar(token.NoPos, nil, "y", types.Typ[types.String]),
|
types.NewVar(token.NoPos, nil, "y", types.Typ[types.String]),
|
||||||
)
|
)
|
||||||
|
|
||||||
results := types.NewTuple(
|
results := types.NewTuple(
|
||||||
types.NewVar(token.NoPos, nil, "", types.Typ[types.Bool]),
|
types.NewVar(token.NoPos, nil, "", types.Typ[types.Bool]),
|
||||||
)
|
)
|
||||||
|
|
||||||
sig := types.NewSignatureType(nil, nil, nil, params, results, false)
|
sig := types.NewSignatureType(nil, nil, nil, params, results, false)
|
||||||
|
|
||||||
fmt.Printf("Signature: %d params, %d results\n", sig.Params().Len(), sig.Results().Len())
|
fmt.Printf("Signature: %d params, %d results\n", sig.Params().Len(), sig.Results().Len())
|
||||||
|
|
||||||
param0 := sig.Params().At(0)
|
param0 := sig.Params().At(0)
|
||||||
fmt.Printf("Param 0: Name=%s, Type=%v\n", param0.Name(), param0.Type())
|
fmt.Printf("Param 0: Name=%s, Type=%v\n", param0.Name(), param0.Type())
|
||||||
|
|
||||||
fmt.Println("SUCCESS: Signature operations work correctly\n")
|
fmt.Println("SUCCESS: Signature operations work correctly\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
func testTuple() {
|
func testTuple() {
|
||||||
fmt.Println("=== Test Tuple ===")
|
fmt.Println("=== Test Tuple ===")
|
||||||
|
|
||||||
tuple := types.NewTuple(
|
tuple := types.NewTuple(
|
||||||
types.NewVar(token.NoPos, nil, "a", types.Typ[types.Int]),
|
types.NewVar(token.NoPos, nil, "a", types.Typ[types.Int]),
|
||||||
types.NewVar(token.NoPos, nil, "b", types.Typ[types.String]),
|
types.NewVar(token.NoPos, nil, "b", types.Typ[types.String]),
|
||||||
)
|
)
|
||||||
|
|
||||||
fmt.Printf("Tuple length: %d\n", tuple.Len())
|
fmt.Printf("Tuple length: %d\n", tuple.Len())
|
||||||
|
|
||||||
var0 := tuple.At(0)
|
var0 := tuple.At(0)
|
||||||
fmt.Printf("Element 0: Name=%s, Type=%v\n", var0.Name(), var0.Type())
|
fmt.Printf("Element 0: Name=%s, Type=%v\n", var0.Name(), var0.Type())
|
||||||
|
|
||||||
fmt.Println("SUCCESS: Tuple operations work correctly\n")
|
fmt.Println("SUCCESS: Tuple operations work correctly\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
func testArray() {
|
func testArray() {
|
||||||
fmt.Println("=== Test Array ===")
|
fmt.Println("=== Test Array ===")
|
||||||
|
|
||||||
arrayType := types.NewArray(types.Typ[types.Int], 10)
|
arrayType := types.NewArray(types.Typ[types.Int], 10)
|
||||||
fmt.Printf("Array type: %v, Elem: %v, Len: %d\n", arrayType, arrayType.Elem(), arrayType.Len())
|
fmt.Printf("Array type: %v, Elem: %v, Len: %d\n", arrayType, arrayType.Elem(), arrayType.Len())
|
||||||
|
|
||||||
fmt.Println("SUCCESS: Array operations work correctly\n")
|
fmt.Println("SUCCESS: Array operations work correctly\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
func testSlice() {
|
func testSlice() {
|
||||||
fmt.Println("=== Test Slice ===")
|
fmt.Println("=== Test Slice ===")
|
||||||
|
|
||||||
sliceType := types.NewSlice(types.Typ[types.String])
|
sliceType := types.NewSlice(types.Typ[types.String])
|
||||||
fmt.Printf("Slice type: %v, Elem: %v\n", sliceType, sliceType.Elem())
|
fmt.Printf("Slice type: %v, Elem: %v\n", sliceType, sliceType.Elem())
|
||||||
|
|
||||||
fmt.Println("SUCCESS: Slice operations work correctly\n")
|
fmt.Println("SUCCESS: Slice operations work correctly\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
func testPointer() {
|
func testPointer() {
|
||||||
fmt.Println("=== Test Pointer ===")
|
fmt.Println("=== Test Pointer ===")
|
||||||
|
|
||||||
ptrType := types.NewPointer(types.Typ[types.Int])
|
ptrType := types.NewPointer(types.Typ[types.Int])
|
||||||
fmt.Printf("Pointer type: %v, Elem: %v\n", ptrType, ptrType.Elem())
|
fmt.Printf("Pointer type: %v, Elem: %v\n", ptrType, ptrType.Elem())
|
||||||
|
|
||||||
fmt.Println("SUCCESS: Pointer operations work correctly\n")
|
fmt.Println("SUCCESS: Pointer operations work correctly\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
func testMap() {
|
func testMap() {
|
||||||
fmt.Println("=== Test Map ===")
|
fmt.Println("=== Test Map ===")
|
||||||
|
|
||||||
mapType := types.NewMap(types.Typ[types.String], types.Typ[types.Int])
|
mapType := types.NewMap(types.Typ[types.String], types.Typ[types.Int])
|
||||||
fmt.Printf("Map type: %v, Key: %v, Elem: %v\n", mapType, mapType.Key(), mapType.Elem())
|
fmt.Printf("Map type: %v, Key: %v, Elem: %v\n", mapType, mapType.Key(), mapType.Elem())
|
||||||
|
|
||||||
fmt.Println("SUCCESS: Map operations work correctly\n")
|
fmt.Println("SUCCESS: Map operations work correctly\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
func testChan() {
|
func testChan() {
|
||||||
fmt.Println("=== Test Chan ===")
|
fmt.Println("=== Test Chan ===")
|
||||||
|
|
||||||
chanType := types.NewChan(types.SendRecv, types.Typ[types.Int])
|
chanType := types.NewChan(types.SendRecv, types.Typ[types.Int])
|
||||||
fmt.Printf("Chan type: %v, Dir: %v, Elem: %v\n", chanType, chanType.Dir(), chanType.Elem())
|
fmt.Printf("Chan type: %v, Dir: %v, Elem: %v\n", chanType, chanType.Dir(), chanType.Elem())
|
||||||
|
|
||||||
sendChan := types.NewChan(types.SendOnly, types.Typ[types.String])
|
sendChan := types.NewChan(types.SendOnly, types.Typ[types.String])
|
||||||
fmt.Printf("SendOnly chan: %v, Dir: %v\n", sendChan, sendChan.Dir())
|
fmt.Printf("SendOnly chan: %v, Dir: %v\n", sendChan, sendChan.Dir())
|
||||||
|
|
||||||
recvChan := types.NewChan(types.RecvOnly, types.Typ[types.Bool])
|
recvChan := types.NewChan(types.RecvOnly, types.Typ[types.Bool])
|
||||||
fmt.Printf("RecvOnly chan: %v, Dir: %v\n", recvChan, recvChan.Dir())
|
fmt.Printf("RecvOnly chan: %v, Dir: %v\n", recvChan, recvChan.Dir())
|
||||||
|
|
||||||
fmt.Println("SUCCESS: Chan operations work correctly\n")
|
fmt.Println("SUCCESS: Chan operations work correctly\n")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user