{"action":"created","issue":{"url":"https://api.github.com/repos/apple/swift/issues/1349","repository_url":"https://api.github.com/repos/apple/swift","labels_url":"https://api.github.com/repos/apple/swift/issues/1349/labels{/name}","comments_url":"https://api.github.com/repos/apple/swift/issues/1349/comments","events_url":"https://api.github.com/repos/apple/swift/issues/1349/events","html_url":"https://github.com/apple/swift/pull/1349","id":134591586,"number":1349,"title":"[stdlib] Fix precondition messages for MutableCollectionType slice replacing","user":{"login":"Jnosh","id":3801202,"avatar_url":"https://avatars.githubusercontent.com/u/3801202?v=3","gravatar_id":"","url":"https://api.github.com/users/Jnosh","html_url":"https://github.com/Jnosh","followers_url":"https://api.github.com/users/Jnosh/followers","following_url":"https://api.github.com/users/Jnosh/following{/other_user}","gists_url":"https://api.github.com/users/Jnosh/gists{/gist_id}","starred_url":"https://api.github.com/users/Jnosh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Jnosh/subscriptions","organizations_url":"https://api.github.com/users/Jnosh/orgs","repos_url":"https://api.github.com/users/Jnosh/repos","events_url":"https://api.github.com/users/Jnosh/events{/privacy}","received_events_url":"https://api.github.com/users/Jnosh/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-02-18T14:14:29Z","updated_at":"2016-02-18T17:00:01Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/apple/swift/pulls/1349","html_url":"https://github.com/apple/swift/pull/1349","diff_url":"https://github.com/apple/swift/pull/1349.diff","patch_url":"https://github.com/apple/swift/pull/1349.patch"},"body":"The precondition messages in `_writeBackMutableSlice` seem to be inverted in their respective meaning.\r\n\r\nYou can test the current behaviour as follows:\r\n```swift\r\nlet ptr = UnsafeMutablePointer<Int>.alloc(100)\r\nptr.initializeFrom(0..<100)\r\nlet ptr2 = UnsafeMutablePointer<Int>.alloc(20)\r\nptr2.initializeFrom(0..<20)\r\n\r\nvar dest = UnsafeMutableBufferPointer(start: ptr, count: 100)\r\nlet source = UnsafeMutableBufferPointer(start: ptr2, count: 20)\r\n\r\n// fatal error: Cannot replace a slice of a MutableCollectionType with a slice of a larger size\r\ndest[20..<50] = source[0..<20]\r\n\r\n// Cannot replace a slice of a MutableCollectionType with a slice of a smaller size\r\ndest[20..<30] = source[0..<20]\r\n```\r\n\r\n(I'm using `UnsafeMutableBufferPointer` since its the only type in the stdlib that uses the default slicing behaviour from `MutableCollectionType`)\r\n\r\nI also looked into adding tests for this but I don't think its currently possible to test for specific trap messages? However CollectionType currently has no trap tests at all so I'm open to adding some as a separate pull request if that makes sense."},"comment":{"url":"https://api.github.com/repos/apple/swift/issues/comments/185815558","html_url":"https://github.com/apple/swift/pull/1349#issuecomment-185815558","issue_url":"https://api.github.com/repos/apple/swift/issues/1349","id":185815558,"user":{"login":"gribozavr","id":1120569,"avatar_url":"https://avatars.githubusercontent.com/u/1120569?v=3","gravatar_id":"","url":"https://api.github.com/users/gribozavr","html_url":"https://github.com/gribozavr","followers_url":"https://api.github.com/users/gribozavr/followers","following_url":"https://api.github.com/users/gribozavr/following{/other_user}","gists_url":"https://api.github.com/users/gribozavr/gists{/gist_id}","starred_url":"https://api.github.com/users/gribozavr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gribozavr/subscriptions","organizations_url":"https://api.github.com/users/gribozavr/orgs","repos_url":"https://api.github.com/users/gribozavr/repos","events_url":"https://api.github.com/users/gribozavr/events{/privacy}","received_events_url":"https://api.github.com/users/gribozavr/received_events","type":"User","site_admin":false},"created_at":"2016-02-18T17:00:01Z","updated_at":"2016-02-18T17:00:01Z","body":"You can write tests for exact messages using `.crashOutputMatches(_isDebugAssertConfiguration() ? \"this should fail\" : \"\")`.  See `validation-test/stdlib/Assert.swift` for an example.\r\n\r\nCollections do have trap tests, see `stdlib/private/StdlibCollectionUnittest/CheckCollectionType.swift` for example.\r\n\r\nThe tricky part is that here we are testing the behavior of the default implementation.  A concrete type could support replacing ranges with ranges of different size.  Thus, we can't just add these tests to `addForwardMutableCollectionTests()`.  My suggestion would be to add a parameter to that function, that communicates the expected behavior (`isFixedLengthCollection`).\r\n\r\nIf you would like to separate that into a different PR, let me know."}}