-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Incorrect warning: '#require(_:_:)' is redundant because '<expression>' never equals 'nil' #79202
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
Labels
bug
A deviation from expected or documented behavior. Also: expected but undesirable behavior.
type checker
Area → compiler: Semantic analysis
Comments
Tracked internally as rdar://137943258 |
grynspan
added a commit
to swiftlang/swift-testing
that referenced
this issue
Feb 11, 2025
When using `try #require()` to unwrap an optional value, we emit a compile-time warning if the value is not actually optional. However, there is a bug in the type checker (swiftlang/swift#79202) that triggers a false positive when downcasting an object (of `class` type), e.g.: ```swift class Animal {} class Duck: Animal {} let beast: Animal = Duck() let definitelyADuck = try #require(beast as? Duck) //⚠️ '#require(_:_:)' is redundant because 'beast as? Duck' never equals 'nil' ``` This change suppresses the warning we emit if the expression contains certain syntax tokens (namely `?`, `nil`, or `Optional`) on the assumption that their presence means the test author is expecting an optional value and we've hit a false positive.
2 tasks
grynspan
added a commit
to swiftlang/swift-testing
that referenced
this issue
Feb 11, 2025
When using `try #require()` to unwrap an optional value, we emit a compile-time warning if the value is not actually optional. However, there is a bug in the type checker (swiftlang/swift#79202) that triggers a false positive when downcasting an object (of `class` type), e.g.: ```swift class Animal {} class Duck: Animal {} let beast: Animal = Duck() let definitelyADuck = try #require(beast as? Duck) //⚠️ '#require(_:_:)' is redundant because 'beast as? Duck' never equals 'nil' ``` This change suppresses the warning we emit if the expression contains certain syntax tokens (namely `?`, `nil`, or `Optional`) on the assumption that their presence means the test author is expecting an optional value and we've hit a false positive. ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
grynspan
added a commit
to swiftlang/swift-testing
that referenced
this issue
Feb 11, 2025
When using `try #require()` to unwrap an optional value, we emit a compile-time warning if the value is not actually optional. However, there is a bug in the type checker (swiftlang/swift#79202) that triggers a false positive when downcasting an object (of `class` type), e.g.: ```swift class Animal {} class Duck: Animal {} let beast: Animal = Duck() let definitelyADuck = try #require(beast as? Duck) //⚠️ '#require(_:_:)' is redundant because 'beast as? Duck' never equals 'nil' ``` This change suppresses the warning we emit if the expression contains certain syntax tokens (namely `?`, `nil`, or `Optional`) on the assumption that their presence means the test author is expecting an optional value and we've hit a false positive. ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
grynspan
added a commit
to swiftlang/swift-testing
that referenced
this issue
Feb 11, 2025
…949) - **Explanation**: Suppress some warnings that occur due to swiftlang/swift#79202 when the compiler selects the wrong overload of `try #require()` for expansion. - **Scope**: Tests using `try #require()` with reference types. - **Issues**: works around swiftlang/swift#79202 - **Original PRs**: #947 - **Risk**: Low, just suppresses some warnings we generate (lack of these warnings is not harmful) - **Testing**: Added a unit test - **Reviewers**: @briancroom
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
bug
A deviation from expected or documented behavior. Also: expected but undesirable behavior.
type checker
Area → compiler: Semantic analysis
Description
In a Swift Testing test, use of
#require
can in some cases result in a build warning about how the expression in the macro can never equal nil, even though it clearly can.For example, in swift-build, some tests have checks like this:
This results in the warning:
But
fileGroup.children
is of type[GroupTreeReference]
, so the check is downcasting it toFileReference
, which seems entirely valid to do, and could end up being nil if the instance is not aFileReference
. It works correctly, so the warning seems incorrect.#require
is being used here because it will emit an issue automatically if the expression is false or nil, which is the desired behavior. This could be worked around by not using#require
and instead handling that case with explicit code, but that seems unnecessary and contrary to the intent of#require
.Reproduction
See Description.
Expected behavior
This warning should not be emitted in cases such as the one in the Description.
Environment
Swift 6
Additional information
No response
The text was updated successfully, but these errors were encountered: