diff --git a/stdlib/public/core/StringInterpolation.swift b/stdlib/public/core/StringInterpolation.swift index 39d4fe3ae7de5..3ba187f16defc 100644 --- a/stdlib/public/core/StringInterpolation.swift +++ b/stdlib/public/core/StringInterpolation.swift @@ -178,7 +178,12 @@ public struct DefaultStringInterpolation: StringInterpolationProtocol { public mutating func appendInterpolation(_ value: T) { _print_unlocked(value, &self) } - + + @_alwaysEmitIntoClient + public mutating func appendInterpolation(_ value: Any.Type) { + _typeName(value, qualified: false).write(to: &self) + } + /// Creates a string from this instance, consuming the instance in the /// process. @inlinable diff --git a/test/IDE/complete_at_top_level.swift b/test/IDE/complete_at_top_level.swift index 681177f0d5372..def887d6f7c19 100644 --- a/test/IDE/complete_at_top_level.swift +++ b/test/IDE/complete_at_top_level.swift @@ -471,7 +471,7 @@ var stringInterp = "\(#^STRING_INTERP_3^#)" _ = "" + "\(#^STRING_INTERP_4^#)" + "" // STRING_INTERP: Begin completions // STRING_INTERP-DAG: Decl[InstanceMethod]/CurrNominal/IsSystem: ['(']{#(value): T#}[')'][#Void#]; -// STRING_INTERP-DAG: Decl[Struct]/CurrModule: FooStruct[#FooStruct#]; +// STRING_INTERP-DAG: Decl[Struct]/CurrModule/TypeRelation[Convertible]: FooStruct[#FooStruct#]; name=FooStruct // STRING_INTERP-DAG: Decl[FreeFunction]/CurrModule/TypeRelation[Invalid]: fooFunc1()[#Void#]; // STRING_INTERP-DAG: Decl[FreeFunction]/CurrModule: optStr()[#String?#]; // STRING_INTERP-DAG: Decl[GlobalVar]/Local: fooObject[#FooStruct#]; diff --git a/test/stdlib/TypeNameInterpolation.swift b/test/stdlib/TypeNameInterpolation.swift new file mode 100644 index 0000000000000..cd92c2641bd11 --- /dev/null +++ b/test/stdlib/TypeNameInterpolation.swift @@ -0,0 +1,105 @@ +// RUN: %target-build-swift -O %s -module-name=test -Xfrontend -sil-verify-all -emit-sil | %FileCheck %s +// RUN: %target-build-swift -Onone %s -module-name=test -Xfrontend -sil-verify-all -emit-sil | %FileCheck %s +// RUN: %target-run-simple-swift +// REQUIRES: executable_test + +import StdlibUnittest + +let TypeNameTests = TestSuite("TypeName") + +class C {} +struct S {} +enum E {} + +protocol P {} +protocol P2 {} +protocol P3 {} +protocol AssociatedTypes { + associatedtype A + associatedtype B + associatedtype C +} + +class Model : AssociatedTypes { + typealias A = C + typealias B = S + typealias C = E +} + +struct Model2 : AssociatedTypes { + typealias A = C + typealias B = S + typealias C = E +} + +class GC {} +struct GS {} +enum GE {} +class GC2 {} + +class SomeOuterClass { + struct SomeInnerStruct {} + struct SomeInnerGenericStruct {} +} + +class SomeOuterGenericClass { + struct SomeInnerStruct {} + struct SomeInnerGenericStruct {} +} + +extension SomeOuterGenericClass { + struct OtherInnerStruct {} + struct OtherInnerGenericStruct {} +} + +@inline(never) +func printTypename(_ s: String) { + print(s) +} + +// CHECK-LABEL: sil {{.*}} @$s4test0A21TypenameInterpolationyyF : +// CHECK-NOT: $ss26DefaultStringInterpolationV06appendC0yyxlF +// CHECK-NOT: _print_unlocked +// CHECK: } // end sil function '$s4test0A21TypenameInterpolationyyF' +@inline(never) +func testTypenameInterpolation() { + expectEqual("Int", "\(Int.self)") + expectEqual("C", "\(C.self)") + expectEqual("S", "\(S.self)") + expectEqual("E", "\(E.self)") + expectEqual("GC", + "\(GC.self)") + expectEqual("GS", + "\(GS.self)") + expectEqual("GE", + "\(GE.self)") + expectEqual("GC2", + "\(GC2.self)") + + expectEqual("P", "\(P.self)") + typealias PP2 = P & P2 + expectEqual("P & P2", + "\(PP2.self)") + expectEqual("Any", "\(Any.self)") + expectEqual("P & P2", "\((P & P2).self)") + + + expectEqual("SomeInnerStruct", + "\(SomeOuterClass.SomeInnerStruct.self)") + expectEqual("SomeInnerGenericStruct", + "\(SomeOuterClass.SomeInnerGenericStruct.self)") + expectEqual("SomeInnerStruct", + "\(SomeOuterGenericClass.SomeInnerStruct.self)") + expectEqual("SomeInnerGenericStruct", + "\(SomeOuterGenericClass.SomeInnerGenericStruct.self)") + expectEqual("OtherInnerStruct", + "\(SomeOuterGenericClass.OtherInnerStruct.self)") + expectEqual("OtherInnerGenericStruct", + "\(SomeOuterGenericClass.OtherInnerGenericStruct.self)") +} + +TypeNameTests.test("Interpolation") { + testTypenameInterpolation() +} + +runAllTests() \ No newline at end of file