Skip to content

Commit 414cfe7

Browse files
committed
Minor changes to keyword-as-identifier diagnostics
- Remove stray newline - Adjust wording when recommending backticks for a keyword identifier - Provide fix-it when encountering a keyword as an identifier rdar://problem/25761380
1 parent bd18590 commit 414cfe7

File tree

10 files changed

+16
-15
lines changed

10 files changed

+16
-15
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ ERROR(expected_pattern,PointsToFirstBadToken,
662662
ERROR(keyword_cant_be_identifier,none,
663663
"keyword '%0' cannot be used as an identifier here", (StringRef))
664664
NOTE(backticks_to_escape,none,
665-
"backticks can escape this name if it is important to use", ())
665+
"if this name is unavoidable, use backticks to escape it", ())
666666
ERROR(expected_rparen_tuple_pattern_list,none,
667667
"expected ')' at end of tuple pattern", ())
668668
ERROR(untyped_pattern_ellipsis,none,

lib/Parse/ParseDecl.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
//===--- ParseDecl.cpp - Swift Language Parser for Declarations -----------===//
32
//
43
// This source file is part of the Swift.org open source project

lib/Parse/ParsePattern.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,8 @@ ParserResult<Pattern> Parser::parsePattern() {
824824
if (Tok.isKeyword() &&
825825
(peekToken().is(tok::colon) || peekToken().is(tok::equal))) {
826826
diagnose(Tok, diag::keyword_cant_be_identifier, Tok.getText());
827-
diagnose(Tok, diag::backticks_to_escape);
827+
diagnose(Tok, diag::backticks_to_escape)
828+
.fixItReplace(Tok.getLoc(), "`" + Tok.getText().str() + "`");
828829
SourceLoc Loc = Tok.getLoc();
829830
consumeToken();
830831
return makeParserErrorResult(new (Context) AnyPattern(Loc));

lib/Parse/Parser.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,8 @@ bool Parser::parseAnyIdentifier(Identifier &Result, SourceLoc &Loc,
603603

604604
if (Tok.isKeyword()) {
605605
diagnose(Tok, diag::keyword_cant_be_identifier, Tok.getText());
606-
diagnose(Tok, diag::backticks_to_escape);
606+
diagnose(Tok, diag::backticks_to_escape)
607+
.fixItReplace(Tok.getLoc(), "`" + Tok.getText().str() + "`");
607608
} else {
608609
diagnose(Tok, D);
609610
}

test/Parse/invalid.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ class VarTester {
9292

9393
func repeat() {}
9494
// expected-error @-1 {{keyword 'repeat' cannot be used as an identifier here}}
95-
// expected-note @-2 {{backticks can escape this name if it is important to use}}
95+
// expected-note @-2 {{if this name is unavoidable, use backticks to escape it}} {{6-12=`repeat`}}
9696

9797
let for = 2
9898
// expected-error @-1 {{keyword 'for' cannot be used as an identifier here}}
99-
// expected-note @-2 {{backticks can escape this name if it is important to use}}
99+
// expected-note @-2 {{if this name is unavoidable, use backticks to escape it}} {{5-8=`for`}}

test/decl/ext/extensions.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ extension S1.NestedStruct {} // no-error
4949
struct S1_2 {
5050
// expected-error @+4 {{type member may not be named 'Type', since it would conflict with the 'foo.Type' expression}}
5151
// expected-error @+3 {{type member may not be named 'Type', since it would conflict with the 'foo.Type' expression}}
52-
// expected-note @+2 {{backticks can escape this name if it is important to use}} {{8-12=`Type`}}
53-
// expected-note @+1 {{backticks can escape this name if it is important to use}} {{8-12=`Type`}}
52+
// expected-note @+2 {{if this name is unavoidable, use backticks to escape it}} {{8-12=`Type`}}
53+
// expected-note @+1 {{if this name is unavoidable, use backticks to escape it}} {{8-12=`Type`}}
5454
enum Type {}
5555
}
5656
struct S1_3 {

test/decl/import/import.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ import struct Swift.nonexistent // expected-error {{no such decl in module}}
4545

4646
import Swift.import.abc // expected-error {{expected identifier in import declaration}}
4747
// expected-error @-1 {{keyword 'import' cannot be used as an identifier here}}
48-
// expected-note @-2 {{backticks can escape this name if it is important to use}}
48+
// expected-note @-2 {{if this name is unavoidable, use backticks to escape it}}
4949
import where Swift.Int // expected-error {{expected identifier}}
5050
// expected-error @-1 {{keyword 'where' cannot be used as an identifier here}}
51-
// expected-note @-2 {{backticks can escape this name if it is important to use}}
51+
// expected-note @-2 {{if this name is unavoidable, use backticks to escape it}}
5252
import 2 // expected-error {{expected identifier}}
5353

5454
import really.nonexistent // expected-error {{no such module 'really.nonexistent'}}

test/decl/nested.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,8 @@ func outerGenericFunction<T>(_ t: T) {
198198
struct S1 {
199199
// expected-error @+4 {{type member may not be named 'Type', since it would conflict with the 'foo.Type' expression}}
200200
// expected-error @+3 {{type member may not be named 'Type', since it would conflict with the 'foo.Type' expression}}
201-
// expected-note @+2 {{backticks can escape this name if it is important to use}} {{8-12=`Type`}}
202-
// expected-note @+1 {{backticks can escape this name if it is important to use}} {{8-12=`Type`}}
201+
// expected-note @+2 {{if this name is unavoidable, use backticks to escape it}} {{8-12=`Type`}}
202+
// expected-note @+1 {{if this name is unavoidable, use backticks to escape it}} {{8-12=`Type`}}
203203
enum Type {
204204
case A
205205
}

test/decl/overload.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,11 +301,11 @@ enum EnumWithMutating {
301301
// https://twitter.com/jadengeller/status/619989059046240256
302302
protocol r21783216a {
303303
// expected-error @+2 {{type member may not be named 'Type', since it would conflict with the 'foo.Type' expression}}
304-
// expected-note @+1 {{backticks can escape this name if it is important to use}} {{18-22=`Type`}}
304+
// expected-note @+1 {{if this name is unavoidable, use backticks to escape it}} {{18-22=`Type`}}
305305
associatedtype Type
306306

307307
// expected-error @+2 {{type member may not be named 'Protocol', since it would conflict with the 'foo.Protocol' expression}}
308-
// expected-note @+1 {{backticks can escape this name if it is important to use}} {{18-26=`Protocol`}}
308+
// expected-note @+1 {{if this name is unavoidable, use backticks to escape it}} {{18-26=`Protocol`}}
309309
associatedtype Protocol
310310
}
311311

test/decl/var/variables.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var bfx : Int, bfy : Int
1515
_ = 10
1616

1717
func _(_ x: Int) {} // expected-error {{keyword '_' cannot be used as an identifier here}}
18-
// expected-note @-1 {{backticks can escape this name if it is important to use}}
18+
// expected-note @-1 {{if this name is unavoidable, use backticks to escape it}}
1919

2020

2121
var self1 = self1 // expected-error {{variable used within its own initial value}}

0 commit comments

Comments
 (0)