From 9690a832b8b7263d2bbcab886e0f5517cd498338 Mon Sep 17 00:00:00 2001 From: Yuxin Wang Date: Mon, 16 Sep 2024 11:47:19 -0400 Subject: [PATCH 1/3] Update --- assertion/function/assertiontree/backprop.go | 2 +- nilaway_test.go | 7 +++++++ testdata/src/go.uber.org/looprange/looprange.go | 11 +++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/assertion/function/assertiontree/backprop.go b/assertion/function/assertiontree/backprop.go index 9460fe56..deea995d 100644 --- a/assertion/function/assertiontree/backprop.go +++ b/assertion/function/assertiontree/backprop.go @@ -410,7 +410,7 @@ func backpropAcrossRange(rootNode *RootAssertionNode, lhs []ast.Expr, rhs ast.Ex } } - rhsType := rootNode.Pass().TypesInfo.Types[rhs].Type + rhsType := types.Unalias(rootNode.Pass().TypesInfo.Types[rhs].Type) // This block breaks down the cases for the `range` statement being analyzed, // starting by switching on how many left-hand operands there are diff --git a/nilaway_test.go b/nilaway_test.go index 7b5c7e5c..fa1f8f86 100644 --- a/nilaway_test.go +++ b/nilaway_test.go @@ -12,6 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. +// Go 1.22 [1] introduces a proper `types.Alias` type for type aliases. The current default is +// disabling such a feature. However, Go official doc suggests that it will be enabled in future Go +// releases. Therefore, here we explicitly set this to `1` to enable the feature to test NilAway's +// ability to handle it. +// [1]: https://tip.golang.org/doc/go1.22 +//go:debug gotypesalias=1 + package nilaway import ( diff --git a/testdata/src/go.uber.org/looprange/looprange.go b/testdata/src/go.uber.org/looprange/looprange.go index c94a744a..b7c00132 100644 --- a/testdata/src/go.uber.org/looprange/looprange.go +++ b/testdata/src/go.uber.org/looprange/looprange.go @@ -196,3 +196,14 @@ func testRangeOverBasicTypes(j int) { } } } + +type Set map[string]bool + +type MyAlias = Set + +//nilable(s) +func testAlias(s MyAlias) { + for myStr := range s { + print(myStr) + } +} \ No newline at end of file From a10c2ec690a7096afbf58a4c6c18d996c09a66b2 Mon Sep 17 00:00:00 2001 From: Yuxin Wang Date: Fri, 4 Oct 2024 17:07:15 -0400 Subject: [PATCH 2/3] Update --- annotation/helper_test.go | 4 ++-- util/util.go | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/annotation/helper_test.go b/annotation/helper_test.go index 7e1bde53..64f2dd17 100644 --- a/annotation/helper_test.go +++ b/annotation/helper_test.go @@ -139,7 +139,7 @@ func collectMethods(t *types.Named, visitedMethods map[string]*types.Func, visit for i := 0; i < s.NumFields(); i++ { f := s.Field(i) if f.Embedded() { - if n, ok := util.UnwrapPtr(f.Type()).(*types.Named); ok { + if n, ok := util.UnwrapPtr(types.Unalias(f.Type())).(*types.Named); ok { collectMethods(n, visitedMethods, visitedStructs) } } @@ -197,7 +197,7 @@ func structsImplementingInterface(interfaceName string, packageName ...string) m if sObj == nil { return true } - sType, ok := sObj.Type().(*types.Named) + sType, ok := types.Unalias(sObj.Type()).(*types.Named) if !ok { return true } diff --git a/util/util.go b/util/util.go index 5d45735f..84b199fc 100644 --- a/util/util.go +++ b/util/util.go @@ -145,7 +145,7 @@ func TypeAsDeeplyStruct(typ types.Type) *types.Struct { } if ptType, ok := typ.(*types.Pointer); ok { - if namedType, ok := ptType.Elem().(*types.Named); ok { + if namedType, ok := types.Unalias(ptType.Elem()).(*types.Named); ok { if resType, ok := namedType.Underlying().(*types.Struct); ok { return resType } @@ -262,7 +262,7 @@ func TypeBarsNilness(t types.Type) bool { return false case *types.Chan: return false - case *types.Named: + case *types.Alias, *types.Named: return TypeBarsNilness(t.Underlying()) case *types.Interface: return false @@ -314,11 +314,11 @@ func funcIsRichCheckEffectReturning(fdecl *types.Func, expectedType types.Type) if n == 0 { return false } - if results.At(n-1).Type() != expectedType { + if !types.Identical(results.At(n-1).Type(), expectedType) { return false } for i := 0; i < n-1; i++ { - if results.At(i).Type() == expectedType { + if types.Identical(results.At(i).Type(), expectedType) { return false } } From 9766ed2860e20c93d4b26e1849252ccf5aba36e5 Mon Sep 17 00:00:00 2001 From: Yuxin Wang Date: Mon, 7 Oct 2024 22:07:48 -0400 Subject: [PATCH 3/3] Update --- testdata/src/go.uber.org/looprange/looprange.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testdata/src/go.uber.org/looprange/looprange.go b/testdata/src/go.uber.org/looprange/looprange.go index b7c00132..ddd22835 100644 --- a/testdata/src/go.uber.org/looprange/looprange.go +++ b/testdata/src/go.uber.org/looprange/looprange.go @@ -206,4 +206,4 @@ func testAlias(s MyAlias) { for myStr := range s { print(myStr) } -} \ No newline at end of file +}