Skip to content

Commit 4e0c360

Browse files
authored
Merge pull request #30827 from owenv/interpolation-edu-note
[Diags] Add an edu note explaining StringInterpolationProtocol informal requirements
2 parents 38768fb + 68530ac commit 4e0c360

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

include/swift/AST/EducationalNotes.def

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,11 @@ EDUCATIONAL_NOTES(property_wrapper_failable_init,
4646
EDUCATIONAL_NOTES(property_wrapper_type_requirement_not_accessible,
4747
"property-wrapper-requirements.md")
4848

49+
EDUCATIONAL_NOTES(missing_append_interpolation,
50+
"string-interpolation-conformance.md")
51+
EDUCATIONAL_NOTES(append_interpolation_static,
52+
"string-interpolation-conformance.md")
53+
EDUCATIONAL_NOTES(append_interpolation_void_or_discardable,
54+
"string-interpolation-conformance.md")
55+
4956
#undef EDUCATIONAL_NOTES

test/Parse/invalid_string_interpolation_protocol.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
// Has a lot of invalid 'appendInterpolation' methods
44
public struct BadStringInterpolation: StringInterpolationProtocol {
5-
// expected-error@-1{{type conforming to 'StringInterpolationProtocol' does not implement a valid 'appendInterpolation' method}}
5+
// expected-error@-1{{type conforming to 'StringInterpolationProtocol' does not implement a valid 'appendInterpolation' method}} {{educational-notes=string-interpolation-conformance}}
66

77
public init(literalCapacity: Int, interpolationCount: Int) {}
88
public mutating func appendLiteral(_: String) {}
99

1010
public static func appendInterpolation(static: ()) {
11-
// expected-warning@-1{{'appendInterpolation' method will never be used because it is static}} {{10-17=}}
11+
// expected-warning@-1{{'appendInterpolation' method will never be used because it is static}} {{10-17=}} {{educational-notes=string-interpolation-conformance}}
1212
}
1313

1414
private func appendInterpolation(private: ()) {
@@ -20,7 +20,7 @@ public struct BadStringInterpolation: StringInterpolationProtocol {
2020
}
2121

2222
public func appendInterpolation(intResult: ()) -> Int {
23-
// expected-warning@-1{{'appendInterpolation' method does not return 'Void' or have a discardable result}} {{10-10=@discardableResult }}
23+
// expected-warning@-1{{'appendInterpolation' method does not return 'Void' or have a discardable result}} {{10-10=@discardableResult }} {{educational-notes=string-interpolation-conformance}}
2424
}
2525
}
2626

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Conforming to `StringInterpolationProtocol`
2+
A type conforming to `ExpressibleByStringInterpolation` uses a helper type called `StringInterpolation` to perform its interpolation. Many types can use `DefaultStringInterpolation`, which implements `String`'s interpolation behavior. Types can also implement custom behavior by providing their own type conforming to `StringInterpolationProtocol`.
3+
4+
In addition to its formal requirements, `init(literalCapacity:interpolationCount:)` and `appendLiteral(_:)`, `StringInterpolationProtocol` has an additional, informal requirement, `appendInterpolation`. String interpolations using `\()` syntax are translated into calls to matching `appendInterpolation` methods.
5+
6+
`StringInterpolationProtocol` conformers must provide at least one `appendInterpolation` method which:
7+
8+
- Is an instance method, as opposed to a `static` or `class` method
9+
- Does not specify a return type, explicitly returns `Void`, or is marked with the `@discardableResult` attribute
10+
- Is at least as accessible as its containing type
11+
12+
There are no restrictions on an `appendInterpolation` method's argument list, generic parameters, availability, or error-throwing behavior.
13+
14+
If `appendInterpolation` is overloaded, the Swift compiler will choose an appropriate overload using the labels and argument types of each interpolation. When choosing an overload, any accessible `appendInterpolation` instance method may be used, even if it does not meet all of the requirements above. However, if a `StringInterpolationProtocol` conformer doesn't have any `appendInterpolation` methods which meet all of the requirments, an error will be reported at compile time.
15+
16+
To learn more about customizing string interpolation behavior, see the standard library documentation of the `ExpressibleByStringInterpolation` and `StringInterpolationProtocol` protocols.

0 commit comments

Comments
 (0)