Skip to content
Open
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
1 change: 1 addition & 0 deletions docs/release-notes/.FSharp.Compiler.Service/11.0.100.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Fix FS0421 "The address of the variable cannot be used at this point" incorrectly raised for the discard pattern `let _ = &expr` when `let x = &expr` compiles. ([Issue #18841](https://github.com/dotnet/fsharp/issues/18841), [PR #19811](https://github.com/dotnet/fsharp/pull/19811))
* Honor `--nowarn` and `--warnaserror` for warnings emitted during command-line option parsing ([Issue #19576](https://github.com/dotnet/fsharp/issues/19576), [PR #19776](https://github.com/dotnet/fsharp/pull/19776))
* Fix `[<return: X>]` prefix attributes being silently dropped on class members, and fix false-positive `AllowMultiple=false` errors when `[<X>]` and `[<return: X>]` are applied to the same binding. ([Issue #17904](https://github.com/dotnet/fsharp/issues/17904), [Issue #19020](https://github.com/dotnet/fsharp/issues/19020), [PR #19738](https://github.com/dotnet/fsharp/pull/19738))
* Fix `=` adjacent to an interpolated string (e.g. `C(Name=$"value")`) being lexed as the invalid operator `=$` instead of an assignment followed by an interpolated string. ([Issue #16696](https://github.com/dotnet/fsharp/issues/16696))
* Fix attributes on return type of unparenthesized tuple methods being silently dropped from IL. ([Issue #462](https://github.com/dotnet/fsharp/issues/462), [PR #19714](https://github.com/dotnet/fsharp/pull/19714))
* Fix false-positive nullness warning (FS3261) when pattern matching narrows nullness inside seq/list/array comprehensions. ([Issue #19644](https://github.com/dotnet/fsharp/issues/19644), [PR #19743](https://github.com/dotnet/fsharp/pull/19743))
* Fix internal error FS0073 "Undefined or unsolved type variable" in IlxGen when nested inline SRTP functions with multiple overloads leave unsolved typars in the non-witness codegen path. ([Issue #19709](https://github.com/dotnet/fsharp/issues/19709), [PR #19710](https://github.com/dotnet/fsharp/pull/19710))
Expand Down
5 changes: 5 additions & 0 deletions src/Compiler/Facilities/prim-lexing.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ type internal LexBuffer<'Char> =
/// The currently matched text as a Span, it is only valid until the lexer is advanced
member LexemeView: System.ReadOnlySpan<'Char>

/// Length of the currently matched lexeme, in characters. Setting this to a value smaller than the
/// actual match effectively rewinds the scanner: the next token will start <c>LexemeLength</c>
/// characters into the previously-matched lexeme. Use with caution.
member LexemeLength: int with get, set

/// Get single character of matched string
member LexemeChar: int -> 'Char

Expand Down
11 changes: 10 additions & 1 deletion src/Compiler/lex.fsl
Original file line number Diff line number Diff line change
Expand Up @@ -975,8 +975,17 @@ rule token (args: LexArgs) (skip: bool) = parse

| ignored_op_char* ('@'|'^') op_char* { checkExprOp lexbuf; INFIX_AT_HAT_OP(lexeme lexbuf) }

// For '=$"' (property/named-arg initialization with an interpolated string, e.g. C(Name=$"123")):
// match the 3 chars, but consume only the '=' and rewind so the next scan begins at '$"',
// letting the regular interpolated-string lexer process it (including any '{...}' holes).
// See https://github.com/dotnet/fsharp/issues/16696.
| '=' '$' '"' {
lexbuf.LexemeLength <- 1
lexbuf.EndPos <- lexbuf.StartPos.ShiftColumnBy(1)
EQUALS }
Comment thread
edgarfgp marked this conversation as resolved.

| ignored_op_char* ('=' | "!=" | '<' | '$') op_char* { checkExprOp lexbuf; INFIX_COMPARE_OP(lexeme lexbuf) }

| ignored_op_char* ('>') op_char* { checkExprGreaterColonOp lexbuf; INFIX_COMPARE_OP(lexeme lexbuf) }

| ignored_op_char* ('&') op_char* { checkExprOp lexbuf; INFIX_AMP_OP(lexeme lexbuf) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,49 @@ let s = $"{f.Invoke(42)}"
"""
|> compileExeAndRun
|> shouldSucceed

// Issue 16696: '=' immediately followed (no space) by an interpolated-string opener was
// greedily lexed as the invalid operator '=$' instead of '=' + an interpolated string.
// The hole {n} proves an interpolated string (not a plain one) is what gets lexed.
[<Fact>]
let ``Issue 16696 - '=' adjacent to an interpolated string binds it`` () =
Fsx """
let n = 42
let x =$"{n}"
if x <> "42" then failwith "expected 42"
"""
|> compileExeAndRun
|> shouldSucceed

// (The triple-quote form '=$"""..."""' is covered by the SyntaxTree baseline
// SynExprInterpolatedStringAdjacentEqualsTripleQuote.fs; '=$"' is a prefix of '=$"""', so the
// same lexer rule handles it after the rewind.)

// The reported cases from the issue: named-argument and record-field initialization.
[<Fact>]
let ``Issue 16696 - '=' adjacent to an interpolated string in named-argument and record contexts`` () =
Fsx """
type C() = member val Name = "" with get, set
type R = { Name: string }
let n = 42
let c = C(Name=$"{n}")
let r = { Name=$"{n}" }
if c.Name <> "42" || r.Name <> "42" then failwith "expected 42"
"""
|> compileExeAndRun
|> shouldSucceed

// Operator lexing is unchanged: a '$' anywhere in an operator is still reserved (FS0035).
// The only thing the fix changes is '=' directly before an interpolated-string opener;
// everything below still lexes as an operator exactly as before.
[<Theory>]
[<InlineData("let x =$abc")>] // '=$' not before a quote
[<InlineData("let (=$) a b = a")>] // defining (=$)
[<InlineData("let f a b = a =$ b")>] // '=$' used as infix
[<InlineData("let (<$>) f x = f x")>] // '$' inside an operator
[<InlineData("let (<=$=>) a b = a")>] // '$' in the middle of an operator
let ``Issue 16696 - operators containing '$' are still rejected (operator lexing unchanged)`` (code: string) =
Fsx code
|> compile
|> shouldFail
|> withDiagnosticMessageMatches "is not permitted as a character in operator names"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let x =$"123"
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
ImplFile
(ParsedImplFileInput
("/root/String/SynExprInterpolatedStringAdjacentEquals.fs", false,
QualifiedNameOfFile SynExprInterpolatedStringAdjacentEquals, [],
[SynModuleOrNamespace
([SynExprInterpolatedStringAdjacentEquals], false, AnonModule,
[Let
(false,
[SynBinding
(None, Normal, false, false, [],
PreXmlDoc ((1,0), FSharp.Compiler.Xml.XmlDocCollector),
SynValData
(None, SynValInfo ([], SynArgInfo ([], false, None)), None),
Named (SynIdent (x, None), false, None, (1,4--1,5)), None,
InterpolatedString
([String ("123", (1,7--1,13))], Regular, (1,7--1,13)),
(1,4--1,5), Yes (1,0--1,13), { LeadingKeyword = Let (1,0--1,3)
InlineKeyword = None
EqualsRange = Some (1,6--1,7) })],
(1,0--1,13), { InKeyword = None })], PreXmlDocEmpty, [], None,
(1,0--2,0), { LeadingKeyword = None })], (true, true),
{ ConditionalDirectives = []
WarnDirectives = []
CodeComments = [] }, set []))
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let x =$"""abc"""
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
ImplFile
(ParsedImplFileInput
("/root/String/SynExprInterpolatedStringAdjacentEqualsTripleQuote.fs",
false,
QualifiedNameOfFile SynExprInterpolatedStringAdjacentEqualsTripleQuote, [],
[SynModuleOrNamespace
([SynExprInterpolatedStringAdjacentEqualsTripleQuote], false,
AnonModule,
[Let
(false,
[SynBinding
(None, Normal, false, false, [],
PreXmlDoc ((1,0), FSharp.Compiler.Xml.XmlDocCollector),
SynValData
(None, SynValInfo ([], SynArgInfo ([], false, None)), None),
Named (SynIdent (x, None), false, None, (1,4--1,5)), None,
InterpolatedString
([String ("abc", (1,7--1,17))], TripleQuote, (1,7--1,17)),
(1,4--1,5), Yes (1,0--1,17), { LeadingKeyword = Let (1,0--1,3)
InlineKeyword = None
EqualsRange = Some (1,6--1,7) })],
(1,0--1,17), { InKeyword = None })], PreXmlDocEmpty, [], None,
(1,0--2,0), { LeadingKeyword = None })], (true, true),
{ ConditionalDirectives = []
WarnDirectives = []
CodeComments = [] }, set []))
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
let n = 42
let x =$"{n}"
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
ImplFile
(ParsedImplFileInput
("/root/String/SynExprInterpolatedStringAdjacentEqualsWithHole.fs", false,
QualifiedNameOfFile SynExprInterpolatedStringAdjacentEqualsWithHole, [],
[SynModuleOrNamespace
([SynExprInterpolatedStringAdjacentEqualsWithHole], false, AnonModule,
[Let
(false,
[SynBinding
(None, Normal, false, false, [],
PreXmlDoc ((1,0), FSharp.Compiler.Xml.XmlDocCollector),
SynValData
(None, SynValInfo ([], SynArgInfo ([], false, None)), None),
Named (SynIdent (n, None), false, None, (1,4--1,5)), None,
Const (Int32 42, (1,8--1,10)), (1,4--1,5), Yes (1,0--1,10),
{ LeadingKeyword = Let (1,0--1,3)
InlineKeyword = None
EqualsRange = Some (1,6--1,7) })], (1,0--1,10),
{ InKeyword = None });
Let
(false,
[SynBinding
(None, Normal, false, false, [],
PreXmlDoc ((2,0), FSharp.Compiler.Xml.XmlDocCollector),
SynValData
(None, SynValInfo ([], SynArgInfo ([], false, None)), None),
Named (SynIdent (x, None), false, None, (2,4--2,5)), None,
InterpolatedString
([String ("", (2,7--2,10)); FillExpr (Ident n, None);
String ("", (2,11--2,13))], Regular, (2,7--2,13)),
(2,4--2,5), Yes (2,0--2,13), { LeadingKeyword = Let (2,0--2,3)
InlineKeyword = None
EqualsRange = Some (2,6--2,7) })],
(2,0--2,13), { InKeyword = None })], PreXmlDocEmpty, [], None,
(1,0--3,0), { LeadingKeyword = None })], (true, true),
{ ConditionalDirectives = []
WarnDirectives = []
CodeComments = [] }, set []))
Loading