Skip to content

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

Conversation

davidungar
Copy link
Contributor

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:

enum A {a1, ... a5}
enum B {b1, ... b4}
var a: A; var b: B
(initialize a and b)
switch (a, b) {
 case: (a1, b1), ... (a5, b4): break // all possibilities
}

With this fix, the check is abandoned after about a second.

@davidungar
Copy link
Contributor Author

@swift-ci please test

@davidungar davidungar requested review from jrose-apple and xedin May 1, 2018 01:10
@swift-ci
Copy link
Contributor

swift-ci commented May 1, 2018

Build failed
Swift Test Linux Platform
Git Sha - 3d41eda9a459aeb9bd9828852368ff0ec7a89ef6

@swift-ci
Copy link
Contributor

swift-ci commented May 1, 2018

Build failed
Swift Test OS X Platform
Git Sha - 3d41eda9a459aeb9bd9828852368ff0ec7a89ef6

@davidungar davidungar force-pushed the rdar-39805050-Switch-takes-too-long-to-compile branch from 3d41eda to df1f4e6 Compare May 1, 2018 07:22
@davidungar
Copy link
Contributor Author

@swift-ci please smoke test

@davidungar
Copy link
Contributor Author

@swift-ci please test source compatibility

@davidungar
Copy link
Contributor Author

@swift-ci Please Test Source Compatibility

Copy link
Contributor

@xedin xedin left a 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.

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;
Copy link
Contributor

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...

Copy link
Contributor

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.)

Copy link
Contributor Author

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?

@CodaFi
Copy link
Contributor

CodaFi commented May 2, 2018

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).

Copy link
Contributor

@jrose-apple jrose-apple left a 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.

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;
Copy link
Contributor

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.)

// 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 {
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Specs added.

@davidungar davidungar force-pushed the rdar-39805050-Switch-takes-too-long-to-compile branch 2 times, most recently from 044f717 to bd901bb Compare May 3, 2018 04:40
@davidungar
Copy link
Contributor Author

@swift-ci please smoke test

@davidungar davidungar force-pushed the rdar-39805050-Switch-takes-too-long-to-compile branch from bd901bb to 0061356 Compare May 3, 2018 04:52
@davidungar
Copy link
Contributor Author

@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=">;
Copy link
Contributor

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.

Copy link
Contributor Author

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.
Copy link
Contributor

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?

Copy link
Contributor Author

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.

@davidungar davidungar force-pushed the rdar-39805050-Switch-takes-too-long-to-compile branch from 0061356 to 037a2c8 Compare May 3, 2018 17:30
@davidungar
Copy link
Contributor Author

@swift-ci please smoke test

@davidungar
Copy link
Contributor Author

@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.

Copy link
Contributor

@jrose-apple jrose-apple left a 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.

@davidungar davidungar merged commit 7b6ec6c into swiftlang:master May 3, 2018
davidungar pushed a commit to davidungar/swift that referenced this pull request May 4, 2018
…ch-takes-too-long-to-compile

Add  cutoff to limit time taking to check exhaustiveness of switch statement.
davidungar pushed a commit that referenced this pull request May 4, 2018
Merge pull request #16271 from davidungar/rdar-39805050-Switch-takes-too-long-to-compile
davidungar pushed a commit to davidungar/swift that referenced this pull request May 4, 2018
…ch-takes-too-long-to-compile

Add  cutoff to limit time taking to check exhaustiveness of switch statement.
@davidungar davidungar deleted the rdar-39805050-Switch-takes-too-long-to-compile branch June 20, 2019 21:27
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.

5 participants