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-9145] No coverage generated for functions in extensions #51641

Closed
hybridcattt opened this issue Oct 30, 2018 · 11 comments
Closed

[SR-9145] No coverage generated for functions in extensions #51641

hybridcattt opened this issue Oct 30, 2018 · 11 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. code coverage Area → source tooling: code coverage compiler The Swift compiler in itself

Comments

@hybridcattt
Copy link

Previous ID SR-9145
Radar rdar://45702366
Original Reporter @hybridcattt
Type Bug
Status Resolved
Resolution Done

Attachment: Download

Environment

Xcode 10.0, Xcode 10.1 (10B61). Swift 4.2, Xcode 11.4

Additional Detail from JIRA
Votes 2
Component/s Compiler
Labels Bug, CodeCoverage
Assignee None
Priority Medium

md5: 7c7ba01e0abdb033ab8272758f7d9824

Issue Description:

In the example below there is no coverage data produced for extensionMethodGeneric() method.
If return r.extensionMethodGeneric() is commented out (e.g. replace with return 0), coverage for extensionMethodGeneric() is gathered as expected (0 hits).

import Foundation

struct MyGenericStruct<T> {
    var value: T
}

class MyClass {
    func method() -> Int {
        let r = MyGenericStruct<Int>(value: 0)
        return r.extensionMethodGeneric()
    }
}

extension MyGenericStruct {
    func extensionMethodGeneric() -> Int {
        print("extensionMethodGeneric called")
        return 0
    }
}
XCTAssertNotNil(MyClass())

Upd: issue also reproduces if instead of a generic type a regular struct is used.

@belkadan
Copy link
Contributor

cc @vedantk

@swift-ci
Copy link
Collaborator

swift-ci commented Nov 1, 2018

Comment by Steve Riggins (JIRA)

I have opened Radar 45468318 for this issue, and it covers a couple others.

However in short:

class FilePrivateSwiftClass {
    
    @discardableResult public class func steveWasHere(value: Int) -> Bool {
        return classHelper(value: value)
    }
    
    fileprivate class func classHelper(value: Int) -> Bool {
        if value >= 42 {
            return true
        }
        
        return false
    }
    
}

class helper gets coverage data. If I then:

extension FilePrivateSwiftClass {
    
    @discardableResult public class func wyattWasHere(value: Int) -> Bool {
        return wyattClassHelper(value: value)
    }
    
    fileprivate class func wyattClassHelper(value: Int) -> Bool {
        if value >= 42 {
            return true
        }
        
        return false
    }


}

wyattClassHelper does not. I can even make it public and test it directly, and it will not get coverage data. It's as if the code does not exist. Now for the fun part:

Remove the test calling wyattWasHere, and wyattClassHelper still has no coverage. However, comment out wyattWasHere, and now wyattClassHelper shows red with no coverage.

@swift-ci
Copy link
Collaborator

Comment by Steve Riggins (JIRA)

Is there any followup on this? Our code coverage is being severely impacted by the changes in 10 (in addition we are seeing random runs report 0% for some files, but that is a different bug and once I have an isolated, reproducible case, will file that issue)

@swift-ci
Copy link
Collaborator

swift-ci commented Apr 1, 2020

Comment by Oleg Danu (JIRA)

Are there any updates here? Since I upgraded to XCode 11.4, the code coverage regressed the same way as described above. Basically, it looks like only some of the class variable definitions get coverage and none of the functions are covered at all, with 100% per file coverage.

@swift-ci
Copy link
Collaborator

Comment by Wes Campaigne (JIRA)

For anyone encountering issues like this: Make sure your testing build configs are set to use "incremental" Swift compilation mode, and not "whole module". The coverage data generated in whole module mode seems to just ignore most Swift code.

@keith
Copy link
Collaborator

keith commented Oct 26, 2020

Note some fixes for WMO + coverage are landing in Xcode 12.2, if this still repros easily can someone test that version to see if it's fixed

@hybridcattt
Copy link
Author

@keith will do!

@swift-ci
Copy link
Collaborator

Comment by Oleg Danu (JIRA)

Setting Swift Compilation Mode to `Incremental` on the Tests target didn't solve the issue for me, on XCode 12.0

@swift-ci
Copy link
Collaborator

Comment by Wes Campaigne (JIRA)

@keith Thanks for the tip! Looks like (for at least one simple test case) WMO + coverage is working in Xcode 12.2 beta 3 where it wasn't in Xcode 12.1.

@hybridcattt
Copy link
Author

Seems to be fixed on Xcode 12.3 and onward (haven't tried on 12.2).

@keith
Copy link
Collaborator

keith commented Feb 16, 2021

AFAICT this is fixed in Xcode 12.4 (maybe earlier too but I didn't test). Although we are using `-num-threads 0` currently since we found this also solved this issue in the past with WMO (I'm not recommending this but just a tip if folks hit this again in the future).

Thanks everyone!

@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. code coverage Area → source tooling: code coverage compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

4 participants