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-5659] String.UnicodeScalarView has breaking changes in latest beta #48229

Closed
swift-ci opened this issue Aug 8, 2017 · 7 comments
Closed
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. standard library Area: Standard library umbrella

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Aug 8, 2017

Previous ID SR-5659
Radar rdar://problem/33779236
Original Reporter Lebedev (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

Version 9.0 beta 5 (9M202q)

Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels Bug
Assignee @milseman
Priority Medium

md5: 15ec6a1ffbb63f82ec32892b988630fb

Issue Description:

// Swift 3.1, works fine

import Cocoa
var str = "Hello, playground"
var scalars = str.unicodeScalars // String.UnicodeScalarView
print(scalars.popFirst() ?? "") 
// Swift 4, Xcode 9 beta 5
 
import Cocoa
var str = "Hello, playground"
var scalars = str.unicodeScalars // String.UnicodeScalarView
print(scalars.popFirst() ?? "") // produces error "cannot use mutating member on immutable value: 'scalars' is immutable"

// That's the "workaround"
var substring = str.unicodeScalars.suffix(from: str.startIndex) // String.UnicodeScalarView.SubSequence
print(substring.popFirst() ?? "")
@belkadan
Copy link
Contributor

belkadan commented Aug 8, 2017

@milseman, you were working on this, right?

@milseman
Copy link
Mannequin

milseman mannequin commented Aug 8, 2017

Yes

@milseman
Copy link
Mannequin

milseman mannequin commented Aug 8, 2017

Aha, popFirst and friends should only be on Collections who are their own Subsequence types. String and associated views should not be their own Subsequence types. Earlier betas had a bug where they were, and even b5 still has a bug where String.CharacterView when explicitly called out is still its own slice type. This bug causes potential "leaks" that Substring was designed to fix, not to mention they pollute the model. There also likely needs to be more compatibility affordances provided by the standard library to improve diagnostics here and ease a migration path (e.g. deprecated rather than unavailable).

As far as this error specifically, that's a very unfortunate error message. I'll see what I can do to hunt it down.

TL;DR: It is by design that Substring and its views have popFirst defined, while String does not. Substring's popFirst does not mutate the underlying String, it just advances the Substring's startIndex. The current implementation, however, has bugs that hurts the user experience.

@milseman
Copy link
Mannequin

milseman mannequin commented Aug 8, 2017

I'll add popFirst as being unavailable on String and its views for a better diagnostic message. Bikeshedding time, what's a good diagnostic here? Strawman:

error: 'popFirst()' is unavailable:
String.UnicodeScalarView is no longer its own SubSequence type; please use 'first', 'dropFirst()', or 'Substring.UnicodeScalarView.popFirst()'.

edit: JIRA formatting

@milseman
Copy link
Mannequin

milseman mannequin commented Aug 8, 2017

@swift-ci create

@milseman
Copy link
Mannequin

milseman mannequin commented Aug 8, 2017

Fix is part of #11373 which is pretty closely related and fixes the dangling String.CharacterView issue.

edit: Specifically, this hash: a5933d2

@milseman
Copy link
Mannequin

milseman mannequin commented Sep 7, 2017

4.0 PR: #11806

@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. standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

2 participants