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-11677] Offer a better diagnostic when && is used in trailing where clause declaration #54086
Comments
Comment by Ziyuan Zhao (JIRA) Hi, I'm a starter and willing to fix this. But I'm not sure where to start, can you give me some advice? |
ninjiacoder (JIRA User) There's a bit at the bottom of Parser::parseGenericWhereClause that tries to check if there's a trailing comma. The easiest thing to do would be to check for the presence of && with Tok.isBinaryOperator() && Tok.getText() == "&&"; Then emit a diagnostic and keep munging text. Let me know if you need any more pointers. When you begin to work on this, be sure to assign yourself to this SR! |
Comment by Ziyuan Zhao (JIRA) @CodaFi Thank you for your advice. I've tried something today. I use `swift/utils/build-script -x` and after couple of hours generated `Swift.xcodeproj`. If I edit the compiler source code for example `ParseGeneric.cpp`, how can I test Swift built by myself? Just need to rebuild the Xcode project with `swift` scheme and test with bin/swift? By the way, I've read the code of `Parser::parseGenericWhereClause`, every if condition has code like `BodyContext->...` & `Status.setIsParseError()`. If I add a check for `&&`, do I need to care about this `BodyContext` & `Status`? |
You can use the scheme selector or the scheme editor to select the `swift` target. From there, any compiler arguments you need to supply, you can put into the `Arguments Passed on Launch` field and just run the compiler like a normal command line tool. An example of arguments you would use would be something like -frontend -parse /Path/To/My/Test/File.swift Which will construct a single frontend invocation that will run off and just parse the file and emit whatever diagnostics. To write a test, you need to attach the -verify flag. You can see an example of doing this in the Parse tests directory.
You shouldn't need to care about these things. They're just part of the way we handle control in the recursive descent parser. |
Comment by Ziyuan Zhao (JIRA) Thanks. I've added the check of `&&` to the bottom of `Parser::parseGenericWhereClause`. It works to emit a diagnostic of replacing "&&" with ",", but other garbage diagnostics still emit. Do I need to find the way to remove them? /Users/ninjiacoder/Development/swift-source/swift/test/Constraints/rdar38225184.swift:1:43: error: use ',' for spliting conditions rather than '&&'
extension Collection where Element == Int && Index == Int {}
^~
,
/Users/ninjiacoder/Development/swift-source/swift/test/Constraints/rdar38225184.swift:1:43: error: expected '{' in extension
extension Collection where Element == Int && Index == Int {}
^
/Users/ninjiacoder/Development/swift-source/swift/test/Constraints/rdar38225184.swift:1:59: error: top-level statement cannot begin with a closure expression
extension Collection where Element == Int && Index == Int {}
^
/Users/ninjiacoder/Development/swift-source/swift/test/Constraints/rdar38225184.swift:1:28: error: use of undeclared type 'Element'
extension Collection where Element == Int && Index == Int {}
^~~~~~~
/Users/ninjiacoder/Development/swift-source/swift/test/Constraints/rdar38225184.swift:1:59: error: closure expression is unused
extension Collection where Element == Int && Index == Int {}
^
/Users/ninjiacoder/Development/swift-source/swift/test/Constraints/rdar38225184.swift:1:59: note: did you mean to use a 'do' statement?
extension Collection where Element == Int && Index == Int {}
^
do |
Make sure you consume the token and continue parsing as though it were a comma. |
Comment by Ziyuan Zhao (JIRA) Oh I see. I just add the following code in the check: consumeToken();
HasNextReq = true; Then the garbage diagnostics disappear. It seems that it's fixed. l've made a [PR](#28033 please review it when you are available. |
Comment by Ziyuan Zhao (JIRA) The PR has been merged |
Additional Detail from JIRA
md5: 50a519e5af58b06b95d65605733d7cf0
Issue Description:
We currently emit a pile of garbage diagnostics about this declaration. We should instead offer a concise diagnostic that replaces the && with a comma.
The text was updated successfully, but these errors were encountered: