Skip to content

Commit bbe3c2b

Browse files
authored
Merge pull request #4607 from DougGregor/fixup-ast-source-ranges
2 parents 1a4328b + df72468 commit bbe3c2b

File tree

9 files changed

+66
-45
lines changed

9 files changed

+66
-45
lines changed

include/swift/AST/Decl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,6 +1703,9 @@ class PatternBindingEntry {
17031703

17041704
/// Override the initializer context.
17051705
void setInitContext(DeclContext *dc) { InitContext = dc; }
1706+
1707+
/// Retrieve the source range covered by this pattern binding.
1708+
SourceRange getSourceRange() const;
17061709
};
17071710

17081711
/// \brief This decl contains a pattern and optional initializer for a set

lib/AST/Decl.cpp

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -918,17 +918,32 @@ VarDecl *PatternBindingEntry::getAnchoringVarDecl() const {
918918
return variables[0];
919919
}
920920

921+
SourceRange PatternBindingEntry::getSourceRange() const {
922+
SourceLoc endLoc;
923+
getPattern()->forEachVariable([&](VarDecl *var) {
924+
auto accessorsEndLoc = var->getBracesRange().End;
925+
if (accessorsEndLoc.isValid()) endLoc = accessorsEndLoc;
926+
});
927+
928+
// Check the initializer.
929+
if (endLoc.isInvalid())
930+
endLoc = getOrigInitRange().End;
931+
932+
// Check the pattern.
933+
if (endLoc.isInvalid())
934+
endLoc = getPattern()->getEndLoc();
935+
936+
SourceLoc startLoc = getPattern()->getStartLoc();
937+
if (startLoc.isValid() != endLoc.isValid()) return SourceRange();
938+
939+
return SourceRange(startLoc, endLoc);
940+
}
941+
921942
SourceRange PatternBindingDecl::getSourceRange() const {
922943
SourceLoc startLoc = getStartLoc();
923-
924-
// Take the init of the last pattern in the list.
925-
if (auto init = getPatternList().back().getInit()) {
926-
SourceLoc EndLoc = init->getEndLoc();
927-
if (EndLoc.isValid())
928-
return { startLoc, EndLoc };
929-
}
930-
// If the last pattern had no init, we take the end of its pattern.
931-
return { startLoc, getPatternList().back().getPattern()->getEndLoc() };
944+
SourceLoc endLoc = getPatternList().back().getSourceRange().End;
945+
if (startLoc.isValid() != endLoc.isValid()) return SourceRange();
946+
return { startLoc, endLoc };
932947
}
933948

934949
static StaticSpellingKind getCorrectStaticSpellingForDecl(const Decl *D) {
@@ -4568,9 +4583,12 @@ SourceRange FuncDecl::getSourceRange() const {
45684583
if (!B->isImplicit())
45694584
return { StartLoc, B->getEndLoc() };
45704585
}
4586+
4587+
if (this->isAccessor())
4588+
return StartLoc;
4589+
45714590
if (getBodyResultTypeLoc().hasLocation() &&
4572-
getBodyResultTypeLoc().getSourceRange().End.isValid() &&
4573-
!this->isAccessor())
4591+
getBodyResultTypeLoc().getSourceRange().End.isValid())
45744592
return { StartLoc, getBodyResultTypeLoc().getSourceRange().End };
45754593
auto LastParamListEndLoc = getParameterLists().back()->getSourceRange().End;
45764594
if (LastParamListEndLoc.isValid())

lib/AST/Pattern.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ SourceRange TuplePattern::getSourceRange() const {
349349
}
350350

