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

Bugfix :: Fix optimizer bug where field.Index included compiler generated static fields #18280

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 15 additions & 5 deletions src/Compiler/TypedTree/TypedTree.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4273,6 +4273,19 @@ type UnionCaseRef =

override x.ToString() = x.CaseName

let findLogicalFieldIndexOfRecordField (tcref:TyconRef) (id:string) =
let arr = tcref.AllFieldsArray

// We are skipping compiler generated fields such as "init@5" from index calculation
let rec go originalIdx skippedItems =
if originalIdx >= arr.Length then error(InternalError(sprintf "field %s not found in type %s" id tcref.LogicalName, tcref.Range))
else
let currentItem = arr[originalIdx]
if currentItem.LogicalName = id then (originalIdx-skippedItems)
else go (originalIdx + 1) (skippedItems + (if currentItem.IsCompilerGenerated && currentItem.IsStatic then 1 else 0))

go 0 0

/// Represents a reference to a field in a record, class or struct
[<NoEquality; NoComparison; StructuredFormatDisplay("{DebugText}")>]
type RecdFieldRef =
Expand Down Expand Up @@ -4316,11 +4329,8 @@ type RecdFieldRef =

member x.Index =
let (RecdFieldRef(tcref, id)) = x
try
// REVIEW: this could be faster, e.g. by storing the index in the NameMap
tcref.AllFieldsArray |> Array.findIndex (fun rfspec -> rfspec.LogicalName = id)
with :? KeyNotFoundException ->
error(InternalError(sprintf "field %s not found in type %s" id tcref.LogicalName, tcref.Range))
findLogicalFieldIndexOfRecordField tcref id


[<DebuggerBrowsable(DebuggerBrowsableState.Never)>]
member x.DebugText = x.ToString()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Test

// https://github.com/dotnet/fsharp/issues/18165

type FooBar =
{ xyz : string }
static let staticLet = 1

let doThing (foo : FooBar) =
let bar = { foo with xyz = foo.xyz }
let baz = { bar with xyz = bar.xyz }
printf "%O" baz

doThing { xyz = "" }
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ init R 2
1
2"""

[<Theory; Directory(__SOURCE_DIRECTORY__, Includes=[|"RecordOptimizerRegression.fs"|])>]
let ``Static let - record optimizer regression`` compilation =
compilation
|> withOptimize
|> verifyCompileAndRun
|> shouldSucceed
|> withStdOutContains """{ xyz = "" }"""



[<Theory; Directory(__SOURCE_DIRECTORY__, Includes=[|"LowercaseDuTest.fs"|])>]
let ``Static let - lowercase DU`` compilation =
compilation
Expand Down
Loading