mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
This changes 'if let' conditions to take general refutable patterns, instead of
taking a irrefutable pattern and implicitly matching against an optional.
Where before you might have written:
if let x = foo() {
you now need to write:
if let x? = foo() {
The upshot of this is that you can write anything in an 'if let' that you can
write in a 'case let' in a switch statement, which is pretty general.
To aid with migration, this special cases certain really common patterns like
the above (and any other irrefutable cases, like "if let (a,b) = foo()", and
tells you where to insert the ?. It also special cases type annotations like
"if let x : AnyObject = " since they are no longer allowed.
For transitional purposes, I have intentionally downgraded the most common
diagnostic into a warning instead of an error. This means that you'll get:
t.swift:26:10: warning: condition requires a refutable pattern match; did you mean to match an optional?
if let a = f() {
^
?
I think this is important to stage in, because this is a pretty significant
source breaking change and not everyone internally may want to deal with it
at the same time. I filed 20166013 to remember to upgrade this to an error.
In addition to being a nice user feature, this is a nice cleanup of the guts
of the compiler, since it eliminates the "isConditional()" bit from
PatternBindingDecl, along with the special case logic in the compiler to handle
it (which variously added and removed Optional around these things).
Swift SVN r26150
138 lines
4.4 KiB
Plaintext
138 lines
4.4 KiB
Plaintext
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See http://swift.org/LICENSE.txt for license information
|
|
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
%{
|
|
|
|
from SwiftIntTypes import *
|
|
|
|
# Number of bits in the Builtin.Word type
|
|
word_bits = int(CMAKE_SIZEOF_VOID_P) * 8
|
|
|
|
IntMax = 'Int%s' % int_max_bits
|
|
UIntMax = 'UInt%s' % int_max_bits
|
|
|
|
}%
|
|
|
|
//===--- Parsing helpers --------------------------------------------------===//
|
|
|
|
/// If text is an ASCII representation in the given `radix` of a
|
|
/// non-negative number <= `maximum`, return that number. Otherwise,
|
|
/// return `nil`.
|
|
///
|
|
/// Note: if `text` begins with `"+"` or `"-"`, even if the rest of
|
|
/// the characters are `"0"`, the result is nil.
|
|
internal func _parseUnsignedAsciiAsUIntMax(
|
|
u16: String.UTF16View, radix: Int, maximum: UIntMax
|
|
) -> UIntMax? {
|
|
if isEmpty(u16) { return nil }
|
|
|
|
let digit = _ascii16("0")..._ascii16("9")
|
|
let lower = _ascii16("a")..._ascii16("z")
|
|
let upper = _ascii16("A")..._ascii16("Z")
|
|
|
|
_precondition(radix > 1, "Radix must be greater than 1")
|
|
_precondition(
|
|
radix <= numericCast(10 + count(lower)),
|
|
"Radix exceeds what can be expressed using the English alphabet")
|
|
|
|
let uRadix = UIntMax(bitPattern: IntMax(radix))
|
|
var result: UIntMax = 0
|
|
for c in u16 {
|
|
let n: UIntMax
|
|
switch c {
|
|
case digit: n = UIntMax(c - digit.startIndex)
|
|
case lower: n = UIntMax(c - lower.startIndex) + 10
|
|
case upper: n = UIntMax(c - upper.startIndex) + 10
|
|
default: return nil
|
|
}
|
|
if n >= uRadix { return nil }
|
|
let (result1, overflow1) = UIntMax.multiplyWithOverflow(result, uRadix)
|
|
let (result2, overflow2) = UIntMax.addWithOverflow(result1, n)
|
|
result = result2
|
|
if overflow1 || overflow2 || result > maximum { return nil }
|
|
}
|
|
return result
|
|
}
|
|
|
|
/// If text is an ASCII representation in the given `radix` of a
|
|
/// non-negative number <= `maximum`, return that number. Otherwise,
|
|
/// return `nil`.
|
|
///
|
|
/// Note: if `text` begins with `"+"` or `"-"`, even if the rest of
|
|
/// the characters are `"0"`, the result is nil.
|
|
internal func _parseAsciiAsUIntMax(
|
|
u16: String.UTF16View, radix: Int, maximum: UIntMax
|
|
) -> UIntMax? {
|
|
if isEmpty(u16) { return nil }
|
|
let c = first(u16)
|
|
if _fastPath(c != _ascii16("-")) {
|
|
let unsignedText
|
|
= c == _ascii16("+") ? unsafeUnwrap(dropFirst(u16)) : u16
|
|
return _parseUnsignedAsciiAsUIntMax(unsignedText, radix, maximum)
|
|
}
|
|
else {
|
|
return _parseAsciiAsIntMax(u16, radix, 0) == 0 ? 0 : nil
|
|
}
|
|
}
|
|
|
|
/// If text is an ASCII representation in the given `radix` of a
|
|
/// number >= -`maximum` - 1 and <= `maximum`, return that number.
|
|
/// Otherwise, return `nil`.
|
|
///
|
|
/// Note: for text matching the regular expression "-0+", the result
|
|
/// is `0`, not `nil`.
|
|
internal func _parseAsciiAsIntMax(
|
|
u16: String.UTF16View, radix: Int, maximum: IntMax
|
|
) -> IntMax? {
|
|
_sanityCheck(maximum >= 0, "maximum should be non-negative")
|
|
|
|
if isEmpty(u16) { return nil }
|
|
|
|
// Drop any leading "-"
|
|
let negative = first(u16) == _ascii16("-")
|
|
let absResultText = negative ? _unsafeUnwrap(dropFirst(u16)) : u16
|
|
|
|
let absResultMax = UIntMax(bitPattern: maximum) + (negative ? 1 : 0)
|
|
|
|
// Parse the result as unsigned
|
|
if let absResult? = _parseAsciiAsUIntMax(absResultText, radix, absResultMax) {
|
|
return IntMax(bitPattern: negative ? 0 &- absResult : absResult)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
//===--- Loop over all integer types --------------------------------------===//
|
|
% for self_ty in all_integer_types(word_bits):
|
|
% signed = self_ty.is_signed
|
|
% Self = self_ty.stdlib_name
|
|
|
|
extension ${Self} {
|
|
/// Construct from an ASCII representation in the given `radix`.
|
|
///
|
|
/// If `text` does not match the regular expression
|
|
/// "[+-][0-9a-zA-Z]+", or the value it denotes in the given `radix`
|
|
/// is not representable, the result is `nil`.
|
|
public init?(_ text: String, radix: Int = 10) {
|
|
if let value? = _parseAsciiAs${'' if signed else 'U'}IntMax(
|
|
text.utf16, radix, ${'' if signed else 'U'}IntMax(${Self}.max)) {
|
|
self.init(
|
|
${'' if Self in (IntMax,UIntMax) else 'truncatingBitPattern:'} value)
|
|
}
|
|
else {
|
|
return nil
|
|
}
|
|
}
|
|
}
|
|
|
|
% end
|
|
|