conf := Config{GoVersion: "go1.17"}
mustTypecheck(src, &conf, nil)
}
+
+func TestIssue68334(t *testing.T) {
+ const src = `
+package p
+
+func f(x int) {
+ for i, j := range x {
+ _, _ = i, j
+ }
+ var a, b int
+ for a, b = range x {
+ _, _ = a, b
+ }
+}
+`
+
+ got := ""
+ conf := Config{
+ GoVersion: "go1.21", // #68334 requires GoVersion <= 1.21
+ Error: func(err error) { got += err.Error() + "\n" }, // #68334 requires Error != nil
+ }
+ typecheck(src, &conf, nil) // do not crash
+
+ want := "p:5:20: cannot range over x (variable of type int): requires go1.22 or later\n" +
+ "p:9:19: cannot range over x (variable of type int): requires go1.22 or later\n"
+ if got != want {
+ t.Errorf("got: %s want: %s", got, want)
+ }
+}
// initialize lhs iteration variable, if any
typ := rhs[i]
- if typ == nil {
+ if typ == nil || typ == Typ[Invalid] {
+ // typ == Typ[Invalid] can happen if allowVersion fails.
obj.typ = Typ[Invalid]
obj.used = true // don't complain about unused variable
continue
}
if rangeOverInt {
- assert(i == 0) // at most one iteration variable (rhs[1] == nil for rangeOverInt)
+ assert(i == 0) // at most one iteration variable (rhs[1] == nil or Typ[Invalid] for rangeOverInt)
check.initVar(obj, &x, "range clause")
} else {
var y operand
// assign to lhs iteration variable, if any
typ := rhs[i]
- if typ == nil {
+ if typ == nil || typ == Typ[Invalid] {
continue
}
if rangeOverInt {
- assert(i == 0) // at most one iteration variable (rhs[1] == nil for rangeOverInt)
+ assert(i == 0) // at most one iteration variable (rhs[1] == nil or Typ[Invalid] for rangeOverInt)
check.assignVar(lhs, nil, &x, "range clause")
// If the assignment succeeded, if x was untyped before, it now
// has a type inferred via the assignment. It must be an integer.
conf := Config{GoVersion: "go1.17"}
mustTypecheck(src, &conf, nil)
}
+
+func TestIssue68334(t *testing.T) {
+ const src = `
+package p
+
+func f(x int) {
+ for i, j := range x {
+ _, _ = i, j
+ }
+ var a, b int
+ for a, b = range x {
+ _, _ = a, b
+ }
+}
+`
+
+ got := ""
+ conf := Config{
+ GoVersion: "go1.21", // #68334 requires GoVersion <= 1.21
+ Error: func(err error) { got += err.Error() + "\n" }, // #68334 requires Error != nil
+ }
+ typecheck(src, &conf, nil) // do not crash
+
+ want := "p:5:20: cannot range over x (variable of type int): requires go1.22 or later\n" +
+ "p:9:19: cannot range over x (variable of type int): requires go1.22 or later\n"
+ if got != want {
+ t.Errorf("got: %s want: %s", got, want)
+ }
+}
// initialize lhs iteration variable, if any
typ := rhs[i]
- if typ == nil {
+ if typ == nil || typ == Typ[Invalid] {
+ // typ == Typ[Invalid] can happen if allowVersion fails.
obj.typ = Typ[Invalid]
obj.used = true // don't complain about unused variable
continue
}
if rangeOverInt {
- assert(i == 0) // at most one iteration variable (rhs[1] == nil for rangeOverInt)
+ assert(i == 0) // at most one iteration variable (rhs[1] == nil or Typ[Invalid] for rangeOverInt)
check.initVar(obj, &x, "range clause")
} else {
var y operand
// assign to lhs iteration variable, if any
typ := rhs[i]
- if typ == nil {
+ if typ == nil || typ == Typ[Invalid] {
continue
}
if rangeOverInt {
- assert(i == 0) // at most one iteration variable (rhs[1] == nil for rangeOverInt)
+ assert(i == 0) // at most one iteration variable (rhs[1] == nil or Typ[Invalid] for rangeOverInt)
check.assignVar(lhs, nil, &x, "range clause")
// If the assignment succeeded, if x was untyped before, it now
// has a type inferred via the assignment. It must be an integer.