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

Wrong method call when binary is built with optimizations

    XMLWordPrintable

    Details

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

      Swift version 5.3-dev (LLVM 36f7fac60d2e676, Swift a5ca83a8b2d499f)
      Target: x86_64-unknown-windows-msvc

      Description

      Under specific conditions binary built with optimizations would call wrong method inside a class. This is the reason why network requests are not working in swift-corelibs-foundation at the moment.

      Sample code have to be split in two sources, otherwise the issue is not reproduced.

      // Grandchild.swift
      class Grandchild: Child {}
      
      // main.swift
      class Root {}
      class Child: Root {
        func doSomething() {
          print("-- \(type(of: self)) - \(#function) - will call doHomework()")
          doHomework()
          print("-- \(type(of: self)) - \(#function) - did call doHomework()")
        }   
        private func eatPopcorn() {
          print("-- \(type(of: self)) - \(#function)")
        }
        func playGames() {
          print("-- \(type(of: self)) - \(#function) - SHOULD NOT HAPPEN")
          eatPopcorn()
        }
        func doHomework() {
          print("-- \(type(of: self)) - \(#function)")
        }
      }
      
      Grandchild().doSomething()
      

      Expected output:

      -- Grandchild - doSomething() - will call doHomework()
      -- Grandchild - doHomework()
      -- Grandchild - doSomething() - did call doHomework()
      

      Actual output:

      -- Grandchild - doSomething() - will call doHomework()
      -- Grandchild - playGames() - SHOULD NOT HAPPEN
      -- Grandchild - eatPopcorn()
      -- Grandchild - doSomething() - did call doHomework()
      

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            lxbndr Alexander Smarus
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: