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

analyze_types: better support for type aliases #23

Open
pohly opened this issue Jan 29, 2023 · 1 comment
Open

analyze_types: better support for type aliases #23

pohly opened this issue Jan 29, 2023 · 1 comment

Comments

@pohly
Copy link
Contributor

pohly commented Jan 29, 2023

The recently merged semantic analysis has one drawback when dealing with type aliases: they always get replaced with the underlying type. Example:

package somepkg

import "example.com/anotherpkg"

type SomeType = anotherpkg.AnotherType

A pattern somepkg.SomeType.ForbiddenMethod does not match because SomeType is an alias. What works is anotherpkg.AnotherType.ForbiddenMethod.

This is a) unexpected and b) can cause a pattern that works with a version of a package where the type is not an alias to stop working when the packages switches to a type alias.

It would be better if forbidigo matched against both the underlying type (anotherpkg.AnotherType) and the alias (somepkg.SomeType).

@pohly
Copy link
Contributor Author

pohly commented Jan 29, 2023

The problem is that in many cases, we get the underlying type from static code analysis:

// If we are lucky, the entire selector expression has a known
// type. We don't care about the value.
selectorText := v.textFor(node)
if typeAndValue, ok := v.runConfig.TypesInfo.Types[selector]; ok {
m, p, ok := pkgFromType(typeAndValue.Type)
if !ok {
v.runConfig.DebugLog("%s: selector %q with supported type %T", location, selectorText, typeAndValue.Type)
}
matchText = m + "." + field
pkgText = p
v.runConfig.DebugLog("%s: selector %q with supported type %q: %q -> %q, package %q", location, selectorText, typeAndValue.Type.String(), srcText, matchText, pkgText)
return []string{matchText}, pkgText
}

I'm not sure whether it is possible to detect that the type was reached through an alias.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant