Skip to content

Incorrect warning with #require in optional assignment #80633

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

Open
macguru opened this issue Apr 8, 2025 · 3 comments
Open

Incorrect warning with #require in optional assignment #80633

macguru opened this issue Apr 8, 2025 · 3 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.

Comments

@macguru
Copy link

macguru commented Apr 8, 2025

Description

When using try #require inside an optional assignment, an incorrect warning ist being produced:

'#require(_:_:)' is redundant because 'x' never equals 'nil' (from macro 'require')

Reproduction

import Testing

struct A {
	var value: Int?
}

@Test func run() throws {
	let one = A()
	var two = A()
	
	two.value = try #require(one.value)
	// warning: '#require(_:_:)' is redundant because 'one.value' never equals 'nil' (from macro 'require')
}

Expected behavior

There should be no warning, the code is correct, as one.value can indeed become nil. Changing the code as follows does not have the warning:

@Test func run() throws {
	let one = A()
	let two = try #require(one.value)
}

Environment

Swift 6.1, Xcode Version 16.3 (16E140)

swift-driver version: 1.120.5 Apple Swift version 6.1 (swiftlang-6.1.0.110.21 clang-1700.0.13.3)
Target: arm64-apple-macosx15.0

Additional information

This was not an issue with Swift 6.0 and previous Xcode releases.

@macguru macguru added the bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. label Apr 8, 2025
@stmontgomery
Copy link
Contributor

stmontgomery commented Apr 8, 2025

I suspect this could be a duplicate of #79202

We added a workaround to Swift Testing (in swiftlang/swift-testing#949) to suppress this warning when certain syntax (such as ?) are seen in the expression, taking it as a hint there may be optionality. But in this issue there is no such syntax to hint on.

Also, the potential original issue I linked above involved a reference (class) type and, and there was a suspicion that was relevant to the bug. But this report is about a simpler value type.

@stmontgomery stmontgomery transferred this issue from swiftlang/swift-testing Apr 8, 2025
@macguru
Copy link
Author

macguru commented Apr 8, 2025

Yes, I've since found out this is also happening with primitive types:

@Test func run() throws {
	let a: String?
	var b: String?
	
	b = try #require(a)
	// warning: '#require(_:_:)' is redundant because 'a' never equals 'nil' (from macro 'require')
	
	_ = b // silence unused declaration warning
}

@grynspan
Copy link
Contributor

grynspan commented Apr 8, 2025

Yes, this is a duplicate. It's not clear to me what the root cause is, but I imagine the core compiler team will know where to look.

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.
Projects
None yet
Development

No branches or pull requests

3 participants