Skip to content

Commit

Permalink
Correct body SynExpr of parsedData of Lambda when wildcards are invol…
Browse files Browse the repository at this point in the history
…ved.
  • Loading branch information
nojaf committed Jul 3, 2021
1 parent 5cfd7ff commit 91f0148
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/fsharp/SyntaxTreeOps.fs
Original file line number Diff line number Diff line change
Expand Up @@ -195,19 +195,30 @@ let rec SimplePatsOfPat synArgNameGenerator p =
| SynPat.Tuple (false, ps, m)

| SynPat.Paren(SynPat.Tuple (false, ps, _), m) ->
let filteredPs =
List.choose
(fun p ->
let p' = SimplePatOfPat synArgNameGenerator p
match (fst p') with
| SynSimplePat.Id(isCompilerGenerated = true) -> None
| _ -> Some p')
ps

let ps2, laterF =
List.foldBack
(fun (p', rhsf) (ps', rhsf') ->
p':: ps',
(composeFunOpt rhsf rhsf'))
(List.map (SimplePatOfPat synArgNameGenerator) ps)
filteredPs
([], None)
SynSimplePats.SimplePats (ps2, m),
laterF

| SynPat.Paren(SynPat.Const (SynConst.Unit, m), _)

| SynPat.Const (SynConst.Unit, m) ->
| SynPat.Const (SynConst.Unit, m)

| SynPat.Wild m ->
SynSimplePats.SimplePats ([], m),
None

Expand Down
61 changes: 61 additions & 0 deletions tests/service/Symbols.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1225,4 +1225,65 @@ module ParsedHashDirective =
]) ])) ->
Assert.AreEqual("40", v)
assertRange (1, 8) (1, 16) m
| _ -> Assert.Fail "Could not get valid AST"

module Lambdas =
[<Test>]
let ``Lambda with two parameters gives correct body`` () =
let parseResults =
getParseResults
"fun a b -> x"

match parseResults with
| ParsedInput.ImplFile (ParsedImplFileInput (modules = [ SynModuleOrNamespace.SynModuleOrNamespace(decls = [
SynModuleDecl.DoExpr(
expr = SynExpr.Lambda(parsedData = Some(_, SynExpr.Ident(ident)))
)
]) ])) ->
Assert.AreEqual("x", ident.idText)
| _ -> Assert.Fail "Could not get valid AST"

[<Test>]
let ``Lambda with wild card parameter gives correct body`` () =
let parseResults =
getParseResults
"fun a _ b -> x"

match parseResults with
| ParsedInput.ImplFile (ParsedImplFileInput (modules = [ SynModuleOrNamespace.SynModuleOrNamespace(decls = [
SynModuleDecl.DoExpr(
expr = SynExpr.Lambda(parsedData = Some(_, SynExpr.Ident(ident)))
)
]) ])) ->
Assert.AreEqual("x", ident.idText)
| _ -> Assert.Fail "Could not get valid AST"

[<Test>]
let ``Lambda with tuple parameter with wild card gives correct body`` () =
let parseResults =
getParseResults
"fun a (b, _) c -> x"

match parseResults with
| ParsedInput.ImplFile (ParsedImplFileInput (modules = [ SynModuleOrNamespace.SynModuleOrNamespace(decls = [
SynModuleDecl.DoExpr(
expr = SynExpr.Lambda(parsedData = Some(_, SynExpr.Ident(ident)))
)
]) ])) ->
Assert.AreEqual("x", ident.idText)
| _ -> Assert.Fail "Could not get valid AST"

[<Test>]
let ``Lambda with wild card that returns a lambda gives correct body`` () =
let parseResults =
getParseResults
"fun _ -> fun _ -> x"

match parseResults with
| ParsedInput.ImplFile (ParsedImplFileInput (modules = [ SynModuleOrNamespace.SynModuleOrNamespace(decls = [
SynModuleDecl.DoExpr(
expr = SynExpr.Lambda(parsedData = Some(_, SynExpr.Lambda(parsedData = Some(_, SynExpr.Ident(ident)))))
)
]) ])) ->
Assert.AreEqual("x", ident.idText)
| _ -> Assert.Fail "Could not get valid AST"

0 comments on commit 91f0148

Please sign in to comment.