351351
SourceRange TypedPattern::getSourceRange() const {
352-
if (isImplicit()) {
352+
if (isImplicit() || isPropagatedType()) {
353353
// If a TypedPattern is implicit, then its type is definitely implicit, so
354354
// we should ignore its location. On the other hand, the sub-pattern can
355355
// be explicit or implicit.

test/Parse/implicit_getter_incomplete.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func test1() {
1212

1313
// Would trigger assertion when AST verifier checks source ranges ("child source range not contained within its parent")
1414
func test2() { // expected-note {{match}}
15-
var a : Int { // expected-note {{match}} expected-note {{did you mean 'a'?}}
15+
var a : Int { // expected-note {{match}}
1616
switch i { // expected-error {{unresolved identifier}}
1717
} // expected-error {{'switch' statement body must have at least one}}
18-
// expected-error 2 {{expected '}'}}
18+
// expected-error@+1 2 {{expected '}'}}

test/SourceKit/DocumentStructure/access_parse.swift.response

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@
437437
key.setter_accessibility: source.lang.swift.accessibility.private,
438438
key.name: "pubPrivSetProp",
439439
key.offset: 1000,
440-
key.length: 24,
440+
key.length: 58,
441441
key.typename: "Int",
442442
key.nameoffset: 1004,
443443
key.namelength: 14,
@@ -450,7 +450,7 @@
450450
key.setter_accessibility: source.lang.swift.accessibility.fileprivate,
451451
key.name: "pubFPSetProp",
452452
key.offset: 1083,
453-
key.length: 22,
453+
key.length: 56,
454454
key.typename: "Int",
455455
key.nameoffset: 1087,
456456
key.namelength: 12,
@@ -463,7 +463,7 @@
463463
key.setter_accessibility: source.lang.swift.accessibility.internal,
464464
key.name: "pubIntSetProp",
465465
key.offset: 1161,
466-
key.length: 23,
466+
key.length: 57,
467467
key.typename: "Int",
468468
key.nameoffset: 1165,
469469
key.namelength: 13,
@@ -475,7 +475,7 @@
475475
key.accessibility: source.lang.swift.accessibility.public,
476476
key.name: "pubGetOnly",
477477
key.offset: 1227,
478-
key.length: 20,
478+
key.length: 44,
479479
key.typename: "Int",
480480
key.nameoffset: 1231,
481481
key.namelength: 10,

test/SourceKit/InterfaceGen/gen_clang_module.swift.helper.response

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public var FooHelperUnnamedEnumeratorA2: Int { get }
197197
key.setter_accessibility: source.lang.swift.accessibility.public,
198198
key.name: "FooHelperUnnamedEnumeratorA1",
199199
key.offset: 123,
200-
key.length: 37,
200+
key.length: 45,
201201
key.typename: "Int",
202202
key.nameoffset: 127,
203203
key.namelength: 28
@@ -208,7 +208,7 @@ public var FooHelperUnnamedEnumeratorA2: Int { get }
208208
key.setter_accessibility: source.lang.swift.accessibility.public,
209209
key.name: "FooHelperUnnamedEnumeratorA2",
210210
key.offset: 176,
211-
key.length: 37,
211+
key.length: 45,
212212
key.typename: "Int",
213213
key.nameoffset: 180,
214214
key.namelength: 28

test/SourceKit/InterfaceGen/gen_clang_module.swift.response

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3894,7 +3894,7 @@ public class FooOverlayClassDerived : Foo.FooOverlayClassBase {
38943894
key.setter_accessibility: source.lang.swift.accessibility.public,
38953895
key.name: "FooEnum1X",
38963896
key.offset: 380,
3897-
key.length: 23,
3897+
key.length: 31,
38983898
key.typename: "FooEnum1",
38993899
key.nameoffset: 384,
39003900
key.namelength: 9
@@ -3989,7 +3989,7 @@ public class FooOverlayClassDerived : Foo.FooOverlayClassBase {
39893989
key.setter_accessibility: source.lang.swift.accessibility.public,
39903990
key.name: "FooEnum2X",
39913991
key.offset: 582,
3992-
key.length: 23,
3992+
key.length: 31,
39933993
key.typename: "FooEnum2",
39943994
key.nameoffset: 586,
39953995
key.namelength: 9
@@ -4000,7 +4000,7 @@ public class FooOverlayClassDerived : Foo.FooOverlayClassBase {
40004000
key.setter_accessibility: source.lang.swift.accessibility.public,
40014001
key.name: "FooEnum2Y",
40024002
key.offset: 621,
4003-
key.length: 23,
4003+
key.length: 31,
40044004
key.typename: "FooEnum2",
40054005
key.nameoffset: 625,
40064006
key.namelength: 9
@@ -4095,7 +4095,7 @@ public class FooOverlayClassDerived : Foo.FooOverlayClassBase {
40954095
key.setter_accessibility: source.lang.swift.accessibility.public,
40964096
key.name: "FooEnum3X",
40974097
key.offset: 822,
4098-
key.length: 23,
4098+
key.length: 31,
40994099
key.typename: "FooEnum3",
41004100
key.nameoffset: 826,
41014101
key.namelength: 9
@@ -4106,7 +4106,7 @@ public class FooOverlayClassDerived : Foo.FooOverlayClassBase {
41064106
key.setter_accessibility: source.lang.swift.accessibility.public,
41074107
key.name: "FooEnum3Y",
41084108
key.offset: 861,
4109-
key.length: 23,
4109+
key.length: 31,
41104110
key.typename: "FooEnum3",
41114111
key.nameoffset: 865,
41124112
key.namelength: 9
@@ -4239,7 +4239,7 @@ public class FooOverlayClassDerived : Foo.FooOverlayClassBase {
42394239
key.setter_accessibility: source.lang.swift.accessibility.public,
42404240
key.name: "enableMince",
42414241
key.offset: 1258,
4242-
key.length: 41,
4242+
key.length: 49,
42434243
key.typename: "FooRuncingOptions",
42444244
key.nameoffset: 1269,
42454245
key.namelength: 11
@@ -4250,7 +4250,7 @@ public class FooOverlayClassDerived : Foo.FooOverlayClassBase {
42504250
key.setter_accessibility: source.lang.swift.accessibility.public,
42514251
key.name: "enableQuince",
42524252
key.offset: 1320,
4253-
key.length: 42,
4253+
key.length: 50,
42544254
key.typename: "FooRuncingOptions",
42554255
key.nameoffset: 1331,
42564256
key.namelength: 12
@@ -4750,7 +4750,7 @@ public class FooOverlayClassDerived : Foo.FooOverlayClassBase {
47504750
key.setter_accessibility: source.lang.swift.accessibility.public,
47514751
key.name: "fooProperty1",
47524752
key.offset: 3560,
4753-
key.length: 23,
4753+
key.length: 35,
47544754
key.typename: "Int32",
47554755
key.nameoffset: 3564,
47564756
key.namelength: 12,
@@ -4763,7 +4763,7 @@ public class FooOverlayClassDerived : Foo.FooOverlayClassBase {
47634763
key.setter_accessibility: source.lang.swift.accessibility.public,
47644764
key.name: "fooProperty2",
47654765
key.offset: 3608,
4766-
key.length: 23,
4766+
key.length: 35,
47674767
key.typename: "Int32",
47684768
key.nameoffset: 3612,
47694769
key.namelength: 12,
@@ -4775,7 +4775,7 @@ public class FooOverlayClassDerived : Foo.FooOverlayClassBase {
47754775
key.accessibility: source.lang.swift.accessibility.public,
47764776
key.name: "fooProperty3",
47774777
key.offset: 3656,
4778-
key.length: 23,
4778+
key.length: 31,
47794779
key.typename: "Int32",
47804780
key.nameoffset: 3660,
47814781
key.namelength: 12,
@@ -4963,7 +4963,7 @@ public class FooOverlayClassDerived : Foo.FooOverlayClassBase {
49634963
key.setter_accessibility: source.lang.swift.accessibility.open,
49644964
key.name: "fooProperty3",
49654965
key.offset: 4226,
4966-
key.length: 23,
4966+
key.length: 31,
49674967
key.typename: "Int32",
49684968
key.nameoffset: 4230,
49694969
key.namelength: 12
@@ -5052,7 +5052,7 @@ public class FooOverlayClassDerived : Foo.FooOverlayClassBase {
50525052
key.setter_accessibility: source.lang.swift.accessibility.public,
50535053
key.name: "FOO_MACRO_1",
50545054
key.offset: 4606,
5055-
key.length: 22,
5055+
key.length: 30,
50565056
key.typename: "Int32",
50575057
key.nameoffset: 4610,
50585058
key.namelength: 11
@@ -5063,7 +5063,7 @@ public class FooOverlayClassDerived : Foo.FooOverlayClassBase {
50635063
key.setter_accessibility: source.lang.swift.accessibility.public,
50645064
key.name: "FOO_MACRO_2",
50655065
key.offset: 4644,
5066-
key.length: 22,
5066+
key.length: 30,
50675067
key.typename: "Int32",
50685068
key.nameoffset: 4648,
50695069
key.namelength: 11
@@ -5074,7 +5074,7 @@ public class FooOverlayClassDerived : Foo.FooOverlayClassBase {
50745074
key.setter_accessibility: source.lang.swift.accessibility.public,
50755075
key.name: "FOO_MACRO_3",
50765076
key.offset: 4682,
5077-
key.length: 22,
5077+
key.length: 30,
50785078
key.typename: "Int32",
50795079
key.nameoffset: 4686,
50805080
key.namelength: 11
@@ -5085,7 +5085,7 @@ public class FooOverlayClassDerived : Foo.FooOverlayClassBase {
50855085
key.setter_accessibility: source.lang.swift.accessibility.public,
50865086
key.name: "FOO_MACRO_4",
50875087
key.offset: 4759,
5088-
key.length: 23,
5088+
key.length: 31,
50895089
key.typename: "UInt32",
50905090
key.nameoffset: 4763,
50915091
key.namelength: 11
@@ -5096,7 +5096,7 @@ public class FooOverlayClassDerived : Foo.FooOverlayClassBase {
50965096
key.setter_accessibility: source.lang.swift.accessibility.public,
50975097
key.name: "FOO_MACRO_5",
50985098
key.offset: 4798,
5099-
key.length: 23,
5099+
key.length: 31,
51005100
key.typename: "UInt64",
51015101
key.nameoffset: 4802,
51025102
key.namelength: 11
@@ -5107,7 +5107,7 @@ public class FooOverlayClassDerived : Foo.FooOverlayClassBase {
51075107
key.setter_accessibility: source.lang.swift.accessibility.public,
51085108
key.name: "FOO_MACRO_REDEF_1",
51095109
key.offset: 4838,
5110-
key.length: 28,
5110+
key.length: 36,
51115111
key.typename: "Int32",
51125112
key.nameoffset: 4842,
51135113
key.namelength: 17
@@ -5118,7 +5118,7 @@ public class FooOverlayClassDerived : Foo.FooOverlayClassBase {
51185118
key.setter_accessibility: source.lang.swift.accessibility.public,
51195119
key.name: "FOO_MACRO_REDEF_2",
51205120
key.offset: 4883,
5121-
key.length: 28,
5121+
key.length: 36,
51225122
key.typename: "Int32",
51235123
key.nameoffset: 4887,
51245124
key.namelength: 17

test/SourceKit/InterfaceGen/gen_clang_module.swift.sub.response

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ public var FooSubUnnamedEnumeratorA1: Int { get }
420420
key.setter_accessibility: source.lang.swift.accessibility.public,
421421
key.name: "FooSubEnum1X",
422422
key.offset: 248,
423-
key.length: 29,
423+
key.length: 37,
424424
key.typename: "FooSubEnum1",
425425
key.nameoffset: 252,
426426
key.namelength: 12
@@ -431,7 +431,7 @@ public var FooSubUnnamedEnumeratorA1: Int { get }
431431
key.setter_accessibility: source.lang.swift.accessibility.public,
432432
key.name: "FooSubEnum1Y",
433433
key.offset: 293,
434-
key.length: 29,
434+
key.length: 37,
435435
key.typename: "FooSubEnum1",
436436
key.nameoffset: 297,
437437
key.namelength: 12
@@ -442,7 +442,7 @@ public var FooSubUnnamedEnumeratorA1: Int { get }
442442
key.setter_accessibility: source.lang.swift.accessibility.public,
443443
key.name: "FooSubUnnamedEnumeratorA1",
444444
key.offset: 339,
445-
key.length: 34,
445+
key.length: 42,
446446
key.typename: "Int",
447447
key.nameoffset: 343,
448448
key.namelength: 25

test/decl/var/static_var.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ struct InMemberFunc {
9292
}
9393
}
9494

95-
struct S { // expected-note 3{{extended type declared here}}
95+
struct S { // expected-note 3{{extended type declared here}} expected-note{{did you mean 'S'?}}
9696
static var v1: Int = 0
9797
class var v2: Int = 0 // expected-error {{class properties are only allowed within classes; use 'static' to declare a static property}} {{3-8=static}}
9898

@@ -116,7 +116,7 @@ extension S {
116116
class let el2: Int = 0 // expected-error {{class properties are only allowed within classes; use 'static' to declare a static property}} {{3-8=static}}
117117
}
118118

119-
enum E { // expected-note 3{{extended type declared here}}
119+
enum E { // expected-note 3{{extended type declared here}} expected-note{{did you mean 'E'?}}
120120
static var v1: Int = 0
121121
class var v2: Int = 0 // expected-error {{class properties are only allowed within classes; use 'static' to declare a static property}} {{3-8=static}}
122122

@@ -141,7 +141,7 @@ extension E {
141141
class let el2: Int = 0 // expected-error {{class properties are only allowed within classes; use 'static' to declare a static property}} {{3-8=static}}
142142
}
143143

144-
class C {
144+
class C { // expected-note{{did you mean 'C'?}}
145145
static var v1: Int = 0
146146
class final var v3: Int = 0 // expected-error {{class stored properties not supported}}
147147
class var v4: Int = 0 // expected-error {{class stored properties not supported}}
@@ -171,7 +171,7 @@ extension C {
171171
static final let el4: Int = 0 // expected-error {{static declarations are already final}} {{10-16=}}
172172
}
173173

174-
protocol P {
174+
protocol P { // expected-note{{did you mean 'P'?}}
175175
// Both `static` and `class` property requirements are equivalent in protocols rdar://problem/17198298
176176
static var v1: Int { get }
177177
class var v2: Int { get } // expected-error {{class properties are only allowed within classes; use 'static' to declare a static property}} {{3-8=static}}

0 commit comments

Comments
 (0)