Skip to content

Instantly share code, notes, and snippets.

@johnno1962
Last active October 23, 2018 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnno1962/fe9eb7a3d0ad45e93b319c6d69771966 to your computer and use it in GitHub Desktop.
Save johnno1962/fe9eb7a3d0ad45e93b319c6d69771966 to your computer and use it in GitHub Desktop.
diff --git a/include/swift/AST/KnownIdentifiers.def b/include/swift/AST/KnownIdentifiers.def
index 5222c7f12e..6f6b921841 100644
--- a/include/swift/AST/KnownIdentifiers.def
+++ b/include/swift/AST/KnownIdentifiers.def
@@ -145,6 +145,7 @@ IDENTIFIER(nilLiteral)
IDENTIFIER(integerLiteral)
IDENTIFIER_(builtinIntegerLiteral)
IDENTIFIER(codepointLiteral)
+IDENTIFIER(characterLiteral)
IDENTIFIER_(MaxBuiltinFloatType)
IDENTIFIER(FloatLiteralType)
IDENTIFIER(floatLiteral)
@@ -154,6 +155,7 @@ IDENTIFIER_(builtinBooleanLiteral)
IDENTIFIER(booleanLiteral)
IDENTIFIER(ExtendedGraphemeClusterLiteralType)
+IDENTIFIER(CharacterLiteralType)
IDENTIFIER_(builtinExtendedGraphemeClusterLiteral)
IDENTIFIER(extendedGraphemeClusterLiteral)
diff --git a/lib/Sema/CSApply.cpp b/lib/Sema/CSApply.cpp
index a62b2dc74c..9c87c7f22b 100644
--- a/lib/Sema/CSApply.cpp
+++ b/lib/Sema/CSApply.cpp
@@ -2064,6 +2064,7 @@ namespace {
bool isStringLiteral = true;
bool isGraphemeClusterLiteral = false;
+ bool isCharacterLiteral = stringLiteral && stringLiteral->isCharacterLiteral();
ProtocolDecl *protocol = tc.getProtocol(
expr->getLoc(), KnownProtocolKind::ExpressibleByStringLiteral);
@@ -2072,7 +2073,8 @@ namespace {
// If the type does not conform to ExpressibleByStringLiteral, it should
// be ExpressibleByExtendedGraphemeClusterLiteral.
protocol = tc.getProtocol(
- expr->getLoc(),
+ expr->getLoc(), isCharacterLiteral ?
+ KnownProtocolKind::ExpressibleByCharacterLiteral :
KnownProtocolKind::ExpressibleByExtendedGraphemeClusterLiteral);
isStringLiteral = false;
isGraphemeClusterLiteral = true;
@@ -2154,10 +2156,14 @@ namespace {
brokenProtocolDiag = diag::string_literal_broken_proto;
brokenBuiltinProtocolDiag = diag::builtin_string_literal_broken_proto;
} else if (isGraphemeClusterLiteral) {
- literalType = tc.Context.Id_ExtendedGraphemeClusterLiteralType;
+ literalType = isCharacterLiteral ?
+ tc.Context.Id_CharacterLiteralType :
+ tc.Context.Id_ExtendedGraphemeClusterLiteralType;
literalFuncName
= DeclName(tc.Context, DeclBaseName::createConstructor(),
- {tc.Context.Id_extendedGraphemeClusterLiteral});
+ {isCharacterLiteral ?
+ tc.Context.Id_characterLiteral :
+ tc.Context.Id_extendedGraphemeClusterLiteral});
builtinLiteralFuncName
= DeclName(tc.Context, DeclBaseName::createConstructor(),
{ tc.Context.Id_builtinExtendedGraphemeClusterLiteral,
diff --git a/stdlib/public/core/Character.swift b/stdlib/public/core/Character.swift
index 761e5d9b00..f96366011c 100644
--- a/stdlib/public/core/Character.swift
+++ b/stdlib/public/core/Character.swift
@@ -498,8 +498,35 @@ extension Character: Hashable {
}
}
-extension Character : ExpressibleByCodepointLiteral {
- public init(codepointLiteral value: IntegerLiteralType) {
- self.init(Unicode.Scalar(_value: UInt32(value)))
+extension Character : ExpressibleByCharacterLiteral {
+// @_transparent
+ public init(codepointLiteral value: UInt32) {
+ self.init(Unicode.Scalar(_value: value))
}
+
+// @_transparent
+ public init(characterLiteral value: Character) {
+ self = value
+ }
+}
+
+
+extension String {
+ @_transparent
+ public static func + (lhs: String, rhs: Character) -> String {
+ var string = lhs
+ string.append(rhs)
+ return string
+ }
+}
+extension Character {
+ @_transparent
+ public static func + (lhs: Character, rhs: Character) -> String {
+ return String(lhs) + rhs
+ }
+
+ @_transparent
+ public static func * (repeatedValue: Character, count: Int) -> String {
+ return String(repeatElement(repeatedValue, count: count))
+ }
}
diff --git a/stdlib/public/core/CompilerProtocols.swift b/stdlib/public/core/CompilerProtocols.swift
index a511348bc4..aee60f5501 100644
--- a/stdlib/public/core/CompilerProtocols.swift
+++ b/stdlib/public/core/CompilerProtocols.swift
@@ -279,25 +279,6 @@ public protocol ExpressibleByIntegerLiteral {
init(integerLiteral value: IntegerLiteralType)
}
-public protocol ExpressibleByCodepointLiteral {
- /// A type that represents a single quoted codepoint literal.
- ///
- associatedtype IntegerLiteralType : _ExpressibleByBuiltinIntegerLiteral
-
- /// Creates an instance initialized to the specified codepoint value.
- ///
- /// Do not call this initializer directly. Instead, initialize a variable or
- /// constant using an integer literal. For example:
- ///
- /// let x = 'A'
- ///
- /// In this example, the assignment to the `x` constant calls this codepoint
- /// literal initializer behind the scenes.
- ///
- /// - Parameter value: The value to create.
- init(codepointLiteral value: IntegerLiteralType)
-}
-
public protocol _ExpressibleByBuiltinFloatLiteral {
init(_builtinFloatLiteral value: _MaxBuiltinFloatType)
}
@@ -374,6 +355,46 @@ public protocol ExpressibleByBooleanLiteral {
init(booleanLiteral value: BooleanLiteralType)
}
+
+
+public protocol _ExpressibleByBuiltinCharacterLiteral :
+ _ExpressibleByBuiltinExtendedGraphemeClusterLiteral {}
+
+extension Character : _ExpressibleByBuiltinCharacterLiteral {}
+
+public protocol ExpressibleByCodepointLiteral {
+ /// A type that represents a single quoted codepoint literal.
+ ///
+ associatedtype IntegerLiteralType : _ExpressibleByBuiltinIntegerLiteral
+
+ /// Creates an instance initialized to the specified codepoint value.
+ ///
+ /// Do not call this initializer directly. Instead, initialize a variable or
+ /// constant using an integer literal. For example:
+ ///
+ /// let x = 'A'
+ ///
+ /// In this example, the assignment to the `x` constant calls this codepoint
+ /// literal initializer behind the scenes.
+ ///
+ /// - Parameter value: The value to create.
+ init(codepointLiteral value: IntegerLiteralType)
+}
+
+public protocol ExpressibleByCharacterLiteral : ExpressibleByCodepointLiteral {
+ associatedtype CharacterLiteralType : _ExpressibleByBuiltinCharacterLiteral
+ init(characterLiteral value: CharacterLiteralType)
+}
+
+extension ExpressibleByCharacterLiteral where CharacterLiteralType == Character {
+// @_transparent
+ public init(codepointLiteral value: UInt32) {
+ self.init(characterLiteral: Character(Unicode.Scalar(_value: value)))
+ }
+}
+
+
+
public protocol _ExpressibleByBuiltinUnicodeScalarLiteral {
init(_builtinUnicodeScalarLiteral value: Builtin.Int32)
}
@@ -449,7 +470,7 @@ public protocol _ExpressibleByBuiltinExtendedGraphemeClusterLiteral
/// To add `ExpressibleByExtendedGraphemeClusterLiteral` conformance to your
/// custom type, implement the required initializer.
public protocol ExpressibleByExtendedGraphemeClusterLiteral
- : ExpressibleByCharacterLiteral {
+ : ExpressibleByUnicodeScalarLiteral {
/// A type that represents an extended grapheme cluster literal.
///
/// Valid types for `ExtendedGraphemeClusterLiteralType` are `Character`,
@@ -463,10 +484,6 @@ public protocol ExpressibleByExtendedGraphemeClusterLiteral
init(extendedGraphemeClusterLiteral value: ExtendedGraphemeClusterLiteralType)
}
-// Used to ensure default type of 'character' literals is `Character`
-public protocol ExpressibleByCharacterLiteral
- : ExpressibleByUnicodeScalarLiteral {}
-
extension ExpressibleByExtendedGraphemeClusterLiteral
where ExtendedGraphemeClusterLiteralType == UnicodeScalarLiteralType {
diff --git a/stdlib/public/core/String.swift b/stdlib/public/core/String.swift
index 5f766ecfde..ea07cde15e 100644
--- a/stdlib/public/core/String.swift
+++ b/stdlib/public/core/String.swift
@@ -1157,9 +1157,3 @@ extension String : CustomStringConvertible {
return self
}
}
-
-extension String : ExpressibleByCodepointLiteral {
- public init(codepointLiteral value: UInt32) {
- self.init(Unicode.Scalar(_value: value))
- }
-}
diff --git a/stdlib/public/core/UnicodeScalar.swift b/stdlib/public/core/UnicodeScalar.swift
index c3c56970f3..784ff552df 100644
--- a/stdlib/public/core/UnicodeScalar.swift
+++ b/stdlib/public/core/UnicodeScalar.swift
@@ -499,7 +499,8 @@ extension Unicode.Scalar {
}
extension Unicode.Scalar : ExpressibleByCodepointLiteral {
- public init(codepointLiteral value: IntegerLiteralType) {
- self.init(_value: UInt32(value))
+// @_transparent
+ public init(codepointLiteral value: UInt32) {
+ self.init(_value: value)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment