Skip to content

Commit 34253a8

Browse files
authored
Merge pull request #82506 from slavapestov/warn-long-expression-type-checking
Sema: Fix the -warn-long-expression-type-checking flag
2 parents ea6ed2f + 487918f commit 34253a8

File tree

11 files changed

+26
-18
lines changed

11 files changed

+26
-18
lines changed

include/swift/Option/FrontendOptions.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,9 @@ def downgrade_typecheck_interface_error : Flag<["-"], "downgrade-typecheck-inter
847847
def enable_volatile_modules : Flag<["-"], "enable-volatile-modules">,
848848
HelpText<"Load Swift modules in memory">;
849849

850+
def solver_memory_threshold_EQ : Joined<["-"], "solver-memory-threshold=">,
851+
HelpText<"Set the upper bound for memory consumption, in bytes, by the constraint solver">;
852+
850853
def solver_expression_time_threshold_EQ : Joined<["-"], "solver-expression-time-threshold=">,
851854
HelpText<"Expression type checking timeout, in seconds">;
852855

include/swift/Option/Options.td

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -799,10 +799,6 @@ def import_cf_types : Flag<["-"], "import-cf-types">,
799799
Flags<[FrontendOption, HelpHidden]>,
800800
HelpText<"Recognize and import CF types as class types">;
801801

802-
def solver_memory_threshold : Separate<["-"], "solver-memory-threshold">,
803-
Flags<[FrontendOption, HelpHidden, DoesNotAffectIncrementalBuild]>,
804-
HelpText<"Set the upper bound for memory consumption, in bytes, by the constraint solver">;
805-
806802
def solver_shrink_unsolved_threshold : Separate<["-"], "solver-shrink-unsolved-threshold">,
807803
Flags<[FrontendOption, HelpHidden, DoesNotAffectIncrementalBuild]>,
808804
HelpText<"Set The upper bound to number of sub-expressions unsolved before termination of the shrink phrase">;

include/swift/Sema/ConstraintSystem.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@ class ExpressionTimer {
247247
bool PrintWarning;
248248

249249
public:
250+
const static unsigned NoLimit = (unsigned) -1;
251+
250252
ExpressionTimer(AnchorType Anchor, ConstraintSystem &CS,
251253
unsigned thresholdInSecs);
252254

@@ -272,6 +274,9 @@ class ExpressionTimer {
272274
/// Return the remaining process time in seconds until the
273275
/// threshold specified during construction is reached.
274276
unsigned getRemainingProcessTimeInSeconds() const {
277+
if (ThresholdInSecs == NoLimit)
278+
return NoLimit;
279+
275280
auto elapsed = unsigned(getElapsedProcessTimeInFractionalSeconds());
276281
return elapsed >= ThresholdInSecs ? 0 : ThresholdInSecs - elapsed;
277282
}

lib/Driver/ToolChains.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,6 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
312312
inputArgs.AddLastArg(arguments, options::OPT_nostdlibimport);
313313
inputArgs.AddLastArg(arguments, options::OPT_parse_stdlib);
314314
inputArgs.AddLastArg(arguments, options::OPT_resource_dir);
315-
inputArgs.AddLastArg(arguments, options::OPT_solver_memory_threshold);
316315
inputArgs.AddLastArg(arguments, options::OPT_value_recursion_threshold);
317316
inputArgs.AddLastArg(arguments, options::OPT_warn_swift3_objc_inference);
318317
inputArgs.AddLastArg(arguments, options::OPT_Rpass_EQ);

lib/Frontend/CompilerInvocation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1945,7 +1945,7 @@ static bool ParseTypeCheckerArgs(TypeCheckerOptions &Opts, ArgList &Args,
19451945
Opts.SwitchCheckingInvocationThreshold);
19461946
setUnsignedIntegerArgument(OPT_debug_constraints_attempt,
19471947
Opts.DebugConstraintSolverAttempt);
1948-
setUnsignedIntegerArgument(OPT_solver_memory_threshold,
1948+
setUnsignedIntegerArgument(OPT_solver_memory_threshold_EQ,
19491949
Opts.SolverMemoryThreshold);
19501950
setUnsignedIntegerArgument(OPT_solver_scope_threshold_EQ,
19511951
Opts.SolverScopeThreshold);

lib/Sema/ConstraintSystem.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,18 @@ ConstraintSystem::~ConstraintSystem() {
141141
void ConstraintSystem::startExpressionTimer(ExpressionTimer::AnchorType anchor) {
142142
ASSERT(!Timer);
143143

144-
unsigned timeout = getASTContext().TypeCheckerOpts.ExpressionTimeoutThreshold;
145-
if (timeout == 0)
146-
return;
144+
const auto &opts = getASTContext().TypeCheckerOpts;
145+
unsigned timeout = opts.ExpressionTimeoutThreshold;
146+
147+
// If either the timeout is set, or we're asked to emit warnings,
148+
// start the timer. Otherwise, don't start the timer, it's needless
149+
// overhead.
150+
if (timeout == 0) {
151+
if (opts.WarnLongExpressionTypeChecking == 0)
152+
return;
153+
154+
timeout = ExpressionTimer::NoLimit;
155+
}
147156

148157
Timer.emplace(anchor, *this, timeout);
149158
}

test/Constraints/issue-52724.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
// RUN: %target-swift-frontend -typecheck -verify %s
2-
3-
// REQUIRES: rdar65007946
1+
// RUN: %target-swift-frontend -typecheck -verify -disable-constraint-solver-performance-hacks %s
42

53
// https://github.com/apple/swift/issues/52724
64

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// RUN: %target-typecheck-verify-swift -warn-long-expression-type-checking=1 -warn-long-function-bodies=1
2-
// REQUIRES: rdar44305428
1+
// RUN: %target-typecheck-verify-swift -warn-long-expression-type-checking=1 -disable-constraint-solver-performance-hacks -warn-long-function-bodies=1
32
@_silgen_name("generic_foo")
43
func foo<T>(_ x: T) -> T
54

@@ -8,6 +7,6 @@ func foo(_ x: Int) -> Int
87

98
func test(m: Double) -> Int {
109
// expected-warning@-1 {{global function 'test(m:)' took}}
11-
return Int(foo(Float(m) / 20) * 20 - 2) + 10
10+
return ~(~(~(~(~(~(~(~(~(~(~(~(0))))))))))))
1211
// expected-warning@-1 {{expression took}}
1312
}

test/Interpreter/objc_protocols.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// REQUIRES: rdar107343134
21
// RUN: %target-run-simple-swift
32
// REQUIRES: executable_test
43
// REQUIRES: objc_interop

test/Misc/expression_too_complex_2.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-typecheck-verify-swift -solver-memory-threshold 32000
1+
// RUN: %target-typecheck-verify-swift -solver-memory-threshold=32000
22
// REQUIRES: OS=ios
33
import UIKit
44
class MyViewCell: UITableViewCell {

0 commit comments

Comments
 (0)