From 267d0a57f217df756d9ac33c6aa4ffbfe3b53097 Mon Sep 17 00:00:00 2001 From: Florian Verdonck Date: Wed, 6 Jul 2022 17:21:23 +0200 Subject: [PATCH] Fix setter first (#13435) * Respect the order of SynBindings in SynMemberDefn.GetSetMember. * Remove unnecessary tests. * Add release notes entry for SynMemberDefn.GetSetMember. --- release-notes.md | 1 + src/Compiler/SyntaxTree/ParseHelpers.fs | 43 ++++------------ .../ClassTypes/GetSetMembers/GetSetMembers.fs | 51 ------------------- .../FSharp.Compiler.ComponentTests.fsproj | 1 - tests/service/Symbols.fs | 35 +++++++++++++ 5 files changed, 45 insertions(+), 86 deletions(-) delete mode 100644 tests/FSharp.Compiler.ComponentTests/Conformance/ClassTypes/GetSetMembers/GetSetMembers.fs diff --git a/release-notes.md b/release-notes.md index 3919cdf564..2b30ad027a 100644 --- a/release-notes.md +++ b/release-notes.md @@ -34,6 +34,7 @@ These release notes track our current efforts to document changes to the F# proj `a + b` is parsed as `SynLongIdent([op_Addition], [], [Some (OriginalNotation "+")])`. * `SynMeasure` was extended with [SynMeasure.Paren](https://fsharp.github.io/fsharp-compiler-docs/reference/fsharp-compiler-syntax-synmeasure.html#Paren) case. * Dynamic expressions (like `x?y`) are now represented as [SynExpr.Dynamic](https://fsharp.github.io/fsharp-compiler-docs/reference/fsharp-compiler-syntax-synexpr.html#Dynamic) in the Untyped Syntax Tree. +* Members with `get` and/or `set` are now represented as [SynMemberDefn.GetSetMember](https://fsharp.github.io/fsharp-compiler-docs/reference/fsharp-compiler-syntax-synmemberdefn.html#GetSetMember) in the Untyped Syntax Tree. ### F# 6.0 / Visual Studio 17.0 diff --git a/src/Compiler/SyntaxTree/ParseHelpers.fs b/src/Compiler/SyntaxTree/ParseHelpers.fs index 54a31687eb..bcaebd20b9 100644 --- a/src/Compiler/SyntaxTree/ParseHelpers.fs +++ b/src/Compiler/SyntaxTree/ParseHelpers.fs @@ -764,42 +764,17 @@ let mkSynMemberDefnGetSet let setter = tryMkSynMemberDefnMember s match getter, setter with - | Some (SynMemberDefn.Member (getBinding, mGet), getIdent), Some (SynMemberDefn.Member (setBinding, mSet), setIdent) -> - let range = unionRanges mGet mSet + | Some (SynMemberDefn.Member (getBinding, m1), GetIdent mGet), Some (SynMemberDefn.Member (setBinding, m2), SetIdent mSet) + | Some (SynMemberDefn.Member (setBinding, m1), SetIdent mSet), Some (SynMemberDefn.Member (getBinding, m2), GetIdent mGet) -> + let range = unionRanges m1 m2 let trivia = - match getIdent, setIdent with - | GetIdent mGet, SetIdent mSet - | SetIdent mSet, GetIdent mGet -> - { - WithKeyword = mWith - GetKeyword = Some mGet - AndKeyword = mAnd - SetKeyword = Some mSet - } - | OtherIdent, GetIdent mGet - | GetIdent mGet, OtherIdent -> - { - WithKeyword = mWith - GetKeyword = Some mGet - AndKeyword = mAnd - SetKeyword = None - } - | OtherIdent, SetIdent mSet - | SetIdent mSet, OtherIdent -> - { - WithKeyword = mWith - GetKeyword = None - AndKeyword = mAnd - SetKeyword = Some mSet - } - | _ -> - { - WithKeyword = mWith - AndKeyword = mAnd - GetKeyword = None - SetKeyword = None - } + { + WithKeyword = mWith + GetKeyword = Some mGet + AndKeyword = mAnd + SetKeyword = Some mSet + } [ SynMemberDefn.GetSetMember(Some getBinding, Some setBinding, range, trivia) ] | Some (SynMemberDefn.Member (binding, m), getOrSet), None diff --git a/tests/FSharp.Compiler.ComponentTests/Conformance/ClassTypes/GetSetMembers/GetSetMembers.fs b/tests/FSharp.Compiler.ComponentTests/Conformance/ClassTypes/GetSetMembers/GetSetMembers.fs deleted file mode 100644 index 5c2d6dcc44..0000000000 --- a/tests/FSharp.Compiler.ComponentTests/Conformance/ClassTypes/GetSetMembers/GetSetMembers.fs +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. - -namespace FSharp.Compiler.ComponentTests.Conformance.ClassTypes - -open Xunit -open FSharp.Test.Compiler - -module GetSetMembers = - - [] - let WithGetAndSet() = - Fsx """ -type Foo() = - let mutable bar = "" - - member this.Bar - with get () = bar - and set nextBar = bar <- nextBar - """ - |> withLangVersion50 - |> typecheck - |> shouldSucceed - |> ignore - - [] - let WithGet() = - Fsx """ -type Foo() = - let mutable bar = "" - - member this.Bar - with get () = bar - """ - |> withLangVersion50 - |> typecheck - |> shouldSucceed - |> ignore - - [] - let WithSet() = - Fsx """ -type Foo() = - let mutable bar = "" - - member this.Bar - with set nextBar = bar <- nextBar - """ - |> withLangVersion50 - |> typecheck - |> shouldSucceed - |> ignore \ No newline at end of file diff --git a/tests/FSharp.Compiler.ComponentTests/FSharp.Compiler.ComponentTests.fsproj b/tests/FSharp.Compiler.ComponentTests/FSharp.Compiler.ComponentTests.fsproj index cf7ae11db7..b41d408da2 100644 --- a/tests/FSharp.Compiler.ComponentTests/FSharp.Compiler.ComponentTests.fsproj +++ b/tests/FSharp.Compiler.ComponentTests/FSharp.Compiler.ComponentTests.fsproj @@ -32,7 +32,6 @@ - diff --git a/tests/service/Symbols.fs b/tests/service/Symbols.fs index 7da84598b3..68e29e03dd 100644 --- a/tests/service/Symbols.fs +++ b/tests/service/Symbols.fs @@ -785,6 +785,41 @@ type Foo = assertRange (3, 4) (5, 54) m | _ -> Assert.Fail "Could not get valid AST" + [] + let ``SynTypeDefn with member with set/get`` () = + let parseResults = + getParseResults + """ +type A() = + member this.Z with set (_:int):unit = () and get():int = 1 +""" + + match parseResults with + | ParsedInput.ImplFile (ParsedImplFileInput (modules = [ SynModuleOrNamespace.SynModuleOrNamespace(decls = [ + SynModuleDecl.Types( + typeDefns = [ SynTypeDefn(typeRepr = SynTypeDefnRepr.ObjectModel(members = [ + SynMemberDefn.ImplicitCtor _ + SynMemberDefn.GetSetMember(Some (SynBinding(headPat = SynPat.LongIdent(extraId = Some getIdent))), + Some (SynBinding(headPat = SynPat.LongIdent(extraId = Some setIdent))), + m, + { WithKeyword = mWith + GetKeyword = Some mGet + AndKeyword = Some mAnd + SetKeyword = Some mSet }) + ])) ] + ) + ]) ])) -> + Assert.AreEqual("get", getIdent.idText) + Assert.AreEqual("set", setIdent.idText) + assertRange (3, 18) (3, 22) mWith + assertRange (3, 23) (3, 26) mSet + assertRange (3, 23) (3, 26) setIdent.idRange + assertRange (3, 45) (3, 48) mAnd + assertRange (3, 49) (3, 52) mGet + assertRange (3, 49) (3, 52) getIdent.idRange + assertRange (3, 4) (3, 62) m + | _ -> Assert.Fail "Could not get valid AST" + module SyntaxExpressions = [] let ``SynExpr.Do contains the range of the do keyword`` () =