Skip to content

Commit

Permalink
resolve issues from feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
williamtetlow committed Jan 29, 2019
1 parent 9f79a3b commit 9dfc288
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
7 changes: 3 additions & 4 deletions src/fsharp/NameResolution.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3156,10 +3156,9 @@ let ResolveNestedField sink (ncenv:NameResolver) nenv ad ty (lid : Ident list) =

let lookup() =
let frefs =
try Map.find id.idText nenv.eFieldLabels |> success
with :? KeyNotFoundException ->
// record label is unknown -> suggest related labels and give a hint to the user
raze (SuggestLabelsOfRelatedRecords g nenv id (otherRecdFlds ty))
match (Map.tryFind id.idText nenv.eFieldLabels) with
| Some field -> success field
| None -> raze (SuggestLabelsOfRelatedRecords g nenv id (otherRecdFlds ty))

// Eliminate duplicates arising from multiple 'open'
frefs
Expand Down
51 changes: 32 additions & 19 deletions src/fsharp/TypeChecker.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6898,7 +6898,7 @@ and TcAssertExpr cenv overallTy env (m:range) tpenv x =
//-------------------------------------------------------------------------

and TcRecdExpr cenv overallTy env tpenv (inherits, optOrigExpr, flds, mWholeExpr) =
let buildForNestdFlds (lidwd : LongIdentWithDots) v =
let transformAstForNestedUpdates (lidwd : LongIdentWithDots) v =
let recdExprCopyInfo ids (optOrigExpr : (SynExpr * BlockSeparator) option) (id : Ident) =
let upToId origSepRng id lidwd =
let rec buildLid res (id : Ident) =
Expand All @@ -6922,21 +6922,35 @@ and TcRecdExpr cenv overallTy env tpenv (inherits, optOrigExpr, flds, mWholeExpr

let totalRange (origId : Ident) (id : Ident) =
mkRange origId.idRange.FileName origId.idRange.End id.idRange.Start

let rangeOfBlockSeperator (id : Ident) =
let idEnd = id.idRange.End
let blockSeperatorStartCol = idEnd.Column
let blockSeperatorEndCol = blockSeperatorStartCol + 4
let blockSeperatorStartPos = mkPos idEnd.Line blockSeperatorStartCol
let blockSeporatorEndPos = mkPos idEnd.Line blockSeperatorEndCol

mkRange id.idRange.FileName blockSeperatorStartPos blockSeporatorEndPos

match optOrigExpr with
| Some (SynExpr.Ident origId, (sepRange, _)) ->
let lid, rng = upToId sepRange id (origId :: ids)
Some (SynExpr.LongIdent (false, LongIdentWithDots(lid, rng), None, totalRange origId id), (id.idRange, None)) // TODO: id.idRange should be the range of the next separator
Some (SynExpr.LongIdent (false, LongIdentWithDots(lid, rng), None, totalRange origId id), (rangeOfBlockSeperator id, None)) // TODO: id.idRange should be the range of the next separator
| _ -> None


let rec synExprRecd copyInfo id flds =
Some(SynExpr.Record((None, (copyInfo id), [ match flds with
| [] -> yield ((LongIdentWithDots ([], []), true), v, None)
| [fldId] -> yield ((LongIdentWithDots ([fldId],[]), true), v, None)
| fldId :: rest ->
let nestedFld = synExprRecd copyInfo fldId
yield ((LongIdentWithDots ([fldId], []), true), nestedFld rest, None)], id.idRange)))
SynExpr.Record(
None,
(copyInfo id),
[ match flds with
| [] -> yield ((LongIdentWithDots ([], []), true), v, None)
| [fldId] -> yield ((LongIdentWithDots ([fldId],[]), true), v, None)
| fldId :: rest ->
let nestedFld = synExprRecd copyInfo fldId
yield ((LongIdentWithDots ([fldId], []), true), nestedFld rest, None) ],
id.idRange)
|> Some

let access, flds = lidwd.Lid |> ResolveNestedField cenv.tcSink cenv.nameResolver env.eNameResEnv env.eAccessRights overallTy

Expand All @@ -6951,32 +6965,31 @@ and TcRecdExpr cenv overallTy env tpenv (inherits, optOrigExpr, flds, mWholeExpr
]

expanded

let reduceNstdUpdates flds =
let grpdByFld = flds |> List.groupBy (fun ((_, fld : Ident), _) -> fld.idText)
let groupUpdatesToNestedFields flds =
let groupedByField = flds |> List.groupBy (fun ((_, fld : Ident), _) -> fld.idText)
[
for (_, flds) in grpdByFld do
for (_, flds) in groupedByField do
if (flds |> List.length < 2) then
yield! flds
else
let rec grpIfNstd res xs =
let rec groupIfNested res xs =
match xs with
| [] -> res
| x::[] -> x :: res
| x::y::ys -> match x, y with
| (lidwid, Some(SynExpr.Record (aBI, aCI, aFlds, aRng))), (_, Some(SynExpr.Record (_, _, bFlds, _))) ->
let combinedFlds = aFlds @ bFlds
let reducedRecd = (lidwid, Some(SynExpr.Record (aBI, aCI, combinedFlds, aRng)))
grpIfNstd (reducedRecd :: res) ys
| _ -> grpIfNstd (x :: res) (y :: ys)
groupIfNested (reducedRecd :: res) ys
| _ -> groupIfNested (x :: res) (y :: ys)

yield! flds |> grpIfNstd []
yield! flds |> groupIfNested []
]

let requiresCtor = (GetCtorShapeCounter env = 1) // Get special expression forms for constructors
let haveCtor = Option.isSome inherits


let optOrigExprInfo, tpenv =
match optOrigExpr with
| None -> None, tpenv
Expand All @@ -7003,8 +7016,8 @@ and TcRecdExpr cenv overallTy env tpenv (inherits, optOrigExpr, flds, mWholeExpr
match lidwd.Lid with
| [] -> ()
| [id] -> yield (([], id), v)
| _ -> yield! buildForNestdFlds lidwd v
] |> reduceNstdUpdates
| _ -> yield! transformAstForNestedUpdates lidwd v
] |> groupUpdatesToNestedFields

match flds with
| [] -> []
Expand Down

0 comments on commit 9dfc288

Please sign in to comment.