Skip to content

[SwiftLexicalLookup] Add support for #if and macro declarations. More ASTScope related fixes. #2883

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 2 commits into from
Nov 13, 2024

Conversation

MAJKFL
Copy link
Contributor

@MAJKFL MAJKFL commented Oct 17, 2024

This PR includes more fixes that bring SwiftLexicalLookup unqualified lookup implementation closer to the ASTScope counterpart. Those include:

  • Handling of #if clauses thanks to SwiftIfConfig.
  • Macro declaration support.
  • Sequential scope improvements. Now, only uses .reversed() for iteration. Since it's more likely code block contains more variable decls than named decls, sequential lookup first goes through all items, and collects named decls.
  • Bring back wildcard token filtering for names from IdentifiableSyntax.
  • More minor fixes.

With this PR, I was able to run validation for the entire Swift standard library with just two remaining bugs!

@MAJKFL
Copy link
Contributor Author

MAJKFL commented Oct 17, 2024

@swift-ci Please test


/// Creates a new lookup configuration.
///
/// - `finishInSequentialScope` - specifies whether lookup should finish
/// in the closest sequential scope. `false` by default.
@_spi(Experimental) public init(
finishInSequentialScope: Bool = false
finishInSequentialScope: Bool = false,
buildConfiguration: BuildConfiguration? = nil
Copy link
Member

Choose a reason for hiding this comment

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

It's probably worth taking in a ConfiguredRegions here rather than a BuildConfiguration. ConfiguredRegions caches results so we aren't re-evaluating the same #ifs repeatedly.

Comment on lines +508 to +511
if !containsExpressionSyntax && pattern.is(ExpressionPatternSyntax.self) {
containsExpressionSyntax = true
}

Copy link
Member

Choose a reason for hiding this comment

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

This is because we suppress the implicit error when there is some kind of pattern? Please leave such a comment with a code example to remind our future selves.

}
}

@_spi(Experimental) extension IfConfigDeclSyntax: IntroducingToSequentialParentScopeSyntax, SequentialScopeSyntax {
Copy link
Member

Choose a reason for hiding this comment

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

This was quite straightforward, I love it!

@DougGregor
Copy link
Member

@swift-ci please test Windows

1 similar comment
@DougGregor
Copy link
Member

@swift-ci please test Windows

@DougGregor
Copy link
Member

@swift-ci please test

@DougGregor
Copy link
Member

@swift-ci please test Windows

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 only skimmed through it and that looked. Thank you, @MAJKFL 😍.

Comment on lines +992 to +996
clause =
clauses
.first { clause in
clause.poundKeyword.tokenKind == .poundElse
}
Copy link
Member

Choose a reason for hiding this comment

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

Do we want to use the #else clause if no configured regions are specified? It’s a question. I don’t know what the correct answer here should be.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that was my intention that when config.buildConfiguration is set to nil it should provide some "default" behavior. I think it makes sense to use #else as the only other logical solution here I can think of would be maybe not including any names from #if clauses? Either way, I'm open to debate and I'm curious what do you think about it.

Copy link
Member

Choose a reason for hiding this comment

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

The only alternative I can think of would be to throw an error or something like that. @DougGregor Do you remember what we do in SwiftIfConfig when we’re unable to answer a query in the build configuration?

Copy link
Member

Choose a reason for hiding this comment

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

SwiftIfConfig treats the condition as having failed if the configuration can't answer the query.

Copy link
Member

Choose a reason for hiding this comment

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

OK, I guess that matches picking the #else clause then.

@MAJKFL MAJKFL merged commit 1cd3534 into swiftlang:main Nov 13, 2024
3 checks passed
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.

3 participants