Skip to content

Commit e60c5c2

Browse files
committed
[SourceKit] Avoid calling AbstractStorageDecl::isSettable during syntactic operations
`isSettable` can trigger typechecking and cause crashes during pure syntactic requests. rdar://60441208
1 parent b4a65a4 commit e60c5c2

File tree

8 files changed

+313
-26
lines changed

8 files changed

+313
-26
lines changed

lib/AST/Decl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3559,7 +3559,7 @@ static Type computeNominalType(NominalTypeDecl *decl, DeclTypeKind kind) {
35593559
// Get the parent type.
35603560
Type Ty;
35613561
DeclContext *dc = decl->getDeclContext();
3562-
if (dc->isTypeContext()) {
3562+
if (!isa<ProtocolDecl>(decl) && dc->isTypeContext()) {
35633563
switch (kind) {
35643564
case DeclTypeKind::DeclaredType: {
35653565
auto *nominal = dc->getSelfNominalTypeDecl();

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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,13 @@ public extension Outer2 {
165165
}
166166
}
167167
#endif
168+
169+
var var_with_didset = 10 {
170+
didSet { print(oldValue) }
171+
}
172+
173+
#if os(iOS)
174+
@objc protocol MyProtocol: NSObjectProtocol {
175+
var thing: NSObject {get}
176+
}
177+
#endif

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

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
key.offset: 0,
3-
key.length: 2587,
3+
key.length: 2744,
44
key.diagnostic_stage: source.diagnostic.stage.swift.parse,
55
key.substructure: [
66
{
@@ -1615,6 +1615,73 @@
16151615
]
16161616
}
16171617
]
1618+
},
1619+
{
1620+
key.kind: source.lang.swift.decl.var.global,
1621+
key.accessibility: source.lang.swift.accessibility.internal,
1622+
key.setter_accessibility: source.lang.swift.accessibility.internal,
1623+
key.name: "var_with_didset",
1624+
key.offset: 2588,
1625+
key.length: 57,
1626+
key.nameoffset: 2592,
1627+
key.namelength: 15,
1628+
key.bodyoffset: 2614,
1629+
key.bodylength: 30
1630+
},
1631+
{
1632+
key.kind: source.lang.swift.expr.call,
1633+
key.name: "print",
1634+
key.offset: 2626,
1635+
key.length: 15,
1636+
key.nameoffset: 2626,
1637+
key.namelength: 5,
1638+
key.bodyoffset: 2632,
1639+
key.bodylength: 8
1640+
},
1641+
{
1642+
key.kind: source.lang.swift.decl.protocol,
1643+
key.accessibility: source.lang.swift.accessibility.internal,
1644+
key.name: "MyProtocol",
1645+
key.offset: 2665,
1646+
key.length: 71,
1647+
key.runtime_name: "_TtP4main10MyProtocol_",
1648+
key.nameoffset: 2674,
1649+
key.namelength: 10,
1650+
key.bodyoffset: 2704,
1651+
key.bodylength: 31,
1652+
key.inheritedtypes: [
1653+
{
1654+
key.name: "NSObjectProtocol"
1655+
}
1656+
],
1657+
key.attributes: [
1658+
{
1659+
key.offset: 2659,
1660+
key.length: 5,
1661+
key.attribute: source.decl.attribute.objc
1662+
}
1663+
],
1664+
key.elements: [
1665+
{
1666+
key.kind: source.lang.swift.structure.elem.typeref,
1667+
key.offset: 2686,
1668+
key.length: 16
1669+
}
1670+
],
1671+
key.substructure: [
1672+
{
1673+
key.kind: source.lang.swift.decl.var.instance,
1674+
key.accessibility: source.lang.swift.accessibility.internal,
1675+
key.name: "thing",
1676+
key.offset: 2709,
1677+
key.length: 25,
1678+
key.typename: "NSObject",
1679+
key.nameoffset: 2713,
1680+
key.namelength: 5,
1681+
key.bodyoffset: 2730,
1682+
key.bodylength: 3
1683+
}
1684+
]
16181685
}
16191686
],
16201687
key.diagnostics: [

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

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
key.offset: 0,
3-
key.length: 2587,
3+
key.length: 2744,
44
key.diagnostic_stage: source.diagnostic.stage.swift.parse,
55
key.substructure: [
66
{
@@ -1615,6 +1615,73 @@
16151615
]
16161616
}
16171617
]
1618+
},
1619+
{
1620+
key.kind: source.lang.swift.decl.var.global,
1621+
key.accessibility: source.lang.swift.accessibility.internal,
1622+
key.setter_accessibility: source.lang.swift.accessibility.internal,
1623+
key.name: "var_with_didset",
1624+
key.offset: 2588,
1625+
key.length: 57,
1626+
key.nameoffset: 2592,
1627+
key.namelength: 15,
1628+
key.bodyoffset: 2614,
1629+
key.bodylength: 30
1630+
},
1631+
{
1632+
key.kind: source.lang.swift.expr.call,
1633+
key.name: "print",
1634+
key.offset: 2626,
1635+
key.length: 15,
1636+
key.nameoffset: 2626,
1637+
key.namelength: 5,
1638+
key.bodyoffset: 2632,
1639+
key.bodylength: 8
1640+
},
1641+
{
1642+
key.kind: source.lang.swift.decl.protocol,
1643+
key.accessibility: source.lang.swift.accessibility.internal,
1644+
key.name: "MyProtocol",
1645+
key.offset: 2665,
1646+
key.length: 71,
1647+
key.runtime_name: "_TtP4main10MyProtocol_",
1648+
key.nameoffset: 2674,
1649+
key.namelength: 10,
1650+
key.bodyoffset: 2704,
1651+
key.bodylength: 31,
1652+
key.inheritedtypes: [
1653+
{
1654+
key.name: "NSObjectProtocol"
1655+
}
1656+
],
1657+
key.attributes: [
1658+
{
1659+
key.offset: 2659,
1660+
key.length: 5,
1661+
key.attribute: source.decl.attribute.objc
1662+
}
1663+
],
1664+
key.elements: [
1665+
{
1666+
key.kind: source.lang.swift.structure.elem.typeref,
1667+
key.offset: 2686,
1668+
key.length: 16
1669+
}
1670+
],
1671+
key.substructure: [
1672+
{
1673+
key.kind: source.lang.swift.decl.var.instance,
1674+
key.accessibility: source.lang.swift.accessibility.internal,
1675+
key.name: "thing",
1676+
key.offset: 2709,
1677+
key.length: 25,
1678+
key.typename: "NSObject",
1679+
key.nameoffset: 2713,
1680+
key.namelength: 5,
1681+
key.bodyoffset: 2730,
1682+
key.bodylength: 3
1683+
}
1684+
]
16181685
}
16191686
],
16201687
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,

0 commit comments

Comments
 (0)