From 7bfdd73a8e0acdce0ac6a38a765f8d06403e7c4d Mon Sep 17 00:00:00 2001 From: Edgar Gonzalez Date: Fri, 6 Sep 2024 16:55:34 +0100 Subject: [PATCH] Improve active pattern error reporting (#17666) * Improve active pattern error reporting * release notes. --- .../release-notes/.FSharp.Compiler.Service/9.0.100.md | 1 + src/Compiler/Checking/Expressions/CheckExpressions.fs | 8 ++++---- .../PatternMatching/Named/E_LargeActivePat01.fs | 11 +++++++++++ .../Conformance/PatternMatching/Named/Named.fs | 6 ++++-- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/docs/release-notes/.FSharp.Compiler.Service/9.0.100.md b/docs/release-notes/.FSharp.Compiler.Service/9.0.100.md index 0eeea7c5e6a..2f370f86aef 100644 --- a/docs/release-notes/.FSharp.Compiler.Service/9.0.100.md +++ b/docs/release-notes/.FSharp.Compiler.Service/9.0.100.md @@ -45,6 +45,7 @@ * Better error reporting for unions with duplicated fields. ([PR #17521](https://github.com/dotnet/fsharp/pull/17521)) * Better error reporting for let bindings. ([PR #17601](https://github.com/dotnet/fsharp/pull/17601)) * Optimize ILTypeDef interface impls reading from metadata. ([PR #17382](https://github.com/dotnet/fsharp/pull/17382)) +* Better error reporting for active patterns. ([PR #17666](https://github.com/dotnet/fsharp/pull/17666)) ### Breaking Changes diff --git a/src/Compiler/Checking/Expressions/CheckExpressions.fs b/src/Compiler/Checking/Expressions/CheckExpressions.fs index d46eb657ee1..98ae7580eba 100644 --- a/src/Compiler/Checking/Expressions/CheckExpressions.fs +++ b/src/Compiler/Checking/Expressions/CheckExpressions.fs @@ -8674,14 +8674,14 @@ and TcUnionCaseOrExnCaseOrActivePatternResultItemThen (cenv: cenv) overallTy env let ucaseAppTy = NewInferenceType g let mkConstrApp, argTys, argNames = match item with - | Item.ActivePatternResult(apinfo, _apOverallTy, n, _) -> + | Item.ActivePatternResult(apinfo, _apOverallTy, n, m) -> let aparity = apinfo.ActiveTags.Length match aparity with | 0 | 1 -> let mkConstrApp _mArgs = function [arg] -> arg | _ -> error(InternalError("ApplyUnionCaseOrExn", mItem)) mkConstrApp, [ucaseAppTy], [ for s, m in apinfo.ActiveTagsWithRanges -> mkSynId m s ] | _ -> - let ucref = mkChoiceCaseRef g mItem aparity n + let ucref = mkChoiceCaseRef g m aparity n let _, _, tinst, _ = FreshenTyconRef2 g mItem ucref.TyconRef let ucinfo = UnionCaseInfo (tinst, ucref) ApplyUnionCaseOrExnTypes mItem cenv env ucaseAppTy (Item.UnionCase(ucinfo, false)) @@ -11059,7 +11059,7 @@ and TcNormalizedBinding declKind (cenv: cenv) env tpenv overallTy safeThisValOpt else rhsExprChecked match apinfoOpt with - | Some (apinfo, apOverallTy, _) -> + | Some (apinfo, apOverallTy, m) -> let activePatResTys = NewInferenceTypes g apinfo.ActiveTags let _, apReturnTy = stripFunTy g apOverallTy let apRetTy = @@ -11080,7 +11080,7 @@ and TcNormalizedBinding declKind (cenv: cenv) env tpenv overallTy safeThisValOpt checkLanguageFeatureError g.langVersion LanguageFeature.StructActivePattern mBinding | ActivePatternReturnKind.RefTypeWrapper -> () - UnifyTypes cenv env mBinding (apinfo.ResultType g rhsExpr.Range activePatResTys apRetTy) apReturnTy + UnifyTypes cenv env mBinding (apinfo.ResultType g m activePatResTys apRetTy) apReturnTy | None -> if isStructRetTy then diff --git a/tests/FSharp.Compiler.ComponentTests/Conformance/PatternMatching/Named/E_LargeActivePat01.fs b/tests/FSharp.Compiler.ComponentTests/Conformance/PatternMatching/Named/E_LargeActivePat01.fs index 72e8b829968..ca65356eff9 100644 --- a/tests/FSharp.Compiler.ComponentTests/Conformance/PatternMatching/Named/E_LargeActivePat01.fs +++ b/tests/FSharp.Compiler.ComponentTests/Conformance/PatternMatching/Named/E_LargeActivePat01.fs @@ -5,3 +5,14 @@ let (|One|Two|Three|Four|Five|Six|Seven|Eight|) x = One +let (|A|B|C|D|E|F|G|H|) x = + match x with + | 0 -> A + | 1 -> B + | 2 -> C + | 3 -> D + | 4 -> E + | 5 -> F + | 6 -> G + | _ -> H + diff --git a/tests/FSharp.Compiler.ComponentTests/Conformance/PatternMatching/Named/Named.fs b/tests/FSharp.Compiler.ComponentTests/Conformance/PatternMatching/Named/Named.fs index 1a542bf6cca..8f7d5a12362 100644 --- a/tests/FSharp.Compiler.ComponentTests/Conformance/PatternMatching/Named/Named.fs +++ b/tests/FSharp.Compiler.ComponentTests/Conformance/PatternMatching/Named/Named.fs @@ -470,8 +470,10 @@ but here has type |> withOptions ["--test:ErrorRanges"] |> typecheck |> shouldFail - |> withSingleDiagnostic (Error 265, Line 6, Col 53, Line 6, Col 56, "Active patterns cannot return more than 7 possibilities") - + |> withDiagnostics [ + (Error 265, Line 6, Col 6, Line 6, Col 47, "Active patterns cannot return more than 7 possibilities") + (Error 265, Line 8, Col 6, Line 8, Col 23, "Active patterns cannot return more than 7 possibilities") + ] // This test was automatically generated (moved from FSharpQA suite - Conformance/PatternMatching/Named) []