Skip to content

Commit 1812830

Browse files
authored
Merge pull request #9294 from ahoppen/SR-4785-fixit-missing-type
[Parser] Add fix-it for missing type errors
2 parents 7b7bb7b + adbe862 commit 1812830

File tree

5 files changed

+15
-10
lines changed

5 files changed

+15
-10
lines changed

lib/Parse/ParseType.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,12 @@ ParserResult<TypeRepr> Parser::parseTypeSimple(Diag<> MessageID,
196196
}
197197
LLVM_FALLTHROUGH;
198198
default:
199-
diagnose(Tok, MessageID);
199+
auto diag = diagnose(Tok, MessageID);
200+
// If the next token is closing or separating, the type was likely forgotten
201+
if (Tok.isAny(tok::r_paren, tok::r_brace, tok::r_square, tok::arrow,
202+
tok::equal, tok::comma, tok::semi)) {
203+
diag.fixItInsert(getEndOfPreviousLoc(), " <#type#>");
204+
}
200205
if (Tok.isKeyword() && !Tok.isAtStartOfLine()) {
201206
ty = makeParserErrorResult(new (Context) ErrorTypeRepr(Tok.getLoc()));
202207
consumeToken();

test/Parse/recovery.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ _ = foobar // OK.
297297
//===--- Recovery for parse errors in types.
298298

299299
struct ErrorTypeInVarDecl1 {
300-
var v1 : // expected-error {{expected type}}
300+
var v1 : // expected-error {{expected type}} {{11-11= <#type#>}}
301301
}
302302

303303
struct ErrorTypeInVarDecl2 {
@@ -311,7 +311,7 @@ struct ErrorTypeInVarDecl3 {
311311
}
312312

313313
struct ErrorTypeInVarDecl4 {
314-
var v1 : Int<, // expected-error {{expected type}}
314+
var v1 : Int<, // expected-error {{expected type}} {{16-16= <#type#>}}
315315
var v2 : Int
316316
}
317317

test/decl/func/functions.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,20 @@ func g_recover_missing_tuple_paren(_ b: Int) {
7171

7272
//===--- Parse errors.
7373

74-
func parseError1a(_ a: ) {} // expected-error {{expected parameter type following ':'}}
74+
func parseError1a(_ a: ) {} // expected-error {{expected parameter type following ':'}} {{23-23= <#type#>}}
7575

76-
func parseError1b(_ a: // expected-error {{expected parameter type following ':'}}
76+
func parseError1b(_ a: // expected-error {{expected parameter type following ':'}} {{23-23= <#type#>}}
7777
) {}
7878

79-
func parseError2(_ a: Int, b: ) {} // expected-error {{expected parameter type following ':'}}
79+
func parseError2(_ a: Int, b: ) {} // expected-error {{expected parameter type following ':'}} {{30-30= <#type#>}}
8080

81-
func parseError3(_ a: unknown_type, b: ) {} // expected-error {{use of undeclared type 'unknown_type'}} expected-error {{expected parameter type following ':'}}
81+
func parseError3(_ a: unknown_type, b: ) {} // expected-error {{use of undeclared type 'unknown_type'}} expected-error {{expected parameter type following ':'}} {{39-39= <#type#>}}
8282

8383
func parseError4(_ a: , b: ) {} // expected-error 2{{expected parameter type following ':'}}
8484

8585
func parseError5(_ a: b: ) {} // expected-error {{use of undeclared type 'b'}} expected-error {{expected ',' separator}} {{24-24=,}} expected-error {{expected parameter name followed by ':'}}
8686

87-
func parseError6(_ a: unknown_type, b: ) {} // expected-error {{use of undeclared type 'unknown_type'}} expected-error {{expected parameter type following ':'}}
87+
func parseError6(_ a: unknown_type, b: ) {} // expected-error {{use of undeclared type 'unknown_type'}} expected-error {{expected parameter type following ':'}} {{39-39= <#type#>}}
8888

8989
func parseError7(_ a: Int, goo b: unknown_type) {} // expected-error {{use of undeclared type 'unknown_type'}}
9090

test/decl/protocol/protocols.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ protocol Test2 {
2727
associatedtype mytype
2828
associatedtype mybadtype = Int
2929

30-
associatedtype V : Test = // expected-error {{expected type in associated type declaration}}
30+
associatedtype V : Test = // expected-error {{expected type in associated type declaration}} {{28-28= <#type#>}}
3131
}
3232

3333
func test1() {

test/expr/expressions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ func test_as_1() {
236236
}
237237
func test_as_2() {
238238
let x: Int = 1
239-
x as [] // expected-error {{expected element type}}
239+
x as [] // expected-error {{expected element type}} {{9-9= <#type#>}}
240240
}
241241

242242
func test_lambda() {

0 commit comments

Comments
 (0)