Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
yuxincs committed Oct 10, 2024
1 parent 9766ed2 commit 3fcaa0b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
26 changes: 26 additions & 0 deletions assertion/function/assertiontree/backprop.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,34 @@ func backpropAcrossRange(rootNode *RootAssertionNode, lhs []ast.Expr, rhs ast.Ex
}
}

// produceNonNil marks the ith lhs expression as nonnil due to limitations of NilAway.
produceNonNil := func(i int) {
if !util.IsEmptyExpr(lhs[i]) {
rootNode.AddProduction(&annotation.ProduceTrigger{
Annotation: &annotation.ProduceTriggerNever{},
Expr: lhs[i],
})
}
}

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

// Go 1.23 introduced the `iter` package, which provides a way to iterate over sequences
// in a generic way. The `iter.Seq` and `iter.Seq2` types are used to represent sequences
// and are used in the `range` statement. We currently do not handle these types yet, so
// here we assume that they are deeply non-nil (by adding nonnil producers).
// TODO: handle that (#287).
if named, ok := rhsType.(*types.Named); ok && named.Obj() != nil && named.Obj().Pkg() != nil && named.Obj().Pkg().Path() == "iter" {
if named.Obj().Name() == "Seq" {
produceNonNil(0)
return nil
} else if named.Obj().Name() == "Seq2" {
produceNonNil(0)
produceNonNil(1)
return nil
}
}

// This block breaks down the cases for the `range` statement being analyzed,
// starting by switching on how many left-hand operands there are
switch len(lhs) {
Expand Down
16 changes: 16 additions & 0 deletions testdata/src/go.uber.org/looprange/looprange.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
// <nilaway no inference>
package looprange

import (
"maps"
"slices"
)

func dummyConsume(interface{}) {}
func dummyBool() bool { return true }

Expand Down Expand Up @@ -207,3 +212,14 @@ func testAlias(s MyAlias) {
print(myStr)
}
}

func testIter() {
i := 42
for element := range slices.Values([]*int{&i, &i, nil}) {

Check failure on line 218 in testdata/src/go.uber.org/looprange/looprange.go

View workflow job for this annotation

GitHub Actions / Test (1.22.x)

undefined: slices.Values
print(*element) // FN: we do not really handle iterators for now, the elements from iterators are assumed to be nonnil.
}
for k, v := range maps.All(map[string]*int{"abc": &i, "def": nil}) {

Check failure on line 221 in testdata/src/go.uber.org/looprange/looprange.go

View workflow job for this annotation

GitHub Actions / Test (1.22.x)

undefined: maps.All
print(k)
print(*v) // FN: we do not really handle iterators for now, the elements from iterators are assumed to be nonnil.
}
}

0 comments on commit 3fcaa0b

Please sign in to comment.