Repository: goplus/gop
Pull request #1483 (2023-10-21 14:30:11)

externType: rec.Use


Patch:
diff --git a/cl/compile.go b/cl/compile.go
index 2adcfc07..38e9aff0 100644
--- a/cl/compile.go
+++ b/cl/compile.go
@@ -324,7 +324,7 @@ func getTypeLoader(ctx *pkgCtx, syms map[string]loader, start token.Pos, name st
 			} else {
 				ctx.handleErrorf(
 					start, "%s redeclared in this block\n\tprevious declaration at %v",
-					name, ctx.Position(ld.start))
+					name, ctx.Position(ld.pos()))
 			}
 			return ld
 		}
@@ -905,7 +905,7 @@ func preloadFile(p *gox.Package, ctx *blockCtx, file string, f *ast.File, gopFil
 								if debugLoad {
 									log.Println("==> Load > AliasType", name)
 								}
-								ctx.pkg.AliasType(name, toType(ctx, t.Type), t.Pos())
+								defs.AliasType(name, toType(ctx, t.Type), t.Pos())
 								return
 							}
 							if debugLoad {
@@ -1145,18 +1145,20 @@ func loadImport(ctx *blockCtx, spec *ast.ImportSpec) {
 		pkg = ctx.pkg.Import(simplifyGopPackage(pkgPath), spec)
 	}
 	var name string
+	var pos token.Pos
+	var specName = spec.Name
 	if rec := ctx.recorder(); rec != nil {
 		defer func() {
-			pkgName := types.NewPkgName(token.NoPos, ctx.pkg.Types, name, pkg.Types)
-			if spec.Name != nil {
-				rec.Def(spec.Name, pkgName)
+			pkgName := types.NewPkgName(pos, ctx.pkg.Types, name, pkg.Types)
+			if specName != nil {
+				rec.Def(specName, pkgName)
 			} else {
 				rec.Implicit(spec, pkgName)
 			}
 		}()
 	}
-	if spec.Name != nil {
-		name = spec.Name.Name
+	if specName != nil {
+		name, pos = specName.Name, specName.NamePos
 		if name == "." {
 			ctx.lookups = append(ctx.lookups, pkg)
 			return
@@ -1166,7 +1168,7 @@ func loadImport(ctx *blockCtx, spec *ast.ImportSpec) {
 			return
 		}
 	} else {
-		name = pkg.Types.Name()
+		name, pos = pkg.Types.Name(), spec.Path.Pos()
 	}
 	ctx.imports[name] = pkg
 }
diff --git a/cl/expr.go b/cl/expr.go
index c23d4c9a..4c918fa3 100644
--- a/cl/expr.go
+++ b/cl/expr.go
@@ -121,7 +121,7 @@ func compileIdent(ctx *blockCtx, ident *ast.Ident, flags int) (obj *gox.PkgRef,
 		}
 		if pr, ok := ctx.findImport(name); ok {
 			if rec := ctx.recorder(); rec != nil {
-				pkgName := types.NewPkgName(token.NoPos, ctx.pkg.Types, name, pr.Types)
+				pkgName := types.NewPkgName(ident.NamePos, ctx.pkg.Types, name, pr.Types)
 				rec.Use(ident, pkgName)
 			}
 			return pr, objPkgRef
diff --git a/cl/func_type_and_var.go b/cl/func_type_and_var.go
index 232ffe08..c81dd25e 100644
--- a/cl/func_type_and_var.go
+++ b/cl/func_type_and_var.go
@@ -192,10 +192,19 @@ func toChanType(ctx *blockCtx, v *ast.ChanType) *types.Chan {
 }
 
 func toExternalType(ctx *blockCtx, v *ast.SelectorExpr) types.Type {
-	name := v.X.(*ast.Ident).Name
+	id := v.X.(*ast.Ident)
+	name := id.Name
 	if pr, ok := ctx.findImport(name); ok {
+		rec := ctx.recorder()
+		if rec != nil {
+			pkgName := types.NewPkgName(id.NamePos, ctx.pkg.Types, name, pr.Types)
+			rec.Use(id, pkgName)
+		}
 		o := pr.TryRef(v.Sel.Name)
 		if t, ok := o.(*types.TypeName); ok {
+			if rec != nil {
+				rec.Use(v.Sel, t)
+			}
 			return t.Type()
 		}
 		ctx.handleErrorf(v.Pos(), "%s.%s is not a type", name, v.Sel.Name)


Test patch:
diff --git a/cl/compile_test.go b/cl/compile_test.go
index 37cf5786..eceb1c45 100644
--- a/cl/compile_test.go
+++ b/cl/compile_test.go
@@ -2206,12 +2206,12 @@ func main() {
 }
 `, `package main
 
+type bar = foo
 type foo struct {
 	p *foo
 	A int
 	B string `+"`tag1:123`"+`
 }
-type bar = foo
 
 func main() {
 	type a struct {