Skip to content

Commit 75656a9

Browse files
authored
Merge pull request #66942 from kavon/parsing-without-improvement
handle parsing `~` before a type in more places
2 parents 44ab03a + 2cbe438 commit 75656a9

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}}
@@ -57,3 +58,24 @@ public enum MoveOnlyE2<T: Equatable> : ~Copyable {
5758
case holding(s: MoveOnlyS1<T>)
5859
consuming func x() {}
5960
}
61+
62+
func more() {
63+
let foo: any ~Copyable = 19 // expected-error@:16 {{cannot suppress conformances here}}
64+
// expected-error@-1 {{cannot find type 'Copyable' in scope}}
65+
66+
let foo: any ~Equatable = 19 // expected-error@:16 {{cannot suppress conformances here}}
67+
}
68+
69+
func blah<T>(_ t: T) where T: ~Copyable, // expected-error@:31 {{cannot suppress conformances here}}
70+
// expected-error@-1 {{cannot find type 'Copyable' in scope}}
71+
72+
T: ~Hashable {} // expected-error@:31 {{cannot suppress conformances here}}
73+
74+
func foo<T: ~Copyable>(x: T) {} // expected-error {{cannot suppress conformances here}}
75+
// expected-error@-1 {{cannot find type 'Copyable' in scope}}
76+
77+
struct Buurap<T: ~Copyable> {} // expected-error {{cannot suppress conformances here}}
78+
// expected-error@-1 {{cannot find type 'Copyable' in scope}}
79+
80+
protocol Foo where Self: ~Copyable {} // expected-error {{cannot suppress conformances here}}
81+
// expected-error@-1 {{cannot find type 'Copyable' in scope}}

0 commit comments

Comments
 (0)