Skip to content

Commit 745b2c2

Browse files
asgerfCopilot
andcommitted
Unified: Pass the setter method's new value as a '#newValue' named parameter
Representing the new value of a setter method as a named parameter with the reserved external name '#newValue', rather than a trailing positional parameter, makes its position independent of the index parameters. This fixes argument mismatch when index parameters with default values are omitted, and distinguishes the setter's Swift shadowing key from getter overloads with one more index parameter. MaD models now refer to the value as 'Argument[#newValue:]', and to the setter as e.g. 'subscript:set(_:#newValue:)'. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent d49f847 commit 745b2c2

12 files changed

Lines changed: 57 additions & 22 deletions

File tree

‎unified/extractor/ast_types.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ named:
542542
# target of an assignment, or is otherwise updated in place, invokes the
543543
# `setter_method` accessor named `foo` (whereas a call in a read position
544544
# invokes the ordinary method `foo`). The setter takes the new value as an
545-
# extra trailing positional parameter.
545+
# extra trailing parameter with the reserved external name `#newValue`.
546546
accessor_declaration:
547547
modifier*: modifier
548548
name_node: identifier

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,8 @@ struct SubscriptAccessor {
188188
/// `accessor_declaration` of kind `setter_method`. The shared parts of the subscript (modifiers, generic and value
189189
/// parameters and the element type) are translated afresh for each accessor,
190190
/// so a getter/setter pair each get their own copy. A setter takes the new
191-
/// value as an extra trailing positional parameter and has no return type.
191+
/// value as an extra trailing parameter named `#newValue` and has no return
192+
/// type.
192193
fn subscript_method(
193194
ctx: &mut yeast::build::BuildCtx<'_, SwiftContext>,
194195
sub: SubscriptContext,
@@ -218,13 +219,18 @@ fn subscript_method(
218219
accessor_kind: (accessor_kind "setter_method" location: {spec})
219220
type_parameter: {type_params}
220221
parameter: {params}
222+
// The new value is passed as a named parameter with the reserved
223+
// external name `#newValue`, so it can't clash with the index
224+
// parameters (including omitted defaults).
221225
parameter: [
222226
(parameter
227+
external_name_node: (identifier "#newValue" location: {spec})
223228
pattern: (identifier #{param_name})
224229
type: {element_type}
225230
location: {param_name})
226231
(parameter
227232
modifier: (modifier "generated")
233+
external_name_node: (identifier "#newValue" location: {spec})
228234
pattern: (identifier #{implicit_param_name} location: {spec})
229235
type: {element_type}
230236
location: {spec})

‎unified/extractor/tests/corpus/swift/subscripts/subscript-get-set.output‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ top_level source="⟨body⟩"
275275
type: identifier "Int" source="Int"
276276
pattern: identifier "other" source="other"
277277
parameter source="⟨pattern⟩"
278+
external_name_node: identifier "#newValue" source="set" (external)
278279
type: identifier "Int" source="Int" (external)
279280
pattern: identifier "v" source="v"
280281
body:
@@ -296,8 +297,9 @@ top_level source="⟨body⟩"
296297
parameter source="⟨pattern⟩: ⟨type⟩"
297298
type: identifier "T" source="T"
298299
pattern: identifier "key" source="key"
299-
parameter source="⟨pattern⟩"
300+
parameter source="⟨external_name_node⟩⟨pattern⟩"
300301
modifier: modifier "generated" source=""
302+
external_name_node: identifier "#newValue" source="set"
301303
type: identifier "T" source="T" (external)
302304
pattern: identifier "newValue" source="set"
303305
body:

‎unified/extractor/tests/corpus/swift/subscripts/subscript-protocol-requirement.output‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@ top_level source="⟨body⟩"
8484
parameter source="⟨pattern⟩: ⟨type⟩"
8585
type: identifier "Int" source="Int"
8686
pattern: identifier "i" source="i"
87-
parameter source="⟨pattern⟩"
87+
parameter source="⟨external_name_node⟩⟨pattern⟩"
8888
modifier: modifier "generated" source=""
89+
external_name_node: identifier "#newValue" source="set"
8990
type: identifier "String" source="String" (external)
9091
pattern: identifier "newValue" source="set"
9192
body: block source=""

‎unified/ql/lib/codeql/unified/internal/FacadeAst.qll‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,8 +480,8 @@ module Unified {
480480
* syntax when a call appears as an assignment target or is otherwise updated in place. Such a call
481481
* to `foo` invokes the `setter_method` accessor named `foo`, rather than the ordinary method `foo`.
482482
*
483-
* The new value is passed as an additional trailing positional parameter. For example, the
484-
* setter of a Swift subscript declaration:
483+
* The new value is passed as an additional trailing parameter with the reserved external name
484+
* `#newValue`. For example, the setter of a Swift subscript declaration:
485485
* ```swift
486486
* subscript(i: Int) -> String {
487487
* get { "42" }

‎unified/ql/lib/codeql/unified/internal/dataflow/DataFlowInstantiation.qll‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,13 @@ module DataFlowInput implements InputSig<Location> {
112112
n.(BuilderNode).isImplicitArgument(call, pos, false)
113113
or
114114
n.(BuilderNode).isSetterValueArgument(call) and
115-
// The new value is passed after the positional arguments, if any
116-
// TODO: This position is wrong if the setter has parameters with default values that are
117-
// omitted at the call site; optional parameters are not yet supported.
118-
pos.asPositional() =
119-
count(Argument arg |
120-
arg = call.asSetterCall().(CallExpr).getAnArgument() and arg.isPositional()
121-
)
115+
if call.asSetterCall() instanceof CallExpr
116+
then
117+
// A setter method takes the new value as a named parameter, `#newValue`
118+
pos.asNamed() = "#newValue"
119+
else
120+
// A property setter takes the new value as its only parameter
121+
pos.asPositional() = 0
122122
or
123123
exists(FlowSummaryOutput::SummaryNode callee |
124124
FlowSummaryOutput::summaryArgumentNode(callee, getSummaryNode(n), pos) and

‎unified/ql/lib/codeql/unified/internal/dataflow/ParameterPositions.qll‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ private newtype TParameterPosition =
88
name = any(Parameter p).getExternalName()
99
or
1010
name = any(Argument arg).getName()
11+
or
12+
name = "#newValue" // the new value passed to a setter method
1113
}
1214

1315
class ParameterPosition extends TParameterPosition {

‎unified/ql/lib/codeql/unified/internal/mad/LegacyMaD.qll‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,13 @@ private predicate methodCallSelector(CallExpr call, string name, string argLabel
8282
* method invoked by a call to `foo` is named `foo:set` in models, such as `subscript:set` for
8383
* `x[i] = y`.
8484
*
85-
* The new value is passed as an extra positional argument after the explicit ones.
85+
* The new value is passed as an extra named argument `#newValue` after the explicit ones, so
86+
* models refer to it as `Argument[#newValue:]`, e.g. `subscript:set(_:#newValue:)`.
8687
*/
8788
pragma[nomagic]
8889
private predicate setterCallSelector(CallExpr call, string name, string argLabels) {
8990
name = call.getCallee().(MemberAccessExpr).getMemberName() + ":set" and
90-
argLabels = getArgLabelsFromCall(call) + "_:"
91+
argLabels = getArgLabelsFromCall(call) + "#newValue:"
9192
}
9293

9394
pragma[nomagic]
@@ -365,11 +366,11 @@ private DataFlow::Node getSinkFromCall(DataFlowCall call, AccessPathToken token)
365366
arg.getName() + ":" = token.getAnArgument()
366367
)
367368
or
368-
// The new value passed to a setter comes after the positional arguments
369+
// The new value passed to a setter method, as the named argument `#newValue`
369370
token.getName() = "Argument" and
371+
token.getAnArgument() = "#newValue:" and
370372
result.(BuilderNode).isSetterValueArgument(call) and
371-
parseIntUnbounded(token.getAnArgument()) =
372-
count(Argument arg | arg = call.getCallExpr().getAnArgument() and arg.isPositional())
373+
call.asSetterCall() instanceof CallExpr
373374
or
374375
token.getName() = "Argument" and
375376
token.getAnArgument() = "self" and

‎unified/ql/test/library-tests/dataflow/subscripts.swift‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func s15() {
188188
class Base {
189189
subscript(i: Int) -> String {
190190
get { "safe" }
191-
set { sink(newValue) } // $ MISSING: hasValueFlow=s15.1
191+
set { sink(newValue) } // $ hasValueFlow=s15.1
192192
}
193193
}
194194
class Derived: Base {
@@ -197,3 +197,14 @@ func s15() {
197197
let d = Derived()
198198
d[0] = source("s15.1")
199199
}
200+
201+
func s16() {
202+
class C {
203+
subscript(i: Int, j: Int = 0) -> String {
204+
get { "safe" }
205+
set { sink(newValue) } // $ hasValueFlow=s16.1
206+
}
207+
}
208+
let c = C()
209+
c[1] = source("s16.1")
210+
}

‎unified/ql/test/library-tests/dataflow/test.expected‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,10 @@ edges
510510
| subscripts.swift:178:27:178:41 | source(...) | subscripts.swift:178:19:178:23 | field | provenance | |
511511
| subscripts.swift:183:19:183:19 | c [field] | subscripts.swift:183:19:183:25 | ... .field | provenance | |
512512
| subscripts.swift:184:10:184:10 | c [field] | subscripts.swift:184:10:184:16 | ... .field | provenance | |
513+
| subscripts.swift:198:5:198:8 | ... .subscript(...) | subscripts.swift:191:24:191:31 | newValue | provenance | |
514+
| subscripts.swift:198:12:198:26 | source(...) | subscripts.swift:198:5:198:8 | ... .subscript(...) | provenance | |
515+
| subscripts.swift:209:5:209:8 | ... .subscript(...) | subscripts.swift:205:24:205:31 | newValue | provenance | |
516+
| subscripts.swift:209:12:209:26 | source(...) | subscripts.swift:209:5:209:8 | ... .subscript(...) | provenance | |
513517
| test.swift:17:10:17:23 | source(...) | test.swift:17:10:17:32 | ... + ... | provenance | |
514518
| test.swift:18:19:18:32 | source(...) | test.swift:18:10:18:32 | ... + ... | provenance | |
515519
| test.swift:20:13:20:26 | source(...) | test.swift:20:10:20:33 | StringInterpolationExpr | provenance | |
@@ -1303,6 +1307,12 @@ nodes
13031307
| subscripts.swift:183:19:183:25 | ... .field | semmle.label | ... .field |
13041308
| subscripts.swift:184:10:184:10 | c [field] | semmle.label | c [field] |
13051309
| subscripts.swift:184:10:184:16 | ... .field | semmle.label | ... .field |
1310+
| subscripts.swift:191:24:191:31 | newValue | semmle.label | newValue |
1311+
| subscripts.swift:198:5:198:8 | ... .subscript(...) | semmle.label | ... .subscript(...) |
1312+
| subscripts.swift:198:12:198:26 | source(...) | semmle.label | source(...) |
1313+
| subscripts.swift:205:24:205:31 | newValue | semmle.label | newValue |
1314+
| subscripts.swift:209:5:209:8 | ... .subscript(...) | semmle.label | ... .subscript(...) |
1315+
| subscripts.swift:209:12:209:26 | source(...) | semmle.label | source(...) |
13061316
| test.swift:9:10:9:21 | source(...) | semmle.label | source(...) |
13071317
| test.swift:17:10:17:23 | source(...) | semmle.label | source(...) |
13081318
| test.swift:17:10:17:32 | ... + ... | semmle.label | ... + ... |
@@ -1705,6 +1715,8 @@ testFailures
17051715
| subscripts.swift:167:10:167:16 | ... .field | subscripts.swift:161:27:161:41 | source(...) | subscripts.swift:167:10:167:16 | ... .field | $@ | subscripts.swift:161:27:161:41 | source(...) | source(...) |
17061716
| subscripts.swift:183:19:183:25 | ... .field | subscripts.swift:178:27:178:41 | source(...) | subscripts.swift:183:19:183:25 | ... .field | $@ | subscripts.swift:178:27:178:41 | source(...) | source(...) |
17071717
| subscripts.swift:184:10:184:16 | ... .field | subscripts.swift:178:27:178:41 | source(...) | subscripts.swift:184:10:184:16 | ... .field | $@ | subscripts.swift:178:27:178:41 | source(...) | source(...) |
1718+
| subscripts.swift:191:24:191:31 | newValue | subscripts.swift:198:12:198:26 | source(...) | subscripts.swift:191:24:191:31 | newValue | $@ | subscripts.swift:198:12:198:26 | source(...) | source(...) |
1719+
| subscripts.swift:205:24:205:31 | newValue | subscripts.swift:209:12:209:26 | source(...) | subscripts.swift:205:24:205:31 | newValue | $@ | subscripts.swift:209:12:209:26 | source(...) | source(...) |
17081720
| test.swift:9:10:9:21 | source(...) | test.swift:9:10:9:21 | source(...) | test.swift:9:10:9:21 | source(...) | $@ | test.swift:9:10:9:21 | source(...) | source(...) |
17091721
| test.swift:17:10:17:32 | ... + ... | test.swift:17:10:17:23 | source(...) | test.swift:17:10:17:32 | ... + ... | $@ | test.swift:17:10:17:23 | source(...) | source(...) |
17101722
| test.swift:18:10:18:32 | ... + ... | test.swift:18:19:18:32 | source(...) | test.swift:18:10:18:32 | ... + ... | $@ | test.swift:18:19:18:32 | source(...) | source(...) |

0 commit comments

Comments
 (0)