Skip to content

Commit d2b2a9b

Browse files
committed
fix resolving module value access from IL, tests
1 parent ddf36b0 commit d2b2a9b

File tree

4 files changed

+70
-1
lines changed

4 files changed

+70
-1
lines changed

src/fsharp/vs/Exprs.fs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,8 +740,28 @@ module FSharpExprConvert =
740740
| [v] ->
741741
let vr = VRefLocal v
742742
makeFSCall isMember vr
743+
| [] ->
744+
let isPropGet = vName.StartsWith("get_", System.StringComparison.Ordinal)
745+
let isPropSet = vName.StartsWith("set_", System.StringComparison.Ordinal)
746+
if isPropGet || isPropSet then
747+
let name = PrettyNaming.ChopPropertyName vName
748+
let findByName =
749+
enclosingEntity.ModuleOrNamespaceType.AllValsAndMembers
750+
|> Seq.filter (fun v -> v.CompiledName = name)
751+
|> List.ofSeq
752+
match findByName with
753+
| [ v ] ->
754+
let m = FSharpMemberOrFunctionOrValue(cenv, VRefLocal v)
755+
if isPropGet then
756+
E.Value m
757+
else
758+
let valR = ConvExpr cenv env callArgs.Head
759+
E.ValueSet (m, valR)
760+
| _ -> failwith "Failed to resolve module value unambigously"
761+
else
762+
failwith "Failed to resolve module member"
743763
| _ ->
744-
failwith "Failed to resolve overload"
764+
failwith "Failed to resolve overloaded module member"
745765
elif enclosingEntity.IsRecordTycon then
746766
if isProp then
747767
let name = PrettyNaming.ChopPropertyName vName

tests/service/ExprTests.fs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,10 @@ let ``Check use of type provider that provides calls to F# code`` () =
917917
"""member get_X37(this) (unitVar1) = let this: Microsoft.FSharp.Core.obj = ("My internal state" :> Microsoft.FSharp.Core.obj) :> ErasedWithConstructor.Provided.MyType in (new C() :> TestTP.Helper.I).DoNothing() @ (40,22--40,46)"""
918918
"""member get_X38(this) (unitVar1) = let this: Microsoft.FSharp.Core.obj = ("My internal state" :> Microsoft.FSharp.Core.obj) :> ErasedWithConstructor.Provided.MyType in new C().VirtualDoNothing() @ (41,22--41,45)"""
919919
"""member get_X39(this) (unitVar1) = let this: Microsoft.FSharp.Core.obj = ("My internal state" :> Microsoft.FSharp.Core.obj) :> ErasedWithConstructor.Provided.MyType in let t: Microsoft.FSharp.Core.int * Microsoft.FSharp.Core.int * Microsoft.FSharp.Core.int = (1,2,3) in let i: Microsoft.FSharp.Core.int = t.Item1 in i @ (42,22--42,51)"""
920+
"""member get_X40(this) (unitVar1) = let this: Microsoft.FSharp.Core.obj = ("My internal state" :> Microsoft.FSharp.Core.obj) :> ErasedWithConstructor.Provided.MyType in (moduleValue <- 1; moduleValue) @ (43,22--43,39)"""
921+
"""member get_X41(this) (unitVar1) = let this: Microsoft.FSharp.Core.obj = ("My internal state" :> Microsoft.FSharp.Core.obj) :> ErasedWithConstructor.Provided.MyType in let x: TestTP.Helper.C = new C() in (x.set_Property(1); x.get_Property()) @ (44,22--44,41)"""
922+
"""member get_X42(this) (unitVar1) = let this: Microsoft.FSharp.Core.obj = ("My internal state" :> Microsoft.FSharp.Core.obj) :> ErasedWithConstructor.Provided.MyType in let x: TestTP.Helper.C = new C() in (x.set_AutoProperty(1); x.get_AutoProperty()) @ (45,22--45,45)"""
923+
"""member get_X43(this) (unitVar1) = let this: Microsoft.FSharp.Core.obj = ("My internal state" :> Microsoft.FSharp.Core.obj) :> ErasedWithConstructor.Provided.MyType in (C.set_StaticAutoProperty (1); C.get_StaticAutoProperty ()) @ (46,22--46,51)"""
920924
]
921925

