Skip to content

Commit

Permalink
[Rust] Fixed inner attributes (#3121)
Browse files Browse the repository at this point in the history
  • Loading branch information
ncave authored Aug 29, 2022
1 parent 7840682 commit 8dc177b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/Fable.AST/Fable.fs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type MemberRefInfo =
NonCurriedArgTypes: Type list option }

type MemberRef =
| MemberRef of entity: EntityRef * info: MemberRefInfo
| MemberRef of declaringEntity: EntityRef * info: MemberRefInfo
| GeneratedMemberRef of GeneratedMember

type DeclaredType =
Expand Down
4 changes: 2 additions & 2 deletions src/Fable.Transforms/Global/Compiler.fs
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ module CompilerExt =
member com.TryGetMember(memberRef: Fable.MemberRef): Fable.MemberFunctionOrValue option =
match memberRef with
| Fable.GeneratedMemberRef gen -> Some(gen :> _)
| Fable.MemberRef(entityRef, memberInfo) ->
com.TryGetEntity(entityRef)
| Fable.MemberRef(declaringEntity, memberInfo) ->
com.TryGetEntity(declaringEntity)
|> Option.bind (fun ent -> ent.TryFindMember(memberInfo))

member com.GetMember(memberRef: Fable.MemberRef): Fable.MemberFunctionOrValue =
Expand Down
53 changes: 40 additions & 13 deletions src/Fable.Transforms/Rust/Fable2Rust.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3197,22 +3197,29 @@ module Util =
let ctxNext = { ctx with ScopedSymbols = scopedSymbols }
mkItemStmt fnItem, ctxNext

let transformAttributes (com: IRustCompiler) ctx (attributes: Fable.Attribute seq) =
let transformAttributes com ctx (attributes: Fable.Attribute seq) =
attributes
|> Seq.collect (fun att ->
// translate test methods attributes
// TODO: support more test frameworks
if att.Entity.FullName.EndsWith(".FactAttribute") then
[mkAttr "test" []]
// custom outer attributes
elif att.Entity.FullName = Atts.rustOuterAttr then
// Rust outer attributes
if att.Entity.FullName = Atts.rustOuterAttr then
match att.ConstructorArgs with
| [:? string as name] -> [mkAttr name []]
| [:? string as name; :? string as value] -> [mkEqAttr name value]
| [:? string as name; :? (obj[]) as items] -> [mkAttr name (items |> Array.map string)]
| _ -> []
// custom inner attributes
elif att.Entity.FullName = Atts.rustInnerAttr then
// translate test methods attributes
// TODO: support more test frameworks
elif att.Entity.FullName.EndsWith(".FactAttribute") then
[mkAttr "test" []]
else []
)
|> Seq.toList

let transformInnerAttributes com ctx (attributes: Fable.Attribute seq) =
attributes
|> Seq.collect (fun att ->
// Rust inner attributes
if att.Entity.FullName = Atts.rustInnerAttr then
match att.ConstructorArgs with
| [:? string as name] -> [mkInnerAttr name []]
| [:? string as name; :? string as value] -> [mkInnerEqAttr name value]
Expand All @@ -3222,6 +3229,23 @@ module Util =
)
|> Seq.toList

let getInnerAttributes (com: IRustCompiler) ctx (decls: Fable.Declaration list) =
decls
|> List.collect (fun decl ->
match decl with
| Fable.ModuleDeclaration decl ->
let ent = com.GetEntity(decl.Entity)
transformInnerAttributes com ctx ent.Attributes
| Fable.ActionDeclaration decl ->
[]
| Fable.MemberDeclaration decl ->
let memb = com.GetMember(decl.MemberRef)
transformInnerAttributes com ctx memb.Attributes
| Fable.ClassDeclaration decl ->
let ent = com.GetEntity(decl.Entity)
transformInnerAttributes com ctx ent.Attributes
)

let transformModuleAction (com: IRustCompiler) ctx (body: Fable.Expr) =
// uses startup::on_startup! for static execution (before main)
let expr = transformExpr com ctx body
Expand Down Expand Up @@ -3856,7 +3880,9 @@ module Util =
let importItems = com.GetAllImports(ctx) |> transformImports com ctx
com.ClearAllImports(ctx)
useItem :: importItems
let attrs = transformAttributes com ctx ent.Attributes
let outerAttrs = transformAttributes com ctx ent.Attributes
let innerAttrs = getInnerAttributes com ctx decl.Members
let attrs = innerAttrs @ outerAttrs
let modDecls = useDecls @ memberDecls
let modItem = modDecls |> mkModItem attrs decl.Name
[modItem |> mkPublicItem]
Expand All @@ -3871,7 +3897,7 @@ module Util =
match decl with
| Fable.ModuleDeclaration decl ->
withCurrentScope ctx (Set.singleton decl.Name) <| fun ctx ->
transformModuleDecl (com: IRustCompiler) ctx decl
transformModuleDecl com ctx decl

| Fable.ActionDeclaration decl ->
withCurrentScope ctx decl.UsedNames <| fun ctx ->
Expand Down Expand Up @@ -4110,6 +4136,7 @@ module Compiler =
let declItems = List.collect (transformDecl com ctx) file.Declarations
let moduleItems = getModuleItems com ctx // global module imports
let crateItems = importItems @ declItems @ moduleItems @ entryPointItems

let crate = mkCrate topAttrs crateItems
let innerAttrs = getInnerAttributes com ctx file.Declarations
let crateAttrs = topAttrs @ innerAttrs
let crate = mkCrate crateAttrs crateItems
crate

0 comments on commit 8dc177b

Please sign in to comment.