Skip to content

[SourceKit] Determine correctly whether typed variables in if/guard/while-let-statements have explicit type annotations #38084

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

Merged
merged 3 commits into from
Jul 2, 2021

Conversation

fwcd
Copy link
Member

@fwcd fwcd commented Jun 24, 2021

As noted in swiftlang/sourcekit-lsp#408 (comment), if/guard/while-lets are currently (incorrectly) always reported as having no explicit type annotation as the TypedPattern is not the top-level-pattern in such declarations. Consider the following example:

if let x: Int = Optional.some(4) {}

Here we get the following AST:

(if_stmt ...
  (pattern
    (pattern_optional_some implicit type='Int?'  <--- this node prevents the current mechanism from discovering the type annotation
      (pattern_typed type='Int'
        (pattern_let implicit type='Int'
          (pattern_named type='Int' 'x'))
        (type_ident
          (component id='Int' bind=Swift.(file).Int))))
    (call_expr ...))
  (brace_stmt ...))

sourcekitd-test therefore reports (explicit type: 0) despite an explicit annotation being present:

<VariableTypes>
(1:8, 1:9): Int (explicit type: 0)
</VariableTypes>

This PR fixes the issue by including an implicitly generated OptionalSomePattern as a special case in VariableTypeCollector and also adds tests to verify that these cases are handled correctly.

cc @ahoppen

fwcd added 2 commits June 24, 2021 21:07
- Add VariableType test case for guarded variables
- Add if-let to VariableType test case
- Add while-let test case for VariableType
- Test pattern matching with VariableType
- Test guard/while-case-let with VariableType
Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

I think it might be worth adding the check to getTypeReprOrParentPatternTypeRepr. It seems generic enough to me that other user’s of the API could also benefit from it.

Or was there a specific reason why you didn’t do that?

@fwcd
Copy link
Member Author

fwcd commented Jun 25, 2021

It would change the semantics of the method slightly (by not just considering the parent pattern), therefore I wasn't sure whether this should be included.

@ahoppen
Copy link
Member

ahoppen commented Jun 25, 2021

I think changing the semantics of getTypeReprOrParentPatternTypeRepr is fine if it doesn’t break any tests – I would consider it an improvement to that method.

I would also leave the implicitness check unless there’s a specific reason to include it that you can explain. Just makes the code simpler and more generic.

As suggested during PR review.

This mechanism seems to be general enough to fit into
getTypeReprOrParentPatternRepr.
@fwcd fwcd force-pushed the fix-sourcekit-var-types-if-let branch from e0d1d98 to b33a202 Compare June 26, 2021 14:19
@ahoppen
Copy link
Member

ahoppen commented Jun 28, 2021

@swift-ci Please smoke test

@fwcd
Copy link
Member Author

fwcd commented Jun 28, 2021

Not entirely sure about the CI failure, but it seems unrelated to the PR?

@ahoppen
Copy link
Member

ahoppen commented Jun 28, 2021

Yes, the CI failure is unrelated. I’ll try again later.

@ahoppen
Copy link
Member

ahoppen commented Jun 29, 2021

@swift-ci Please smoke test Linux

1 similar comment
@ahoppen
Copy link
Member

ahoppen commented Jun 30, 2021

@swift-ci Please smoke test Linux

@ahoppen ahoppen merged commit 92b7e13 into swiftlang:main Jul 2, 2021
@fwcd fwcd deleted the fix-sourcekit-var-types-if-let branch July 2, 2021 10:51
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