Skip to content

[SR-11818] Bad downcast to Self not caught at runtime #54229

Closed
@swift-ci

Description

@swift-ci
Previous ID SR-11818
Radar rdar://problem/57369535
Original Reporter Alvae (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

macOS 10.14.6
Apple Swift version 5.1.2 (swiftlang-1100.0.278 clang-1100.0.33.9)
Target: x86_64-apple-darwin18.7.0

Additional Detail from JIRA
Votes 0
Component/s
Labels Bug
Assignee None
Priority Medium

md5: fbb65ac0c98f2c39a39b37ff1ba9c5f0

Issue Description:

The following code has undefined behavior:

protocol Boxable {
  func boxed() -> Self
}

class List: Boxable {
  let next: List?
  init(next: List?) { self.next = next }
  func boxed() -> Self { List(next: self) as! Self }
}

class DerivedList: List {
  let foo: String
  init(foo: String, next: List?) { self.foo = foo; super.init(next: next) }
}

let derived = DerivedList(foo: "bar", next: nil).boxed()
print(derived.foo)

I would have expected the downcast to Self in List.boxed() to crash the program when I called boxed on DerivedList's instance. As it didn't, the property derived.foo is actually uninitialized.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions