Skip to content

Change diagnostic error thrown for when string interpolations aren't closed by a parenthesis #58882

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 49 commits into from
Jun 4, 2022
Merged
Show file tree
Hide file tree
Changes from 42 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
b0e552d
Change error diagnosed when a string interpolation isnt closed
NSAntoine May 12, 2022
ec4f51c
change string_interpolation_unclosed to sound more natural
NSAntoine May 12, 2022
719fe1e
(potentionally) fix test errors
NSAntoine May 12, 2022
e8aad99
ACTUALLY fix tests, revert last commit
NSAntoine May 13, 2022
4e41d38
Add note and fix-it for string interpolation not ended with a )
NSAntoine May 13, 2022
6ab71cb
im dumb and forgot to remove this semicolon
NSAntoine May 13, 2022
e57780f
fix tests..again..
NSAntoine May 13, 2022
97b23c7
fix tests
NSAntoine May 14, 2022
03b2c4e
oops, i did not mean to push that yet
NSAntoine May 14, 2022
572177c
Fix fix-it for fixing unclosed string interpolation being given for a…
NSAntoine May 15, 2022
ec17988
try different approach
NSAntoine May 15, 2022
dabfdc0
Cast to char * to fix compilation error
NSAntoine May 15, 2022
4ca418a
change declaration of fixItLoc
NSAntoine May 15, 2022
95ca0b8
Remove string interpolation fix-it note, now instead attach the fix-i…
NSAntoine May 15, 2022
b93a70d
add this back to reflect previous change
NSAntoine May 15, 2022
4ef5c99
Trigger string_interpolation_unclosed only when CurPtr ends in a \r o…
NSAntoine May 15, 2022
e2626ee
fix indentation
NSAntoine May 15, 2022
c47deb5
remove fix-it
NSAntoine May 15, 2022
87acdff
Change string_interpolation_unclosed to be diagnosed with CurPtr, add…
NSAntoine May 15, 2022
1614242
Add note & tests requested
NSAntoine May 15, 2022
95e2730
Update lib/Parse/Lexer.cpp
NSAntoine May 16, 2022
bc58ed9
remove unused note
NSAntoine May 16, 2022
8523850
Merge branch 'serena/interpolation-and-unterminated-string-notes' of …
NSAntoine May 16, 2022
f34cfd9
this is what a day of no sleep does to a person
NSAntoine May 16, 2022
7121a5b
diagnose lex_unterminated_string for when CurPtr isnt )
NSAntoine May 16, 2022
a431b20
don't diagnose lex_unterminated_string when string literal is multili…
NSAntoine May 16, 2022
9dff8c8
im dumb
NSAntoine May 16, 2022
3acf0d4
changes
NSAntoine May 19, 2022
bcfdbac
emit diag::lex_unterminated_string as a fallback, and some other stuff
NSAntoine May 19, 2022
8a2cb84
add back some more comments, remove some useless stuff
NSAntoine May 19, 2022
76d4448
Check for if CurPtr is BufferEnd when checking for if CurPtr is \n or \r
NSAntoine May 19, 2022
787fb1a
Add back `continue` statement and correct grammar
NSAntoine May 19, 2022
68a7549
Don't drefrence CurPtr when comparing to BufferEnd
NSAntoine May 19, 2022
2dc8cbb
how did i forget this
NSAntoine May 19, 2022
caeffb9
shouldn't have been there
NSAntoine May 19, 2022
1616a5c
This should be correct now..
NSAntoine May 19, 2022
7e640b1
this isn't needed apperantly
NSAntoine May 19, 2022
152bf41
Refactor position when diagnosing lex_unterminated_string
NSAntoine May 19, 2022
045abec
Restructure handling when string interpolation isn't ended by a paren…
NSAntoine May 20, 2022
bb91faa
Correct indentation
NSAntoine May 20, 2022
35a7008
fix indentation.. again..
NSAntoine May 20, 2022
27a6f95
Fix tests and add one more test
NSAntoine May 20, 2022
0df097d
move test to end of file
NSAntoine May 20, 2022
5925ea5
Fix tests finally.. maybe..
NSAntoine May 20, 2022
5e1d60c
Point to --TmpPtr instead
NSAntoine May 24, 2022
ec37cc9
Stop diagnosing opening_paren when diagnosing string_interpolation_un…
NSAntoine May 24, 2022
e27e78e
Fixes
NSAntoine May 24, 2022
be472ed
revert unneeded changes made by previous commit
NSAntoine May 24, 2022
61c4a8c
change message of string_interpolation_unclosed
NSAntoine Jun 4, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -1356,6 +1356,11 @@ ERROR(expected_type_after_as,none,
ERROR(string_interpolation_extra,none,
"extra tokens after interpolated string expression", ())

// String interpolation isnt closed by a )
// ie: `let west = "ye \("`
ERROR(string_interpolation_unclosed, none,
"expected ')' at end of string interpolation", ())

// Interpolations with parameter labels or multiple values
WARNING(string_interpolation_list_changing,none,
"interpolating multiple values will not form a tuple in Swift 5", ())
Expand Down
22 changes: 16 additions & 6 deletions lib/Parse/Lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1884,13 +1884,23 @@ void Lexer::lexStringLiteral(unsigned CustomDelimiterLen) {
// Successfully scanned the body of the expression literal.
++CurPtr;
continue;
} else if ((*CurPtr == '\r' || *CurPtr == '\n') && IsMultilineString) {
// The only case we reach here is unterminated single line string in the
// interpolation. For better recovery, go on after emitting an error.
diagnose(CurPtr, diag::lex_unterminated_string);
wasErroneous = true;
continue;
} else {
if ((*CurPtr == '\r' || *CurPtr == '\n') && IsMultilineString) {
diagnose(CurPtr, diag::string_interpolation_unclosed);
diagnose(--TmpPtr, diag::opening_paren);

// The only case we reach here is unterminated single line string in
// the interpolation. For better recovery, go on after emitting
// an error.
diagnose(CurPtr, diag::lex_unterminated_string);
wasErroneous = true;
continue;
} else if (!IsMultilineString || CurPtr == BufferEnd) {
diagnose(CurPtr, diag::string_interpolation_unclosed);
diagnose(--TmpPtr, diag::opening_paren);
}

// As a fallback, just emit an unterminated string error.
diagnose(TokStart, diag::lex_unterminated_string);
return formToken(tok::unknown, TokStart);
}
Expand Down
7 changes: 4 additions & 3 deletions test/Parse/multiline_errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,14 @@ _ = "hello\("""
world
"""
)!"
// expected-error@-4 {{unterminated string literal}}
// expected-error@-2 {{expected ')' at end of string interpolation}} expected-note @-4 {{to match this opening '('}}
// expected-error@-2 {{unterminated string literal}}

_ = "hello\(
"""
world
""")!"
// expected-error@-4 {{unterminated string literal}}
// expected-error@-4 {{expected ')' at end of string interpolation}} expected-note @-4 {{to match this opening '('}}
// expected-error@-2 {{unterminated string literal}}

_ = """
Expand Down Expand Up @@ -190,4 +190,5 @@ let _ = """
\("bar
baz
"""
// expected-error@-3 {{unterminated string literal}}
// expected-error@-3 {{expected ')' at end of string interpolation}} expected-note @-3 {{to match this opening '('}}
// expected-error@-4 {{unterminated string literal}}
Expand Down
3 changes: 2 additions & 1 deletion test/Parse/string_literal_eof1.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// RUN: %target-typecheck-verify-swift

// NOTE: DO NOT add a newline at EOF.
// expected-error@+1 {{unterminated string literal}}
// expected-error@+2 {{unterminated string literal}}
// expected-error@+1 {{expected ')' at end of string interpolation}} expected-note @+1 {{to match this opening '('}}
_ = "foo\(
1 change: 1 addition & 0 deletions test/Parse/string_literal_eof2.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// RUN: %target-typecheck-verify-swift

// NOTE: DO NOT add a newline at EOF.
// expected-error@+2 {{expected ')' at end of string interpolation}} expected-note @+2 {{to match this opening '('}}
// expected-error@+1 {{unterminated string literal}}
_ = "foo\("bar
3 changes: 2 additions & 1 deletion test/Parse/string_literal_eof5.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// RUN: %target-typecheck-verify-swift

// NOTE: DO NOT add a newline at EOF.
// expected-error@+1 {{unterminated string literal}}
// expected-error@+2 {{unterminated string literal}}
// expected-error@+3 {{expected ')' at end of string interpolation}} expected-note @+3 {{to match this opening '('}}
_ = """
foo
\(
3 changes: 2 additions & 1 deletion test/Parse/string_literal_eof6.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// RUN: %target-typecheck-verify-swift

// NOTE: DO NOT add a newline at EOF.
// expected-error@+1 {{unterminated string literal}}
// expected-error@+2 {{unterminated string literal}}
// expected-error@+3 {{expected ')' at end of string interpolation}} expected-note @+3 {{to match this opening '('}}
_ = """
foo
\("bar
4 changes: 2 additions & 2 deletions test/Parse/string_literal_eof7.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// RUN: %target-typecheck-verify-swift

// expected-error@+4 {{unterminated string literal}}
// expected-error@+1 {{unterminated string literal}}
// expected-error@+4 {{expected ')' at end of string interpolation}} expected-note @+4 {{to match this opening '('}}
// expected-error@+1 {{unterminated string literal}} expected-error@+3 {{unterminated string literal}}
_ = """
foo
\("bar
Expand Down
27 changes: 27 additions & 0 deletions test/Parse/unclosed-string-interpolation.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// RUN: %target-typecheck-verify-swift

let mid = "pete"

_ = "mid == \(pete"
// expected-error @-1 {{expected ')' at end of string interpolation}} expected-note @-1 {{to match this opening '('}}

let theGoat = "kanye \("
// expected-error @-1 {{expected ')' at end of string interpolation}} expected-note @-1 {{to match this opening '('}}

let equation1 = "2 + 2 = \(2 + 2"
// expected-error @-1 {{expected ')' at end of string interpolation}} expected-note @-1 {{to match this opening '('}}

_ = """
\(
"""
// expected-error @-2 {{expected ')' at end of string interpolation}} expected-note @-2 {{to match this opening '('}}

let s = "\(x"; print(x)
// expected-error @-1 {{expected ')' at end of string interpolation}} expected-note @-1 {{to match this opening '('}}

let zzz = "\(x; print(x)
// expected-error @-1 {{expected ')' at end of string interpolation}} expected-note @-1 {{to match this opening '('}}

let goatedAlbum = "The Life Of \("Pablo"
// expected-error @-1 {{expected ')' at end of string interpolation}} expected-note @-1 {{to match this opening '('}}
// expected-error @-1 {{unterminated string literal}}
15 changes: 8 additions & 7 deletions test/expr/expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -475,9 +475,9 @@ func stringliterals(_ d: [String: Int]) {
let x = 4
"Hello \(x+1) world" // expected-warning {{string literal is unused}}

"Error: \(x+1"; // expected-error {{unterminated string literal}}
"Error: \(x+1"; // expected-error {{expected ')' at end of string interpolation}} expected-note {{to match this opening '('}}

"Error: \(x+1 // expected-error {{unterminated string literal}}
"Error: \(x+1 // expected-error {{expected ')' at end of string interpolation}} expected-note {{to match this opening '('}}
; // expected-error {{';' statements are not allowed}}

// rdar://14050788 [DF] String Interpolations can't contain quotes
Expand All @@ -488,15 +488,16 @@ func stringliterals(_ d: [String: Int]) {
"test \("quoted-paren (")"
"test \("\\")"
"test \("\n")"
"test \("\")" // expected-error {{unterminated string literal}}
"test \("\")" // expected-error {{expected ')' at end of string interpolation}} expected-note {{to match this opening '('}}

"test \
// expected-error @-1 {{unterminated string literal}} expected-error @-1 {{invalid escape sequence in literal}}
"test \("\
// expected-error @-1 {{unterminated string literal}}
// expected-error @-1 {{expected ')' at end of string interpolation}} expected-note @-1 {{to match this opening '('}}
"test newline \("something" +
"something else")"
// expected-error @-2 {{unterminated string literal}} expected-error @-1 {{unterminated string literal}}
// expected-error @-2 {{expected ')' at end of string interpolation}} expected-note @-2 {{to match this opening '('}}
// expected-error @-2 {{unterminated string literal}}

// expected-warning @+2 {{variable 'x2' was never used; consider replacing with '_' or removing it}}
// expected-error @+1 {{unterminated string literal}}
Expand Down Expand Up @@ -939,10 +940,10 @@ let _ = 0xFFF_FFFF_FFFF_FFFF as Int64

// rdar://problem/20289969 - string interpolation with comment containing ')' or '"'
let _ = "foo \(42 /* ) " ) */)"
let _ = "foo \(foo // ) " // expected-error {{unterminated string literal}}
let _ = "foo \(foo // ) " // expected-error {{expected ')' at end of string interpolation}} expected-note {{to match this opening '('}}
let _ = "foo \(42 /*
* multiline comment
*/)end"
// expected-error @-3 {{unterminated string literal}}
// expected-error @-3 {{expected ')' at end of string interpolation}} expected-note @-3 {{to match this opening '('}}
// expected-error @-2 {{expected expression}}
// expected-error @-3 {{unterminated string literal}}