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

pars.fsy: capture empty interpolated string fills and report error #9849

Merged
merged 2 commits into from
Aug 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion src/fsharp/FSComp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1532,5 +1532,6 @@ forFormatInvalidForInterpolated4,"Interpolated strings used as type IFormattable
3379,parsEofInInterpolatedString,"Incomplete interpolated string begun at or before here"
3380,parsEofInInterpolatedVerbatimString,"Incomplete interpolated verbatim string begun at or before here"
3381,parsEofInInterpolatedTripleQuoteString,"Incomplete interpolated triple-quote string begun at or before here"
3382,lexRBraceInInterpolatedString,"A '}}' character must be escaped (by doubling) in an interpolated string."
3382,parsEmptyFillInInterpolatedString,"Invalid interpolated string. This interpolated string expression fill is empty, an expression was expected."
3383,lexRBraceInInterpolatedString,"A '}}' character must be escaped (by doubling) in an interpolated string."
#3501 "This construct is not supported by your version of the F# compiler" CompilerMessage(ExperimentalAttributeMessages.NotSupportedYet, 3501, IsError=true)
14 changes: 14 additions & 0 deletions src/fsharp/pars.fsy
Original file line number Diff line number Diff line change
Expand Up @@ -5504,6 +5504,13 @@ interpolatedStringParts:
| INTERP_STRING_PART interpolatedStringFill interpolatedStringParts
{ SynInterpolatedStringPart.String (fst $1, rhs parseState 1) :: SynInterpolatedStringPart.FillExpr $2 :: $3 }

| INTERP_STRING_PART interpolatedStringParts
{
let rbrace = parseState.InputEndPosition 1
let lbrace = parseState.InputStartPosition 2
reportParseErrorAt (mkSynRange rbrace lbrace) (FSComp.SR.parsEmptyFillInInterpolatedString())
SynInterpolatedStringPart.String (fst $1, rhs parseState 1) :: $2 }

/* INTERP_STRING_BEGIN_END */
/* INTERP_STRING_BEGIN_PART int32 INTERP_STRING_END */
/* INTERP_STRING_BEGIN_PART int32 INTERP_STRING_PART int32 INTERP_STRING_END */
Expand All @@ -5513,6 +5520,13 @@ interpolatedString:

| INTERP_STRING_BEGIN_END
{ [ SynInterpolatedStringPart.String (fst $1, rhs parseState 1) ] }

| INTERP_STRING_BEGIN_PART interpolatedStringParts
{
let rbrace = parseState.InputEndPosition 1
let lbrace = parseState.InputStartPosition 2
reportParseErrorAt (mkSynRange rbrace lbrace) (FSComp.SR.parsEmptyFillInInterpolatedString())
SynInterpolatedStringPart.String (fst $1, rhs parseState 1) :: $2 }

opt_HIGH_PRECEDENCE_APP:
| HIGH_PRECEDENCE_BRACK_APP { }
Expand Down
21 changes: 15 additions & 6 deletions tests/fsharp/Compiler/Language/StringInterpolation.fs
Original file line number Diff line number Diff line change
Expand Up @@ -683,8 +683,17 @@ let xd = $"%A{}" // empty expression
"""
CompilerAssert.TypeCheckWithErrorsAndOptions [| "--langversion:preview" |]
code
[|(FSharpErrorSeverity.Error, 10, (2, 15, 2, 17),
"Unexpected interpolated string (final part) in binding")
[|(FSharpErrorSeverity.Error, 3382, (2, 15, 2, 15),
"Invalid interpolated string. This interpolated string expression fill is empty, an expression was expected.")
|]

let code = """
let xd = $"%A{ }" // empty expression
"""
CompilerAssert.TypeCheckWithErrorsAndOptions [| "--langversion:preview" |]
code
[|(FSharpErrorSeverity.Error, 3382, (2, 15, 2, 17),
"Invalid interpolated string. This interpolated string expression fill is empty, an expression was expected.")
|]

[<Test>]
Expand Down Expand Up @@ -818,29 +827,29 @@ let TripleInterpolatedInVerbatimInterpolated = $\"123{456}789{$\"\"\"012\"\"\"}3
let code = "let x1 = $\"}\""
CompilerAssert.TypeCheckWithErrorsAndOptions [| "--langversion:preview" |]
code
[|(FSharpErrorSeverity.Error, 3382, (1, 10, 1, 14),
[|(FSharpErrorSeverity.Error, 3383, (1, 10, 1, 14),
"A '}' character must be escaped (by doubling) in an interpolated string.")|]

[<Test>]
let ``String interpolation extra right brace verbatim`` () =
let code = "let x1 = @$\"}\""
CompilerAssert.TypeCheckWithErrorsAndOptions [| "--langversion:preview" |]
code
[|(FSharpErrorSeverity.Error, 3382, (1, 10, 1, 15),
[|(FSharpErrorSeverity.Error, 3383, (1, 10, 1, 15),
"A '}' character must be escaped (by doubling) in an interpolated string.")|]

[<Test>]
let ``String interpolation extra right brace triple`` () =
let code = "let x1 = $\"\"\"}\"\"\""
CompilerAssert.TypeCheckWithErrorsAndOptions [| "--langversion:preview" |]
code
[|(FSharpErrorSeverity.Error, 3382, (1, 10, 1, 18),
[|(FSharpErrorSeverity.Error, 3383, (1, 10, 1, 18),
"A '}' character must be escaped (by doubling) in an interpolated string.")|]

[<Test>]
let ``String interpolation extra right brace single quote with hole`` () =
let code = "let x1 = $\"{0}}\""
CompilerAssert.TypeCheckWithErrorsAndOptions [| "--langversion:preview" |]
code
[|(FSharpErrorSeverity.Error, 3382, (1, 14, 1, 17),
[|(FSharpErrorSeverity.Error, 3383, (1, 14, 1, 17),
"A '}' character must be escaped (by doubling) in an interpolated string.")|]