Skip to content

Commit cc60ed6

Browse files
dsymeKevinRansom
authored andcommitted
code cleanup prior to optional interop improvements (#7276)
* add test cases we need to make work * cleanup method call argument processing
1 parent 969a9c4 commit cc60ed6

21 files changed

Lines changed: 664 additions & 518 deletions

src/fsharp/ConstraintSolver.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1931,7 +1931,7 @@ and SolveTypeIsNonNullableValueType (csenv: ConstraintSolverEnv) ndeep m2 trace
19311931
| _ ->
19321932
let underlyingTy = stripTyEqnsAndMeasureEqns g ty
19331933
if isStructTy g underlyingTy then
1934-
if isAppTy g underlyingTy && tyconRefEq g g.system_Nullable_tcref (tcrefOfAppTy g underlyingTy) then
1934+
if isNullableTy g underlyingTy then
19351935
return! ErrorD (ConstraintSolverError(FSComp.SR.csTypeParameterCannotBeNullable(), m, m))
19361936
else
19371937
return! ErrorD (ConstraintSolverError(FSComp.SR.csGenericConstructRequiresStructType(NicePrint.minimalStringOfType denv ty), m, m2))

src/fsharp/MethodCalls.fs

Lines changed: 375 additions & 76 deletions
Large diffs are not rendered by default.

src/fsharp/TastOps.fs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -839,16 +839,6 @@ let tryNiceEntityRefOfTyOption ty =
839839
| TType_app (tcref, _) -> Some tcref
840840
| TType_measure (Measure.Con tcref) -> Some tcref
841841
| _ -> None
842-
843-
let (|NullableTy|_|) g ty =
844-
match tryAppTy g ty with
845-
| ValueSome (tcref, [tyarg]) when tyconRefEq g tcref g.system_Nullable_tcref -> Some tyarg
846-
| _ -> None
847-
848-
let (|StripNullableTy|) g ty =
849-
match tryAppTy g ty with
850-
| ValueSome (tcref, [tyarg]) when tyconRefEq g tcref g.system_Nullable_tcref -> tyarg
851-
| _ -> ty
852842

853843
let mkInstForAppTy g ty =
854844
match tryAppTy g ty with
@@ -3125,6 +3115,31 @@ let destOptionTy g ty =
31253115
| ValueSome ty -> ty
31263116
| ValueNone -> failwith "destOptionTy: not an option type"
31273117

3118+
let isNullableTy (g: TcGlobals) ty =
3119+
match tryDestAppTy g ty with
3120+
| ValueNone -> false
3121+
| ValueSome tcref -> tyconRefEq g g.system_Nullable_tcref tcref
3122+
3123+
let tryDestNullableTy g ty =
3124+
match argsOfAppTy g ty with
3125+
| [ty1] when isNullableTy g ty -> ValueSome ty1
3126+
| _ -> ValueNone
3127+
3128+
let destNullableTy g ty =
3129+
match tryDestNullableTy g ty with
3130+
| ValueSome ty -> ty
3131+
| ValueNone -> failwith "destNullableTy: not a Nullable type"
3132+
3133+
let (|NullableTy|_|) g ty =
3134+
match tryAppTy g ty with
3135+
| ValueSome (tcref, [tyarg]) when tyconRefEq g tcref g.system_Nullable_tcref -> Some tyarg
3136+
| _ -> None
3137+
3138+
let (|StripNullableTy|) g ty =
3139+
match tryDestNullableTy g ty with
3140+
| ValueSome tyarg -> tyarg
3141+
| _ -> ty
3142+
31283143
let isLinqExpressionTy g ty =
31293144
match tryDestAppTy g ty with
31303145
| ValueNone -> false

src/fsharp/TastOps.fsi

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1388,7 +1388,7 @@ val mkVoidPtrTy : TcGlobals -> TType
13881388
/// Build a single-dimensional array type
13891389
val mkArrayType : TcGlobals -> TType -> TType
13901390

1391-
/// Determine is a type is an option type
1391+
/// Determine if a type is an option type
13921392
val isOptionTy : TcGlobals -> TType -> bool
13931393

13941394
/// Take apart an option type
@@ -1397,6 +1397,15 @@ val destOptionTy : TcGlobals -> TType -> TType
13971397
/// Try to take apart an option type
13981398
val tryDestOptionTy : TcGlobals -> TType -> ValueOption<TType>
13991399

1400+
/// Determine is a type is a System.Nullable type
1401+
val isNullableTy : TcGlobals -> TType -> bool
1402+
1403+
/// Try to take apart a System.Nullable type
1404+
val tryDestNullableTy: TcGlobals -> TType -> ValueOption<TType>
1405+
1406+
/// Take apart a System.Nullable type
1407+
val destNullableTy: TcGlobals -> TType -> TType
1408+
14001409
/// Determine if a type is a System.Linq.Expression type
14011410
val isLinqExpressionTy : TcGlobals -> TType -> bool
14021411

src/fsharp/TypeChecker.fs

Lines changed: 153 additions & 400 deletions
Large diffs are not rendered by default.

src/fsharp/xlf/FSComp.txt.es.xlf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@
124124
</trans-unit>
125125
<trans-unit id="followingPatternMatchClauseHasWrongType">
126126
<source>All branches of a pattern match expression must return values of the same type as the first branch, which here is '{0}'. This branch returns a value of type '{1}'.</source>
127-
<target state="translated">Todas las ramas de una expresión de coincidencia de patrón deben devolver valores del mismo tipo. La primera rama devolvió un valor de tipo "{0}", pero esta rama devolvió un valor de tipo "\{1 \}".</target>
127+
<target state="new">All branches of a pattern match expression must return values of the same type as the first branch, which here is '{0}'. This branch returns a value of type '{1}'.</target>
128128
<note />
129129
</trans-unit>
130130
<trans-unit id="patternMatchGuardIsNotBool">

tests/fsharp/core/fsfromfsviacs/lib3.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,29 @@ public static void SomeMethod() { }
2424

2525
}
2626
}
27+
namespace CSharpOptionalParameters
28+
{
29+
// This should be preferred over the same type in lib2.cs
30+
public class SomeClass
31+
{
32+
public SomeClass() { }
33+
public static int MethodTakingOptionals(int x = 3, string y = "abc", double d = 5.0)
34+
{
35+
return x + y.Length + (int) d;
36+
}
37+
public static int MethodTakingNullableOptionalsWithDefaults(int? x = 3, string y = "abc", double? d = 5.0)
38+
{
39+
return (x.HasValue ? x.Value : -100) + y.Length + (int) (d.HasValue ? d.Value : 0.0);
40+
}
41+
public static int MethodTakingNullableOptionals(int? x = null, string y = null, double? d = null)
42+
{
43+
int length;
44+
if (y == null)
45+
length = -1;
46+
else
47+
length = y.Length;
48+
return (x.HasValue ? x.Value : -1) + length + (int) (d.HasValue ? d.Value : -1.0);
49+
}
50+
}
51+
52+
}

