Skip to content

[String.Index] Obsolete encodedOffset var/init #22108

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

Merged
merged 1 commit into from
Feb 19, 2019

Conversation

milseman
Copy link
Member

@milseman milseman commented Jan 25, 2019

[String.Index] Deprecate encodedOffset var/init

String.Index has an encodedOffset-based initializer and computed
property that exists for serialization purposes. It was documented as
UTF-16 in the SE proposal introducing it, which was String's
underlying encoding at the time, but the dream of String even then was
to abstract away whatever encoding happend to be used.

Serialization needs an explicit encoding for serialized indices to
make sense: the offsets need to align with the view. With String
utilizing UTF-8 encoding for native contents in Swift 5, serialization
isn't necessarily the most efficient in UTF-16.

Furthermore, the majority of usage of encodedOffset in the wild is
buggy and operates under the assumption that a UTF-16 code unit
was a Swift Character, which isn't even valid if the String is known
to be all-ASCII (because CR-LF).

This change introduces a set of new initializers and methods with an
explicit encoding (through the type of the provided view) for
serialization. It also adds an init and method for String's default
view, which most code in the wild should migrate to if it eliminates
their underlying bugs.

Resolves SR-9749. Much thanks to @norio-nomura for catching this!

rdar://problem/47605131

@milseman milseman changed the title [String.Index] Deprecate/obsolete encodedOffset var/init [WIP DO NOT MERGE][String.Index] Deprecate/obsolete encodedOffset var/init Jan 25, 2019
@milseman milseman force-pushed the en_gadus_offset branch 2 times, most recently from dd38b3b to 0cff8fb Compare January 26, 2019 01:29
@airspeedswift
Copy link
Member

@swift-ci please smoke test

@milseman
Copy link
Member Author

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 0cff8fb

@milseman
Copy link
Member Author

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 0cff8fb

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 0cff8fb

@milseman milseman changed the title [WIP DO NOT MERGE][String.Index] Deprecate/obsolete encodedOffset var/init [WIP DO NOT MERGE][String.Index] Obsolete encodedOffset var/init Jan 29, 2019
@milseman milseman changed the title [WIP DO NOT MERGE][String.Index] Obsolete encodedOffset var/init [String.Index] Obsolete encodedOffset var/init Feb 14, 2019
String.Index has an encodedOffset-based initializer and computed
property that exists for serialization purposes. It was documented as
UTF-16 in the SE proposal introducing it, which was String's
underlying encoding at the time, but the dream of String even then was
to abstract away whatever encoding happend to be used.

Serialization needs an explicit encoding for serialized indices to
make sense: the offsets need to align with the view. With String
utilizing UTF-8 encoding for native contents in Swift 5, serialization
isn't necessarily the most efficient in UTF-16.

Furthermore, the majority of usage of encodedOffset in the wild is
buggy and operates under the assumption that a UTF-16 code unit was a
Swift Character, which isn't even valid if the String is known to be
all-ASCII (because CR-LF).

This change introduces a pair of semantics-preserving alternatives to
encodedOffset that explicitly call out the UTF-16 assumption. These
serve as a gentle off-ramp for current mis-uses of encodedOffset.
@tkremenek
Copy link
Member

This proposal has been accepted and can be merged.

@milseman
Copy link
Member Author

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 415cc8f

@milseman
Copy link
Member Author

Linux Failures seems unrelated:

18:32:51 
<EXPR>:0: error: TestProcess.test_passthrough_environment : threw error "The operation could not be completed"

@milseman
Copy link
Member Author

@swift-ci please test Linux Platform

@available(swift, deprecated: 4.2, message: """
encodedOffset has been deprecated as most common usage is incorrect. \
Use utf16Offset(in:) to achieve the same behavior.
""")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Should the initializer and property be deprecated: 4.2 or deprecated: 5.0?

  • Should the deprecated APIs be moved to MigrationSupport.swift if possible?

  • A multi-line message (even with escaped line breaks) won't display properly in Xcode 10.1 (and perhaps in Xcode 10.2 beta).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I believe that availability matches the language version and not deployment target. This should be deprecated regardless of what is passed in -swift-version. @jrose-apple / @moiseev what do you think?

  2. I think MigrationSupport is done for obsoleted. We should probably obsolete this around 5.1 or so and move it then.

  3. I'll have to try that then.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MigrationSupport has some non-obsoleted APIs:

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 415cc8f

@milseman
Copy link
Member Author

Linux PR bot has been failing in multiple PRs. Trying again

@milseman
Copy link
Member Author

@swift-ci please test linux platform

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 415cc8f

@tkremenek
Copy link
Member

@swift-ci clean test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 415cc8f

@milseman
Copy link
Member Author

@swift-ci please test linux platform

1 similar comment
@milseman
Copy link
Member Author

@swift-ci please test linux platform

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 415cc8f

@milseman
Copy link
Member Author

Linux failure is unrelated and appearing on other PRs:
<EXPR>:0: error: TestProcess.test_passthrough_environment : threw error "The operation could not be completed"

@milseman
Copy link
Member Author

@swift-ci please test linux platform

@milseman milseman merged commit f879607 into swiftlang:master Feb 19, 2019
@milseman milseman deleted the en_gadus_offset branch February 19, 2019 22:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants