Skip to content

Commit f0c220a

Browse files
committed
#1455 fix trait calls not resolved from base classes
1 parent 7f4acbd commit f0c220a

File tree

6 files changed

+54
-15
lines changed

6 files changed

+54
-15
lines changed

src/compiler/WebSharper.Compiler.FSharp/CodeReader.fs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1388,7 +1388,11 @@ let rec transformExpression (env: Environment) (expr: FSharpExpr) =
13881388
)
13891389
|> List.tryPick (fun t ->
13901390
if t.HasTypeDefinition then
1391-
t.TypeDefinition.TryGetMembersFunctionsAndValues()
1391+
let rec getAllMembers (t: FSharpEntity) =
1392+
match t.BaseType with
1393+
| None -> t.TryGetMembersFunctionsAndValues() :> _ seq
1394+
| Some bt -> Seq.append (t.TryGetMembersFunctionsAndValues()) (getAllMembers bt.TypeDefinition)
1395+
getAllMembers t.TypeDefinition
13921396
|> Seq.tryPick (fun mem ->
13931397
match sr.ReadMember mem with
13941398
| Member.Method(inst, m) when inst = isInstance && m.Value.MethodName = traitName ->

src/compiler/WebSharper.Compiler/Compilation.fs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -749,15 +749,22 @@ type Compilation(meta: Info, ?hasGraph) =
749749
| _ ->
750750
failwithf "Error looking up abstract method generics %s.%s" typ.Value.FullName meth.Value.MethodName
751751

752-
member this.GetMethods typ =
752+
member this.GetMethods (typ: Concrete<TypeDefinition>) =
753+
let typEnt = typ.Entity
753754
compilingMethods |> Seq.choose (fun (KeyValue ((td, m), _)) ->
754-
if td = typ then Some m else None
755+
if td = typEnt then Some (typ, m) else None
755756
) |> Seq.append (
756-
match this.TryLookupClassInfo typ with
757-
| Some (_, cls) when not (this.IsInterface(typ)) -> cls.Methods.Keys :> _ seq
757+
match this.TryLookupClassInfo typEnt with
758+
| Some (_, cls) when not (this.IsInterface(typEnt)) ->
759+
match cls.BaseClass with
760+
| None ->
761+
cls.Methods.Keys |> Seq.map (fun m -> typ, m)
762+
| Some bTyp ->
763+
let gsArr = typ.Generics |> List.toArray
764+
Seq.append (cls.Methods.Keys |> Seq.map (fun m -> typ, m)) (this.GetMethods { bTyp with Generics = bTyp.Generics |> List.map (fun p -> p.SubstituteGenerics(gsArr)) })
758765
| _ ->
759-
match this.TryLookupInterfaceInfo typ with
760-
| Some intf -> intf.Methods.Keys :> _ seq
766+
match this.TryLookupInterfaceInfo typEnt with
767+
| Some intf -> intf.Methods.Keys |> Seq.map (fun m -> typ, m)
761768
| _ ->
762769
Seq.empty
763770
)

src/compiler/WebSharper.Compiler/CompilationHelpers.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ let varEvalOrder (vars : Id list) expr =
481481
| Import _
482482
| Interface _
483483
| XmlComment _
484+
| LazyClass _
484485
-> fail()
485486

486487
eval expr

src/compiler/WebSharper.Compiler/Translator.fs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,26 +1221,26 @@ type DotNetToJavaScript private (comp: Compilation, ?inProgress) =
12211221
match typ with
12221222
| ConcreteType ct ->
12231223
//let trmv = trmv.SubstituteGenerics(Array.ofList (typ :: meth.Generics))
1224-
let methods = comp.GetMethods ct.Entity
1224+
let methods = comp.GetMethods ct
12251225
let getMethods pars ret =
1226-
methods |> Seq.choose (fun m ->
1226+
methods |> Seq.choose (fun (mtyp, m) ->
12271227
let tcSig = FSharpFuncType (TupleType (pars, false), ret)
12281228
let mv = m.Value
12291229
if mv.MethodName = mName then
12301230
let mSig = FSharpFuncType (TupleType (mv.Parameters, false), mv.ReturnType)
12311231
if Type.IsGenericCompatible(mSig, tcSig) then
1232-
Some m
1232+
Some (mtyp, m)
12331233
else
12341234
None
12351235
else None
12361236
)
12371237
|> List.ofSeq
12381238
match getMethods trmv.Parameters trmv.ReturnType with
1239-
| [ m ] ->
1240-
this.TransformCall(thisObj, ct, Generic m meth.Generics, args) |> Some
1239+
| [ (mtyp, m) ] ->
1240+
this.TransformCall(thisObj, mtyp, Generic m meth.Generics, args) |> Some
12411241
| [] ->
12421242
let targets =
1243-
methods |> Seq.choose (fun m ->
1243+
methods |> Seq.choose (fun (_, m) ->
12441244
let mv = m.Value
12451245
if mv.MethodName = mName then
12461246
let mSig = FSharpFuncType (TupleType (mv.Parameters, false), mv.ReturnType)
@@ -2097,7 +2097,7 @@ type DotNetToJavaScript private (comp: Compilation, ?inProgress) =
20972097
TypeOf "function"
20982098
| M.FSharpRecordInfo _
20992099
| M.FSharpAnonRecordInfo _
2100-
| M.StructInfo _ ->
2100+
| M.StructInfo ->
21012101
PlainObject false
21022102
| M.FSharpUnionInfo ui ->
21032103
PlainObject ui.HasNull

tests/WebSharper.Tests/ASTTests.fsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ let stExpr s = WebSharper.Core.AST.StatementExpr(s, None)
346346

347347
open System.IO
348348

349-
let translate isBundle source =
349+
let translate isBundle (source: string) =
350350

351351
let tempFileName = Path.GetTempFileName()
352352
let fileName = Path.ChangeExtension(tempFileName, ".fs")

tests/WebSharper.Tests/Regression.fs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,29 @@ module Bug1390 =
568568
type IValue<'T> =
569569
abstract member Value : 'T with get
570570

571+
[<JavaScript>]
572+
module Bug1455 =
573+
[<Inline>]
574+
let inline ( := ) (o: ^x) (v: ^a) = (^x: (member Value: ^a with set) (o, v))
575+
576+
type [<AbstractClass>] ValueRef<'T>() =
577+
abstract Get : unit -> 'T
578+
abstract Set : 'T -> unit
579+
member this.Value
580+
with [<Inline>] get() = this.Get()
581+
and [<Inline>] set v = this.Set v
582+
583+
and ConcreteValueRef<'T>(initVal) =
584+
inherit ValueRef<'T>()
585+
let mutable value = initVal
586+
default this.Get() = value
587+
default this.Set(v) = value <- v
588+
589+
let test() =
590+
let v = ConcreteValueRef ""
591+
v := "test"
592+
v.Get()
593+
571594
[<JavaScript>]
572595
let Tests =
573596
TestCategory "Regression" {
@@ -1124,4 +1147,8 @@ let Tests =
11241147
i.Value
11251148
equal test 4
11261149
}
1150+
1151+
Test "#1455 Inlines not resolved from base classes" {
1152+
equal (Bug1455.test()) "test"
1153+
}
11271154
}

0 commit comments

Comments
 (0)