Skip to content

Commit 6cb3382

Browse files
committed
Sema: Fix source ranges for string literals in TypeRefinementContext.
Since string literals are only a single token from the perspective of the parser, the source range for a pattern binding decl that is initialized with a string literal expression only extends to the beginning of the string literal. To ensure the TypeRefinementContext for a pattern binding decl includes the entire contents of the init expression we must calculate the character source range of the decl instead. Resolves rdar://110952225 and #77050.
1 parent 4c45a4d commit 6cb3382

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -782,10 +782,12 @@ class TypeRefinementContextBuilder : private ASTWalker {
782782
// it.
783783
assert(D->getSourceRange().isValid());
784784

785+
auto &Context = D->getASTContext();
786+
SourceRange Range;
785787
if (auto *storageDecl = dyn_cast<AbstractStorageDecl>(D)) {
786788
// Use the declaration's availability for the context when checking
787789
// the bodies of its accessors.
788-
SourceRange Range = storageDecl->getSourceRange();
790+
Range = storageDecl->getSourceRange();
789791

790792
// HACK: For synthesized trivial accessors we may have not a valid
791793
// location for the end of the braces, so in that case we will fall back
@@ -797,11 +799,12 @@ class TypeRefinementContextBuilder : private ASTWalker {
797799
if (BracesRange.isValid()) {
798800
Range.widen(BracesRange);
799801
}
800-
801-
return Range;
802+
} else {
803+
Range = D->getSourceRangeIncludingAttrs();
802804
}
803805

804-
return D->getSourceRangeIncludingAttrs();
806+
Range.End = Lexer::getLocForEndOfToken(Context.SourceMgr, Range.End);
807+
return Range;
805808
}
806809

807810
// Creates an implicit decl TRC specifying the deployment
@@ -827,7 +830,7 @@ class TypeRefinementContextBuilder : private ASTWalker {
827830
if (auto bodyStmt = tlcd->getBody()) {
828831
pushDeclBodyContext(
829832
tlcd, {{bodyStmt, createImplicitDeclContextForDeploymentTarget(
830-
tlcd, tlcd->getSourceRange())}});
833+
tlcd, refinementSourceRangeForDecl(tlcd))}});
831834
}
832835
return;
833836
}

test/Sema/availability_refinement_contexts.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,18 @@ var someComputedGlobalVar: Int {
299299
set { }
300300
}
301301

302+
// CHECK-NEXT: {{^}} (decl_implicit version=50 decl=interpolated
303+
// CHECK-NEXT: {{^}} (decl_implicit version=50 decl=string
304+
305+
func testStringInterpolation() {
306+
let interpolated = """
307+
\([""].map {
308+
let string = $0
309+
return string
310+
})
311+
"""
312+
}
313+
302314
// CHECK-NEXT: {{^}} (decl version=51 decl=FinalDecl
303315

304316
@available(OSX 51, *)

0 commit comments

Comments
 (0)