Skip to content

Commit f74befd

Browse files
authored
Handle newly-introduced types.Alias type (#286)
Go 1.22 [1] introduces a proper `types.Alias` type for type aliases. This PR adds handling (by simply unwrapping alias node - effectively restoring previous behavior) for such a type. Further simplification and refactors will be in future PRs.
1 parent 045b340 commit f74befd

File tree

5 files changed

+25
-7
lines changed

5 files changed

+25
-7
lines changed

annotation/helper_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func collectMethods(t *types.Named, visitedMethods map[string]*types.Func, visit
139139
for i := 0; i < s.NumFields(); i++ {
140140
f := s.Field(i)
141141
if f.Embedded() {
142-
if n, ok := util.UnwrapPtr(f.Type()).(*types.Named); ok {
142+
if n, ok := util.UnwrapPtr(types.Unalias(f.Type())).(*types.Named); ok {
143143
collectMethods(n, visitedMethods, visitedStructs)
144144
}
145145
}
@@ -197,7 +197,7 @@ func structsImplementingInterface(interfaceName string, packageName ...string) m
197197
if sObj == nil {
198198
return true
199199
}
200-
sType, ok := sObj.Type().(*types.Named)
200+
sType, ok := types.Unalias(sObj.Type()).(*types.Named)
201201
if !ok {
202202
return true
203203
}

assertion/function/assertiontree/backprop.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ func backpropAcrossRange(rootNode *RootAssertionNode, lhs []ast.Expr, rhs ast.Ex
410410
}
411411
}
412412

413-
rhsType := rootNode.Pass().TypesInfo.Types[rhs].Type
413+
rhsType := types.Unalias(rootNode.Pass().TypesInfo.Types[rhs].Type)
414414

415415
// This block breaks down the cases for the `range` statement being analyzed,
416416
// starting by switching on how many left-hand operands there are

nilaway_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
// Go 1.22 [1] introduces a proper `types.Alias` type for type aliases. The current default is
16+
// disabling such a feature. However, Go official doc suggests that it will be enabled in future Go
17+
// releases. Therefore, here we explicitly set this to `1` to enable the feature to test NilAway's
18+
// ability to handle it.
19+
// [1]: https://tip.golang.org/doc/go1.22
20+
//go:debug gotypesalias=1
21+
1522
package nilaway
1623

1724
import (

testdata/src/go.uber.org/looprange/looprange.go

+11
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,14 @@ func testRangeOverBasicTypes(j int) {
196196
}
197197
}
198198
}
199+
200+
type Set map[string]bool
201+
202+
type MyAlias = Set
203+
204+
//nilable(s)
205+
func testAlias(s MyAlias) {
206+
for myStr := range s {
207+
print(myStr)
208+
}
209+
}

util/util.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func TypeAsDeeplyStruct(typ types.Type) *types.Struct {
145145
}
146146

147147
if ptType, ok := typ.(*types.Pointer); ok {
148-
if namedType, ok := ptType.Elem().(*types.Named); ok {
148+
if namedType, ok := types.Unalias(ptType.Elem()).(*types.Named); ok {
149149
if resType, ok := namedType.Underlying().(*types.Struct); ok {
150150
return resType
151151
}
@@ -262,7 +262,7 @@ func TypeBarsNilness(t types.Type) bool {
262262
return false
263263
case *types.Chan:
264264
return false
265-
case *types.Named:
265+
case *types.Alias, *types.Named:
266266
return TypeBarsNilness(t.Underlying())
267267
case *types.Interface:
268268
return false
@@ -314,11 +314,11 @@ func funcIsRichCheckEffectReturning(fdecl *types.Func, expectedType types.Type)
314314
if n == 0 {
315315
return false
316316
}
317-
if results.At(n-1).Type() != expectedType {
317+
if !types.Identical(results.At(n-1).Type(), expectedType) {
318318
return false
319319
}
320320
for i := 0; i < n-1; i++ {
321-
if results.At(i).Type() == expectedType {
321+
if types.Identical(results.At(i).Type(), expectedType) {
322322
return false
323323
}
324324
}

0 commit comments

Comments
 (0)