Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error reporting for CE match! #17789

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/release-notes/.FSharp.Compiler.Service/9.0.200.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
* Make ILTypeDef interface impls calculation lazy. ([PR #17392](https://github.com/dotnet/fsharp/pull/17392))
* Better ranges for CE `let!` and `use!` error reporting. ([PR #17712](https://github.com/dotnet/fsharp/pull/17712))
* Better ranges for CE `do!` error reporting. ([PR #17779](https://github.com/dotnet/fsharp/pull/17779))
* Better ranges for CE `match!`. ([PR #17789](https://github.com/dotnet/fsharp/pull/17789))

### Breaking Changes
2 changes: 1 addition & 1 deletion src/Compiler/Checking/Expressions/CheckExpressions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6004,7 +6004,7 @@ and TcExprUndelayed (cenv: cenv) (overallTy: OverallTy) env tpenv (synExpr: SynE
error(Error(FSComp.SR.tcConstructRequiresSequenceOrComputations(), m))

| SynExpr.DoBang (trivia = { DoBangKeyword = m })
| SynExpr.MatchBang (range = m)
| SynExpr.MatchBang (trivia = { MatchBangKeyword = m })
| SynExpr.WhileBang (range = m)
| SynExpr.LetOrUseBang (trivia = { LetOrUseBangKeyword = m }) ->
error(Error(FSComp.SR.tcConstructRequiresComputationExpression(), m))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,4 +442,55 @@ query {
|> shouldFail
|> withDiagnostics [
(Error 3143, Line 4, Col 5, Line 4, Col 9, "'let!', 'use!' and 'do!' expressions may not be used in queries")
]

[<Fact>]
let ``This control construct may only be used if the computation expression builder defines a 'Bind' method(match!)`` () =
Fsx """
module Result =
let zip x1 x2 =
match x1,x2 with
| Ok x1res, Ok x2res -> Ok (x1res, x2res)
| Error e, _ -> Error e
| _, Error e -> Error e

type ResultBuilder() =
member _.MergeSources(t1: Result<'T,'U>, t2: Result<'T1,'U>) = Result.zip t1 t2
member _.BindReturn(x: Result<'T,'U>, f) = Result.map f x
member _.Delay(f) = f()

member _.TryWith(r: Result<'T,'U>, f) =
match r with
| Ok x -> Ok x
| Error e -> f e

let result = ResultBuilder()

let run r2 r3 =
result {
match! r2 with
| Ok x -> return x
| Error e -> return e
}
"""
|> ignoreWarnings
|> typecheck
|> shouldFail
|> withDiagnostics [
(Error 708, Line 23, Col 9, Line 23, Col 15, "This control construct may only be used if the computation expression builder defines a 'Bind' method")
]

[<Fact>]
let ``This construct may only be used within computation expressions(match!)`` () =
Fsx """
let run r2 r3 =
match! r2 with
| Ok x -> x
| Error e -> e
"""
|> ignoreWarnings
|> typecheck
|> shouldFail
|> withDiagnostics [
(Error 750, Line 3, Col 5, Line 3, Col 11, "This construct may only be used within computation expressions")
]
4 changes: 2 additions & 2 deletions tests/fsharp/typecheck/sigs/neg104.vsbsl
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ neg104.fs(35,19,35,20): parse error FS0010: Unexpected symbol '}' in definition.

neg104.fs(36,1,36,1): parse error FS0010: Incomplete structured construct at or before this point in implementation file

neg104.fs(8,9,8,30): typecheck error FS0750: This construct may only be used within computation expressions
neg104.fs(8,9,8,15): typecheck error FS0750: This construct may only be used within computation expressions

neg104.fs(10,9,10,30): typecheck error FS0750: This construct may only be used within computation expressions
neg104.fs(10,9,10,15): typecheck error FS0750: This construct may only be used within computation expressions

neg104.fs(20,9,20,15): typecheck error FS0025: Incomplete pattern matches on this expression.

Expand Down
Loading