Skip to content

Reject bad string interpolations #17074

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 8 commits into from
Jun 13, 2018
Merged

Reject bad string interpolations #17074

merged 8 commits into from
Jun 13, 2018

Conversation

beccadax
Copy link
Contributor

@beccadax beccadax commented Jun 8, 2018

String interpolations with multiple comma-separated expressions or argument labels were being incorrectly accepted. This change makes both of them into errors, with appropriate fix-its.

The comma-separated-expressions error could be a warning if we wanted to preserve source compatibility, but as far as I can tell there are no examples of it in the source compatibility suite. The argument-label one can cause an error/crash in serialization if not diagnosed.

Resolves SR-7937 and SR-7958.


// Test with too few interpolated values.
func mamaBear() {
_ = "\()" // expected-error{{expected expression in list of expressions}}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Any way that we can also make this error less cryptic as part of this change? It’s essentially nonsense right now, at least to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It was surprisingly nontrivial, but I've just pushed changes to do it. The new version emits the same "interpolations should be a single expression" error message for empty interpolations, but provides a different fix-it telling the user to delete the empty interpolation.

@@ -1927,6 +1927,37 @@ parseStringSegments(SmallVectorImpl<Lexer::StringSegment> &Segments,
Status |= E;
if (E.isNonNull()) {
Exprs.push_back(E.get());

if (auto tuple = dyn_cast<TupleExpr>(Exprs.back())) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the expression ever not a TupleExpr? Maybe use cast<> here instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The expression actually should be a ParenExpr. I don’t know of a reason it should be a TupleExpr without one of the nested conditions being true, but if it ever was, as far as I know it would be valid.

@CodaFi
Copy link
Contributor

CodaFi commented Jun 10, 2018

Fingers crossed

@swift-ci please test source compatibility

@CodaFi
Copy link
Contributor

CodaFi commented Jun 10, 2018

Source Compatibility Suite Passed

Kill It With 🔥🔥🔥


// Test with an argument label
func funkyBear() {
_ = "\(describing: str)" // expected-error{{interpolations cannot start with a keyword argument}} {{10-22=}}
Copy link
Contributor

Choose a reason for hiding this comment

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

A suggestion: Try another String initializer or two.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

beccadax added 8 commits June 11, 2018 03:12
String interpolations with multiple comma-separate expressions or argument labels were being incorrectly accepted.
Fixes a test at Parse/recovery.swift:799 which the previous commit broke.
A previous version of this test used FileCheck instead of -verify, and the run line wasn’t properly corrected to use -verify.
Ensures that we don’t get different results from an initializer that doesn’t exist or doesn’t take a String.
We now diagnose the error and remove the label before it has an opportunity to crash.
@beccadax
Copy link
Contributor Author

With the crasher in and fixed, I think this one's ready to go.

@CodaFi
Copy link
Contributor

CodaFi commented Jun 11, 2018

@swift-ci please smoke test

@CodaFi
Copy link
Contributor

CodaFi commented Jun 13, 2018

⛵️

@CodaFi CodaFi merged commit fc23f34 into swiftlang:master Jun 13, 2018
@beccadax beccadax deleted the labels-are-so-reductive branch June 13, 2018 03:41
CodaFi added a commit to CodaFi/swift that referenced this pull request Jun 13, 2018
CodaFi added a commit that referenced this pull request Jun 14, 2018
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.

4 participants