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

[Parser] Cleans up parsing of function parameter attributes #1812

Merged
merged 1 commit into from
Mar 29, 2016

Conversation

manavgabhawala
Copy link
Contributor

What's in this pull request?

This pull request cleans up parsing of parameter attributes by parsing the inout, let and var tokens better. It cleans up the implementation to work better with SE-0003 by giving better fix-its and diagnostics for var as parameter attributes. It implements SE-0053 to disallow let as an attribute. It provides better fixits for inout that are either duplicated or misplaced to better implement SE-0031.

Resolved bug number:


Before merging this pull request to apple/swift repository:

  • Test pull request on Swift continuous integration.

Triggering Swift CI

The swift-ci is triggered by writing a comment on this PR addressed to the GitHub user @swift-ci. Different tests will run depending on the specific comment that you use. The currently available comments are:

Smoke Testing

Platform Comment
All supported platforms @swift-ci Please smoke test
OS X platform @swift-ci Please smoke test OS X platform
Linux platform @swift-ci Please smoke test Linux platform

Validation Testing

Platform Comment
All supported platforms @swift-ci Please test
OS X platform @swift-ci Please test OS X platform
Linux platform @swift-ci Please test Linux platform

Note: Only members of the Apple organization can trigger swift-ci.

@manavgabhawala manavgabhawala changed the title Cleans up parsing of function parameter attributes [Parser] Cleans up parsing of function parameter attributes Mar 23, 2016
@tkremenek
Copy link
Member

@swift-ci Please smoke test

@tkremenek
Copy link
Member

@lattner can you review this change?

@lattner
Copy link
Contributor

lattner commented Mar 24, 2016

Yes, I'll review it, hopefully tomorrow.

@manavgabhawala
Copy link
Contributor Author

@lattner or @tkremenek can you please run the tests again? I fixed all the ones that were failing and squashed to a single commit.

@tkremenek
Copy link
Member

@swift-ci please test


// ('inout' | 'let')?
bool hasSpecifier = false;
while (Tok.isAny(tok::kw_inout, tok::kw_let, tok::kw_var)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure why you changed the comment here, given that kw_var is still accepted.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We check for kw_var to give the warning but the code would be invalid if var was still there. I'm not sure whether the comment is supposed to reflect what's valid or what's checked for.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd prefer to have the in-function comments correlate to the code. It makes sense to keep the comments at the top of the function aligned with the formal grammar.

@lattner
Copy link
Contributor

lattner commented Mar 25, 2016

I made a few comments above, the test failures seem spurious.

@manavgabhawala
Copy link
Contributor Author

@lattner I changed it so that the 'var' detection now happens in sema, and provides a nifty fixit to place a shadow copy. I tried to make it so that indentation would work too, but tbh I'm not completely sure that's the right way. I also fixed the comments, and the test case with the let/var diff, I moved it to another test case because the diagnostic was not getting a chance to run because of all the other errors in the file.

@lattner
Copy link
Contributor

lattner commented Mar 28, 2016

@swift-ci please test

ERROR(parameter_let_as_attr,none,
"'let' before a parameter type is not allowed, place it before the parameter name instead", ())
ERROR(parameter_var_as_attr,none,
"'var' as a parameter attribute is not allowed, consider creating a shadow copy in the function body or using 'inout' for pass-by-reference", ())
Copy link
Contributor

Choose a reason for hiding this comment

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

Please indent the "'var... line to match the placement of the "(parameter_var_as_attr" on the previous line. Similarly, please de-indent the "'let' before ... " line a few lines above.

@lattner
Copy link
Contributor

lattner commented Mar 28, 2016

Detailed comments in the patch. This is looking great, thank you for working on this!

@lattner
Copy link
Contributor

lattner commented Mar 28, 2016

To confirm, SE-0053 is accepted.

@manavgabhawala
Copy link
Contributor Author

@lattner SE-0053 is implemented along with all the other changes you requested. Turns out the Lexer already had a function for determining whitespace so I used that instead of making a new one in the SourceManager.

@lattner
Copy link
Contributor

lattner commented Mar 29, 2016

Okay great. Should we then allow let var and inout be argument labels, or warn/error and give fixit information for those?

I think we should warn/error on them with migration away from the syntax where possible. That said, we shouldn't bother with this if they occur after the colon, since that is not syntax we have ever accepted.

@lattner
Copy link
Contributor

lattner commented Mar 29, 2016

@swift-ci please test

@lattner
Copy link
Contributor

lattner commented Mar 29, 2016

The test failure is a win: you fixed a crasher :-)

@manavgabhawala
Copy link
Contributor Author

Unfortunately, it still crashes with the inout moved to the right location.

@lattner
Copy link
Contributor

lattner commented Mar 29, 2016

Yep, please, and remove the --crash from the run line. Thanks!

@lattner
Copy link
Contributor

lattner commented Mar 29, 2016

Ok, reviewing the patch now.

"parameter may not have multiple 'inout', 'var', or 'let' specifiers",
())
ERROR(parameter_let_as_attr,none,
"'let' as a parameter attribute is not allowed", (bool))

Copy link
Contributor

Choose a reason for hiding this comment

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

The (bool) parameter is not used and is always false. You can drop it. I was thinking that you can do something like:

"'%select{let|var}0' as a parameter attribute is not allowed"

Where the bool is "isVar()"

Copy link
Contributor

Choose a reason for hiding this comment

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

But given your logic, I can see how that doesn't make sense anymore! So I think you should just drop the bool.

