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-8279] Array - Fatal error: Index out of range #50810

Closed
swift-ci opened this issue Jul 17, 2018 · 2 comments
Closed

[SR-8279] Array - Fatal error: Index out of range #50810

swift-ci opened this issue Jul 17, 2018 · 2 comments

Comments

@swift-ci
Copy link
Collaborator

@swift-ci swift-ci commented Jul 17, 2018

Previous ID SR-8279
Radar None
Original Reporter Em (JIRA User)
Type Bug
Status Resolved
Resolution Invalid

Attachment: Download

Additional Detail from JIRA
Votes 1
Component/s
Labels Bug, Runtime
Assignee None
Priority Medium

md5: 026c4d8e4dfba6db92c712ef6b41a38e

Issue Description:

Is receiving a fatal error in variable sized array when setting an object at an index desired behaviour?

typealias Coordinate2D = Double
typealias Point2D = [Coordinate2D]

extension Array where Element == Coordinate2D {
  init(x: Coordinate2D, y: Coordinate2D) {
    self.init([x,y])
  }

  var x: Coordinate2D { get { return self[0] } set { self[0] = newValue } }
  var y: Coordinate2D { get { return self[1] } set { self[1] = newValue } }
}

var array = [Point2D]()

let point = Point2D.init(x: 0, y: 0)
array[0] = point // fatal error: index out of range

Above code reproduces the issue in the playground resulting in indefinite execution time.

The project reproducing the above mentioned error is in the attachments.

@belkadan
Copy link
Contributor

@belkadan belkadan commented Jul 17, 2018

Yes, this is expected behavior. Array out-of-bounds errors are a sign that the program is incorrect and that the developer needs to investigate the issue. This is consistent with the behavior of Objective-C's NSArray, as well as array types in many other languages.

@Azoy
Copy link
Member

@Azoy Azoy commented Jul 17, 2018

You can solve this issue by simply appending point:

array.append(point)

@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
Projects
None yet
Development

No branches or pull requests

3 participants