Skip to content

Commit 802af8c

Browse files
authored
Merge pull request #30459 from akyrtzi/avoid-isSettable-for-syntactic
[SourceKit] Avoid calling `AbstractStorageDecl::isSettable` during syntactic operations
2 parents 2c1c13d + b3b3cd3 commit 802af8c

File tree

7 files changed

+171
-25
lines changed

7 files changed

+171
-25
lines changed

test/SourceKit/DocumentStructure/Inputs/invalid.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
extension Worker {
2+
public protocol BasicWorker: Worker {
3+
var eventLoop
4+
}
5+
}
6+
17
class 3 {}
28

39
extension OuterCls {

test/SourceKit/DocumentStructure/Inputs/main.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,7 @@ public extension Outer2 {
168168

169169
@objc(FPBarProto)
170170
protocol BarProtocol {}
171+
172+
var var_with_didset = 10 {
173+
didSet { print(oldValue) }
174+
}

test/SourceKit/DocumentStructure/structure.swift.empty.response

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
key.offset: 0,
3-
key.length: 2630,
3+
key.length: 2689,
44
key.diagnostic_stage: source.diagnostic.stage.swift.parse,
55
key.substructure: [
66
{
@@ -1623,6 +1623,28 @@
16231623
key.attribute: source.decl.attribute.objc.name
16241624
}
16251625
]
1626+
},
1627+
{
1628+
key.kind: source.lang.swift.decl.var.global,
1629+
key.accessibility: source.lang.swift.accessibility.internal,
1630+
key.setter_accessibility: source.lang.swift.accessibility.internal,
1631+
key.name: "var_with_didset",
1632+
key.offset: 2631,
1633+
key.length: 57,
1634+
key.nameoffset: 2635,
1635+
key.namelength: 15,
1636+
key.bodyoffset: 2657,
1637+
key.bodylength: 30
1638+
},
1639+
{
1640+
key.kind: source.lang.swift.expr.call,
1641+
key.name: "print",
1642+
key.offset: 2669,
1643+
key.length: 15,
1644+
key.nameoffset: 2669,
1645+
key.namelength: 5,
1646+
key.bodyoffset: 2675,
1647+
key.bodylength: 8
16261648
}
16271649
],
16281650
key.diagnostics: [

test/SourceKit/DocumentStructure/structure.swift.foobar.response

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
key.offset: 0,
3-
key.length: 2630,
3+
key.length: 2689,
44
key.diagnostic_stage: source.diagnostic.stage.swift.parse,
55
key.substructure: [
66
{
@@ -1623,6 +1623,28 @@
16231623
key.attribute: source.decl.attribute.objc.name
16241624
}
16251625
]
1626+
},
1627+
{
1628+
key.kind: source.lang.swift.decl.var.global,
1629+
key.accessibility: source.lang.swift.accessibility.internal,
1630+
key.setter_accessibility: source.lang.swift.accessibility.internal,
1631+
key.name: "var_with_didset",
1632+
key.offset: 2631,
1633+
key.length: 57,
1634+
key.nameoffset: 2635,
1635+
key.namelength: 15,
1636+
key.bodyoffset: 2657,
1637+
key.bodylength: 30
1638+
},
1639+
{
1640+
key.kind: source.lang.swift.expr.call,
1641+
key.name: "print",
1642+
key.offset: 2669,
1643+
key.length: 15,
1644+
key.nameoffset: 2669,
1645+
key.namelength: 5,
1646+
key.bodyoffset: 2675,
1647+
key.bodylength: 8
16261648
}
16271649
],
16281650
key.diagnostics: [

test/SourceKit/DocumentStructure/structure.swift.invalid.response

Lines changed: 75 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,122 @@
11
{
22
key.offset: 0,
3-
key.length: 82,
3+
key.length: 166,
44
key.diagnostic_stage: source.diagnostic.stage.swift.parse,
55
key.substructure: [
6+
{
7+
key.kind: source.lang.swift.decl.extension,
8+
key.name: "Worker",
9+
key.offset: 0,
10+
key.length: 82,
11+
key.nameoffset: 10,
12+
key.namelength: 6,
13+
key.bodyoffset: 18,
14+
key.bodylength: 63,
15+
key.substructure: [
16+
{
17+
key.kind: source.lang.swift.decl.protocol,
18+
key.accessibility: source.lang.swift.accessibility.public,
19+
key.name: "BasicWorker",
20+
key.offset: 28,
21+
key.length: 52,
22+
key.nameoffset: 37,
23+
key.namelength: 11,
24+
key.bodyoffset: 58,
25+
key.bodylength: 21,
26+
key.inheritedtypes: [
27+
{
28+
key.name: "Worker"
29+
}
30+
],
31+
key.attributes: [
32+
{
33+
key.offset: 21,
34+
key.length: 6,
35+
key.attribute: source.decl.attribute.public
36+
}
37+
],
38+
key.elements: [
39+
{
40+
key.kind: source.lang.swift.structure.elem.typeref,
41+
key.offset: 50,
42+
key.length: 6
43+
}
44+
],
45+
key.substructure: [
46+
{
47+
key.kind: source.lang.swift.decl.var.instance,
48+
key.accessibility: source.lang.swift.accessibility.public,
49+
key.setter_accessibility: source.lang.swift.accessibility.public,
50+
key.name: "eventLoop",
51+
key.offset: 63,
52+
key.length: 13,
53+
key.nameoffset: 67,
54+
key.namelength: 9
55+
}
56+
]
57+
}
58+
]
59+
},
660
{
761
key.kind: source.lang.swift.decl.class,
862
key.accessibility: source.lang.swift.accessibility.internal,
963
key.name: "3",
10-
key.offset: 0,
64+
key.offset: 84,
1165
key.length: 10,
12-
key.nameoffset: 6,
66+
key.nameoffset: 90,
1367
key.namelength: 1,
14-
key.bodyoffset: 9,
68+
key.bodyoffset: 93,
1569
key.bodylength: 0
1670
},
1771
{
1872
key.kind: source.lang.swift.decl.extension,
1973
key.name: "OuterCls",
20-
key.offset: 12,
74+
key.offset: 96,
2175
key.length: 43,
22-
key.nameoffset: 22,
76+
key.nameoffset: 106,
2377
key.namelength: 8,
24-
key.bodyoffset: 32,
78+
key.bodyoffset: 116,
2579
key.bodylength: 22,
2680
key.substructure: [
2781
{
2882
key.kind: source.lang.swift.decl.class,
2983
key.name: "InnerCls1",
30-
key.offset: 35,
84+
key.offset: 119,
3185
key.length: 18,
32-
key.nameoffset: 41,
86+
key.nameoffset: 125,
3387
key.namelength: 9,
34-
key.bodyoffset: 52,
88+
key.bodyoffset: 136,
3589
key.bodylength: 0
3690
}
3791
]
3892
},
3993
{
4094
key.kind: source.lang.swift.expr.closure,
41-
key.offset: 57,
95+
key.offset: 141,
4296
key.length: 24,
4397
key.nameoffset: 0,
4498
key.namelength: 0,
45-
key.bodyoffset: 58,
99+
key.bodyoffset: 142,
46100
key.bodylength: 22,
47101
key.substructure: [
48102
{
49103
key.kind: source.lang.swift.stmt.brace,
50-
key.offset: 57,
104+
key.offset: 141,
51105
key.length: 24,
52106
key.nameoffset: 0,
53107
key.namelength: 0,
54-
key.bodyoffset: 58,
108+
key.bodyoffset: 142,
55109
key.bodylength: 22,
56110
key.substructure: [
57111
{
58112
key.kind: source.lang.swift.decl.class,
59113
key.accessibility: source.lang.swift.accessibility.private,
60114
key.name: "MyCoolClass",
61-
key.offset: 59,
115+
key.offset: 143,
62116
key.length: 22,
63-
key.nameoffset: 65,
117+
key.nameoffset: 149,
64118
key.namelength: 11,
65-
key.bodyoffset: 78,
119+
key.bodyoffset: 162,
66120
key.bodylength: 2
67121
}
68122
]
@@ -72,31 +126,31 @@
72126
],
73127
key.diagnostics: [
74128
{
75-
key.line: 1,
129+
key.line: 7,
76130
key.column: 7,
77131
key.filepath: invalid.swift,
78132
key.severity: source.diagnostic.severity.error,
79133
key.description: "class name can only start with a letter or underscore, not a number",
80134
key.diagnostic_stage: source.diagnostic.stage.swift.parse
81135
},
82136
{
83-
key.line: 7,
137+
key.line: 13,
84138
key.column: 1,
85139
key.filepath: invalid.swift,
86140
key.severity: source.diagnostic.severity.error,
87141
key.description: "top-level statement cannot begin with a closure expression",
88142
key.diagnostic_stage: source.diagnostic.stage.swift.parse
89143
},
90144
{
91-
key.line: 11,
145+
key.line: 17,
92146
key.column: 1,
93147
key.filepath: invalid.swift,
94148
key.severity: source.diagnostic.severity.error,
95149
key.description: "expected '}' at end of closure",
96150
key.diagnostic_stage: source.diagnostic.stage.swift.parse,
97151
key.diagnostics: [
98152
{
99-
key.line: 7,
153+
key.line: 13,
100154
key.column: 1,
101155
key.filepath: invalid.swift,
102156
key.severity: source.diagnostic.severity.note,

test/SourceKit/DocumentStructure/structure.swift.response

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
key.offset: 0,
3-
key.length: 2630,
3+
key.length: 2689,
44
key.diagnostic_stage: source.diagnostic.stage.swift.parse,
55
key.substructure: [
66
{
@@ -1623,6 +1623,28 @@
16231623
key.attribute: source.decl.attribute.objc.name
16241624
}
16251625
]
1626+
},
1627+
{
1628+
key.kind: source.lang.swift.decl.var.global,
1629+
key.accessibility: source.lang.swift.accessibility.internal,
1630+
key.setter_accessibility: source.lang.swift.accessibility.internal,
1631+
key.name: "var_with_didset",
1632+
key.offset: 2631,
1633+
key.length: 57,
1634+
key.nameoffset: 2635,
1635+
key.namelength: 15,
1636+
key.bodyoffset: 2657,
1637+
key.bodylength: 30
1638+
},
1639+
{
1640+
key.kind: source.lang.swift.expr.call,
1641+
key.name: "print",
1642+
key.offset: 2669,
1643+
key.length: 15,
1644+
key.nameoffset: 2669,
1645+
key.namelength: 5,
1646+
key.bodyoffset: 2675,
1647+
key.bodylength: 8
16261648
}
16271649
],
16281650
key.diagnostics: [

tools/SourceKit/lib/SwiftLang/SwiftEditor.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1181,9 +1181,25 @@ static Optional<AccessLevel> inferAccessSyntactically(const ValueDecl *D) {
11811181
llvm_unreachable("Unhandled DeclContextKind in switch.");
11821182
}
11831183

1184+
/// Document structure is a purely syntactic request that shouldn't require name lookup
1185+
/// or type-checking, so this is a best-effort computation.
1186+
static bool inferIsSettableSyntactically(const AbstractStorageDecl *D) {
1187+
if (auto *VD = dyn_cast<VarDecl>(D)) {
1188+
if (VD->isLet())
1189+
return false;
1190+
}
1191+
if (D->hasParsedAccessors()) {
1192+
return D->getParsedAccessor(AccessorKind::Set) != nullptr ||
1193+
D->getParsedAccessor(AccessorKind::WillSet) != nullptr ||
1194+
D->getParsedAccessor(AccessorKind::DidSet) != nullptr;
1195+
} else {
1196+
return true;
1197+
}
1198+
}
1199+
11841200
static Optional<AccessLevel>
11851201
inferSetterAccessSyntactically(const AbstractStorageDecl *D) {
1186-
if (!D->isSettable(/*UseDC=*/nullptr))
1202+
if (!inferIsSettableSyntactically(D))
11871203
return None;
11881204
if (auto *AA = D->getAttrs().getAttribute<SetterAccessAttr>())
11891205
return AA->getAccess();

0 commit comments

Comments
 (0)