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

Commit6cbf78a

Browse files
smoothdeveloperKevinRansom
authored andcommitted
Fix for generic constraint evaluation error in FSI (#2590)
* Add a test to reproduce the issue* proposed fix for #2411* do not try to check non nominal argument types* move local functions out (temporarily)* try other fsimode???* * try to move back local functions, adding type annotation might help with proto compiler* disable test through pipe, no clue why it fails in fsharqa, works locally* fix naming for policheck
1 parent4a43f48 commit6cbf78a

File tree

5 files changed

+63
-7
lines changed

5 files changed

+63
-7
lines changed

‎src/absil/ilreflect.fs‎

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -662,9 +662,8 @@ let convFieldSpec cenv emEnv fspec =
662662
//----------------------------------------------------------------------------
663663
// convMethodRef
664664
//----------------------------------------------------------------------------
665-
666665
letqueryableTypeGetMethodBySearch cenv emEnv parentT(mref:ILMethodRef)=
667-
assert(not(typeIsNotQueryable(parentT)));
666+
assert(not(typeIsNotQueryable(parentT)))
668667
letcconv=(if mref.CallingConv.IsStaticthen BindingFlags.Staticelse BindingFlags.Instance)
669668
letmethInfos= parentT.GetMethods(cconv||| BindingFlags.Public||| BindingFlags.NonPublic)|> Array.toList
670669
(* First, filter on name, if unique, then binding "done"*)
@@ -675,24 +674,58 @@ let queryableTypeGetMethodBySearch cenv emEnv parentT (mref:ILMethodRef) =
675674
methInfo
676675
|_->
677676
(* Second, type match. Note type erased (non-generic) F# code would not type match but they have unique names*)
677+
678+
letsatisfiesParameter(a:Type option)(p:Type)=
679+
match awith
680+
| None->true
681+
| Some a->
682+
if
683+
// obvious case
684+
p.IsAssignableFrom a
685+
thentrue
686+
elif
687+
// both are generic
688+
p.IsGenericType&& a.IsGenericType
689+
// non obvious due to contravariance: Action<T> where T : IFoo accepts Action<FooImpl> (for FooImpl : IFoo)
690+
&& p.GetGenericTypeDefinition().IsAssignableFrom(a.GetGenericTypeDefinition())
691+
thentrue
692+
elsefalse
693+
694+
letsatisfiesAllParameters(args:Type option array)(ps:Type array)=
695+
if Array.length args<> Array.length psthenfalse
696+
else Array.forall2 satisfiesParameter args ps
697+
678698
letselect(methInfo:MethodInfo)=
679699
(* mref implied Types*)
680700
letmtyargTIs= getGenericArgumentsOfMethod methInfo
701+
681702
if mtyargTIs.Length<> mref.GenericAritythenfalse(* method generic arity mismatch*)else
703+
704+
(* methInfo implied Types*)
705+
letmethodParameters= methInfo.GetParameters()
706+
letargTypes= mref.ArgTypes|> List.toArray
707+
if argTypes.Length<> methodParameters.Lengththenfalse(* method argument length mismatch*)else
708+
709+
lethaveArgTs= methodParameters|> Array.map(fun param-> param.ParameterType)
710+
letmrefParameterTypes= argTypes|> Array.map(fun t->if t.IsNominalthen Some(convTypeRefAux cenv t.TypeRef)else None)
711+
712+
// we should reject methods which don't satisfy parameter types by also checking
713+
// type parameters which can be contravariant for delegates for example
714+
// see https://github.com/Microsoft/visualfsharp/issues/2411
715+
// without this check, subsequent call to convTypes would fail because it
716+
// constructs generic type without checking constraints
717+
ifnot(satisfiesAllParameters mrefParameterTypes haveArgTs)thenfalseelse
718+
682719
letargTs,resT=
683720
letemEnv= envPushTyvars emEnv(Array.append tyargTs mtyargTIs)
684721
letargTs= convTypes cenv emEnv mref.ArgTypes
685722
letresT= convType cenv emEnv mref.ReturnType
686723
argTs,resT
687724

688-
(* methInfo implied Types*)
689-
letmethodParameters= methInfo.GetParameters()
690-
691725
lethaveResT= methInfo.ReturnType
692726
(* check for match*)
693727
if argTs.Length<> methodParameters.Lengththenfalse(* method argument length mismatch*)else
694-
lethaveArgTs= methodParameters|> Array.map(fun param-> param.ParameterType)|> Array.toList
695-
letres= equalTypes resT haveResT&& equalTypeLists argTs haveArgTs
728+
letres= equalTypes resT haveResT&& equalTypeLists argTs(haveArgTs|> Array.toList)
696729
res
697730

698731
match List.tryFind select methInfoswith
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#r"lib.dll"
2+
3+
openSystem.Reflection
4+
5+
typeFezImpl()=
6+
interface IFez
7+
typeBarImpl()=
8+
interface IBar
9+
Fez.Do<FezImpl>("",(fun _->()))
10+
Fez.Do<BarImpl>("",(fun _->()))// produced internal error: https://github.com/Microsoft/visualfsharp/issues/2411
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SOURCE=app.fsx COMPILE_ONLY=1 FSIMODE=EXEC SCFLAGS="--nologo" PRECMD="csc -t:library lib.cs"# mode exec: used to produce internal error
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
usingSystem;
2+
publicdelegatevoidFezHandler<T>(Tobj)whereT:IFez;
3+
publicdelegatevoidBarHandler<T>(Tobj)whereT:IBar;
4+
publicinterfaceIFez{}
5+
6+
publicinterfaceIBar{}
7+
publicclassFez
8+
{
9+
publicstaticvoidDo<T>(strings,FezHandler<T>a)whereT:IFez{System.Console.WriteLine("dooifez");}
10+
publicstaticvoidDo<T>(strings,BarHandler<T>b)whereT:IBar{System.Console.WriteLine("dooibar");}
11+
}

‎tests/fsharpqa/Source/test.lst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ Misc01Globalization
251251
Misc01,NoMTImport
252252
Misc01,NoMT..\..\..\testsprivate\fsharpqa\Source\InteractiveSession\AssemblyLoading
253253
Misc01,NoMTInteractiveSession\Misc
254+
Misc01,NoMTInteractiveSession\Misc\GenericConstraintWoes\issue2411
254255
Misc01Libraries\Control
255256
Misc01Libraries\Core\collections
256257
Misc01Libraries\Core\ExtraTopLevelOperators

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp