Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CodeCompletion] Rework getOperatorCompletions() #20632

Merged
merged 5 commits into from
Nov 22, 2018

Conversation

rintaro
Copy link
Member

@rintaro rintaro commented Nov 16, 2018

In postfix completion, for operator completion, we do:

  1. Type check the operand without applying it, but set the resolved type to the root of the expression.
  2. For each possible operators:
    1. Build temporary binary/postfix expression
    2. Perform type checking to see whether the operator is applicable

This could be very slow especially if the operand is complex.

  • Introduce ReusePrecheckedType option to constraint system. With this option, CSGen respects pre-stored types in expressions and doesn't take its sub-expressions into account.

    • Improve type checking performance because type variables aren't generated for sub-expressions of LHS (45511835)
    • Guarantee that the operand is not modified by the type checker because expression walkers in CSGen doesn't walk into the operand.
  • Introduce TypeChecker::findLHS() to find LHS for a infix operator from pre-folded expression. We used to foldSequence() temporary SequenceExpr and find 'CodeCompletionExpr' for each attempt.

    • No need to flatten folded expression after initial type-checking.
    • Save memory of temporary BinaryExpr which used to be allocated by foldSequence().
    • Improve accuracy of the completion. foldSequence() recovers invalid combination of operators by left associative manner (with diagnostics). This used to cause false-positive results. For instance, a == b <HERE> used to suggest == operator. findLHS() returns nullptr for such invalid combination.

rdar://problem/45511835
https://bugs.swift.org/browse/SR-9061

@rintaro rintaro requested review from xedin and benlangmuir November 16, 2018 10:37
@rintaro
Copy link
Member Author

rintaro commented Nov 16, 2018

@swift-ci Please smoke test

Copy link
Contributor

@xedin xedin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! But I wonder if it's possible to avoid dummy expression, instead generate constraints only for Apply + RHS and just cache types of the left-hand side in the constraint system (because that sub-tree is already type-checked), since you are already using solve that doesn't call ExprRewriter this might be simpler solution for the problem at hand.

* Introduce `FixedTypeExpr` as the LHS for type checking attempt.
  * Guarantees that the actual LHS is not modified by the type checker.
  * Improves type checking performance because it doesn't have
    any sub-expressions. (45511835)

* Introduce `TypeChecker::findLHS()` to find LHS for a infix operator
  from pre-folded expression. We used to `foldSequence()` temporary
  `SequenceExpr` and find 'CodeCompletionExpr' for each attempt.
  * No need to flatten folded expression after initial type-checking.
  * Save memory of temporary `BinaryExpr` which used to be allocated by
    `foldSequence()`.
  * Improve accuracy of the completion. `foldSequence()` recovers invalid
    combination of operators by `left` associative manner (with
    diagnostics). This used to cause false-positive results. For instance,
    `1 == 1 <HERE>` used to suggest `==` operator. `findLHS()` returns
    `nullptr` for such invalid combination.

rdar://problem/45511835
https://bugs.swift.org/browse/SR-9061
…nOperator

There's no reason to pack it into 'Type'.
@rintaro rintaro force-pushed the ide-codecompletion-sequence2 branch from b7ca9e0 to 1a936f9 Compare November 19, 2018 04:35
@rintaro
Copy link
Member Author

rintaro commented Nov 19, 2018

@xedin Thanks! I updated PR. Added constraint system flag ReusePrecheckedType to reuse LHS type and not to take its subexpressions into account. What do you think?

@rintaro
Copy link
Member Author

rintaro commented Nov 19, 2018

@swift-ci Please smoke test

Copy link
Contributor

@xedin xedin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think new approach makes sense! One more thing - I think it would be fair to assert that expressions don't have types with type variables in them, because solver isn't supposed to set types like that to AST.

@rintaro
Copy link
Member Author

rintaro commented Nov 20, 2018

@xedin Thanks!

it would be fair to assert that expressions don't have types with type variables in them

Sorry to ask a dumb question but what is "them" do you mean? input LHS?

@xedin
Copy link
Contributor

xedin commented Nov 20, 2018

Ah no problem! I mean expressions should always either have a valid type (including error) or unresolved type, because CSGen never sets types on expressions directly anymore, but rather uses a separate cache associated with each constraint system to hold intermisiate types with type variables.

@rintaro
Copy link
Member Author

rintaro commented Nov 21, 2018

Thanks for the explanation. I added assertion in CSGen.
@swift-ci Please smoke test

@rintaro rintaro merged commit 5df5711 into swiftlang:master Nov 22, 2018
rintaro added a commit to rintaro/swift that referenced this pull request Nov 27, 2018
In postfix completion, for operator completion, we do:

  1. Type check the operand without applying it, but set the resolved
     type to the root of the expression.
  2. For each possible operators:
      i. Build temporary binary/postfix expression
      ii. Perform type checking to see whether the operator is applicable 

This could be very slow especially if the operand is complex.

* Introduce `ReusePrecheckedType` option to constraint system. With
  this option, CSGen respects pre-stored types in expressions and doesn't
  take its sub-expressions into account.
  * Improve type checking performance because type variables aren't
     generated for sub-expressions of LHS (45511835)
  * Guarantee that the operand is not modified by the type checker because
     expression walkers in `CSGen` doesn't walk into the operand.

* Introduce `TypeChecker::findLHS()` to find LHS for a infix operator from
  pre-folded expression. We used to `foldSequence()` temporary
  `SequenceExpr` and find 'CodeCompletionExpr' for each attempt.
  * No need to flatten folded expression after initial type-checking.
  * Save memory of temporary `BinaryExpr` which used to be allocated by
    `foldSequence()`.
  * Improve accuracy of the completion. `foldSequence()` recovers invalid
    combination of operators by `left` associative manner (with
    diagnostics). This used to cause false-positive results. For instance,
    `a == b <HERE>` used to suggest `==` operator. `findLHS()` returns
    `nullptr` for such invalid combination.

rdar://problem/45511835
https://bugs.swift.org/browse/SR-9061
@rintaro rintaro deleted the ide-codecompletion-sequence2 branch November 27, 2018 04:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants