Skip to content

Commit 8f042b1

Browse files
authored
Preview>V10:AllowAccessModifiersToAutoPropertiesGettersAndSetters (#18736)
* Preview>V10:AllowAccessModifiersToAutoPropertiesGettersAndSetters * Minimize changes in printing caused by AllowAccessModifiersToAutoPropertiesGettersAndSetters The change AllowAccessModifiersToAutoPropertiesGettersAndSetters affected printing of indexed members and also lead to dropping of "with get,set" for auto properties - the current `replace with ` logic has some flaws. If those are fixed, the behavior can be brought back - pay special attention to indexer related tests, in error messages and generated signatures. Special care also for properties where getter and setter have differing types. * Fix GetValSignatureText for indexer properties (avoid regression that drops indexer argument types)
1 parent e84544a commit 8f042b1

File tree

9 files changed

+40
-56
lines changed

9 files changed

+40
-56
lines changed

src/Compiler/Checking/NicePrint.fs

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,9 +1413,10 @@ module PrintTastMemberOrVals =
14131413
if short then
14141414
tauL --- withGet
14151415
else
1416-
let nameL = layoutMemberName denv vref niceMethodTypars argInfos tagProperty vref.DisplayNameCoreMangled prefixAccessModifier
1416+
let prefixAccess = prefixAccessModifier || isNil argInfos
1417+
let nameL = layoutMemberName denv vref niceMethodTypars argInfos tagProperty vref.DisplayNameCoreMangled prefixAccess
14171418
let nameL = if short then nameL else mkInlineL denv vref.Deref nameL
1418-
stat --- ((nameL |> addColonL) ^^ (if isNil argInfos && not supportAccessModifiersBeforeGetSet then tauL else tauL --- withGet))
1419+
stat --- ((nameL |> addColonL) ^^ (if isNil argInfos then tauL else (tauL --- withGet)))
14191420
prettyTyparInst, resL
14201421

14211422
| SynMemberKind.PropertySet ->
@@ -1927,11 +1928,8 @@ module TastDefinitionPrinting =
19271928
let layoutPropInfo denv (infoReader: InfoReader) m (pinfo: PropInfo) : Layout list =
19281929
let amap = infoReader.amap
19291930

1930-
let supportAccessModifiersBeforeGetSet =
1931-
denv.g.langVersion.SupportsFeature Features.LanguageFeature.AllowAccessModifiersToAutoPropertiesGettersAndSetters
1932-
19331931
match pinfo.ArbitraryValRef with
1934-
| Some vref when not supportAccessModifiersBeforeGetSet ->
1932+
| Some vref ->
19351933
match pinfo with
19361934
| DifferentGetterAndSetter(getValRef, setValRef) ->
19371935
let getSuffix = if pinfo.IsIndexer then emptyL else WordL.keywordWith ^^ WordL.keywordGet
@@ -1951,26 +1949,6 @@ module TastDefinitionPrinting =
19511949
[ propL ^^ WordL.keywordWith ^^ wordL (tagText "get, set") ]
19521950
else
19531951
[ propL ]
1954-
1955-
| Some vref ->
1956-
let propL = PrintTastMemberOrVals.prettyLayoutOfValOrMemberNoInst denv infoReader vref
1957-
if pinfo.HasGetter && pinfo.HasSetter then
1958-
let rec ``replace 'with'`` layout newLayout =
1959-
match layout with
1960-
| Node(Leaf (text = text), _, _) when text.Text = "with" -> newLayout
1961-
| Node(l, r, i) -> Node(l, ``replace 'with'`` r newLayout , i)
1962-
| Attr(text, attr, l) -> Attr(text, attr, ``replace 'with'`` l newLayout )
1963-
| Leaf _
1964-
| ObjLeaf _ -> layout
1965-
1966-
let getterAccess, setterAccess =
1967-
pinfo.GetterMethod.ArbitraryValRef |> Option.map _.Accessibility |> Option.defaultValue taccessPublic,
1968-
pinfo.SetterMethod.ArbitraryValRef |> Option.map _.Accessibility |> Option.defaultValue taccessPublic
1969-
let getSet =
1970-
WordL.keywordWith ^^ layoutAccessibilityCore denv getterAccess ^^ wordL (tagText "get") ^^ RightL.comma --- layoutAccessibilityCore denv setterAccess ^^ wordL (tagText "set")
1971-
[ ``replace 'with'`` propL getSet ]
1972-
else
1973-
[ propL ]
19741952
| None ->
19751953

19761954
let modifierAndMember =

src/Compiler/Facilities/LanguageFeatures.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,11 @@ type LanguageVersion(versionText) =
236236
LanguageFeature.BetterAnonymousRecordParsing, languageVersion100
237237
LanguageFeature.ScopedNowarn, languageVersion100
238238
LanguageFeature.AllowTypedLetOrUseBang, languageVersion100
239+
LanguageFeature.AllowAccessModifiersToAutoPropertiesGettersAndSetters, languageVersion100
239240

240241
// F# preview (still preview in 10.0)
241242
LanguageFeature.UnmanagedConstraintCsharpInterop, previewVersion // not enabled because: https://github.com/dotnet/fsharp/issues/17509
242243
LanguageFeature.FromEndSlicing, previewVersion // Unfinished features --- needs work
243-
LanguageFeature.AllowAccessModifiersToAutoPropertiesGettersAndSetters, previewVersion // Stopped printing arguments to indexed properties
244244
]
245245

246246
static let defaultLanguageVersion = LanguageVersion("default")

src/Compiler/Symbols/Symbols.fs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2401,22 +2401,24 @@ type FSharpMemberOrFunctionOrValue(cenv, d:FSharpMemberOrValData, item) =
24012401
let stringValOfPropInfo (p: PropInfo) =
24022402
let supportAccessModifiersBeforeGetSet =
24032403
cenv.g.langVersion.SupportsFeature Features.LanguageFeature.AllowAccessModifiersToAutoPropertiesGettersAndSetters
2404-
if not supportAccessModifiersBeforeGetSet then
2405-
match p with
2406-
| DifferentGetterAndSetter(getValRef, setValRef) ->
2407-
let g = NicePrint.stringValOrMember displayEnv cenv.infoReader getValRef
2408-
let s = NicePrint.stringValOrMember displayEnv cenv.infoReader setValRef
2409-
$"{g}\n{s}"
2410-
| _ ->
2411-
let t = p.GetPropertyType(cenv.amap, m) |> NicePrint.layoutType displayEnv |> LayoutRender.showL
2412-
let withGetSet =
2413-
if p.HasGetter && p.HasSetter then "with get, set"
2414-
elif p.HasGetter then "with get"
2415-
elif p.HasSetter then "with set"
2416-
else ""
2417-
2418-
$"member %s{p.DisplayName}: %s{t} %s{withGetSet}"
2419-
else
2404+
2405+
match p with
2406+
| DifferentGetterAndSetter(getValRef, setValRef) when (not supportAccessModifiersBeforeGetSet || p.IsIndexer) ->
2407+
let g = NicePrint.stringValOrMember displayEnv cenv.infoReader getValRef
2408+
let s = NicePrint.stringValOrMember displayEnv cenv.infoReader setValRef
2409+
$"{g}\n{s}"
2410+
2411+
| _ when not supportAccessModifiersBeforeGetSet->
2412+
let t = p.GetPropertyType(cenv.amap, m) |> NicePrint.layoutType displayEnv |> LayoutRender.showL
2413+
let withGetSet =
2414+
if p.HasGetter && p.HasSetter then "with get, set"
2415+
elif p.HasGetter then "with get"
2416+
elif p.HasSetter then "with set"
2417+
else ""
2418+
2419+
$"member %s{p.DisplayName}: %s{t} %s{withGetSet}"
2420+
2421+
| _ ->
24202422
let layoutAccessibilityCore (denv: DisplayEnv) accessibility =
24212423
let isInternalCompPath x =
24222424
match x with

tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/MemberDefinitions/MethodsAndProperties/AutoPropsWithModifierBeforeGetSet.fs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,9 @@ but its signature specifies
165165
member A.E: int with set
166166
The accessibility specified in the signature is more than that specified in the implementation")
167167
(Error 0034, Line 7, Col 16, Line 7, Col 17, "Module 'Program' contains
168-
member A.E: int with internal get
168+
member internal A.E: int
169169
but its signature specifies
170-
member A.E: int with get
170+
member A.E: int
171171
The accessibility specified in the signature is more than that specified in the implementation")
172172
]
173173

@@ -192,9 +192,9 @@ type A =
192192
|> compile
193193
|> shouldFail
194194
|> withDiagnostics [
195-
(Error 3350, Line 5, Col 24, Line 5, Col 32, "Feature 'Allow access modifiers to auto properties getters and setters' is not available in F# 8.0. Please use language version 'PREVIEW' or greater.")
196-
(Error 3350, Line 6, Col 24, Line 6, Col 32, "Feature 'Allow access modifiers to auto properties getters and setters' is not available in F# 8.0. Please use language version 'PREVIEW' or greater.")
197-
(Error 3350, Line 7, Col 24, Line 7, Col 32, "Feature 'Allow access modifiers to auto properties getters and setters' is not available in F# 8.0. Please use language version 'PREVIEW' or greater.")
198-
(Error 3350, Line 4, Col 32, Line 4, Col 40, "Feature 'Allow access modifiers to auto properties getters and setters' is not available in F# 8.0. Please use language version 'PREVIEW' or greater.")
199-
(Error 3350, Line 6, Col 32, Line 6, Col 40, "Feature 'Allow access modifiers to auto properties getters and setters' is not available in F# 8.0. Please use language version 'PREVIEW' or greater.")
195+
(Error 3350, Line 5, Col 24, Line 5, Col 32, "Feature 'Allow access modifiers to auto properties getters and setters' is not available in F# 8.0. Please use language version 10.0 or greater.")
196+
(Error 3350, Line 6, Col 24, Line 6, Col 32, "Feature 'Allow access modifiers to auto properties getters and setters' is not available in F# 8.0. Please use language version 10.0 or greater.")
197+
(Error 3350, Line 7, Col 24, Line 7, Col 32, "Feature 'Allow access modifiers to auto properties getters and setters' is not available in F# 8.0. Please use language version 10.0 or greater.")
198+
(Error 3350, Line 4, Col 32, Line 4, Col 40, "Feature 'Allow access modifiers to auto properties getters and setters' is not available in F# 8.0. Please use language version 10.0 or greater.")
199+
(Error 3350, Line 6, Col 32, Line 6, Col 40, "Feature 'Allow access modifiers to auto properties getters and setters' is not available in F# 8.0. Please use language version 10.0 or greater.")
200200
]

