Skip to content

Commit e77c0ef

Browse files
dulmarodmeta-codesync[bot]
authored andcommitted
[swift] Fix crash in model when more than two args are passed
Summary: We expected the model for DerivedEnumEquals to expect 2 arguments, but on complex enums this isn't always the case. Reviewed By: ezgicicek Differential Revision: D89719686 fbshipit-source-id: 10c6a0dc0a95dc180586a090cb101e80d7aee105
1 parent f46f151 commit e77c0ef

File tree

3 files changed

+1026
-444
lines changed

3 files changed

+1026
-444
lines changed

infer/src/pulse/PulseModelsSwift.ml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,15 @@ let builtins_matcher builtin args : unit -> unit DSL.model_monad =
117117
| DynamicCall ->
118118
let arg, args = ProcnameDispatcherBuiltins.expect_at_least_1_arg args builtin_s in
119119
dynamic_call arg args
120-
| DerivedEnumEquals ->
121-
let arg1, arg2 = ProcnameDispatcherBuiltins.expect_2_args args builtin_s in
122-
derived_enum_equals arg1 arg2
120+
| DerivedEnumEquals -> (
121+
let arg1, arg2, args = ProcnameDispatcherBuiltins.expect_at_least_2_args args builtin_s in
122+
(* we are modelling the case for simple enums where there are two args here, in the case
123+
of complex enums there can be more args, but we are not modelling that yet. *)
124+
match args with
125+
| [] ->
126+
derived_enum_equals arg1 arg2
127+
| _ ->
128+
unknown args )
123129

124130

125131
let matchers : matcher list =

infer/tests/codetoanalyze/swift/frontend/student.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,3 +266,22 @@ func using_enums_with_saturday_ok() {
266266
let today = Day.saturday
267267
assert(isWeekend(today) == true)
268268
}
269+
270+
// Example that shows we do not crash on complex enums
271+
// We haven't yet checked that we translate correctly the semantics of this code,
272+
// but it doesn't crash and it doesn't have typechecking errors.
273+
enum CoverPictureModel: Equatable {
274+
case empty
275+
case upload(imageName: String)
276+
}
277+
class Profile {
278+
public private(set) var coverPictureModel: CoverPictureModel = .empty {
279+
didSet {
280+
guard coverPictureModel != oldValue else { return }
281+
updateCollectionViewForItems(["coverPicture"])
282+
}
283+
}
284+
func updateCollectionViewForItems(_ items: [String]) {
285+
print("Updating collection view for items: \(items)")
286+
}
287+
}

0 commit comments

Comments
 (0)