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
[SR-13098] Diagnostic improvement: encountering an unexpected statement at type scope #55544
Comments
@swift-ci create |
Comment by Daniel Sweeney (JIRA) Taking a look at this one if that's OK. |
Sure thing. Please feel free to ask for help as you need it. 🙂 |
Comment by Daniel Sweeney (JIRA) trying some variations and reading the code:
The first few variations work OK. It's the property access with an identifier at the beginning that triggers the problem.
which is probably what's triggering the diagnostics we get, with another predicate someplace to get the behavior with choices:
Thinking about the consequences to 1. above now. |
Comment by Daniel Sweeney (JIRA) Adding I guess option 4. above would be to handle this situation separately without piggybacking on the |
Looking at the debugger, the Ideally, we would try parsing as a statement and if that succeeds, give the user an extra hint that we found a statement even though we were expecting a declaration. I don't know how easy that would be to implement though (and if it might lead to degradation of other diagnostics). That said, I did try something like the following: diff --git a/include/swift/AST/DiagnosticsParse.def b/include/swift/AST/DiagnosticsParse.def
index 1a79bcc0da3..3257d72a53a 100644
--- a/include/swift/AST/DiagnosticsParse.def
+++ b/include/swift/AST/DiagnosticsParse.def
@@ -219,2 +219,3 @@ ERROR(disallowed_var_multiple_getset,none,
" getters/setters", ())
+NOTE(found_unexpected_stmt,none, "found a unexpected statement here", ())
diff --git a/lib/Parse/ParseDecl.cpp b/lib/Parse/ParseDecl.cpp
index 8c2b0a42e0c..9c9c876273a 100644
--- a/lib/Parse/ParseDecl.cpp
+++ b/lib/Parse/ParseDecl.cpp
@@ -3926,3 +3926,4 @@ Parser::parseDecl(ParseDeclOptions Flags,
const bool IsProbablyFuncDecl =
- Tok.isIdentifierOrUnderscore() || Tok.isAnyOperator();
+ (Tok.isIdentifierOrUnderscore() || Tok.isAnyOperator())
+ && peekToken().is(tok::l_paren);
@@ -3954,2 +3955,11 @@ Parser::parseDecl(ParseDeclOptions Flags,
}
+
+ auto startLoc = Tok.getLoc();
+ auto startPosition = this->getParserPosition();
+ ParserResult<Stmt> tryParseAsStmt = this->parseStmt();
+ if (tryParseAsStmt.isParseError())
+ this->backtrackToPosition(startPosition);
+ else
+ diagnose(startLoc, diag::found_unexpected_stmt);
}
However, this ends up (a) printing a diagnostic (undesirable, given that we might not actually have a statement) and (b) crashing with the following signature: Assertion failed: (InitialLoc != P.Tok.getLoc() && "parser did not make progress, this can result in infinite loop"), function ~AssertParserMadeProgressBeforeLeavingScopeRAII, file /Users/varun/foss-swift/swift/include/swift/Parse/Parser.h, line 253. I don't know why it didn't consume anything or why it failed to parse as a statement. I don't have much experience working on the parser, so I'm somewhat unsure if it is possible to implement what I'm looking for without major changes... |
Comment by Daniel Sweeney (JIRA) Thanks Varun! I was thinking about trying to parse a statement like you did above. I think declarations are statements in the grammar, so it's possible that I think catching and diagnosing this condition in the |
Comment by Daniel Sweeney (JIRA) I put a pull request through just now at #32984 I left it as a draft--I hope you can see it. I created a new case for the Right now this is just a note but I can convert it to an error obviously. Also I think I dropped a line from one of the other test cases--I have to fix that. I'm trying to think of other situations where this occurs. In this case we have the |
Additional Detail from JIRA
md5: 8298353d748ac718f859d5f973f05863
Issue Description:
Consider the following minimal example:
A more realistic version can be seen here: https://developer.apple.com/forums/thread/651253
The cascade of errors is quite intimidating. We should:
1. Avoid the cascade of errors, hopefully providing only one or two errors.
2. Provide a more useful diagnostic. For example, we might want to point out that assignments are not permitted at class scope, they must be inside a function. We might also want to say that we expected a property or a function declaration here.
The text was updated successfully, but these errors were encountered: