Skip to content

[SourceKit] Don’t provide incorrect semantic highlighting of nil in case statements as enum decl element #60328

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 1 commit into from
Aug 4, 2022

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented Aug 1, 2022

If a 'nil' literal occurs in a swift-case statement, it gets replaced by a reference to 'Optional.none' in the AST. We want to continue highlighting 'nil' as a keyword and not as an enum element. Detect this case if 'none' was spelled with 3 characters, which indicates that it was rewritten from 'nil'.

Resolves swiftlang/sourcekit-lsp#599
rdar://97961865

@ahoppen ahoppen requested a review from rintaro August 1, 2022 07:35
@ahoppen ahoppen force-pushed the pr/nil-highlighting-in-case branch from 2b75a15 to 51bb420 Compare August 1, 2022 07:36
Comment on lines 985 to 990
if (D == D->getASTContext().getOptionalNoneDecl() && Length == 3) {
// If a 'nil' literal occurs in a swift-case statement, it gets replaced
// by a reference to 'Optional.none' in the AST. We want to continue
// highlighting 'nil' as a keyword and not as an enum element.
// Detect this case if 'none' was spelled with 3 characters, which
// indicates that it was rewritten from 'nil'.
Copy link
Member

@rintaro rintaro Aug 3, 2022

Choose a reason for hiding this comment

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

Length == 3 might be enough in real world, but SM.extractText(Range, BufferID) == "nil" is more explicit, safe, and not so heavy. If we don't have strong reason not to, could you check the actual spelling?

Copy link
Member

@rintaro rintaro Aug 3, 2022

Choose a reason for hiding this comment

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

Also, other "deciding annotate or not" checks are in visitDeclReference() instead of annotate(). Could you move this logic to it?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good suggestions, thanks. I though I would have needed to go to the lexer to get the original source text but of course I don’t.

@ahoppen ahoppen force-pushed the pr/nil-highlighting-in-case branch from 51bb420 to 872f01d Compare August 3, 2022 20:17
@ahoppen
Copy link
Member Author

ahoppen commented Aug 3, 2022

@swift-ci Please smoke test

Comment on lines 972 to 973
// Detect this case if 'none' was spelled with 3 characters, which
// indicates that it was rewritten from 'nil'.
Copy link
Member

@rintaro rintaro Aug 3, 2022

Choose a reason for hiding this comment

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

The last sentence is not correct anymore :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for catching it. I removed the last sentence.

@@ -963,6 +963,17 @@ class SemanticAnnotator : public SourceEntityWalker {
if (AvailableAttr::isUnavailable(D))
return true;

auto &SM = D->getASTContext().SourceMgr;
if (D == D->getASTContext().getOptionalNoneDecl() &&
SM.extractText(Range) == "nil") {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
SM.extractText(Range) == "nil") {
SM.extractText(Range, BufferID) == "nil") {

So it skips findBufferContainingLoc() operation

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch. Thanks 👍

… case statements as enum decl element

If a 'nil' literal occurs in a swift-case statment, it gets replaced by a reference to 'Optional.none' in the AST. We want to continue highlighting 'nil' as a keyword and not as an enum element.

Resolves swiftlang/sourcekit-lsp#599
rdar://97961865
@ahoppen ahoppen force-pushed the pr/nil-highlighting-in-case branch from 872f01d to 1e9eac6 Compare August 3, 2022 20:42
@ahoppen
Copy link
Member Author

ahoppen commented Aug 3, 2022

@swift-ci Please smoke test

@ahoppen ahoppen merged commit 38648ec into swiftlang:main Aug 4, 2022
@ahoppen ahoppen deleted the pr/nil-highlighting-in-case branch August 4, 2022 06:46
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.

nil should still be highlighted as a keyword in a case pattern
2 participants