File tree Expand file tree Collapse file tree 2 files changed +18
-1
lines changed
Expand file tree Collapse file tree 2 files changed +18
-1
lines changed Original file line number Diff line number Diff line change @@ -636,9 +636,18 @@ const ExternalSourceLocs *Decl::getSerializedLocs() const {
636636 return &NullLocs;
637637 }
638638
639+ CharSourceRange BufferRange = SM.getRangeForBuffer (BufferID);
639640 auto ResolveLoc = [&](const ExternalSourceLocs::RawLoc &Raw) -> SourceLoc {
641+ // If the underlying source has been updated and the swiftsourceinfo hasn't,
642+ // make sure we don't produce invalid source locations. Ideally would check
643+ // the file hasn't been modified.
644+ if (Raw.Offset > BufferRange.getByteLength ())
645+ return SourceLoc ();
646+
640647 // If the decl had a presumed loc, create its virtual file so that
641- // getPresumedLineAndColForLoc works from serialized locations as well.
648+ // getPresumedLineAndColForLoc works from serialized locations as well. No
649+ // need to check the buffer range, the directive must be before the location
650+ // itself.
642651 if (Raw.Directive .isValid ()) {
643652 auto &LD = Raw.Directive ;
644653 SourceLoc Loc = SM.getLocForOffset (BufferID, LD.Offset );
Original file line number Diff line number Diff line change @@ -59,6 +59,14 @@ open class SubClass: ParentClass {
5959// CHECK-OUTOFDATE-NOT: moda.ParentClass:{{.*}}: note:
6060// CHECK-OUTOFDATE: moda.swift:{{.*}}: note:
6161
62+ // Underlying file is empty, the locations are now completely invalid (ie. not
63+ // within the buffer). Make sure there's no crash and that we fallback to using
64+ // the generated source.
65+ // RUN: rm %t/moda.swift
66+ // RUN: touch %t/moda.swift
67+ // RUN: not %target-swift-frontend -typecheck -I %t/mods -D MODB %s 2>&1 | %FileCheck -check-prefix=CHECK-EMPTY %s
68+ // CHECK-EMPTY: moda.ParentClass:{{.*}}: note:
69+
6270// The file and line from a location directive should be used whether or not it
6371// exists - the actual source still comes from the original file, so that's what
6472// matters in terms of whether generated code is used or not
You can’t perform that action at this time.
0 commit comments