Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2bd48e8

Browse files
dotnet-botKevinRansom
authored andcommitted
Fixdotnet#3737 - allow inheriting from FSharpFunc (dotnet#4804) (dotnet#4853)
* allow inheriting from FSharpFunc* allow inheriting from FSharpFunc*fixdotnet#4569* fix tests* fix test* fix error
1 parent92f6ea0 commit2bd48e8

File tree

5 files changed

+72
-8
lines changed

5 files changed

+72
-8
lines changed

‎src/fsharp/InfoReader.fs‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,17 @@ let rec GetImmediateIntrinsicMethInfosOfTypeAux (optFilter,ad) g amap m origTy m
6868
mdefs|> List.map(fun mdef-> MethInfo.CreateILMeth(amap, m, origTy, mdef))
6969

7070
| FSharpOrArrayOrByrefOrTupleOrExnTypeMetadata->
71-
// Tuple types also support theproperties Item1-8,Rest from the compiled tuple type
71+
// Tuple types also support themethods get_Item1-8,get_Rest from the compiled tuple type.
7272
// In this case convert to the .NET Tuple type that carries metadata and try again
7373
if isAnyTupleTy g metadataTythen
7474
letbetterMetadataTy= helpEnsureTypeHasMetadata g metadataTy
7575
GetImmediateIntrinsicMethInfosOfTypeAux(optFilter,ad) g amap m origTy betterMetadataTy
76+
// Function types support methods FSharpFunc<_,_>.FromConverter and friends from .NET metadata,
77+
// but not instance methods (you can't write "f.Invoke(x)", you have to write "f x")
78+
elif isFunTy g metadataTythen
79+
letbetterMetadataTy= helpEnsureTypeHasMetadata g metadataTy
80+
GetImmediateIntrinsicMethInfosOfTypeAux(optFilter,ad) g amap m origTy betterMetadataTy
81+
|> List.filter(fun minfo->not minfo.IsInstance)
7682
else
7783
match tryDestAppTy g metadataTywith
7884
| None->[]
@@ -158,7 +164,7 @@ let rec GetImmediateIntrinsicPropInfosOfTypeAux (optFilter,ad) g amap m origTy m
158164
| FSharpOrArrayOrByrefOrTupleOrExnTypeMetadata->
159165
// Tuple types also support the properties Item1-8, Rest from the compiled tuple type
160166
// In this case convert to the .NET Tuple type that carries metadata and try again
161-
if isAnyTupleTy g metadataTythen
167+
if isAnyTupleTy g metadataTy|| isFunTy g metadataTythen
162168
letbetterMetadataTy= helpEnsureTypeHasMetadata g metadataTy
163169
GetImmediateIntrinsicPropInfosOfTypeAux(optFilter,ad) g amap m origTy betterMetadataTy
164170
else
@@ -456,9 +462,9 @@ let rec GetIntrinsicConstructorInfosOfTypeAux (infoReader:InfoReader) m origTy m
456462
|> List.map(fun mdef-> MethInfo.CreateILMeth(amap, m, origTy, mdef))
457463

458464
| FSharpOrArrayOrByrefOrTupleOrExnTypeMetadata->
459-
// Tuple types also supportthe properties Item1-8, Rest fromthecompiled tuple type
460-
// In this case convert to the.NET Tuple type that carries metadata and try again
461-
if isAnyTupleTy g metadataTythen
465+
// Tuple types also supportconstructors. In this case convert tothe.NET Tuple type that carries metadata and try again
466+
//Function types also support constructors.In this case convert to theFSharpFunc type that carries metadata and try again
467+
if isAnyTupleTy g metadataTy|| isFunTy g metadataTythen
462468
letbetterMetadataTy= helpEnsureTypeHasMetadata g metadataTy
463469
GetIntrinsicConstructorInfosOfTypeAux infoReader m origTy betterMetadataTy
464470
else

‎src/fsharp/TastOps.fs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -799,9 +799,11 @@ let helpEnsureTypeHasMetadata g ty =
799799
if isAnyTupleTy g tythen
800800
let(tupInfo,tupElemTys)= destAnyTupleTy g ty
801801
mkOuterCompiledTupleTy g(evalTupInfoIsStruct tupInfo) tupElemTys
802+
elif isFunTy g tythen
803+
let(a,b)= destFunTy g ty
804+
mkAppTy g.fastFunc_tcr[a; b]
802805
else ty
803-
804-
806+
805807
//---------------------------------------------------------------------------
806808
// Equivalence of types up to alpha-equivalence
807809
//---------------------------------------------------------------------------

