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-10558] Failed to cast to a concrete class type after sequence's init(elements: Sequence) #52958

Closed
swift-ci opened this issue Apr 26, 2019 · 2 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself regression swift 5.0

Comments

@swift-ci
Copy link
Contributor

Previous ID SR-10558
Radar rdar://problem/50245539
Original Reporter davionliu30 (JIRA User)
Type Bug
Status Resolved
Resolution Duplicate
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, 5.0Regression, Miscompile
Assignee @slavapestov
Priority Medium

md5: 307874635addc56b0dbbc8c59870d49d

duplicates:

  • SR-10285 Subclass of a class that conforms to a protocol and extends NSObject is not instantiate correctly

Issue Description:

Failed to cast to a concrete class type after sequence's init(elements: Sequence)

Please refer ChartsOrg/Charts#3917

This seems introduced with Xcode 10.2

for code like:

// demo code       
        let chartView = BarChartView()
        view.addSubview(chartView)
        chartView.frame = view.bounds
        chartView.autoresizingMask = [.flexibleWidth, .flexibleHeight]


        let count = 10


        let values = (1...count).enumerated().map { BarChartDataEntry(x: (Double)($0.offset), y: (Double)($0.element)) }
        
        let set = BarChartDataSet(values)
        let data = BarChartData(dataSet: set)
        chartView.data = data

let set = BarChartDataSet(values) is eventually calling

Swift.RangeReplaceableCollection.init<A where A1: Swift.Sequence, A.Element == A1.Element>(A1) -> A

but later in BarChartRenderer's initBuffers() func

line 75: let set = barData.dataSets[i] as! IBarChartDataSet

it failed to cast to IBarChartDataSet or BarChartDataSet

I checked when break at

let set = BarChartDataSet(values)
let data = BarChartData(dataSet: set)

set is still BarChartDatSet:

> (lldb) fr v set
(Charts.BarChartDataSet) set = 0x000000028109c000 

but in

let set = barData.dataSets[i] as! IBarChartDataSet

it's

>(lldb) p barData.dataSets[i]
(_TtC6Charts12ChartDataSet) $R6 = 0x000000028109c000

note the pointer address is still the same, but failed to cast to BarChartDataSet or IBarChartDataSet

@belkadan
Copy link
Contributor

Thanks for the reproducer! Reduced:

import Foundation

protocol Foo {
  init()
}
extension Foo {
  init(oops: Int) {
    print("protocol extension")
    self.init()
  }
}

class Base: NSObject, Foo {
  override required init() {
    print("init base")
  }
}

class Sub: Base {
  required init() {
    print("init sub")
  }
}

print("\t", type(of: Sub() as Any)) // Sub
print("\t", type(of: Sub(oops: 0) as Any)) // Base

Looks like the NSObject-ness is relevant here, though I'm not sure how yet. @slavapestov, does this look familiar?

@slavapestov
Copy link
Contributor

Smells like SR-10285. I'll take a look. Thanks for the reduction Jordan!

@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 itself regression swift 5.0
Projects
None yet
Development

No branches or pull requests

4 participants