Uploaded image for project: 'Swift'
  1. Swift
  2. SR-1327

Ignored expressions should be correctly evaluated if they have side effects

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Medium
    • Resolution: Done
    • Component/s: Compiler
    • Labels:
    • Environment:

      Apple Swift version 4.1 (swiftlang-902.0.34 clang-902.0.30)
      Target: x86_64-apple-darwin17.3.0

      Description

      The following examples should all crash at runtime, but don't:

      // 1
      var x: Int?
      let t = type(of: x!)
      print(t) // Int
      
      // 2
      struct S {
        var x: Int {
          get { fatalError() }
          set {}
        }
      }
      
      var s = S()
      let kp = \S.x
      let t2 = type(of: s[keyPath: kp])
      print(t2) // Int
      
      // 3
      import Foundation
      
      @objc protocol P {}
      var n: P.Protocol?
      var y: Protocol = n!
      print(y) // <Protocol: 0x1005a98f0>
      
      

      The problem is that currently with expressions like type(of:), the subexpression can be emitted as an "ignored expression" (SILGenExr.cpp, SILGenFunction::emitIgnoredExpr), meaning that for loads, we try not to actually perform the load it if we can avoid it (if loading won't have side-effects, that is).

      However, currently we base the side-effects of an lvalue load off whether the components are physical or not. This is incorrect as physical components such as force unwrapping and key-paths can have side effects.

        Attachments

          Activity

            People

            Assignee:
            originaluser2 Hamish Knight
            Reporter:
            originaluser2 Hamish Knight
            Votes:
            2 Vote for this issue
            Watchers:
            4 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: