Skip to content

Commit 5dce8a0

Browse files
committed
Unified: Accessors inherit modifiers
1 parent fd2bfc8 commit 5dce8a0

5 files changed

Lines changed: 23 additions & 10 deletions

File tree

‎unified/extractor/src/languages/swift/swift.rs‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,13 +326,15 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
326326
rule!(
327327
(variableDecl
328328
bindingSpecifier: @@spec
329+
modifiers: _* @mods
329330
bindings: (patternBinding
330331
pattern: (identifierPattern identifier: @@name)
331332
typeAnnotation: (typeAnnotation type: @ty)
332333
accessorBlock: (accessorBlock accessors: (codeBlockItem)+) @body))
333334
=>
334335
(accessor_declaration
335336
modifier: (modifier #{spec})
337+
modifier: {mods}
336338
name_node: (identifier #{name})
337339
type: {ty}
338340
accessor_kind: {ctx.literal_at_start_of("accessor_kind", "get", body)}
@@ -356,14 +358,16 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
356358
rule!(
357359
(variableDecl
358360
bindingSpecifier: @@spec
361+
modifiers: _* @mods
359362
bindings: (patternBinding
360363
pattern: (identifierPattern identifier: @@name)
361364
typeAnnotation: (typeAnnotation type: @ty)
362365
initializer: (initializerClause value: @@val)?
363366
accessorBlock: (accessorBlock accessors: (accessorDecl)+ @@accessors)))
364367
=>
365368
member* {
366-
ctx.outer_modifiers = vec![tree!((modifier #{spec}))];
369+
let binding = tree!((modifier #{spec}));
370+
ctx.outer_modifiers = std::iter::once(binding).chain(mods).collect();
367371
ctx.property_name_node = Some(tree!((identifier #{name})));
368372
let mut result = Vec::new();
369373
if let Some(val) = val {
@@ -397,7 +401,8 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
397401
// (`get`/`set`/`willSet`/`didSet`). The body is optional: a body-bearing
398402
// accessor (a computed getter/setter, or a `willSet`/`didSet` observer)
399403
// carries the binding modifier and a translated body, whereas a bodyless
400-
// one (a protocol requirement) carries neither. The property context is
404+
// one (a protocol requirement) carries neither. Both retain the other
405+
// property modifiers, such as `static`. The property context is
401406
// read out *before* translating the body, which resets `ctx` so the
402407
// accessor's context does not leak into the body subtree.
403408
rule!(
@@ -408,7 +413,8 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
408413
if body.is_some() {
409414
ctx.outer_modifiers.clone()
410415
} else {
411-
Vec::new()
416+
// The binding modifier is always first; omit only that.
417+
ctx.outer_modifiers.iter().skip(1).copied().collect()
412418
}
413419
}
414420
modifier: {chained_modifier(&mut ctx)}

‎unified/extractor/tests/corpus/swift/types/computed-property.output‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Rect {
22
var w: Double
33
var h: Double
4-
var area: Double {
4+
public var area: Double {
55
return w * h
66
}
77
}
@@ -60,6 +60,8 @@ sourceFile
6060
variableDecl
6161
attributes:
6262
modifiers:
63+
declModifier
64+
name: public
6365
bindingSpecifier: var
6466
bindings:
6567
patternBinding
@@ -113,8 +115,10 @@ top_level source="⟨body⟩"
113115
modifier: modifier "var" source="var"
114116
pattern: identifier "h" source="h"
115117
type: identifier "Double" source="Double"
116-
accessor_declaration source="⟨modifier⟩ ⟨name_node⟩: ⟨type⟩ ⟨body⟩"
117-
modifier: modifier "var" source="var"
118+
accessor_declaration source="⟨modifier⟩ ⟨modifier⟩ ⟨name_node⟩: ⟨type⟩ ⟨body⟩"
119+
modifier:
120+
modifier "var" source="var"
121+
modifier "public" source="public"
118122
name_node: identifier "area" source="area"
119123
accessor_kind: accessor_kind "get" source=""
120124
type: identifier "Double" source="Double"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Rect {
22
var w: Double
33
var h: Double
4-
var area: Double {
4+
public var area: Double {
55
return w * h
66
}
77
}

‎unified/extractor/tests/corpus/swift/types/protocol-with-read-only-and-read-write-property-requirements.output‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
protocol P {
2-
var foo: Int { get }
2+
static var foo: Int { get }
33
var bar: String { get set }
44
var count: Int { get }
55
}
@@ -24,6 +24,8 @@ sourceFile
2424
variableDecl
2525
attributes:
2626
modifiers:
27+
declModifier
28+
name: static
2729
bindingSpecifier: var
2830
bindings:
2931
patternBinding
@@ -106,11 +108,12 @@ top_level source="⟨body⟩"
106108
body:
107109
block source="⟨stmt⟩"
108110
stmt:
109-
class_like_declaration source="⟨modifier⟩ ⟨name_node⟩ {\n var foo: Int { ⟨member⟩ }\n var bar: String { ⟨member⟩ ⟨member⟩ }\n var count: Int { ⟨member⟩ }\n}"
111+
class_like_declaration source="⟨modifier⟩ ⟨name_node⟩ {\n static var foo: Int { ⟨member⟩ }\n var bar: String { ⟨member⟩ ⟨member⟩ }\n var count: Int { ⟨member⟩ }\n}"
110112
modifier: modifier "protocol" source="protocol"
111113
name_node: identifier "P" source="P"
112114
member:
113115
accessor_declaration source="⟨accessor_kind⟩"
116+
modifier: modifier "static" source="static" (external)
114117
name_node: identifier "foo" source="foo" (external)
115118
accessor_kind: accessor_kind "get" source="get"
116119
type: identifier "Int" source="Int" (external)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
protocol P {
2-
var foo: Int { get }
2+
static var foo: Int { get }
33
var bar: String { get set }
44
var count: Int { get }
55
}

0 commit comments

Comments
 (0)