Skip to content

Commit

Permalink
Improve active pattern error reporting (#17666)
Browse files Browse the repository at this point in the history
* Improve active pattern error reporting

* release notes.
  • Loading branch information
edgarfgp authored Sep 6, 2024
1 parent 20e794d commit 7bfdd73
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/release-notes/.FSharp.Compiler.Service/9.0.100.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 4 additions & 4 deletions src/Compiler/Checking/Expressions/CheckExpressions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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 =
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Original file line number Diff line number Diff line change
Expand Up @@ -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)
[<Theory; Directory(__SOURCE_DIRECTORY__, Includes=[|"E_MulticasePartialNotAllowed01.fs"|])>]
Expand Down

0 comments on commit 7bfdd73

Please sign in to comment.