Skip to content
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

Contradictory compiler diagnostics regarding termination and return analysis #73574

Open
sarahnicoleboo opened this issue May 10, 2024 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels

Comments

@sarahnicoleboo
Copy link

Description

Contradictory compiler diagnostics are given in certain cases of infinite loops inside functions.
As seen in the reproduction below there is an error provided for lack of a return, while simultaneously providing a warning/note that the compiler has done enough analysis to see that the ternary expressions will always result in the true branch(which is the literal value true).
The same program that uses the literal true rather than the ternary compiles without error. (Or another simple expression such as 1==1)

Reproduction

func myFunc() -> Int {
	while (true ? true : true) {}
}

Compiler output:

test.swift:2:23: warning: will never be executed
        while (true ? true : true) {}
                             ^
test.swift:2:9: note: condition always evaluates to true
        while (true ? true : true) {}
               ^
test.swift:3:1: error: missing return in global function expected to return 'Int'
}
^

Expected behavior

Better diagnostics and possible documentation of termination analysis limitations.
If Swift is employing partial termination analysis for loops, the compiler should reflect that with appropriate diagnostics.

Environment

swift-driver version: 1.90.11.1 Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
Target: arm64-apple-macosx14.0

Additional information

No response

@sarahnicoleboo sarahnicoleboo added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels May 10, 2024
@tbkka
Copy link
Contributor

tbkka commented May 10, 2024

I think both the warning and the note are referring only to the ternary:
The "else" part of the ternary will never be evaluated because the "condition" part is always true. The note in this case is clarifying the warning message. Which is why you get neither message if you use a simple true literal.

The interesting part is that this affects the error diagnostic about the missing return. It looks like the warning is somehow causing the termination analysis to not run at all.

@sarahnicoleboo
Copy link
Author

Yeah, the warning and note are only referring to the ternary and they're both valid. I just thought it was interesting that the compiler can figure that out (that the else branch will never be executed). Which in turn means that it will always execute the first branch or "if" branch. Yet it simultaneously can't figure out that the "if" branch(which will always be executed) is just the literal true when trying to determine termination.
My first thoughts were that it seems like maybe there's either two different analyses happening that aren't "talking" to each other or that maybe there's some special casing around which operators are allowed to be analyzed for termination analyses.

I can see that Swift has limited termination analysis but I don't have clarification on the specific scope of what is expected behavior and what isn't.

For example the same program with a simple logical or doesn't compile either (gives missing return error):

func myFunc() -> Int {
	while (true || true) {}
}

But the same program with an exactly equals operator does compile:

func myFunc() -> Int {
	while (true == true) {}
}

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. triage needed This issue needs more specific labels
Projects
None yet
Development

No branches or pull requests

2 participants