Skip to content

Commit 5d24d97

Browse files
authored
[Sema] Generate TRC for unparsed functions right before type-checking (#40126)
Fix an issue where `if #available(...)` conditions were ignored in emit-module-separately builds or when skipping non-inlinable function bodies. To handle functions for which parsing is delayed but type-checking is not delayed, move up building the delayed TRC to the TypeCheckFunctionBodyRequest service. rdar://85265934
1 parent 476f634 commit 5d24d97

File tree

5 files changed

+18
-3
lines changed

5 files changed

+18
-3
lines changed

include/swift/AST/SourceFile.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ class SourceFile final : public FileUnit {
516516
/// null if the context hierarchy has not been built yet. Use
517517
/// TypeChecker::getOrBuildTypeRefinementContext() to get a built
518518
/// root of the hierarchy.
519-
TypeRefinementContext *getTypeRefinementContext();
519+
TypeRefinementContext *getTypeRefinementContext() const;
520520

521521
/// Set the root refinement context for the file.
522522
void setTypeRefinementContext(TypeRefinementContext *TRC);

lib/AST/Module.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2857,7 +2857,7 @@ SynthesizedFileUnit &SourceFile::getOrCreateSynthesizedFile() {
28572857
return *SynthesizedFile;
28582858
}
28592859

2860-
TypeRefinementContext *SourceFile::getTypeRefinementContext() {
2860+
TypeRefinementContext *SourceFile::getTypeRefinementContext() const {
28612861
return TRC;
28622862
}
28632863

lib/Sema/TypeCheckStmt.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1979,6 +1979,10 @@ TypeCheckFunctionBodyRequest::evaluate(Evaluator &evaluator,
19791979
if (tyOpts.DebugTimeFunctionBodies || tyOpts.WarnLongFunctionBodies)
19801980
timer.emplace(AFD);
19811981

1982+
auto SF = AFD->getParentSourceFile();
1983+
if (SF)
1984+
TypeChecker::buildTypeRefinementContextHierarchyDelayed(*SF, AFD);
1985+
19821986
BraceStmt *body = AFD->getBody();
19831987
assert(body && "Expected body to type-check");
19841988

lib/Sema/TypeChecker.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,6 @@ static void typeCheckDelayedFunctions(SourceFile &SF) {
256256
++currentFunctionIdx) {
257257
auto *AFD = SF.DelayedFunctions[currentFunctionIdx];
258258
assert(!AFD->getDeclContext()->isLocalContext());
259-
TypeChecker::buildTypeRefinementContextHierarchyDelayed(SF, AFD);
260259
(void)AFD->getTypecheckedBody();
261260
}
262261

test/Sema/availability_and_delayed_parsing.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@ public func foo() { }
1717
// TRC-API: (root versions=[10.10.0,+Inf)
1818
// TRC-API: (decl versions=[10.12,+Inf) decl=foo()
1919

20+
struct S {
21+
fileprivate var actual: [String] = [] {
22+
didSet {
23+
if #available(macOS 10.15, *) {
24+
foo()
25+
}
26+
}
27+
}
28+
}
29+
// TRC-API: (condition_following_availability versions=[10.15,+Inf)
30+
// TRC-API: (if_then versions=[10.15,+Inf)
31+
2032
@inlinable public func inlinableFunc() {
2133
if #available(macOS 10.12, *) {
2234
foo()

0 commit comments

Comments
 (0)