Skip to content

Commit 5a28850

Browse files
authored
Merge pull request #66943 from kavon/5.9-parsing-without-improvement
[5.9🍒] handle parsing `~` before a type in more places
2 parents 855acff + aae257c commit 5a28850

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

lib/Parse/ParseGeneric.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ Parser::parseGenericParametersBeforeWhere(SourceLoc LAngleLoc,
9999
ParserResult<TypeRepr> Ty;
100100

101101
if (Tok.isAny(tok::identifier, tok::code_complete, tok::kw_protocol,
102-
tok::kw_Any)) {
102+
tok::kw_Any) || Tok.isTilde()) {
103103
Ty = parseType();
104104
} else if (Tok.is(tok::kw_class)) {
105105
diagnose(Tok, diag::unexpected_class_constraint);

lib/Parse/ParseType.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,15 @@ ParserResult<TypeRepr> Parser::parseTypeSimple(
154154
Diag<> MessageID, ParseTypeReason reason) {
155155
ParserResult<TypeRepr> ty;
156156

157+
158+
// Prevent the use of ~ as prefix for a type. We specially parse them
159+
// in inheritance clauses elsewhere.
160+
if (Tok.isTilde()) {
161+
auto tildeLoc = consumeToken();
162+
diagnose(tildeLoc, diag::cannot_suppress_here)
163+
.fixItRemoveChars(tildeLoc, tildeLoc);
164+
}
165+
157166
if (Tok.is(tok::kw_inout)
158167
|| (canHaveParameterSpecifierContextualKeyword()
159168
&& (Tok.getRawText().equals("__shared")

test/Parse/without_copyable.swift

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ protocol Rope<Element>: ~Copyable { // expected-error {{cannot suppress conforma
3535
extension S: ~Copyable {} // expected-error {{cannot suppress conformances here}}
3636
// expected-error@-1 {{cannot find type 'Copyable' in scope}}
3737

38-
func takeNoncopyableGeneric<T: ~Copyable>(_ t: T) {} // expected-error {{expected a class type or protocol-constrained type restricting 'T'}}
38+
func takeNoncopyableGeneric<T: ~Copyable>(_ t: T) {} // expected-error {{cannot suppress conformances here}}
39+
// expected-error@-1 {{cannot find type 'Copyable' in scope}}
3940

4041
@_moveOnly struct ExtraNonCopyable: // expected-error {{duplicate attribute}}{{1-12=}}
4142
~Copyable // expected-note {{attribute already specified here}}
@@ -45,3 +46,24 @@ func takeNoncopyableGeneric<T: ~Copyable>(_ t: T) {} // expected-error {{expecte
4546
struct HasADeinit: ~Copyable {
4647
deinit {}
4748
}
49+
50+
func more() {
51+
let foo: any ~Copyable = 19 // expected-error@:16 {{cannot suppress conformances here}}
52+
// expected-error@-1 {{cannot find type 'Copyable' in scope}}
53+
54+
let foo: any ~Equatable = 19 // expected-error@:16 {{cannot suppress conformances here}}
55+
}
56+
57+
func blah<T>(_ t: T) where T: ~Copyable, // expected-error@:31 {{cannot suppress conformances here}}
58+
// expected-error@-1 {{cannot find type 'Copyable' in scope}}
59+
60+
T: ~Hashable {} // expected-error@:31 {{cannot suppress conformances here}}
61+
62+
func foo<T: ~Copyable>(x: T) {} // expected-error {{cannot suppress conformances here}}
63+
// expected-error@-1 {{cannot find type 'Copyable' in scope}}
64+
65+
struct Buurap<T: ~Copyable> {} // expected-error {{cannot suppress conformances here}}
66+
// expected-error@-1 {{cannot find type 'Copyable' in scope}}
67+
68+
protocol Foo where Self: ~Copyable {} // expected-error {{cannot suppress conformances here}}
69+
// expected-error@-1 {{cannot find type 'Copyable' in scope}}

0 commit comments

Comments
 (0)