Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid panic when embedding a builtin alias #806

Merged
merged 1 commit into from
Nov 15, 2023
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
21 changes: 13 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1357,7 +1357,8 @@ type (
objectString = string // as per recordedObjectString

typeName struct {
PkgPath, Name string
PkgPath string // empty if builtin
Name string
}
)

Expand Down Expand Up @@ -1532,8 +1533,10 @@ func computePkgCache(fsCache *cache.Cache, lpkg *listedPackage, pkg *types.Packa
continue
}
aliasTypeName := typeName{
PkgPath: obj.Pkg().Path(),
Name: obj.Name(),
Name: obj.Name(),
}
if pkg := obj.Pkg(); pkg != nil {
aliasTypeName.PkgPath = pkg.Path()
}
computed.EmbeddedAliasFields[vrStr] = aliasTypeName
}
Expand Down Expand Up @@ -1894,19 +1897,21 @@ func (tf *transformer) transformGoFile(file *ast.File) *ast.File {
vrStr := recordedObjectString(vr)
aliasTypeName, ok := tf.curPkgCache.EmbeddedAliasFields[vrStr]
if ok {
pkg2 := tf.pkg
if path := aliasTypeName.PkgPath; pkg2.Path() != path {
aliasScope := tf.pkg.Scope()
if path := aliasTypeName.PkgPath; path == "" {
aliasScope = types.Universe
} else if path != tf.pkg.Path() {
// If the package is a dependency, import it.
// We can't grab the package via tf.pkg.Imports,
// because some of the packages under there are incomplete.
// ImportFrom will cache complete imports, anyway.
var err error
pkg2, err = tf.origImporter.ImportFrom(path, parentWorkDir, 0)
pkg2, err := tf.origImporter.ImportFrom(path, parentWorkDir, 0)
if err != nil {
panic(err)
}
aliasScope = pkg2.Scope()
}
tname, ok := pkg2.Scope().Lookup(aliasTypeName.Name).(*types.TypeName)
tname, ok := aliasScope.Lookup(aliasTypeName.Name).(*types.TypeName)
if !ok {
panic(fmt.Sprintf("EmbeddedAliasFields pointed %q to a missing type %q", vrStr, aliasTypeName))
}
Expand Down
7 changes: 7 additions & 0 deletions testdata/script/syntax.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,13 @@ var _ = embeddingAliasSameName{
Reader: nil,
}

type embeddingBuiltinAlias struct {
byte
}

var _ = embeddingBuiltinAlias{3}
var _ = embeddingBuiltinAlias{byte: 3}

-- external/external.go --
package external

Expand Down