Skip to content

[Parser] Add fix-it for missing type errors #9294

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/Parse/ParseType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,12 @@ ParserResult<TypeRepr> Parser::parseTypeSimple(Diag<> MessageID,
}
LLVM_FALLTHROUGH;
default:
diagnose(Tok, MessageID);
auto diag = diagnose(Tok, MessageID);
// If the next token is closing or separating, the type was likely forgotten
if (Tok.isAny(tok::r_paren, tok::r_brace, tok::r_square, tok::arrow,
tok::equal, tok::comma, tok::semi)) {
diag.fixItInsert(getEndOfPreviousLoc(), " <#type#>");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't perfect because the space isn't always needed, but I can't think of a way to test whether it's needed without looking at the previous token, which is a bit tricky. I guess this is good enough!

}
if (Tok.isKeyword() && !Tok.isAtStartOfLine()) {
ty = makeParserErrorResult(new (Context) ErrorTypeRepr(Tok.getLoc()));
consumeToken();
Expand Down
4 changes: 2 additions & 2 deletions test/Parse/recovery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ _ = foobar // OK.
//===--- Recovery for parse errors in types.

struct ErrorTypeInVarDecl1 {
var v1 : // expected-error {{expected type}}
var v1 : // expected-error {{expected type}} {{11-11= <#type#>}}
}

struct ErrorTypeInVarDecl2 {
Expand All @@ -311,7 +311,7 @@ struct ErrorTypeInVarDecl3 {
}

struct ErrorTypeInVarDecl4 {
var v1 : Int<, // expected-error {{expected type}}
var v1 : Int<, // expected-error {{expected type}} {{16-16= <#type#>}}
var v2 : Int
}

Expand Down
10 changes: 5 additions & 5 deletions test/decl/func/functions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,20 @@ func g_recover_missing_tuple_paren(_ b: Int) {

//===--- Parse errors.

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

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

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

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

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

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

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

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

Expand Down
2 changes: 1 addition & 1 deletion test/decl/protocol/protocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ protocol Test2 {
associatedtype mytype
associatedtype mybadtype = Int

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

func test1() {
Expand Down
2 changes: 1 addition & 1 deletion test/expr/expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func test_as_1() {
}
func test_as_2() {
let x: Int = 1
x as [] // expected-error {{expected element type}}
x as [] // expected-error {{expected element type}} {{9-9= <#type#>}}
}

func test_lambda() {
Expand Down