mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
* [CodeCompletion] Restrict ancestor search to brace
This change allows ExprParentFinder to restrict certain searches for parents to just AST nodes within the nearest surrounding BraceStmt. In the string interpolation rework, BraceStmts can appear in new places in the AST; this keeps code completion from looking at irrelevant context.
NFC in this commit, but keeps code completion from crashing once TapExpr is introduced.
* Remove test relying on ExpressibleByStringInterpolation being deprecated
Since soon enough, it won’t be anymore.
* [AST] Introduce TapExpr
TapExpr allows a block of code to to be inserted between two expressions, accessing and potentially mutating the result of its subexpression before giving it to its parent expression. It’s roughly equivalent to this function:
func _tap<T>(_ value: T, do body: (inout T) throws -> Void) rethrows -> T {
var copy = value
try body(©)
return copy
}
Except that it doesn’t use a closure, so no variables are captured and no call frame is (even notionally) added.
This commit does not include tests because nothing in it actually uses TapExpr yet. It will be used by string interpolation.
* SE-0228: Fix ExpressibleByStringInterpolation
This is the bulk of the implementation of the string interpolation rework. It includes a redesigned AST node, new parsing logic, new constraints and post-typechecking code generation, and new standard library types and members.
* [Sema] Rip out typeCheckExpressionShallow()
With new string interpolation in place, it is no longer used by anything in the compiler.
* [Sema] Diagnose invalid StringInterpolationProtocols
StringInterpolationProtocol informally requires conforming types to provide at least one method with the base name “appendInterpolation” with no (or a discardable) return value and visibility at least as broad as the conforming type’s. This change diagnoses an error when a conforming type does not have a method that meets those criteria.
* [Stdlib] Fix map(String.init) source break
Some users, including some in the source compatibility suite, accidentally used init(stringInterpolationSegment:) by writing code like `map(String.init)`. Now that these intializers have been removed, the remaining initializers often end up tying during overload resolution. This change adds several overloads of `String.init(describing:)` which will break these ties in cases where the compiler previously selected `String.init(stringInterpolationSegment:)`.
* [Sema] Make callWitness() take non-mutable arrays
It doesn’t actually need to mutate them.
* [Stdlib] Improve floating-point interpolation performance
This change avoids constructing a String when interpolating a Float, Double, or Float80. Instead, we write the characters to a fixed-size buffer and then append them directly to the string’s storage.
This seems to improve performance for all three types, but especially for Double and Float80, which cannot always fit into a small string when stringified.
* [NameLookup] Improve MemberLookupTable invalidation
In rare cases usually involving generated code, an overload added by an extension in the middle of a file would not be visible below it if the type had lazy members and the same base name had already been referenced above the extension. This change essentially dirties a type’s member lookup table whenever an extension is added to it, ensuring the entries in it will be updated.
This change also includes some debugging improvements for NameLookup.
* [SILOptimizer] XFAIL dead object removal failure
The DeadObjectRemoval pass in SILOptimizer does not currently remove reworked string interpolations as well as the old design because their effects cannot be described by @_effects(readonly). That causes a test failure on Linux. This change temporarily silences that test. The SILOptimizer issue has been filed as SR-9008.
* Confess string interpolation’s source stability sins
* [Parser] Parse empty interpolations
Previously, the parser had an odd asymmetry which caused the same function to accept foo(), but reject “\()”. This change fixes the issue.
Already tested by test/Parse/try.swift, which uses this construct in one of its throwing interpolation tests.
* [Sema] Fix batch-mode-only lazy var bug
The temporary variable used by string interpolation needs to be recontextualized when it’s inserted into a synthesized getter. Fixes a compilation failure in Alamofire.
I’ll probably follow up on this bug a bit more after merging.
187 lines
6.9 KiB
Swift
187 lines
6.9 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See https://swift.org/LICENSE.txt for license information
|
|
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// A type that can represent a string as a collection of characters.
|
|
///
|
|
/// Do not declare new conformances to `StringProtocol`. Only the `String` and
|
|
/// `Substring` types in the standard library are valid conforming types.
|
|
public protocol StringProtocol
|
|
: BidirectionalCollection,
|
|
TextOutputStream, TextOutputStreamable,
|
|
LosslessStringConvertible, ExpressibleByStringInterpolation,
|
|
Hashable, Comparable
|
|
where Iterator.Element == Character, SubSequence : StringProtocol,
|
|
StringInterpolation == DefaultStringInterpolation {
|
|
|
|
associatedtype UTF8View : /*Bidirectional*/Collection
|
|
where UTF8View.Element == UInt8 // Unicode.UTF8.CodeUnit
|
|
|
|
associatedtype UTF16View : BidirectionalCollection
|
|
where UTF16View.Element == UInt16 // Unicode.UTF16.CodeUnit
|
|
|
|
associatedtype UnicodeScalarView : BidirectionalCollection
|
|
where UnicodeScalarView.Element == Unicode.Scalar
|
|
|
|
associatedtype SubSequence = Substring
|
|
|
|
var utf8: UTF8View { get }
|
|
var utf16: UTF16View { get }
|
|
var unicodeScalars: UnicodeScalarView { get }
|
|
|
|
func hasPrefix(_ prefix: String) -> Bool
|
|
func hasSuffix(_ prefix: String) -> Bool
|
|
|
|
func lowercased() -> String
|
|
func uppercased() -> String
|
|
|
|
/// Creates a string from the given Unicode code units in the specified
|
|
/// encoding.
|
|
///
|
|
/// - Parameters:
|
|
/// - codeUnits: A collection of code units encoded in the encoding
|
|
/// specified in `sourceEncoding`.
|
|
/// - sourceEncoding: The encoding in which `codeUnits` should be
|
|
/// interpreted.
|
|
init<C: Collection, Encoding: Unicode.Encoding>(
|
|
decoding codeUnits: C, as sourceEncoding: Encoding.Type
|
|
)
|
|
where C.Iterator.Element == Encoding.CodeUnit
|
|
|
|
/// Creates a string from the null-terminated, UTF-8 encoded sequence of
|
|
/// bytes at the given pointer.
|
|
///
|
|
/// - Parameter nullTerminatedUTF8: A pointer to a sequence of contiguous,
|
|
/// UTF-8 encoded bytes ending just before the first zero byte.
|
|
init(cString nullTerminatedUTF8: UnsafePointer<CChar>)
|
|
|
|
/// Creates a string from the null-terminated sequence of bytes at the given
|
|
/// pointer.
|
|
///
|
|
/// - Parameters:
|
|
/// - nullTerminatedCodeUnits: A pointer to a sequence of contiguous code
|
|
/// units in the encoding specified in `sourceEncoding`, ending just
|
|
/// before the first zero code unit.
|
|
/// - sourceEncoding: The encoding in which the code units should be
|
|
/// interpreted.
|
|
init<Encoding: Unicode.Encoding>(
|
|
decodingCString nullTerminatedCodeUnits: UnsafePointer<Encoding.CodeUnit>,
|
|
as sourceEncoding: Encoding.Type)
|
|
|
|
/// Calls the given closure with a pointer to the contents of the string,
|
|
/// represented as a null-terminated sequence of UTF-8 code units.
|
|
///
|
|
/// The pointer passed as an argument to `body` is valid only during the
|
|
/// execution of `withCString(_:)`. Do not store or return the pointer for
|
|
/// later use.
|
|
///
|
|
/// - Parameter body: A closure with a pointer parameter that points to a
|
|
/// null-terminated sequence of UTF-8 code units. If `body` has a return
|
|
/// value, that value is also used as the return value for the
|
|
/// `withCString(_:)` method. The pointer argument is valid only for the
|
|
/// duration of the method's execution.
|
|
/// - Returns: The return value, if any, of the `body` closure parameter.
|
|
func withCString<Result>(
|
|
_ body: (UnsafePointer<CChar>) throws -> Result) rethrows -> Result
|
|
|
|
/// Calls the given closure with a pointer to the contents of the string,
|
|
/// represented as a null-terminated sequence of code units.
|
|
///
|
|
/// The pointer passed as an argument to `body` is valid only during the
|
|
/// execution of `withCString(encodedAs:_:)`. Do not store or return the
|
|
/// pointer for later use.
|
|
///
|
|
/// - Parameters:
|
|
/// - body: A closure with a pointer parameter that points to a
|
|
/// null-terminated sequence of code units. If `body` has a return
|
|
/// value, that value is also used as the return value for the
|
|
/// `withCString(encodedAs:_:)` method. The pointer argument is valid
|
|
/// only for the duration of the method's execution.
|
|
/// - targetEncoding: The encoding in which the code units should be
|
|
/// interpreted.
|
|
/// - Returns: The return value, if any, of the `body` closure parameter.
|
|
func withCString<Result, Encoding: Unicode.Encoding>(
|
|
encodedAs targetEncoding: Encoding.Type,
|
|
_ body: (UnsafePointer<Encoding.CodeUnit>) throws -> Result
|
|
) rethrows -> Result
|
|
|
|
/// The entire String onto whose slice this view is a projection.
|
|
var _wholeString : String { get }
|
|
/// The range of storage offsets of this view in `_wholeString`.
|
|
var _encodedOffsetRange : Range<Int> { get }
|
|
}
|
|
|
|
extension StringProtocol {
|
|
public var _wholeString: String {
|
|
return String(self)
|
|
}
|
|
|
|
public var _encodedOffsetRange: Range<Int> {
|
|
return 0 ..< numericCast(self.utf16.count)
|
|
}
|
|
}
|
|
|
|
/// A protocol that provides fast access to a known representation of String.
|
|
///
|
|
/// Can be used to specialize generic functions that would otherwise end up
|
|
/// doing grapheme breaking to vend individual characters.
|
|
@usableFromInline // FIXME(sil-serialize-all)
|
|
internal protocol _SwiftStringView {
|
|
/// A `String`, having the same contents as `self`, that may be unsuitable for
|
|
/// long-term storage.
|
|
var _ephemeralContent : String { get }
|
|
|
|
/// A `String`, having the same contents as `self`, that is suitable for
|
|
/// long-term storage.
|
|
//
|
|
// FIXME: Remove once _StringGuts has append(contentsOf:).
|
|
var _persistentContent : String { get }
|
|
|
|
/// The entire String onto whose slice this view is a projection.
|
|
var _wholeString : String { get }
|
|
/// The range of storage offsets of this view in `_wholeString`.
|
|
var _encodedOffsetRange : Range<Int> { get }
|
|
}
|
|
|
|
extension _SwiftStringView {
|
|
@inlinable // FIXME(sil-serialize-all)
|
|
internal var _ephemeralContent : String { return _persistentContent }
|
|
}
|
|
|
|
extension StringProtocol {
|
|
@inlinable // FIXME(sil-serialize-all)
|
|
public // Used in the Foundation overlay
|
|
var _ephemeralString : String {
|
|
if _fastPath(self is _SwiftStringView) {
|
|
return (self as! _SwiftStringView)._ephemeralContent
|
|
}
|
|
return String(self)
|
|
}
|
|
}
|
|
|
|
extension String : _SwiftStringView {
|
|
@inlinable // FIXME(sil-serialize-all)
|
|
internal var _persistentContent : String {
|
|
return self
|
|
}
|
|
|
|
@inlinable // FIXME(sil-serialize-all)
|
|
public var _wholeString : String {
|
|
return self
|
|
}
|
|
|
|
@inlinable // FIXME(sil-serialize-all)
|
|
public var _encodedOffsetRange : Range<Int> {
|
|
return 0..<_guts.count
|
|
}
|
|
}
|
|
|