[SR-2989] Multi-line function chains evaluated before breakpoint hits Created: 19 Oct 2016  Updated: 17 Apr 2018  Resolved: 17 Apr 2018

Status: Resolved
Project: Swift
Component/s: LLDB for Swift

Type: Bug Priority: Medium
Reporter: Ross LeBeau Assignee: Vedant Kumar
Resolution: Won't Do Votes: 1
Labels: None
Environment:

Swift 3.0
macOS 10.12 16A323


Radar URL: rdar://28883410

 Description   

In certain cases, if you have a multi-line function chain, placing a breakpoint on the first line will actually result in the function chain being evaluated before the breakpoint hits.

For example:

func printMarker(_ x: Int) -> Int {
    print("marker")
    return x
}

struct User {
    let name: String
    let age: Int
}

struct Message {
    func mentionedUsersF() -> [User] {
        return [User(name: "Achilles", age: 32), User(name: "Tortoise", age: 105)]
    }
}

let message = Message()

var ages: [Int]

ages = [1, 2, 3]

ages = message
    .mentionedUsersF()
    .map { $0.age }
    .map(printMarker)

print(ages)

Place a breakpoint on the `ages = message` line, and you will see that "marker" has been printed at the time of breaking. The `ages` variable is still equal to `[1, 2, 3]`, however, so it hasn't been set yet.

This also occurs if you use a computed property instead of a function.

This does not happen if:

  • Any of the function calls are moved up to the first line
  • The struct contains a non-computed property (it can be unused, as long as there is one)
  • A class is used instead of struct
  • Functions that are not members of a struct are used instead


 Comments   
Comment by Jordan Rose [ 20 Oct 2016 ]

Adrian Prantl, Enrico Granata, what do you think?

Comment by Adrian Prantl [ 20 Oct 2016 ]

From the description this definitely sounds like a compiler bug.

Comment by Vedant Kumar [ 17 Apr 2018 ]

I looked into this recently and found that there isn't a good way to address this problem.

Any fix that enables stepping on the line "ages = message" prior to a call to map would result in inconsistent stepping behavior between Swift and C-based languages. Further, it might result in spurious extra stepping onto lines which just contain a reference to a decl.

Generated at Fri Dec 14 02:02:20 CST 2018 using Jira 7.13.0#713000-sha1:fbf406879436de2f3fb1cfa09c7fa556fb79615a.