-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Add cutoff to limit time taking to check exhaustiveness of switch statement. #16271
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
Add cutoff to limit time taking to check exhaustiveness of switch statement. #16271
Conversation
@swift-ci please test |
Build failed |
Build failed |
3d41eda
to
df1f4e6
Compare
@swift-ci please smoke test |
@swift-ci please test source compatibility |
@swift-ci Please Test Source Compatibility |
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.
LGTM! I think it makes sense to make threshold configurable as a frontend optional.
lib/Sema/TypeCheckSwitchStmt.cpp
Outdated
auto uncovered = totalSpace.minus(coveredSpace, TC, DC).simplify(TC, DC); | ||
// Why this number? Times out in about a second on my machine. | ||
// (It's arbitrary, but will keep the compiler from taking too much time.) | ||
const int maxMinusCount = 200000; |
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.
I think we need to make this a frontend flag so it's configurable to a different value if needed...
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.
Agreed. Also, instead of "my machine", can you put some basic specs? (Model and clock speed, maybe.)
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.
@AnnaZaks I think you voiced an objection to a flag for this. Would you be OK with me putting one in?
This seems a much more reasonable heuristic than the space size one. Once this is merged, I'm going to look into killing it (again). |
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.
I don't like this because "number of minus
operations" seems completely arbitrary, but maybe we don't have anything better.
lib/Sema/TypeCheckSwitchStmt.cpp
Outdated
auto uncovered = totalSpace.minus(coveredSpace, TC, DC).simplify(TC, DC); | ||
// Why this number? Times out in about a second on my machine. | ||
// (It's arbitrary, but will keep the compiler from taking too much time.) | ||
const int maxMinusCount = 200000; |
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.
Agreed. Also, instead of "my machine", can you put some basic specs? (Model and clock speed, maybe.)
lib/Sema/TypeCheckSwitchStmt.cpp
Outdated
// remaining minus operations allowed before abandoning the computation. | ||
// Returns None if the computation "timed out". | ||
Optional<Space> minus(const Space &other, TypeChecker &TC, | ||
const DeclContext *DC, int *minusCount) const { |
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.
Nitpick: indentation got messed up.
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.
Fixed.
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.
Specs added.
044f717
to
bd901bb
Compare
@swift-ci please smoke test |
bd901bb
to
0061356
Compare
@swift-ci please smoke test |
@@ -345,6 +345,8 @@ def warn_long_expression_type_checking_EQ : Joined<["-"], "warn-long-expression- | |||
|
|||
def solver_expression_time_threshold_EQ : Joined<["-"], "solver-expression-time-threshold=">; | |||
|
|||
def switch_checking_invocation_threshold : Joined<["-"], "switch-checking-invocation-threshold=">; |
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.
Nitpick: this ought to have _EQ
on the end to match the spelling of the option.
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.
Tx. Done.
@@ -997,10 +1005,25 @@ class TypeChecker final : public LazyResolver { | |||
/// the upper bound for the number of seconds we'll let the | |||
/// expression type checker run before considering an expression | |||
/// "too complex". | |||
/// If zero, do not limit the checking. |
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.
Is it worth building this into SwitchCheckingInvocationThreshold too?
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.
Yes, it is, but the comment was incomplete. Fixed.
0061356
to
037a2c8
Compare
@swift-ci please smoke test |
@jrose-apple Thanks for reviewing this. I think I've addressed your concerns and hope to land and cherry pick it by end of tomorrow, after which it apparently gets harder. |
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.
@xedin already approved this but it looks good to me too.
…ch-takes-too-long-to-compile Add cutoff to limit time taking to check exhaustiveness of switch statement.
Merge pull request #16271 from davidungar/rdar-39805050-Switch-takes-too-long-to-compile
…ch-takes-too-long-to-compile Add cutoff to limit time taking to check exhaustiveness of switch statement.
A quick-fix to rdar://39805050: Stop checking for exhaustion in a switch statement if it goes on for too long. Uses the same logic as if the space is too large to avoid issuing a diagnostic.
Without this fix, the following takes several minutes to type-check:
With this fix, the check is abandoned after about a second.