Skip to content
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

Closed
CodaFi opened this issue Oct 28, 2019 · 8 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself good first issue Good for newcomers parser Area → compiler: The legacy C++ parser

Comments

@CodaFi
Copy link
Member

CodaFi commented Oct 28, 2019

Previous ID SR-11677
Radar rdar://38225184
Original Reporter @CodaFi
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, Parser, StarterBug
Assignee ninjiacoder (JIRA)
Priority Medium

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.

extension Collection where Element == Int && Index == Int {
  
}
@swift-ci
Copy link
Collaborator

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?

@CodaFi
Copy link
Member Author

CodaFi commented Oct 31, 2019

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!

@swift-ci
Copy link
Collaborator

swift-ci commented Nov 1, 2019

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`?

@CodaFi
Copy link
Member Author

CodaFi commented Nov 2, 2019

   If I edit the compiler source code for example `ParseGeneric.cpp`, how can I test Swift built by myself? 

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.

By the way, I've read the code of `Parser::parseGenericWhereClause`...

You shouldn't need to care about these things. They're just part of the way we handle control in the recursive descent parser.

@swift-ci
Copy link
Collaborator

swift-ci commented Nov 2, 2019

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 

@CodaFi
Copy link
Member Author

CodaFi commented Nov 2, 2019

Make sure you consume the token and continue parsing as though it were a comma.

@swift-ci
Copy link
Collaborator

swift-ci commented Nov 2, 2019

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.

@swift-ci
Copy link
Collaborator

swift-ci commented Nov 4, 2019

Comment by Ziyuan Zhao (JIRA)

The PR has been merged

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself good first issue Good for newcomers parser Area → compiler: The legacy C++ parser
Projects
None yet
Development

No branches or pull requests

2 participants