Skip to content

Commit

Permalink
[Rust] Added support for Dictionary/HashSet comparers (#3893)
Browse files Browse the repository at this point in the history
* [JS/TS] Updated Decimal tests

* [Rust] Added support for Dictionary/HashSet comparers

* [Rust] Updated support for interface object expressions

* [Rust] fixed no_std build

* [Rust] Fixed threaded build
  • Loading branch information
ncave authored Sep 19, 2024
1 parent 6089f30 commit 2c1fe37
Show file tree
Hide file tree
Showing 25 changed files with 675 additions and 465 deletions.
101 changes: 44 additions & 57 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -193,18 +193,12 @@
"preLaunchTask": "build",
"program": "${workspaceFolder}/src/Fable.Cli/bin/Debug/net6.0/fable.dll",
"args": [
"--",
"src/fable-library-dart",
"--outDir",
"temp/fable-library-dart",
"--fableLib",
"./temp/fable-library-dart",
"--lang",
"dart",
"--exclude",
"Fable.Core",
"--define",
"FABLE_LIBRARY",
"--outDir", "temp/fable-library-dart",
"--fableLib", "./temp/fable-library-dart",
"--exclude", "Fable.Core",
"--define", "FABLE_LIBRARY",
"--lang", "Dart",
"--noCache"
]
},
Expand All @@ -216,21 +210,14 @@
"program": "${workspaceFolder}/src/Fable.Cli/bin/Debug/net6.0/fable.dll",
"args": [
"src/fable-library-ts",
"--outDir",
"temp/fable-library-ts",
"--fableLib",
"./temp/fable-library-ts",
"--lang",
"typescript",
"--exclude",
"Fable.Core",
"--define",
"FABLE_LIBRARY",
"--noCache",
"--typedArrays",
"false",
"--define",
"FX_NO_BIGINT"
"--outDir", "temp/fable-library-ts",
"--fableLib", "./temp/fable-library-ts",
"--exclude", "Fable.Core",
"--define", "FABLE_LIBRARY",
"--define", "FX_NO_BIGINT",
"--typedArrays", "false",
"--lang", "TypeScript",
"--noCache"
]
},
{
Expand All @@ -241,16 +228,11 @@
"program": "${workspaceFolder}/src/Fable.Cli/bin/Debug/net6.0/fable.dll",
"args": [
"src/fable-library-py/fable_library",
"--outDir",
"temp/fable-library-py/fable_library",
"--fableLib",
".",
"--lang",
"python",
"--exclude",
"Fable.Core",
"--define",
"FABLE_LIBRARY",
"--outDir", "temp/fable-library-py/fable_library",
"--fableLib", ".",
"--exclude", "Fable.Core",
"--define", "FABLE_LIBRARY",
"--lang", "Python",
"--noCache"
]
},
Expand All @@ -262,29 +244,34 @@
"program": "${workspaceFolder}/src/Fable.Cli/bin/Debug/net6.0/fable.dll",
"args": [
"src/fable-library-rust/src",
"--outDir",
"temp/fable-library-rust/src",
"--fableLib",
".",
"--lang",
"rust",
"--exclude",
"Fable.Core",
"--define",
"FABLE_LIBRARY",
"--noCache"
"--outDir", "temp/fable-library-rust/src",
"--fableLib", ".",
"--exclude", "Fable.Core",
"--define", "FABLE_LIBRARY",
"--lang", "Rust",
"--noCache",
"--noParallelTypeCheck",
"--test:MSBuildCracker"
]
},
{
"name": "Fable.Cli on ../fable-test",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceRoot}/src/Fable.Cli/bin/Debug/net6.0/fable.dll",
"args": ["--outDir", "${workspaceRoot}/../fable-test", "--fableLib", "${workspaceRoot}/temp/fable-library-rust", "--exclude", "Fable.Core", "--lang", "Rust", "--noCache", "--noParallelTypeCheck", "--test:MSBuildCracker"],
"cwd": "${workspaceRoot}/../fable-test",
"stopAtEntry": false,
"console": "internalConsole"
"name": "Fable.Cli on ../fable-test",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceRoot}/src/Fable.Cli/bin/Debug/net6.0/fable.dll",
"args": [
"--outDir", "${workspaceRoot}/../fable-test",
"--fableLib", "${workspaceRoot}/temp/fable-library-rust",
"--exclude", "Fable.Core",
"--lang", "Rust",
"--noCache",
"--noParallelTypeCheck",
"--test:MSBuildCracker"
],
"cwd": "${workspaceRoot}/../fable-test",
"stopAtEntry": false,
"console": "internalConsole"
}
]
}
}
2 changes: 2 additions & 0 deletions src/Fable.Cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* [JS/TS] Add support for `OrdinalIgnoreCase` overload for `String.EndsWith` (#3892) (by @goswinr)
* [JS/TS] Add `uri.Port`, `uri.IsDefaultPort` (by @MangelMaxime)
* [Rust] Added support for Dictionary/HashSet comparers (by @ncave)
* [Rust] Updated support for interface object expressions (by @ncave)

### Changed

Expand Down
18 changes: 13 additions & 5 deletions src/Fable.Cli/Main.fs
Original file line number Diff line number Diff line change
Expand Up @@ -478,20 +478,30 @@ type FableCompilerState =
and FableCompiler(checker: InteractiveChecker, projCracked: ProjectCracked, fableProj: Project) =
let agent =
MailboxProcessor<FableCompilerMsg>.Start(fun agent ->
let startInThreadPool toMsg work =
let postTo toMsg work =
async {
try
let! result = work ()
toMsg result |> agent.Post
with e ->
UnexpectedError e |> agent.Post
}
|> Async.Start

let startInThreadPool toMsg work = postTo toMsg work |> Async.Start

let runSynchronously toMsg work =
postTo toMsg work |> Async.RunSynchronously

let fableCompile state fileName =
let fableProj = state.FableProj

startInThreadPool
let runner =
// for Rust, sequential compilation captures all imports and namespaces
match projCracked.CliArgs.CompilerOptions.Language with
| Rust -> runSynchronously // sequential file compilation
| _ -> startInThreadPool // parallel file compilation

runner
FableFileCompiled
(fun () ->
async {
Expand Down Expand Up @@ -574,7 +584,6 @@ and FableCompiler(checker: InteractiveChecker, projCracked: ProjectCracked, fabl
// Print F# AST to file
if projCracked.CliArgs.PrintAst then
let outPath = getOutPath projCracked.CliArgs state.PathResolver file.FileName

let outDir = IO.Path.GetDirectoryName(outPath)
Printers.printAst outDir [ file ]

Expand All @@ -586,7 +595,6 @@ and FableCompiler(checker: InteractiveChecker, projCracked: ProjectCracked, fabl
state
else
let state = { state with FableProj = state.FableProj.Update([ file ]) }

fableCompile state fileName

return! loop state
Expand Down
8 changes: 1 addition & 7 deletions src/Fable.Transforms/Dart/Fable2Dart.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2182,13 +2182,7 @@ module Util =
: string list
=

let genParams =
(Set.empty, argTypes)
||> List.fold (fun genArgs t ->
(genArgs, FSharp2Fable.Util.getGenParamNames t)
||> List.fold (fun genArgs n -> Set.add n genArgs)
)
|> List.ofSeq
let genParams = argTypes |> FSharp2Fable.Util.getGenParamNames

let genParams =
match genParams, ctx.EntityAndMemberGenericParams with
Expand Down
20 changes: 15 additions & 5 deletions src/Fable.Transforms/FSharp2Fable.Util.fs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,8 @@ type FsEnt(maybeAbbrevEnt: FSharpEntity) =
| None -> ent.LogicalName

static member Ref(ent: FSharpEntity) : Fable.EntityRef =
let ent = Helpers.nonAbbreviatedDefinition ent

let path =
match ent.Assembly.FileName with
| Some asmPath ->
Expand Down Expand Up @@ -2092,15 +2094,16 @@ module Util =

let getMemberGenArgs (memb: Fable.MemberFunctionOrValue) : Fable.Type list = getMemberGenParams memb |> List.map snd

let getTypeGenParams (typ: Fable.Type) : (string * Fable.Type) list =
let rec getTypeGenParams (typ: Fable.Type) : (string * Fable.Type) list =
let getGenParams (types: Fable.Type list) : (string * Fable.Type) list =
let rec findGenParams typ =
match typ with
| Fable.GenericParam(name, false, _) as t -> [ (name, t) ]
| t -> t.Generics |> List.collect getTypeGenParams
| t -> t.Generics |> List.collect findGenParams

getTypeGenParams typ |> List.distinctBy fst
types |> List.collect findGenParams |> List.distinctBy fst

let getGenParamNames (typ: Fable.Type) : string list = getTypeGenParams typ |> List.map fst
let getGenParamNames (types: Fable.Type list) : string list = getGenParams types |> List.map fst
let getGenParamTypes (types: Fable.Type list) : Fable.Type list = getGenParams types |> List.map snd

/// We can add a suffix to the entity name for special methods, like reflection declaration
let entityIdentWithSuffix (com: Compiler) (ent: Fable.EntityRef) suffix =
Expand Down Expand Up @@ -2710,6 +2713,13 @@ module Util =
)
|> snd

let getInterfaceMembers (com: Compiler) (ent: Fable.Entity) =
ent.AllInterfaces
|> Seq.collect (fun ifc ->
let ifcEnt = com.GetEntity(ifc.Entity)
ifcEnt.MembersFunctionsAndValues |> Seq.map (fun memb -> ifc, memb)
)

let hasInterface fullName (ent: Fable.Entity) =
ent.AllInterfaces |> Seq.exists (fun ifc -> ifc.Entity.FullName = fullName)

Expand Down
4 changes: 1 addition & 3 deletions src/Fable.Transforms/Fable2Babel.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1271,9 +1271,7 @@ module Util =

let typeParams =
types
|> List.collect FSharp2Fable.Util.getTypeGenParams
|> List.distinctBy fst
|> List.map snd
|> FSharp2Fable.Util.getGenParamTypes
|> List.filter (fun typ ->
match typ with
| Fable.GenericParam(name = name) ->
Expand Down
10 changes: 9 additions & 1 deletion src/Fable.Transforms/Global/Naming.fs
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,16 @@ module Naming =
else
s.Substring(i1 + 1, i2 - i1 - 1)

let splitFirstBy (sep: string) (s: string) =
let i = s.IndexOf(sep, StringComparison.Ordinal)

if i < 0 then
s, ""
else
s.Substring(0, i), s.Substring(i + sep.Length)

let splitLastBy (sep: string) (s: string) =
let i = s.LastIndexOf(sep)
let i = s.LastIndexOf(sep, StringComparison.Ordinal)

if i < 0 then
"", s
Expand Down
4 changes: 2 additions & 2 deletions src/Fable.Transforms/Python/Fable2Python.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1342,12 +1342,12 @@ module Util =
let undefined _range : Expression = Expression.none

let getGenericTypeParams (types: Fable.Type list) =
types |> List.collect FSharp2Fable.Util.getGenParamNames |> Set.ofList
types |> FSharp2Fable.Util.getGenParamNames |> Set.ofList

// Returns type parameters that is used more than once
let getRepeatedGenericTypeParams ctx (types: Fable.Type list) =
types
|> List.collect FSharp2Fable.Util.getGenParamNames
|> FSharp2Fable.Util.getGenParamNames
|> List.append (ctx.ScopedTypeParams |> Set.toList)
|> List.countBy id
|> List.choose (fun (param, count) ->
Expand Down
Loading

0 comments on commit 2c1fe37

Please sign in to comment.