tests/FSharp.Compiler.ComponentTests/Signatures/MemberTests.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type Foo =
2222
2323
new: unit -> Foo
2424
25-
member internal X: key1: obj * key2: obj -> bool with get
25+
member X: key1: obj * key2: obj -> bool with internal get
2626
2727
member X: key1: obj * key2: obj -> obj with set"""
2828

@@ -46,4 +46,4 @@ type Foo =
4646
4747
member Y: char with get
4848
49-
member internal Y: char with set"""
49+
member Y: char with internal set"""

tests/FSharp.Compiler.ComponentTests/Signatures/TestHelpers.fs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ open FSharp.Test.Compiler
77
let prependNewline v = String.Concat("\n", v)
88

99
let assertEqualIgnoreLineEnding (x: string) (y: string) =
10+
if (x<>y) then
11+
printfn "Expected:\n%s\n\nActual:\n%s" x y
1012
Assert.Equal(x, y, ignoreLineEndingDifferences = true)
1113

1214
let assertSingleSignatureBinding implementation signature =

tests/FSharp.Compiler.Service.Tests/Symbols.fs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,8 @@ module GetValSignatureText =
786786
| :? FSharpMemberOrFunctionOrValue as mfv ->
787787
let expected = expected.Replace("\r", "")
788788
let signature = mfv.GetValSignatureText(symbolUse.DisplayContext, symbolUse.Range)
789+
if expected <> signature.Value then
790+
printfn $"Expected:\n{expected}\n\n\nActual:\n{signature.Value}"
789791
Assert.Equal(expected, signature.Value)
790792
| symbol -> failwith $"Expected FSharpMemberOrFunctionOrValue, got %A{symbol}"
791793

tests/ILVerify/ilverify_FSharp.Compiler.Service_Debug_netstandard2.0.bsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
[IL]: Error [StackUnexpected]: : <StartupCode$FSharp-Compiler-Service>.$ServiceLexing+clo@924-515::Invoke([FSharp.Core]Microsoft.FSharp.Core.FSharpFunc`2<System.Tuple`3<FSharp.Compiler.Parser+token,int32,int32>,Microsoft.FSharp.Core.Unit>)][offset 0x00000082][found Char] Unexpected type on the stack.
4141
[IL]: Error [StackUnexpected]: : <StartupCode$FSharp-Compiler-Service>.$ServiceLexing+clo@924-515::Invoke([FSharp.Core]Microsoft.FSharp.Core.FSharpFunc`2<System.Tuple`3<FSharp.Compiler.Parser+token,int32,int32>,Microsoft.FSharp.Core.Unit>)][offset 0x0000008B][found Char] Unexpected type on the stack.
4242
[IL]: Error [StackUnexpected]: : <StartupCode$FSharp-Compiler-Service>.$ServiceLexing+clo@924-515::Invoke([FSharp.Core]Microsoft.FSharp.Core.FSharpFunc`2<System.Tuple`3<FSharp.Compiler.Parser+token,int32,int32>,Microsoft.FSharp.Core.Unit>)][offset 0x00000094][found Char] Unexpected type on the stack.
43-
[IL]: Error [StackUnexpected]: : <StartupCode$FSharp-Compiler-Service>.$Symbols+fullName@2496-1::Invoke([FSharp.Core]Microsoft.FSharp.Core.Unit)][offset 0x00000015][found Char] Unexpected type on the stack.
43+
[IL]: Error [StackUnexpected]: : <StartupCode$FSharp-Compiler-Service>.$Symbols+fullName@2498-1::Invoke([FSharp.Core]Microsoft.FSharp.Core.Unit)][offset 0x00000015][found Char] Unexpected type on the stack.
4444
[IL]: Error [StackUnexpected]: : FSharp.Compiler.CreateILModule+MainModuleBuilder::ConvertProductVersionToILVersionInfo(string)][offset 0x00000011][found Char] Unexpected type on the stack.
4545
[IL]: Error [StackUnexpected]: : FSharp.Compiler.StaticLinking+TypeForwarding::followTypeForwardForILTypeRef([FSharp.Compiler.Service]FSharp.Compiler.AbstractIL.IL+ILTypeRef)][offset 0x00000010][found Char] Unexpected type on the stack.
4646
[IL]: Error [StackUnexpected]: : FSharp.Compiler.CompilerOptions::getCompilerOption([FSharp.Compiler.Service]FSharp.Compiler.CompilerOptions+CompilerOption, [FSharp.Core]Microsoft.FSharp.Core.FSharpOption`1<int32>)][offset 0x000000E6][found Char] Unexpected type on the stack.
@@ -60,7 +60,7 @@
6060
[IL]: Error [StackUnexpected]: : FSharp.Compiler.CompilerConfig+TcConfig::.ctor([FSharp.Compiler.Service]FSharp.Compiler.CompilerConfig+TcConfigBuilder, bool)][offset 0x00000634][found Char] Unexpected type on the stack.
6161
[IL]: Error [StackUnexpected]: : FSharp.Compiler.PatternMatchCompilation::isProblematicClause([FSharp.Compiler.Service]FSharp.Compiler.PatternMatchCompilation+MatchClause)][offset 0x00000065][found Byte] Unexpected type on the stack.
6262
[IL]: Error [StackUnexpected]: : <StartupCode$FSharp-Compiler-Service>.$FSharp.Compiler.PatternMatchCompilation::.cctor()][offset 0x00000015][found Boolean] Unexpected type on the stack.
63-
[IL]: Error [StackUnexpected]: : FSharp.Compiler.NicePrint+TastDefinitionPrinting+meths@2092-3::Invoke([FSharp.Compiler.Service]FSharp.Compiler.Infos+MethInfo)][offset 0x000000BE][found Char] Unexpected type on the stack.
63+
[IL]: Error [StackUnexpected]: : FSharp.Compiler.NicePrint+TastDefinitionPrinting+meths@2070-3::Invoke([FSharp.Compiler.Service]FSharp.Compiler.Infos+MethInfo)][offset 0x000000BE][found Char] Unexpected type on the stack.
6464
[IL]: Error [StackUnexpected]: : FSharp.Compiler.NicePrint+PrintUtilities::layoutXmlDoc([FSharp.Compiler.Service]FSharp.Compiler.TypedTreeOps+DisplayEnv, bool, [FSharp.Compiler.Service]FSharp.Compiler.Xml.XmlDoc, [FSharp.Compiler.Service]FSharp.Compiler.Text.Layout)][offset 0x00000033][found Char] Unexpected type on the stack.
6565
[IL]: Error [StackUnexpected]: : FSharp.Compiler.TypeProviders::ValidateNamespaceName(string, [FSharp.Compiler.Service]FSharp.Compiler.Tainted`1<Microsoft.FSharp.Core.CompilerServices.ITypeProvider>, [FSharp.Compiler.Service]FSharp.Compiler.Text.Range, string)][offset 0x00000063][found Char] Unexpected type on the stack.
6666
[IL]: Error [StackUnexpected]: : FSharp.Compiler.TypeProviders::ValidateExpectedName([FSharp.Compiler.Service]FSharp.Compiler.Text.Range, string[], string, [FSharp.Compiler.Service]FSharp.Compiler.Tainted`1<FSharp.Compiler.TypeProviders+ProvidedType>)][offset 0x000000AD][found Char] Unexpected type on the stack.

tests/ILVerify/ilverify_FSharp.Compiler.Service_Release_netstandard2.0.bsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
[IL]: Error [StackUnexpected]: : <StartupCode$FSharp-Compiler-Service>.$ServiceLexing+clo@924-530::Invoke([FSharp.Core]Microsoft.FSharp.Core.FSharpFunc`2<System.Tuple`3<FSharp.Compiler.Parser+token,int32,int32>,Microsoft.FSharp.Core.Unit>)][offset 0x00000064][found Char] Unexpected type on the stack.
4040
[IL]: Error [StackUnexpected]: : <StartupCode$FSharp-Compiler-Service>.$ServiceLexing+clo@924-530::Invoke([FSharp.Core]Microsoft.FSharp.Core.FSharpFunc`2<System.Tuple`3<FSharp.Compiler.Parser+token,int32,int32>,Microsoft.FSharp.Core.Unit>)][offset 0x0000006D][found Char] Unexpected type on the stack.
4141
[IL]: Error [StackUnexpected]: : <StartupCode$FSharp-Compiler-Service>.$ServiceLexing+clo@924-530::Invoke([FSharp.Core]Microsoft.FSharp.Core.FSharpFunc`2<System.Tuple`3<FSharp.Compiler.Parser+token,int32,int32>,Microsoft.FSharp.Core.Unit>)][offset 0x00000076][found Char] Unexpected type on the stack.
42-
[IL]: Error [StackUnexpected]: : <StartupCode$FSharp-Compiler-Service>.$Symbols+fullName@2496-3::Invoke([FSharp.Core]Microsoft.FSharp.Core.Unit)][offset 0x00000030][found Char] Unexpected type on the stack.
42+
[IL]: Error [StackUnexpected]: : <StartupCode$FSharp-Compiler-Service>.$Symbols+fullName@2498-3::Invoke([FSharp.Core]Microsoft.FSharp.Core.Unit)][offset 0x00000030][found Char] Unexpected type on the stack.
4343
[IL]: Error [StackUnexpected]: : FSharp.Compiler.Driver+ProcessCommandLineFlags@286-1::Invoke(string)][offset 0x0000000B][found Char] Unexpected type on the stack.
4444
[IL]: Error [StackUnexpected]: : FSharp.Compiler.Driver+ProcessCommandLineFlags@286-1::Invoke(string)][offset 0x00000014][found Char] Unexpected type on the stack.
4545
[IL]: Error [StackUnexpected]: : FSharp.Compiler.CreateILModule+MainModuleBuilder::ConvertProductVersionToILVersionInfo(string)][offset 0x00000010][found Char] Unexpected type on the stack.
@@ -65,7 +65,7 @@
6565
[IL]: Error [StackUnexpected]: : FSharp.Compiler.IlxGen::HashRangeSorted([S.P.CoreLib]System.Collections.Generic.IDictionary`2<!!0,System.Tuple`2<int32,!!1>>)][offset 0x00000012][found ref '[FSharp.Compiler.Service]FSharp.Compiler.IlxGen+HashRangeSorted@1873<T1>'][expected ref '[FSharp.Core]Microsoft.FSharp.Core.FSharpFunc`2<System.Tuple`2<int32,T0>,T0>'] Unexpected type on the stack.
6666
[IL]: Error [StackUnexpected]: : FSharp.Compiler.PatternMatchCompilation::isProblematicClause([FSharp.Compiler.Service]FSharp.Compiler.PatternMatchCompilation+MatchClause)][offset 0x00000040][found Byte] Unexpected type on the stack.
6767
[IL]: Error [StackUnexpected]: : <StartupCode$FSharp-Compiler-Service>.$FSharp.Compiler.PatternMatchCompilation::.cctor()][offset 0x0000000B][found Boolean] Unexpected type on the stack.
68-
[IL]: Error [StackUnexpected]: : FSharp.Compiler.NicePrint+TastDefinitionPrinting+meths@2092-3::Invoke([FSharp.Compiler.Service]FSharp.Compiler.Infos+MethInfo)][offset 0x000000B3][found Char] Unexpected type on the stack.
68+
[IL]: Error [StackUnexpected]: : FSharp.Compiler.NicePrint+TastDefinitionPrinting+meths@2070-3::Invoke([FSharp.Compiler.Service]FSharp.Compiler.Infos+MethInfo)][offset 0x000000B3][found Char] Unexpected type on the stack.
6969
[IL]: Error [StackUnexpected]: : FSharp.Compiler.NicePrint+PrintUtilities::layoutXmlDoc([FSharp.Compiler.Service]FSharp.Compiler.TypedTreeOps+DisplayEnv, bool, [FSharp.Compiler.Service]FSharp.Compiler.Xml.XmlDoc, [FSharp.Compiler.Service]FSharp.Compiler.Text.Layout)][offset 0x00000034][found Char] Unexpected type on the stack.
7070
[IL]: Error [StackUnexpected]: : FSharp.Compiler.TypeProviders::ValidateNamespaceName(string, [FSharp.Compiler.Service]FSharp.Compiler.Tainted`1<Microsoft.FSharp.Core.CompilerServices.ITypeProvider>, [FSharp.Compiler.Service]FSharp.Compiler.Text.Range, string)][offset 0x00000074][found Char] Unexpected type on the stack.
7171
[IL]: Error [StackUnexpected]: : FSharp.Compiler.TypeProviders::ValidateExpectedName([FSharp.Compiler.Service]FSharp.Compiler.Text.Range, string[], string, [FSharp.Compiler.Service]FSharp.Compiler.Tainted`1<FSharp.Compiler.TypeProviders+ProvidedType>)][offset 0x000000A8][found Char] Unexpected type on the stack.

0 commit comments

Comments
 (0)