From cd1b71791bb3af0948c8f73f402e0bde41170d65 Mon Sep 17 00:00:00 2001 From: Eugene Auduchinok Date: Wed, 6 Dec 2023 13:55:59 +0100 Subject: [PATCH 1/5] Checker/patterns: recover on unifying union types --- src/Compiler/Checking/CheckPatterns.fs | 11 ++++++--- tests/service/PatternMatchCompilationTests.fs | 24 +++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/Compiler/Checking/CheckPatterns.fs b/src/Compiler/Checking/CheckPatterns.fs index 741d6c8f8fc..00229e5eb4b 100644 --- a/src/Compiler/Checking/CheckPatterns.fs +++ b/src/Compiler/Checking/CheckPatterns.fs @@ -579,9 +579,14 @@ and ApplyUnionCaseOrExn m (cenv: cenv) env overallTy item = CheckUnionCaseAccessible cenv.amap m ad ucref |> ignore let resTy = actualResultTyOfUnionCase ucinfo.TypeInst ucref let inst = mkTyparInst ucref.TyconRef.TyparsNoRange ucinfo.TypeInst - UnifyTypes cenv env m overallTy resTy - let mkf mArgs args = TPat_unioncase(ucref, ucinfo.TypeInst, args, unionRanges m mArgs) - mkf, actualTysOfUnionCaseFields inst ucref, [ for f in ucref.AllFieldsAsList -> f] + let mkf = + try + UnifyTypes cenv env m overallTy resTy + fun mArgs args -> TPat_unioncase(ucref, ucinfo.TypeInst, args, unionRanges m mArgs) + with RecoverableException e -> + errorRecovery e m + fun _ _ -> TPat_error m + mkf, actualTysOfUnionCaseFields inst ucref, [ for f in ucref.AllFieldsAsList -> f ] | _ -> invalidArg "item" "not a union case or exception reference" diff --git a/tests/service/PatternMatchCompilationTests.fs b/tests/service/PatternMatchCompilationTests.fs index 735d4635fab..a7b607d4577 100644 --- a/tests/service/PatternMatchCompilationTests.fs +++ b/tests/service/PatternMatchCompilationTests.fs @@ -187,6 +187,30 @@ match None with dumpDiagnostics checkResults |> shouldEqual [ ] +[] +let ``Union case 10 - Wrong type`` () = + let _, checkResults = getParseAndCheckResults """ +match Some 1 with +| Some(Some "") as a -> a |> ignore +""" + assertHasSymbolUsages ["a"] checkResults + dumpDiagnostics checkResults |> shouldEqual [ + "(3,7--3,14): This expression was expected to have type\u001d 'int' \u001dbut here has type\u001d ''a option'" + "(2,6--2,12): Incomplete pattern matches on this expression." + ] + +[] +let ``Union case 11 - Wrong type`` () = + let _, checkResults = getParseAndCheckResults """ +match Some 1 with +| Some(Some("", i)) as a -> a, i |> ignore +""" + assertHasSymbolUsages ["a"; "i"] checkResults + dumpDiagnostics checkResults |> shouldEqual [ + "(3,7--3,18): This expression was expected to have type\u001d 'int' \u001dbut here has type\u001d ''a option'"; + "(2,6--2,12): Incomplete pattern matches on this expression." + ] + [] #if !NETCOREAPP [] From c6f12d7245b273403853f404bf19ce1781337c07 Mon Sep 17 00:00:00 2001 From: Eugene Auduchinok Date: Wed, 6 Dec 2023 14:38:27 +0100 Subject: [PATCH 2/5] Update baselines --- .../ObjectExpressions/ObjectExpressions.fs | 16 ++++++++++++---- .../PatternMatching/ConsList/ConsList.fs | 8 ++++++-- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/tests/FSharp.Compiler.ComponentTests/Conformance/Expressions/ObjectExpressions/ObjectExpressions.fs b/tests/FSharp.Compiler.ComponentTests/Conformance/Expressions/ObjectExpressions/ObjectExpressions.fs index f45e1bf1f45..6ef849136ae 100644 --- a/tests/FSharp.Compiler.ComponentTests/Conformance/Expressions/ObjectExpressions/ObjectExpressions.fs +++ b/tests/FSharp.Compiler.ComponentTests/Conformance/Expressions/ObjectExpressions/ObjectExpressions.fs @@ -116,12 +116,20 @@ let implSomeDU = |> typecheck |> shouldFail |> withDiagnostics [ - (Error 1, Line 31, Col 15, Line 31, Col 18, "This expression was expected to have type + Error 1, Line 31, Col 15, Line 31, Col 18, "This expression was expected to have type 'AsString' but here has type - 'SomeDu' ") - ] - + 'SomeDu' " + Error 1, Line 32, Col 15, Line 32, Col 18, "This expression was expected to have type + 'AsString' +but here has type + 'SomeDu' " + Error 1, Line 33, Col 15, Line 33, Col 18, "This expression was expected to have type + 'AsString' +but here has type + 'SomeDu' " + Warning 25, Line 30, Col 19, Line 30, Col 23, "Incomplete pattern matches on this expression."] + [] let ``Object expression implementing multiple interfaces`` () = Fsx """ diff --git a/tests/FSharp.Compiler.ComponentTests/Conformance/PatternMatching/ConsList/ConsList.fs b/tests/FSharp.Compiler.ComponentTests/Conformance/PatternMatching/ConsList/ConsList.fs index 2bae41654a8..d0f92838187 100644 --- a/tests/FSharp.Compiler.ComponentTests/Conformance/PatternMatching/ConsList/ConsList.fs +++ b/tests/FSharp.Compiler.ComponentTests/Conformance/PatternMatching/ConsList/ConsList.fs @@ -25,10 +25,14 @@ module ConsList = |> typecheck |> shouldFail |> withDiagnostics [ - (Error 1, Line 4, Col 21, Line 4, Col 28, "This expression was expected to have type + Error 1, Line 4, Col 21, Line 4, Col 28, "This expression was expected to have type 'int' but here has type - ''a list' ") + ''a list' " + Error 1, Line 5, Col 21, Line 5, Col 33, "This expression was expected to have type + 'int' +but here has type + ''a list' " ] // This test was automatically generated (moved from FSharpQA suite - Conformance/PatternMatching/ConsList) From a9715cf51ade6dab9ca7eaa8b8940a7ac03244e3 Mon Sep 17 00:00:00 2001 From: Eugene Auduchinok Date: Wed, 6 Dec 2023 15:37:00 +0100 Subject: [PATCH 3/5] Baselines --- tests/fsharp/typecheck/sigs/neg103.bsl | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/fsharp/typecheck/sigs/neg103.bsl b/tests/fsharp/typecheck/sigs/neg103.bsl index bd0e3db1a8a..e371cbd64c3 100644 --- a/tests/fsharp/typecheck/sigs/neg103.bsl +++ b/tests/fsharp/typecheck/sigs/neg103.bsl @@ -4,6 +4,8 @@ neg103.fs(7,12,7,22): typecheck error FS0001: This expression was expected to ha but here has type 'string' +neg103.fs(11,5,11,11): typecheck error FS0025: Incomplete pattern matches on this expression. + neg103.fs(12,7,12,15): typecheck error FS0001: This expression was expected to have type 'int' but here has type @@ -14,6 +16,12 @@ neg103.fs(17,7,17,15): typecheck error FS0001: This expression was expected to h but here has type 'MyUnion' +neg103.fs(12,18,12,23): typecheck error FS0001: This expression was expected to have type + +neg103.fs(12,26,12,34): typecheck error FS0001: This expression was expected to have type + +neg103.fs(15,5,15,11): typecheck error FS0025: Incomplete pattern matches on this expression. + neg103.fs(21,7,21,9): typecheck error FS0001: This expression was expected to have type 'Async' but here has type @@ -21,7 +29,13 @@ but here has type neg103.fs(20,5,20,11): typecheck error FS0025: Incomplete pattern matches on this expression. +neg103.fs(24,9,24,15): typecheck error FS0025: Incomplete pattern matches on this expression. + neg103.fs(25,11,25,19): typecheck error FS0001: This expression was expected to have type 'int' but here has type 'MyUnion' + +neg103.fs(25,22,25,27): typecheck error FS0001: This expression was expected to have type + +neg103.fs(25,30,25,38): typecheck error FS0001: This expression was expected to have type From 937fa3cdba9389c5a2a15b57d33a9b83c809eabc Mon Sep 17 00:00:00 2001 From: Eugene Auduchinok Date: Wed, 6 Dec 2023 16:27:41 +0100 Subject: [PATCH 4/5] Baselines --- tests/fsharp/typecheck/sigs/neg103.vsbsl | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/fsharp/typecheck/sigs/neg103.vsbsl b/tests/fsharp/typecheck/sigs/neg103.vsbsl index bd0e3db1a8a..7bb76aecb8d 100644 --- a/tests/fsharp/typecheck/sigs/neg103.vsbsl +++ b/tests/fsharp/typecheck/sigs/neg103.vsbsl @@ -4,11 +4,19 @@ neg103.fs(7,12,7,22): typecheck error FS0001: This expression was expected to ha but here has type 'string' +neg103.fs(11,5,11,11): typecheck error FS0025: Incomplete pattern matches on this expression. + neg103.fs(12,7,12,15): typecheck error FS0001: This expression was expected to have type 'int' but here has type 'MyUnion' +neg103.fs(12,18,12,23): typecheck error FS0001: This expression was expected to have type + +neg103.fs(12,26,12,34): typecheck error FS0001: This expression was expected to have type + +neg103.fs(15,5,15,11): typecheck error FS0025: Incomplete pattern matches on this expression. + neg103.fs(17,7,17,15): typecheck error FS0001: This expression was expected to have type 'int' but here has type @@ -21,7 +29,13 @@ but here has type neg103.fs(20,5,20,11): typecheck error FS0025: Incomplete pattern matches on this expression. +neg103.fs(24,9,24,15): typecheck error FS0025: Incomplete pattern matches on this expression. + neg103.fs(25,11,25,19): typecheck error FS0001: This expression was expected to have type 'int' but here has type 'MyUnion' + +neg103.fs(25,22,25,27): typecheck error FS0001: This expression was expected to have type + +neg103.fs(25,30,25,38): typecheck error FS0001: This expression was expected to have type From a90e8475dddbceaad3ca70e7c6f2dfd372f9e647 Mon Sep 17 00:00:00 2001 From: Eugene Auduchinok Date: Wed, 6 Dec 2023 17:57:52 +0100 Subject: [PATCH 5/5] Disable test on desktop due to wrong line separators --- tests/service/PatternMatchCompilationTests.fs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/service/PatternMatchCompilationTests.fs b/tests/service/PatternMatchCompilationTests.fs index a7b607d4577..04da829f524 100644 --- a/tests/service/PatternMatchCompilationTests.fs +++ b/tests/service/PatternMatchCompilationTests.fs @@ -188,6 +188,9 @@ match None with ] [] +#if !NETCOREAPP +[] +#endif let ``Union case 10 - Wrong type`` () = let _, checkResults = getParseAndCheckResults """ match Some 1 with @@ -200,6 +203,9 @@ match Some 1 with ] [] +#if !NETCOREAPP +[] +#endif let ``Union case 11 - Wrong type`` () = let _, checkResults = getParseAndCheckResults """ match Some 1 with