Skip to content

Conversation

@jamieQ
Copy link
Contributor

@jamieQ jamieQ commented Oct 24, 2025

Previously we would emit a diagnostic in cases in which a throwing function returning a structurally-uninhabited type was called within an optional try expression. Such cases can never produce a result other than nil, so the existing diagnostic seemed not particularly helpful. This change attempts to address this and other minor inconsistencies by expanding the isDiscardableType() logic to encompass "structurally uninhabited" types (e.g. tuples that eventually bottom out in an uninhabited type in some position), and also look through any levels of optionality to find such types. This makes the unused expression result diagnostics a bit more consistent in that they will now allow 'uninteresting' unused results to be implicitly discarded. In particular, the following cases now have the same diagnostic behavior (namely, do not produce a warning for the unused results):

func maybeNever() -> Never? { nil }
func maybeVoid() -> Void? { nil }

maybeNever() // used to warn with: 'warning: result of call to 'maybeNever()' is unused'
maybeVoid()

Resolves: #85092

Previously we would emit a diagnostic in cases in which a throwing
function returning a structurally-unihabited type was called within an
optional try expression. Such cases can never produce a return value, so
suppress the existing diagnostic.
@jamieQ
Copy link
Contributor Author

jamieQ commented Oct 24, 2025

@swift-ci please smoke test

Copy link
Contributor

@hamishknight hamishknight left a comment

Choose a reason for hiding this comment

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

Thank you! This looks reasonable to me

@grynspan
Copy link
Contributor

Looks good to me. Is there a practical difference between isUninhabited() and isStructurallyUninhabited() that we need to consider?

@xedin
Copy link
Contributor

xedin commented Oct 24, 2025

It has to do with tuples, I think what we have in the PR is reasonable.

@hamishknight hamishknight merged commit bafd04d into swiftlang:main Oct 24, 2025
3 checks passed
@jamieQ jamieQ deleted the try-never-discard-diags branch October 24, 2025 19:25
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.

Compiler complains about an unused Never? returned from a throwing function

4 participants