Skip to content

Commit 8732d9a

Browse files
authored
Merge pull request #77055 from tshortli/source-ranges-are-weird
Sema: Fix source ranges for string literals in TypeRefinementContext
2 parents e362264 + 6cb3382 commit 8732d9a

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

lib/AST/TypeRefinementContext.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,7 @@ verificationError(ASTContext &ctx, llvm::StringRef msg,
526526
auto context = std::get<1>(pair);
527527
llvm::errs() << label << ":\n";
528528
context->print(llvm::errs(), ctx.SourceMgr);
529+
llvm::errs() << "\n";
529530
}
530531
abort();
531532
}
@@ -570,18 +571,20 @@ void TypeRefinementContext::verify(const TypeRefinementContext *parent,
570571
if (!SrcRange.isValid())
571572
verificationError(ctx, "invalid source range", {{"node", this}});
572573

574+
// Child nodes must be contained by their parents in all dimensions (source
575+
// range, introduction version, etc).
573576
if (getReason() != Reason::Root) {
574577
auto parentRange = parent->SrcRange;
575578
if (parentRange.isValid() &&
576579
!(srcMgr.isAtOrBefore(parentRange.Start, SrcRange.Start) &&
577580
srcMgr.isAtOrBefore(SrcRange.End, parentRange.End)))
578581
verificationError(ctx, "child source range not contained",
579-
{{"child", this}, {"parent", this}});
582+
{{"child", this}, {"parent", parent}});
580583
}
581584

582585
if (!AvailabilityInfo.isContainedIn(parent->AvailabilityInfo))
583586
verificationError(ctx, "child availability range not contained",
584-
{{"child", this}, {"parent", this}});
587+
{{"child", this}, {"parent", parent}});
585588
}
586589

587590
void TypeRefinementContext::verify(ASTContext &ctx) {

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)