‎src/fsharp/TypeChecker.fs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13285,6 +13285,7 @@ module MutRecBindingChecking =
1328513285
// Phase2B: typecheck the argument to an 'inherits' call and build the new object expr for the inherit-call
1328613286
| Phase2AInherit (synBaseTy, arg, baseValOpt, m) ->
1328713287
let baseTy, tpenv = TcType cenv NoNewTypars CheckCxs ItemOccurence.Use envInstance tpenv synBaseTy
13288+
let baseTy = baseTy |> helpEnsureTypeHasMetadata cenv.g
1328813289
let inheritsExpr, tpenv = TcNewExpr cenv envInstance tpenv baseTy (Some synBaseTy.Range) true arg m
1328913290
let envInstance = match baseValOpt with Some baseVal -> AddLocalVal cenv.tcSink scopem baseVal envInstance | None -> envInstance
1329013291
let envNonRec = match baseValOpt with Some baseVal -> AddLocalVal cenv.tcSink scopem baseVal envNonRec | None -> envNonRec
@@ -15093,6 +15094,15 @@ module EstablishTypeDefinitionCores =
1509315094
| SynTypeDefnSimpleRepr.Enum _ ->
1509415095
Some(cenv.g.system_Enum_typ)
1509515096

15097+
// Allow super type to be a function type but convert back to FSharpFunc<A,B> to make sure it has metadata
15098+
// (We don't apply the same rule to tuple types, i.e. no F#-declared inheritors of those are permitted)
15099+
let super =
15100+
super |> Option.map (fun ty ->
15101+
if isFunTy cenv.g ty then
15102+
let (a,b) = destFunTy cenv.g ty
15103+
mkAppTy cenv.g.fastFunc_tcr [a; b]
15104+
else ty)
15105+
1509615106
// Publish the super type
1509715107
tycon.TypeContents.tcaug_super <- super
1509815108

‎tests/fsharp/core/subtype/test.fsx‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1873,6 +1873,51 @@ module InferenceRegression4040C =
18731873
printfn"%A"(Foo.Test42)
18741874

18751875

1876+
moduleTestInheritFunc=
1877+
typeFoo()=
1878+
inherit FSharpFunc<int,int>()
1879+
override__.Invoke(a:int)= a+1
1880+
1881+
check"cnwcki1"((Foo()|> box|> unbox<int-> int>)5)6
1882+
1883+
moduleTestInheritFuncGeneric=
1884+
typeFoo<'T,'U>()=
1885+
inherit FSharpFunc<'T,'T>()
1886+
override__.Invoke(a:'T)= a
1887+
1888+
check"cnwcki2"((Foo<int,int>()|> box|> unbox<int-> int>)5)5
1889+
1890+
1891+
moduleTestInheritFunc2=
1892+
typeFoo()=
1893+
inherit OptimizedClosures.FSharpFunc<int,int,int>()
1894+
overridef.Invoke(a:int)=(fun u-> f.Invoke(a,u))
1895+
override__.Invoke(a:int,b:int)= a+ b+1
1896+
1897+
check"cnwcki3"((Foo()|> box|> unbox<int-> int-> int>)56)12
1898+
1899+
moduleTestInheritFunc3=
1900+
typeFoo()=
1901+
inherit OptimizedClosures.FSharpFunc<int,int,int,int>()
1902+
overridef.Invoke(t)=(fun u v-> f.Invoke(t,u,v))
1903+
override__.Invoke(a:int,b:int,c:int)= a+ b+ c+1
1904+
1905+
check"cnwcki4"((Foo()|> box|> unbox<int-> int-> int-> int>)567)19
1906+
1907+
#if!NETCOREAPP1_0
1908+
1909+
moduleTestConverter=
1910+
openSystem
1911+
1912+
letfromConverter(f:Converter<'T1,'X>)= FSharp.Core.FSharpFunc.FromConverter f
1913+
letimplicitConv(f:Converter<'T1,'X>)= FSharp.Core.FSharpFunc.op_Implicit f
1914+
lettoConverter(f:'T1->'X)= FSharp.Core.FSharpFunc.ToConverter f
1915+
lettoConverter2(f:FSharpFunc<'T1,'X>)= FSharp.Core.FSharpFunc.ToConverter f
1916+
1917+
test"cenwceoiwe1"((id|> toConverter|> fromConverter)6=6)
1918+
test"cenwceoiwe2"((id|> toConverter|> fromConverter|> toConverter2|> implicitConv)6=6)
1919+
#endif
1920+
18761921

18771922
#if TESTS_AS_APP
18781923
letRUN()=!failures

‎tests/fsharpqa/Source/Diagnostics/General/E_AreYouMissingAnArgumentToAFunction01.fs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// #Regression #Diagnostics
22
// Regression test for FSHARP1.0:2804
33
// Make sure we don't emit ?. (notice that the error message changed a bit since the bug was opened)
4-
//<Expects status="error" span="(6,32-6,37)" id="FS0001">Expecting a type supporting the operator 'op_Explicit' but given a function type\. You may be missing an argument to a function\.$</Expects>
4+
//<Expects status="error" span="(7,32-7,33)" id="FS0001">This expression was expected to have type</Expects>
5+
//<Expects status="error" span="(7,35-7,37)" id="FS0001">This expression was expected to have type</Expects>
56

67
letf(x:int list)= int32(x>>32)
78

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp