Skip to content

Commit dedba4d

Browse files
committed
[Performance Hints] Restrict analysis to primary source files
When Swift code is compiled or analyzed under whole module compilation mode, the compiler receives all the imported files along with the source file being compiled (the primary source file). Currently, the performance assistant runs on all of these files, repeating analysis work, since the non-primary source files will be treated as primary in separate runs. Furthermore, declarations in non-primary source files don't have type information, leading to crashes, since performance hint generation require type information. In this commit, we have restricted the performance hint analysis to run only on primary source files.
1 parent 298ba58 commit dedba4d

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

lib/Sema/PerformanceHints.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ class PerformanceHintDiagnosticWalker final : public ASTWalker {
168168

169169
evaluator::SideEffect EmitPerformanceHints::evaluate(Evaluator &evaluator,
170170
SourceFile *SF) const {
171+
assert(SF->isPrimary() &&
172+
"Performance hints can only be generated for primary source files!");
171173
PerformanceHintDiagnosticWalker::check(SF);
172174
return {};
173175
}

lib/Sema/TypeChecker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ TypeCheckPrimaryFileRequest::evaluate(Evaluator &eval, SourceFile *SF) const {
385385
CheckInconsistentWeakLinkedImportsRequest{SF->getParentModule()}, {});
386386

387387
// Opt-in performance hint diagnostics for performance-critical code.
388-
if (performanceHintDiagnosticsEnabled(Ctx))
388+
if (performanceHintDiagnosticsEnabled(Ctx) && SF->isPrimary())
389389
evaluateOrDefault(Ctx.evaluator, EmitPerformanceHints{SF}, {});
390390

391391
// Perform various AST transforms we've been asked to perform.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %{python} %utils/split_file.py -o %t %s
3+
// RUN: %target-swift-frontend -typecheck -primary-file %t/a.swift %t/b.swift -Wwarning PerformanceHints -diagnostic-style llvm -verify
4+
5+
// BEGIN a.swift
6+
func foo() -> [Int] { // expected-warning {{Performance: 'foo()' returns an array, leading to implicit copies. Consider using an 'inout' parameter instead.}}
7+
return [1, 2, 3, 4, 5, 6]
8+
}
9+
10+
// BEGIN b.swift
11+
protocol Animal {
12+
var Name: String { get }
13+
}
14+
15+
typealias AnyAnimal = any Animal

0 commit comments

Comments
 (0)