tests/fsharp/core/fsfromfsviacs/test.fsx

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,53 @@ let _ = test "structunion394b36" (Lib.NestedStructUnionsTests.testPattern3mut(u2
7171

7272
// F# option implicit converter tests
7373

74-
let testFsOpt() =
75-
let testOpt (t : 'T option) =
76-
test (sprintf "fsimplicitconv (%A)" t) (ApiWrapper.ConsumeOptionalParam<'T>(t) = t)
77-
78-
testOpt(Option<int>.None)
79-
testOpt(Some 42)
80-
81-
// check that implicit conversion of optionals does
82-
// differentiate between 'null' and 'Some null'
83-
testOpt(Option<string>.None)
84-
testOpt(Option<string>.Some null)
85-
testOpt(Some "")
86-
testOpt(Some "test")
87-
88-
testFsOpt()
89-
74+
module TestConsumeOptionalParameter =
75+
let testFsOpt() =
76+
let testOpt (t : 'T option) =
77+
test (sprintf "fsimplicitconv (%A)" t) (ApiWrapper.ConsumeOptionalParam<'T>(t) = t)
78+
79+
testOpt(Option<int>.None)
80+
testOpt(Some 42)
81+
82+
// check that implicit conversion of optionals does
83+
// differentiate between 'null' and 'Some null'
84+
testOpt(Option<string>.None)
85+
testOpt(Option<string>.Some null)
86+
testOpt(Some "")
87+
testOpt(Some "test")
88+
89+
testFsOpt()
90+
91+
module TestConsumeCSharpOptionalParameter =
92+
open System
93+
open CSharpOptionalParameters
94+
check "csoptional23982f31" (SomeClass.MethodTakingOptionals()) 11
95+
check "csoptional23982f32" (SomeClass.MethodTakingOptionals(x = 6)) 14
96+
check "csoptional23982f33" (SomeClass.MethodTakingOptionals(y = "aaaaaa")) 14
97+
check "csoptional23982f34" (SomeClass.MethodTakingOptionals(d = 8.0)) 14
98+
99+
check "csoptional23982f41" (SomeClass.MethodTakingNullableOptionalsWithDefaults()) 11
100+
check "csoptional23982f42" (SomeClass.MethodTakingNullableOptionalsWithDefaults(x = Nullable 6)) 14
101+
check "csoptional23982f43" (SomeClass.MethodTakingNullableOptionalsWithDefaults(y = "aaaaaa")) 14
102+
check "csoptional23982f44" (SomeClass.MethodTakingNullableOptionalsWithDefaults(d = Nullable 8.0)) 14
103+
104+
check "csoptional23982f51" (SomeClass.MethodTakingNullableOptionals()) -3
105+
check "csoptional23982f52" (SomeClass.MethodTakingNullableOptionals(x = Nullable 6)) 4
106+
check "csoptional23982f53" (SomeClass.MethodTakingNullableOptionals(y = "aaaaaa")) 4
107+
check "csoptional23982f54" (SomeClass.MethodTakingNullableOptionals(d = Nullable 8.0)) 6
108+
109+
// These require https://github.com/fsharp/fslang-suggestions/issues/774 to be implemented
110+
//check "csoptional23982f3no" (SomeClass.SomeMethod(?x = Some 6)) 14
111+
//check "csoptional23982f3no" (SomeClass.SomeMethod(?y = Some "aaaaaa")) 14
112+
//check "csoptional23982f3no" (SomeClass.SomeMethod(?d = Some 8.0)) 14
113+
//check "csoptional23982f3no" (SomeClass.SomeMethod(?x = None)) 11
114+
//check "csoptional23982f3no" (SomeClass.SomeMethod(?y = None)) 11
115+
//check "csoptional23982f3no" (SomeClass.SomeMethod(?d = None)) 11
116+
117+
//check "csoptional23982f42" (SomeClass.MethodTakingNullableOptionalsWithDefaults(x = 6)) 14
118+
//check "csoptional23982f44" (SomeClass.MethodTakingNullableOptionalsWithDefaults(d = 8.0)) 14
119+
//check "csoptional23982f52" (SomeClass.MethodTakingNullableOptionals(x = 6)) 4
120+
//check "csoptional23982f54" (SomeClass.MethodTakingNullableOptionals(d = 8.0)) 6
90121

91122
module NestedStructPatternMatchingAcrossAssemblyBoundaries =
92123
open Lib.NestedStructUnionsTests

vsintegration/src/FSharp.ProjectSystem.PropertyPages/Resources/xlf/Microsoft.VisualStudio.Editors.Designer.cs.xlf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1828,8 +1828,9 @@ CONSIDER: get this from CodeDom</note>
18281828
</trans-unit>
18291829
<trans-unit id="RSE_GraphicSizeFormat">
18301830
<source>{0} x {1}</source>
1831-
<target state="translated">{0} x {1}</target>
1831+
<target state="needs-review-translation">{0} x {1}</target>
18321832
<note>Format string for showing a graphic's size
1833+
18331834
# {0} = width (as an integer)
18341835
# {1} = height (as an integer)
18351836
#Example, for a bitmap of width=123, height = 456, the English version of this string would be "123x456"</note>

vsintegration/src/FSharp.ProjectSystem.PropertyPages/Resources/xlf/Microsoft.VisualStudio.Editors.Designer.de.xlf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1828,8 +1828,9 @@ CONSIDER: get this from CodeDom</note>
18281828
</trans-unit>
18291829
<trans-unit id="RSE_GraphicSizeFormat">
18301830
<source>{0} x {1}</source>
1831-
<target state="translated">{0} x {1}</target>
1831+
<target state="needs-review-translation">{0} x {1}</target>
18321832
<note>Format string for showing a graphic's size
1833+
18331834
# {0} = width (as an integer)
18341835
# {1} = height (as an integer)
18351836
#Example, for a bitmap of width=123, height = 456, the English version of this string would be "123x456"</note>

0 commit comments

Comments
 (0)