-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Exclude definitions from inactive code checking in #if #86262
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
base: main
Are you sure you want to change the base?
Conversation
| switch self { | ||
| case .name(let name): | ||
| if let identifier = token.identifier, identifier.name == name { | ||
| // Skip if this is a declaration |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hi @taka-2120 – thanks for taking a look into fixing this issue. i'm not personally familiar with the relevant code here, but i do find it a little unexpected that the change to resolve the issue would be located here. i would have expected something in MiscDiagnostics to have to change since i thought that was where the warnings were coming from. could you help me better understand what this is effectively doing please?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @jamieQ, thank you for reviewing.
As you noted, the warning we're coming from MiscDiagnostics, and the code I changed indirectly called from MiscDiagnostics.
search(syntax:, configuredRegions:) is called in inactiveCodeContainsReference (swift_ASTGen_inactiveCodeContainsReference). swift_ASTGen_inactiveCodeContainsReference called in VarDeclUsageChecker in MiscDiagnostics.
The search method is used for checking active references of the .name .
However, the problem was that compiler mistakingly recognize the definitions in #if true are used in the definitions in #else .
I implemented the code ignoring definitions for that reason.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DougGregor it looks to me like the change here #76327 is responsible for the difference in diagnostic suppression starting in 6.1. i think that was a behavioral change at the time, at least for some diagnostics like the ones mentioned here and within the linked issue.
setting aside the exact implementation details, do you think adjusting the handling such that the check for references within inactive regions excludes new declarations with the same name (which i think is what's going on here) makes sense?
|
Can you give a concrete example of a problem this PR solves? |
@grynspan Even when values defined in a func g() {
#if true
var x = 0 // no warning – unexpected
weak var y: F? // no warning – unexpected
let z = 0 // no warning – unexpected
#else
var x = 0 // no warning – expected
weak var y: F? // no warning – expected
let z = 0 // no warning – expected
#endif
}However, if the definitions in the func g() {
#if true
var x = 0 // warning – expected
weak var y: F? // warning – expected
let z = 0 // warning – expected
#endif
} |
Resolves #86059
What I did