@@ -2303,7 +2303,10 @@ Nodes:
2303
2303
break Nodes
2304
2304
}
2305
2305
case * ast.AssignStmt :
2306
- return expectedAssignStmtCandidate (c , node , inf )
2306
+ objType , assignees := expectedAssignStmtTypes (c , node )
2307
+ inf .objType = objType
2308
+ inf .assignees = assignees
2309
+ return inf
2307
2310
case * ast.ValueSpec :
2308
2311
inf .objType = expectedValueSpecType (c , node )
2309
2312
return
@@ -2506,12 +2509,11 @@ func inferExpectedResultTypes(c *completer, callNodeIdx int) []types.Type {
2506
2509
expectedResults = append (expectedResults , expectedCompositeLiteralType (enclosingCompositeLiteral , c .pos ))
2507
2510
}
2508
2511
case * ast.AssignStmt :
2509
- inf := expectedAssignStmtCandidate (c , node , candidateInference {})
2510
- if len (inf .assignees ) > 0 {
2511
- expectedResults = make ([]types.Type , len (inf .assignees ))
2512
- copy (expectedResults , inf .assignees )
2513
- } else if inf .objType != nil {
2514
- expectedResults = append (expectedResults , inf .objType )
2512
+ objType , assignees := expectedAssignStmtTypes (c , node )
2513
+ if len (assignees ) > 0 {
2514
+ return assignees
2515
+ } else if objType != nil {
2516
+ expectedResults = append (expectedResults , objType )
2515
2517
}
2516
2518
case * ast.ValueSpec :
2517
2519
if resultType := expectedValueSpecType (c , node ); resultType != nil {
@@ -2576,34 +2578,33 @@ func expectedValueSpecType(c *completer, node *ast.ValueSpec) types.Type {
2576
2578
return nil
2577
2579
}
2578
2580
2579
- // expectedAssignStmtCandidate returns information about the expected candidate
2580
- // for a AssignStmt at the query position.
2581
- func expectedAssignStmtCandidate (c * completer , node * ast.AssignStmt , inf candidateInference ) candidateInference {
2581
+ // expectedAssignStmtTypes returns the inference objType and assignees for the assignment.
2582
+ func expectedAssignStmtTypes (c * completer , node * ast.AssignStmt ) (objType types.Type , assignees []types.Type ) {
2582
2583
// Only rank completions if you are on the right side of the token.
2583
2584
if c .pos > node .TokPos {
2584
2585
i := exprAtPos (c .pos , node .Rhs )
2585
2586
if i >= len (node .Lhs ) {
2586
2587
i = len (node .Lhs ) - 1
2587
2588
}
2588
2589
if tv , ok := c .pkg .TypesInfo ().Types [node .Lhs [i ]]; ok {
2589
- inf . objType = tv .Type
2590
+ objType = tv .Type
2590
2591
}
2591
2592
2592
2593
// If we have a single expression on the RHS, record the LHS
2593
2594
// assignees so we can favor multi-return function calls with
2594
2595
// matching result values.
2595
2596
if len (node .Rhs ) <= 1 {
2596
2597
for _ , lhs := range node .Lhs {
2597
- inf . assignees = append (inf . assignees , c .pkg .TypesInfo ().TypeOf (lhs ))
2598
+ assignees = append (assignees , c .pkg .TypesInfo ().TypeOf (lhs ))
2598
2599
}
2599
2600
} else {
2600
2601
// Otherwise, record our single assignee, even if its type is
2601
2602
// not available. We use this info to downrank functions
2602
2603
// with the wrong number of result values.
2603
- inf . assignees = append (inf . assignees , c .pkg .TypesInfo ().TypeOf (node .Lhs [i ]))
2604
+ assignees = append (assignees , c .pkg .TypesInfo ().TypeOf (node .Lhs [i ]))
2604
2605
}
2605
2606
}
2606
- return inf
2607
+ return objType , assignees
2607
2608
}
2608
2609
2609
2610
// expectedReturnStmtType returns the expected type of a return statement.
0 commit comments