@lattner
Copy link
Contributor

lattner commented Mar 29, 2016

And... that's all I've got, some of them are super picky :-) Thank you again for working on this, the fixits & migration that you're providing are really top notch!

@manavgabhawala
Copy link
Contributor Author

@lattner fixed everything, I think. Thank you so much for your peer reviews, its been really awesome to have the legendary Chris Lattner review my code :)

@lattner
Copy link
Contributor

lattner commented Mar 29, 2016

If I'm not mistaken, fixItRemove already handles this. In fact the comments there talk about this issue to (quoted below):

Ah, you're right, thanks!

@lattner
Copy link
Contributor

lattner commented Mar 29, 2016

@swift-ci Please test

@lattner
Copy link
Contributor

lattner commented Mar 29, 2016

The revised patch looks great. Once testing is done, I'll merge it.

In a follow-on PR, please update the changelog to mention the changes to formerly valid code (e.g. that inout/var/let before the colon) are now errors. Also, please add some a test to show that the invalid code is being rejected with the new diagnostics. Thanks again!

@lattner
Copy link
Contributor

lattner commented Mar 29, 2016

It looks like your refactor introduced some crashes:

swift: /home/buildnode/jenkins/workspace/swift-PR-Linux/swift/lib/AST/DiagnosticEngine.cpp:188: swift::InFlightDiagnostic &swift::InFlightDiagnostic::fixItReplaceChars(swift::SourceLoc, swift::SourceLoc, llvm::StringRef): Assertion `IsActive && "Cannot modify an inactive diagnostic"' failed.
#0 0x0000000003198f98 llvm::sys::PrintStackTrace(llvm::raw_ostream&) (/home/buildnode/jenkins/workspace/swift-PR-Linux/buildbot_linux/swift-linux-x86_64/bin/swift+0x3198f98)
#1 0x0000000003197766 llvm::sys::RunSignalHandlers() (/home/buildnode/jenkins/workspace/swift-PR-Linux/buildbot_linux/swift-linux-x86_64/bin/swift+0x3197766)
#2 0x0000000003199aca SignalHandler(int) (/home/buildnode/jenkins/workspace/swift-PR-Linux/buildbot_linux/swift-linux-x86_64/bin/swift+0x3199aca)
#3 0x00007f55cb21e340 restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x10340)
#4 0x00007f55c9bf5cc9 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x36cc9)
#5 0x00007f55c9bf90d8 abort (/lib/x86_64-linux-gnu/libc.so.6+0x3a0d8)
#6 0x00007f55c9beeb86 (/lib/x86_64-linux-gnu/libc.so.6+0x2fb86)
#7 0x00007f55c9beec32 (/lib/x86_64-linux-gnu/libc.so.6+0x2fc32)
#8 0x000000000108a560 swift::InFlightDiagnostic::fixItReplaceChars(swift::SourceLoc, swift::SourceLoc, llvm::StringRef) (/home/buildnode/jenkins/workspace/swift-PR-Linux/buildbot_linux/swift-linux-x86_64/bin/swift+0x108a560)
#9 0x0000000000eb9285 validateParameterType(swift::ParamDecl
, swift::DeclContext
, swift::OptionSet<swift::TypeResolutionFlags, unsigned int>, swift::GenericTypeResolver_, swift::TypeChecker&) (/home/buildnode/jenkins/workspace/swift-PR-Linux/buildbot_linux/swift-linux-x86_64/bin/swift+0xeb9285)
#10 0x0000000000eb8c05 swift::TypeChecker::typeCheckParameterList(swift::ParameterList_, swift::DeclContext_, swift::OptionSet<swift::TypeResolutionFlags, unsigned int>, swift::GenericTypeResolver_) (/home/buildnode/jenkins/workspace/swift-PR-Linux/buildbot_linux/swift-linux-x86_64/bin/swift+0xeb8c05)
#11 0x0000000000e8f3d4 (anonymous namespace)::DeclChecker::visitFuncDecl(swift::FuncDecl*) (/home/buildnode/jenkins/workspace/swift-PR-Linux/buildbot_linux/swift-linux-x86_64/bin/swift+0xe8f3d4)
#12

…3. Fixes SR-979, SR-1020 and cleans up implementation of SE-0003. Provides better fix-its and diagnostics for misplaced 'inout' and prohibits 'var' and 'let' from parameter attributes
@manavgabhawala
Copy link
Contributor Author

@lattner Should be fixed now. For the follow up PR do you want me to add tests to a separate file because this one already modifies and adds some tests that check for invalid var, let and inout. Specifically, Parse/invalid.swift and Sema/immutablility.swift run these tests and expect errors. What kind of tests you think I should add in addition to these, or are these enough?

@lattner
Copy link
Contributor

lattner commented Mar 29, 2016

Ah, if these are already covered, then we're good, thanks!

@lattner
Copy link
Contributor

lattner commented Mar 29, 2016

@swift-ci please test

@lattner
Copy link
Contributor

lattner commented Mar 29, 2016

@swift-ci Please test

@manavgabhawala
Copy link
Contributor Author

@lattner it looks like the linux build timed out. Is there anything I need to fix or will running it again fix it?

@lattner lattner merged commit 34ca3e9 into swiftlang:master Mar 29, 2016
@lattner
Copy link
Contributor

lattner commented Mar 29, 2016

It should be fine, I'll run some local tests. Thanks again for implementing this! Please close out the SR's, and send a PR for the changelog.

-Chris

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants