Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-6035] After upgrade to swift 3. Struct assignment not working as expected when release build. #48592

Closed
swift-ci opened this issue Sep 30, 2017 · 2 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself optimized only Flag: An issue whose reproduction requires optimized compilation

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-6035
Radar rdar://problem/34753633
Original Reporter Andrwyw (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

xcode 8.3.3/xcode 9

swift 3

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, 3.1Regression, Miscompile, OptimizedOnly
Assignee @eeckstein
Priority Medium

md5: 9c72e63b3d8e38edf067e8064f9d14eb

Issue Description:

My project using swift 3 on xcode 8.3.3 and xcode 9.They have same problem.Before convert to swift 3, using swift 2.3.3 * xcode 8.2 is Ok.

Problem : one function return a struct , but the value of stuct is not expected ,both default value.

ps:using inout can solve problem, but i still want to know the real reason.

I write a simple demo code :

import UIKit


protocol EncodingProtocol {
    mutating func decodeFromStream()
}

class ViewController: UIViewController {

override func viewDidLoad() {

    super.viewDidLoad()

    let ip: EventIpRes = EventIpRes()
    ip.decodeFromStream()

    print("111 \(ip.m_ip)")

    }
}

struct IpInfo : EncodingProtocol { 

    var ip: Int = 0

    mutating func decodeFromStream() {
        ip = 110110110
    }
}

class EventIpRes : EncodingProtocol{// #​2. not impelement EncodingProtocol, everything is ok.

    var m_ip = IpInfo()

    func decodeFromStream(){
        let testIp = ProtoBufSerializer.decodeStruct(event: m_ip) as! IpInfo
        print("222 \(testIp)") // #​0. Problem: testIp is default value..

        m_ip = testIp
    }
}

class ProtoBufSerializer {

    class func decodeStruct(event: EncodingProtocol)-> EncodingProtocol{
        // print("33 \(event)")// #​1. uncomment this line, everything is ok.

        var new = event
        new.decodeFromStream()
        return new
    }

}

#0. Problem: testIp is default value..

some test I've tried :

  • build configuration : debug is ok ,release mode has problem.

  • release build , swift compiler optimization level:fast,whole module optimization has problem,others are ok.

#1. add a print statement in function , everything is ok.

#2. not impelement EncodingProtocol, everything is ok.

PS: Although demo code looks strange, but i think it is correct in grammar. it should work fine.....

@belkadan
Copy link
Contributor

belkadan commented Oct 2, 2017

Here's a version with no dependencies on UIKit.

protocol EncodingProtocol {
    mutating func decodeFromStream()
}

struct IpInfo : EncodingProtocol { 

    var ip: Int = 0

    mutating func decodeFromStream() {
        ip = 110110110
    }
}

class EventIpRes : EncodingProtocol{// #​2. not impelement EncodingProtocol, everything is ok.

    var m_ip = IpInfo()

    func decodeFromStream(){
        let testIp = ProtoBufSerializer.decodeStruct(event: m_ip) as! IpInfo
        print("222 \(testIp)") // #​0. Problem: testIp is default value..

        m_ip = testIp
    }
}

class ProtoBufSerializer {

    class func decodeStruct(event: EncodingProtocol)-> EncodingProtocol{
        // print("33 \(event)")// #​1. uncomment this line, everything is ok.

        var new = event
        new.decodeFromStream()
        return new
    }
}

let ip: EventIpRes = EventIpRes()
ip.decodeFromStream()

print("111 \(ip.m_ip)")

The issue only occurs under -O -wmo.

@swift-ci create

@eeckstein
Copy link
Member

fixed in #12234

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself optimized only Flag: An issue whose reproduction requires optimized compilation
Projects
None yet
Development

No branches or pull requests

3 participants