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

Field sharing for [<Struct>] DUs if they have equal name and equal type #15738

Merged
merged 34 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
83cc0e4
internalerror with MakeValueAssign fixed?
T-Gro Jun 16, 2023
4e98daa
Merge branch 'main' of https://github.com/T-Gro/fsharp
T-Gro Jun 22, 2023
5f3e7a7
Merge remote-tracking branch 'upstream/main'
T-Gro Jun 27, 2023
d5365f0
Merge remote-tracking branch 'upstream/main'
T-Gro Jul 4, 2023
fbe8632
Merge remote-tracking branch 'upstream/main'
T-Gro Jul 10, 2023
202d49d
Merge branch 'dotnet:main' into main
T-Gro Jul 11, 2023
86b9ab8
Merge branch 'dotnet:main' into main
T-Gro Jul 14, 2023
7e72ccc
Merge remote-tracking branch 'upstream/main'
T-Gro Jul 25, 2023
59d1b41
Adding cancellation suppot for lexing and parsing
T-Gro Jul 25, 2023
151e108
x
T-Gro Jul 25, 2023
6509b51
Merge remote-tracking branch 'upstream/main' into struct-dus-do-my-best
T-Gro Jul 26, 2023
38d7f36
tests covering non working case, fallback fix (still kind of hack)
T-Gro Jul 27, 2023
ed17a33
Change construction methods for [<Struct>] unions to enable creating …
T-Gro Jul 27, 2023
9542088
Merge branch 'main' into struct-dus-do-my-best
T-Gro Jul 27, 2023
8a9aa38
add failing cases
T-Gro Jul 28, 2023
47b7763
Merge branch 'struct-dus-do-my-best' of https://github.com/T-Gro/fsha…
T-Gro Jul 28, 2023
1471810
Fix single case DU wrapper and marker "types"
T-Gro Jul 28, 2023
6fda6a6
failing test for custom ValueOption
T-Gro Jul 28, 2023
2d90917
adjusting .maxstack
T-Gro Jul 28, 2023
26994d0
IL baselines updated
T-Gro Jul 28, 2023
f0f0108
Revert "IL baselines updated"
T-Gro Jul 28, 2023
f54cbb9
now the right IL bsl updates
T-Gro Jul 28, 2023
30ae168
Merge branch 'main' into struct-dus-do-my-best
T-Gro Jul 31, 2023
7aee265
Merge branch 'main' into struct-dus-do-my-best
T-Gro Aug 1, 2023
641aeeb
struct DU - reuse fields if they have same name and type
T-Gro Aug 2, 2023
2eb8f22
formatting
T-Gro Aug 2, 2023
299e0eb
Update src/Compiler/FSComp.txt
T-Gro Nov 27, 2023
dcfb88c
Merge branch 'main' into struct-DUs-field-sharing
T-Gro Nov 27, 2023
8d15608
Automated command ran: fantomas
github-actions[bot] Nov 27, 2023
c990570
fix
T-Gro Nov 27, 2023
a231d18
xlf files updated
T-Gro Nov 27, 2023
f031c50
fix tests
T-Gro Nov 28, 2023
476f1dd
change error code
T-Gro Nov 28, 2023
da016da
Merge branch 'main' into struct-DUs-field-sharing
T-Gro Nov 28, 2023
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
26 changes: 20 additions & 6 deletions src/Compiler/Checking/CheckDeclarations.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3357,12 +3357,26 @@ module EstablishTypeDefinitionCores =

let multiCaseUnionStructCheck (unionCases: UnionCase list) =
if tycon.IsStructRecordOrUnionTycon && unionCases.Length > 1 then
let fieldNames = [ for uc in unionCases do for ft in uc.FieldTable.TrueInstanceFieldsAsList do yield (ft.LogicalName, ft.Range) ]
let distFieldNames = fieldNames |> List.distinctBy fst
if distFieldNames.Length <> fieldNames.Length then
let fieldRanges = distFieldNames |> List.map snd
for m in fieldRanges do
errorR(Error(FSComp.SR.tcStructUnionMultiCaseDistinctFields(), m))
let allTypesEqual listOfNamesAndTypes =
match listOfNamesAndTypes with
| [] | [_] -> true
| (_,headField : RecdField) :: tail ->
let headType = headField.FormalType
tail |> List.forall (fun (_,elemType) -> typeEquivAux EraseAll g elemType.FormalType headType)
T-Gro marked this conversation as resolved.
Show resolved Hide resolved

let diagnostics,duplicateCriteria =
if cenv.g.langVersion.SupportsFeature(LanguageFeature.ReuseSameFieldsInStructUnions) then
FSComp.SR.tcStructUnionMultiCaseFieldsSameType, allTypesEqual >> not
else
FSComp.SR.tcStructUnionMultiCaseDistinctFields, (fun group -> group.Length > 1)

[ for uc in unionCases do for ft in uc.FieldTable.TrueInstanceFieldsAsList do yield(ft.LogicalName,ft) ]
|> List.groupBy fst
|> List.filter (fun (_name,group) -> duplicateCriteria group)
|> List.iter (fun (_,dups) ->
for _,ft in dups do
T-Gro marked this conversation as resolved.
Show resolved Hide resolved
errorR(Error(diagnostics(), ft.Range)))


// Notify the Language Service about field names in record/class declaration
let ad = envinner.AccessRights
Expand Down
12 changes: 10 additions & 2 deletions src/Compiler/CodeGen/EraseUnions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1232,6 +1232,8 @@ let mkClassUnionDef
|> Array.tryFindIndex (fun t -> t.IsNullary)
|> Option.defaultValue -1

let fieldsEmitted = new HashSet<_>()

for cidx, alt in Array.indexed cud.UnionCases do
if
repr.RepresentAlternativeAsFreshInstancesOfRootClass(info, alt)
Expand All @@ -1251,7 +1253,7 @@ let mkClassUnionDef
let ctor =
// Structs with fields are created using static makers methods
// Structs without fields can share constructor for the 'tag' value, we just create one
if isStruct && cidx <> minNullaryIdx then
if isStruct && not (cidx = minNullaryIdx) then
[]
else
[
Expand All @@ -1268,6 +1270,12 @@ let mkClassUnionDef
|> addMethodGeneratedAttrs
]

let fieldsToBeAddedIntoType =
T-Gro marked this conversation as resolved.
Show resolved Hide resolved
alt.FieldDefs
|> Array.filter (fun f -> fieldsEmitted.Add(struct (f.LowerName, f.Type)))

let fields = fieldsToBeAddedIntoType |> Array.map mkUnionCaseFieldId |> Array.toList

let props, meths =
mkMethodsAndPropertiesForFields
(addMethodGeneratedAttrs, addPropertyGeneratedAttrs)
Expand All @@ -1276,7 +1284,7 @@ let mkClassUnionDef
cud.DebugImports
cud.HasHelpers
baseTy
alt.FieldDefs
fieldsToBeAddedIntoType

yield (fields, (ctor @ meths), props)
]
Expand Down
2 changes: 2 additions & 0 deletions src/Compiler/FSComp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1730,6 +1730,8 @@ featureUnmanagedConstraintCsharpInterop,"Interop between C#'s and F#'s unmanaged
3582,tcInfoIfFunctionShadowsUnionCase,"This is a function definition that shadows a union case. If this is what you want, ignore or suppress this warning. If you want it to be a union case deconstruction, add parentheses."
3583,unnecessaryParentheses,"Parentheses can be removed."
3584,tcDotLambdaAtNotSupportedExpression,"Shorthand lambda syntax is only supported for atomic expressions, such as method, property, field or indexer on the implied '_' argument. For example: 'let f = _.Length'."
3585,tcStructUnionMultiCaseFieldsSameType,"If a multicase union type is a struct, then all fields with the same name must be of the same type. This rule applies also to the generated 'Item' name in case of unnamed fields."
featureReuseSameFieldsInStructUnions,"Share underlying fields in a [<Struct>] discriminated union as long as they have same name and type"
3855,tcNoStaticMemberFoundForOverride,"No static abstract member was found that corresponds to this override"
3859,tcNoStaticPropertyFoundForOverride,"No static abstract property was found that corresponds to this override"
3860,chkStaticMembersOnObjectExpressions,"Object expressions cannot implement interfaces with static abstract members or declare static members."
Expand Down
3 changes: 3 additions & 0 deletions src/Compiler/Facilities/LanguageFeatures.fs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ type LanguageFeature =
| WarningWhenTailRecAttributeButNonTailRecUsage
| UnmanagedConstraintCsharpInterop
| WhileBang
| ReuseSameFieldsInStructUnions
| ExtendedFixedBindings
| PreferStringGetPinnableReference
| PreferExtensionMethodOverPlainProperty
Expand Down Expand Up @@ -188,6 +189,7 @@ type LanguageVersion(versionText) =
// F# preview
LanguageFeature.FromEndSlicing, previewVersion
LanguageFeature.UnmanagedConstraintCsharpInterop, previewVersion
LanguageFeature.ReuseSameFieldsInStructUnions, previewVersion
LanguageFeature.PreferExtensionMethodOverPlainProperty, previewVersion
LanguageFeature.WarningIndexedPropertiesGetSetSameType, previewVersion
LanguageFeature.WarningWhenTailCallAttrOnNonRec, previewVersion
Expand Down Expand Up @@ -325,6 +327,7 @@ type LanguageVersion(versionText) =
| LanguageFeature.WarningWhenTailRecAttributeButNonTailRecUsage -> FSComp.SR.featureChkNotTailRecursive ()
| LanguageFeature.UnmanagedConstraintCsharpInterop -> FSComp.SR.featureUnmanagedConstraintCsharpInterop ()
| LanguageFeature.WhileBang -> FSComp.SR.featureWhileBang ()
| LanguageFeature.ReuseSameFieldsInStructUnions -> FSComp.SR.featureReuseSameFieldsInStructUnions ()
| LanguageFeature.ExtendedFixedBindings -> FSComp.SR.featureExtendedFixedBindings ()
| LanguageFeature.PreferStringGetPinnableReference -> FSComp.SR.featurePreferStringGetPinnableReference ()
| LanguageFeature.PreferExtensionMethodOverPlainProperty -> FSComp.SR.featurePreferExtensionMethodOverPlainProperty ()
Expand Down
1 change: 1 addition & 0 deletions src/Compiler/Facilities/LanguageFeatures.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type LanguageFeature =
| WarningWhenTailRecAttributeButNonTailRecUsage
| UnmanagedConstraintCsharpInterop
| WhileBang
| ReuseSameFieldsInStructUnions
| ExtendedFixedBindings
| PreferStringGetPinnableReference
/// RFC-1137
Expand Down
10 changes: 10 additions & 0 deletions src/Compiler/xlf/FSComp.txt.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Compiler/xlf/FSComp.txt.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Compiler/xlf/FSComp.txt.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Compiler/xlf/FSComp.txt.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Compiler/xlf/FSComp.txt.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Compiler/xlf/FSComp.txt.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/Compiler/xlf/FSComp.txt.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading