Skip to content

Commit bd7b193

Browse files
griesemergopherbot
authored andcommitted
go/types, types2: adjust Checker.recordCommaOkTypes signature
By changing the signature to accept a slice rather than an array, we can avoid creating the array in the first place. Functionally, we now also record comma-ok types if the corresponding assignment was incorrect. But this change provides more (not less) information through the API and only so if the program is incorrect in the first place. Change-Id: I0d629441f2f890a37912171fb26ef0e75827ce23 Reviewed-on: https://go-review.googlesource.com/c/go/+/478218 Auto-Submit: Robert Griesemer <[email protected]> Run-TryBot: Robert Griesemer <[email protected]> Reviewed-by: Robert Findley <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 0ed364a commit bd7b193

File tree

4 files changed

+33
-32
lines changed

4 files changed

+33
-32
lines changed

src/cmd/compile/internal/types2/assignments.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -373,11 +373,9 @@ func (check *Checker) initVars(lhs []*Var, orig_rhs []syntax.Expr, returnStmt sy
373373
}
374374

375375
if commaOk {
376-
var a [2]Type
377-
for i := range a {
378-
a[i] = check.initVar(lhs[i], rhs[i], context)
379-
}
380-
check.recordCommaOkTypes(orig_rhs[0], a)
376+
check.initVar(lhs[0], rhs[0], context)
377+
check.initVar(lhs[1], rhs[1], context)
378+
check.recordCommaOkTypes(orig_rhs[0], rhs)
381379
return
382380
}
383381

@@ -412,11 +410,9 @@ func (check *Checker) assignVars(lhs, orig_rhs []syntax.Expr) {
412410
}
413411

414412
if commaOk {
415-
var a [2]Type
416-
for i := range a {
417-
a[i] = check.assignVar(lhs[i], rhs[i])
418-
}
419-
check.recordCommaOkTypes(orig_rhs[0], a)
413+
check.assignVar(lhs[0], rhs[0])
414+
check.assignVar(lhs[1], rhs[1])
415+
check.recordCommaOkTypes(orig_rhs[0], rhs)
420416
return
421417
}
422418

src/cmd/compile/internal/types2/check.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -504,22 +504,27 @@ func (check *Checker) recordBuiltinType(f syntax.Expr, sig *Signature) {
504504
}
505505
}
506506

507-
func (check *Checker) recordCommaOkTypes(x syntax.Expr, a [2]Type) {
507+
// recordCommaOkTypes updates recorded types to reflect that x is used in a commaOk context
508+
// (and therefore has tuple type).
509+
func (check *Checker) recordCommaOkTypes(x syntax.Expr, a []*operand) {
508510
assert(x != nil)
509-
if a[0] == nil || a[1] == nil {
511+
assert(len(a) == 2)
512+
if a[0].mode == invalid {
510513
return
511514
}
512-
assert(isTyped(a[0]) && isTyped(a[1]) && (isBoolean(a[1]) || a[1] == universeError))
515+
t0, t1 := a[0].typ, a[1].typ
516+
assert(isTyped(t0) && isTyped(t1) && (isBoolean(t1) || t1 == universeError))
513517
if m := check.Types; m != nil {
514518
for {
515519
tv := m[x]
516520
assert(tv.Type != nil) // should have been recorded already
517521
pos := x.Pos()
518522
tv.Type = NewTuple(
519-
NewVar(pos, check.pkg, "", a[0]),
520-
NewVar(pos, check.pkg, "", a[1]),
523+
NewVar(pos, check.pkg, "", t0),
524+
NewVar(pos, check.pkg, "", t1),
521525
)
522526
m[x] = tv
527+
// if x is a parenthesized expression (p.X), update p.X
523528
p, _ := x.(*syntax.ParenExpr)
524529
if p == nil {
525530
break
@@ -535,8 +540,8 @@ func (check *Checker) recordCommaOkTypes(x syntax.Expr, a [2]Type) {
535540
assert(tv.Type != nil) // should have been recorded already
536541
pos := x.Pos()
537542
tv.Type = NewTuple(
538-
NewVar(pos, check.pkg, "", a[0]),
539-
NewVar(pos, check.pkg, "", a[1]),
543+
NewVar(pos, check.pkg, "", t0),
544+
NewVar(pos, check.pkg, "", t1),
540545
)
541546
x.SetTypeInfo(tv)
542547
p, _ := x.(*syntax.ParenExpr)

src/go/types/assignments.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -365,11 +365,9 @@ func (check *Checker) initVars(lhs []*Var, origRHS []ast.Expr, returnStmt ast.St
365365
}
366366

367367
if commaOk {
368-
var a [2]Type
369-
for i := range a {
370-
a[i] = check.initVar(lhs[i], rhs[i], context)
371-
}
372-
check.recordCommaOkTypes(origRHS[0], a)
368+
check.initVar(lhs[0], rhs[0], context)
369+
check.initVar(lhs[1], rhs[1], context)
370+
check.recordCommaOkTypes(origRHS[0], rhs)
373371
return
374372
}
375373

@@ -394,11 +392,9 @@ func (check *Checker) assignVars(lhs, origRHS []ast.Expr) {
394392
}
395393

396394
if commaOk {
397-
var a [2]Type
398-
for i := range a {
399-
a[i] = check.assignVar(lhs[i], rhs[i])
400-
}
401-
check.recordCommaOkTypes(origRHS[0], a)
395+
check.assignVar(lhs[0], rhs[0])
396+
check.assignVar(lhs[1], rhs[1])
397+
check.recordCommaOkTypes(origRHS[0], rhs)
402398
return
403399
}
404400

src/go/types/check.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -478,20 +478,24 @@ func (check *Checker) recordBuiltinType(f ast.Expr, sig *Signature) {
478478
}
479479
}
480480

481-
func (check *Checker) recordCommaOkTypes(x ast.Expr, a [2]Type) {
481+
// recordCommaOkTypes updates recorded types to reflect that x is used in a commaOk context
482+
// (and therefore has tuple type).
483+
func (check *Checker) recordCommaOkTypes(x ast.Expr, a []*operand) {
482484
assert(x != nil)
483-
if a[0] == nil || a[1] == nil {
485+
assert(len(a) == 2)
486+
if a[0].mode == invalid {
484487
return
485488
}
486-
assert(isTyped(a[0]) && isTyped(a[1]) && (isBoolean(a[1]) || a[1] == universeError))
489+
t0, t1 := a[0].typ, a[1].typ
490+
assert(isTyped(t0) && isTyped(t1) && (isBoolean(t1) || t1 == universeError))
487491
if m := check.Types; m != nil {
488492
for {
489493
tv := m[x]
490494
assert(tv.Type != nil) // should have been recorded already
491495
pos := x.Pos()
492496
tv.Type = NewTuple(
493-
NewVar(pos, check.pkg, "", a[0]),
494-
NewVar(pos, check.pkg, "", a[1]),
497+
NewVar(pos, check.pkg, "", t0),
498+
NewVar(pos, check.pkg, "", t1),
495499
)
496500
m[x] = tv
497501
// if x is a parenthesized expression (p.X), update p.X

0 commit comments

Comments
 (0)