922926
let members =
@@ -1038,6 +1042,24 @@ let ``Check use of type provider that provides calls to F# code`` () =
10381042
"get_X39: TestProject.Class1 -> Microsoft.FSharp.Core.unit -> Microsoft.FSharp.Core.int"
10391043
"t: Microsoft.FSharp.Core.int * Microsoft.FSharp.Core.int * Microsoft.FSharp.Core.int"
10401044
"i: Microsoft.FSharp.Core.int"
1045+
"get_X40: TestProject.Class1 -> Microsoft.FSharp.Core.unit -> Microsoft.FSharp.Core.int"
1046+
"moduleValue: Microsoft.FSharp.Core.int"
1047+
"moduleValue: Microsoft.FSharp.Core.int"
1048+
"get_X41: TestProject.Class1 -> Microsoft.FSharp.Core.unit -> Microsoft.FSharp.Core.int"
1049+
".ctor: Microsoft.FSharp.Core.unit -> TestTP.Helper.C"
1050+
"x: TestTP.Helper.C"
1051+
"set_Property: Microsoft.FSharp.Core.int -> Microsoft.FSharp.Core.unit"
1052+
"x: TestTP.Helper.C"
1053+
"get_Property: Microsoft.FSharp.Core.unit -> Microsoft.FSharp.Core.int"
1054+
"get_X42: TestProject.Class1 -> Microsoft.FSharp.Core.unit -> Microsoft.FSharp.Core.int"
1055+
".ctor: Microsoft.FSharp.Core.unit -> TestTP.Helper.C"
1056+
"x: TestTP.Helper.C"
1057+
"set_AutoProperty: Microsoft.FSharp.Core.int -> Microsoft.FSharp.Core.unit"
1058+
"x: TestTP.Helper.C"
1059+
"get_AutoProperty: Microsoft.FSharp.Core.unit -> Microsoft.FSharp.Core.int"
1060+
"get_X43: TestProject.Class1 -> Microsoft.FSharp.Core.unit -> Microsoft.FSharp.Core.int"
1061+
"set_StaticAutoProperty: Microsoft.FSharp.Core.int -> Microsoft.FSharp.Core.unit"
1062+
"get_StaticAutoProperty: Microsoft.FSharp.Core.unit -> Microsoft.FSharp.Core.int"
10411063
]
10421064

10431065
#endif

tests/service/data/TestProject/Library.fs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,7 @@ type Class1() =
4040
member this.X37 = T().InterfaceDoNothing()
4141
member this.X38 = T().OverrideDoNothing()
4242
member this.X39 = T().TupleConstructionAndGet()
43+
member this.X40 = T().ModuleValue()
44+
member this.X41 = T().ClassProperty()
45+
member this.X42 = T().ClassAutoProperty()
46+
member this.X43 = T().ClassStaticAutoProperty()

tests/service/data/TestTP/Library.fs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ module Helper =
1414
let doNothingGeneric(x:'T) = ()
1515
let doNothingGenericWithConstraint(x: 'T when 'T: equality) = ()
1616
let doNothingGenericWithTypeConstraint(x: 'T when 'T :> _ seq) = ()
17+
18+
let mutable moduleValue = 0
1719

1820
type I =
1921
abstract DoNothing: unit -> unit
@@ -24,6 +26,7 @@ module Helper =
2426

2527
type C() =
2628
inherit B()
29+
let mutable p = 0
2730
static member DoNothing() = ()
2831
static member DoNothingOneArg(x:int) = ()
2932
static member DoNothingOneArg(x:string) = ()
@@ -42,6 +45,10 @@ module Helper =
4245
member __.InstanceDoNothingWithCompiledName() = ()
4346
override __.VirtualDoNothing() = ()
4447

48+
member __.Property with get() = p and set v = p <- v
49+
member val AutoProperty = 0 with get, set
50+
static member val StaticAutoProperty = 0 with get, set
51+
4552
interface I with
4653
member this.DoNothing() = ()
4754

@@ -257,6 +264,22 @@ type BasicProvider (config : TypeProviderConfig) as this =
257264
InvokeCode = fun args -> <@@ (CSharpClass(0) :> ICSharpExplicitInterface).ExplicitMethod("x") @@>)
258265
myType.AddMember(someMethod)
259266

267+
let someMethod = ProvidedMethod("ModuleValue", [], typeof<int>,
268+
InvokeCode = fun args -> <@@ Helper.moduleValue <- 1; Helper.moduleValue @@>)
269+
myType.AddMember(someMethod)
270+
271+
let someMethod = ProvidedMethod("ClassProperty", [], typeof<int>,
272+
InvokeCode = fun args -> <@@ let x = Helper.C() in x.Property <- 1; x.Property @@>)
273+
myType.AddMember(someMethod)
274+
275+
let someMethod = ProvidedMethod("ClassAutoProperty", [], typeof<int>,
276+
InvokeCode = fun args -> <@@ let x = Helper.C() in x.AutoProperty <- 1; x.AutoProperty @@>)
277+
myType.AddMember(someMethod)
278+
279+
let someMethod = ProvidedMethod("ClassStaticAutoProperty", [], typeof<int>,
280+
InvokeCode = fun args -> <@@ Helper.C.StaticAutoProperty <- 1; Helper.C.StaticAutoProperty @@>)
281+
myType.AddMember(someMethod)
282+
260283
[myType]
261284

262285
do

0 commit comments

Comments
 (0)