Skip to content

Commit 875113b

Browse files
auduchinokclaude
andcommitted
Checker: report Bind resolution failures at the bound expression
Give the builder.Bind/BindN/BindReturn call synthesized for let!, and!, use! and match! a method identifier range covering the bound sources, so an overload resolution failure is reported there instead of at the first expression of the rest of the computation expression. RIDER-130406 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
1 parent 409c7a6 commit 875113b

3 files changed

Lines changed: 125 additions & 7 deletions

File tree

‎docs/release-notes/.FSharp.Compiler.Service/11.0.200.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Fixed
66

77
* Capture diagnostics emitted when transparent project checking finishes a project, including FS0247 for namespace-module name collisions. ([Issue #6036](https://github.com/dotnet/fsharp/issues/6036), [PR #20706](https://github.com/dotnet/fsharp/pull/20706))
8+
* Checker: report Bind resolution failures at the bound expression ([PR #20747](https://github.com/dotnet/fsharp/pull/20747))
89
* Split Linux CoreCLR CI into two parallel test batches to reduce test turnaround time. ([PR #20587](https://github.com/dotnet/fsharp/pull/20587))
910
* Fix `NativePtr.stackalloc` used inside the `with` handler, filter or `finally` block of a `try` expression producing an assembly that throws `InvalidProgramException` at method load. Direct or mandatory-inline allocations are now reported at compile time as error FS3924; calls to non-inline allocation helpers, including handler-local helpers, remain legal and retain their method boundary during inlining and let-binding elimination. ([Issue #20295](https://github.com/dotnet/fsharp/issues/20295), [PR #20491](https://github.com/dotnet/fsharp/pull/20491))
1011
* `FsiEvaluationSession.EvalInteraction` and `EvalInteractionNonThrowing` evaluate every `;;`-separated interaction in the text they are given, as their documentation has always said, instead of silently discarding everything after the first. ([PR #20396](https://github.com/dotnet/fsharp/pull/20396))

‎src/Compiler/Checking/Expressions/CheckComputationExpressions.fs‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,10 @@ let inline addBindDebugPoint spBind e =
8686

8787
let inline mkSynDelay2 (e: SynExpr) = mkSynDelay (e.Range.MakeSynthetic()) e
8888

89-
/// Make a builder.Method(...) call
90-
let mkSynCall nm (m: range) args builderValName =
89+
/// The method identifier range is where an overload resolution failure of the call is reported
90+
let mkSynCallWithMethodRange nm (mMethod: range) (m: range) args builderValName =
9191
let m = m.MakeSynthetic() // Mark as synthetic so the language service won't pick it up.
92+
let mMethod = mMethod.MakeSynthetic()
9293

9394
let args =
9495
match args with
@@ -97,7 +98,11 @@ let mkSynCall nm (m: range) args builderValName =
9798
| args -> SynExpr.Paren(SynExpr.Tuple(false, args, [], m), range0, None, m)
9899

99100
let builderVal = mkSynIdGet m builderValName
100-
mkSynApp1 (SynExpr.DotGet(builderVal, range0, SynLongIdent([ mkSynId m nm ], [], [ None ]), m)) args m
101+
mkSynApp1 (SynExpr.DotGet(builderVal, range0, SynLongIdent([ mkSynId mMethod nm ], [], [ None ]), m)) args m
102+
103+
/// Make a builder.Method(...) call
104+
let mkSynCall nm (m: range) args builderValName =
105+
mkSynCallWithMethodRange nm m m args builderValName
101106

102107
// Optionally wrap sources of "let!", "yield!", "use!" in "query.Source"
103108
let mkSourceExpr callExpr sourceMethInfo builderValName =
@@ -2080,7 +2085,7 @@ let rec TryTranslateComputationExpression
20802085
let rhsExpr =
20812086
mkSourceExprConditional isFromSource rhsExpr ceenv.sourceMethInfo ceenv.builderValName
20822087

2083-
mkSynCall "Bind" mBind [ rhsExpr; consumeExpr ] ceenv.builderValName
2088+
mkSynCallWithMethodRange "Bind" rhsExpr.Range mBind [ rhsExpr; consumeExpr ] ceenv.builderValName
20842089
|> addBindDebugPoint spBind
20852090

20862091
Some(translatedCtxt bindExpr)
@@ -2340,7 +2345,7 @@ let rec TryTranslateComputationExpression
23402345
SynExpr.MatchLambda(false, trivia.MatchBangKeyword, clauses, DebugPointAtBinding.NoneAtInvisible, trivia.MatchBangKeyword)
23412346

23422347
let callExpr =
2343-
mkSynCall "Bind" trivia.MatchBangKeyword [ inputExpr; consumeExpr ] ceenv.builderValName
2348+
mkSynCallWithMethodRange "Bind" expr.Range trivia.MatchBangKeyword [ inputExpr; consumeExpr ] ceenv.builderValName
23442349
|> addBindDebugPoint spMatch
23452350

23462351
Some(translatedCtxt callExpr)
@@ -2692,6 +2697,11 @@ and TranslateComputationExpressionBind
26922697

26932698
let innerRange = innerComp.Range
26942699

2700+
let mSources =
2701+
match bindArgs with
2702+
| first :: rest -> rest |> List.fold (fun m (e: SynExpr) -> unionRanges m e.Range) first.Range
2703+
| [] -> bindRange
2704+
26952705
let innerCompReturn = convertSimpleReturnToExpr ceenv comp varSpace innerComp
26962706

26972707
match innerCompReturn with
@@ -2711,7 +2721,7 @@ and TranslateComputationExpressionBind
27112721
innerRange
27122722
)
27132723

2714-
translatedCtxt (mkSynCall bindName bindRange (bindArgs @ [ consumeExpr ]) ceenv.builderValName)
2724+
translatedCtxt (mkSynCallWithMethodRange bindName mSources bindRange (bindArgs @ [ consumeExpr ]) ceenv.builderValName)
27152725

27162726
match customOpInfo with
27172727
| None -> dataCompPriorToOp
@@ -2737,7 +2747,7 @@ and TranslateComputationExpressionBind
27372747
)
27382748

27392749
let bindCall =
2740-
mkSynCall bindName holeFill.Range (bindArgs @ [ consumeExpr ]) ceenv.builderValName
2750+
mkSynCallWithMethodRange bindName mSources holeFill.Range (bindArgs @ [ consumeExpr ]) ceenv.builderValName
27412751

27422752
translatedCtxt (bindCall |> addBindDebugPoint))
27432753

‎tests/FSharp.Compiler.ComponentTests/Language/ComputationExpressionTests.fs‎

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2945,3 +2945,110 @@ let test() =
29452945
|> typecheck
29462946
|> shouldFail
29472947
|> withErrorCode 750
2948+
2949+
let private builders =
2950+
FSharp """
2951+
[<AutoOpen>]
2952+
module Builders
2953+
type Builder() =
2954+
member _.Bind(x: int, f: int -> int) = 0
2955+
member _.Bind(x: string, f: string -> int) = 0
2956+
member _.Bind2(x: int, y: int, f: int * int -> int) = 0
2957+
member _.Bind2(x: string, y: string, f: string * string -> int) = 0
2958+
member _.Using(x: System.IDisposable, f: System.IDisposable -> int) = 0
2959+
member _.Return(x: int) = 0
2960+
type ReturnBuilder() =
2961+
member _.BindReturn(x: int, f: int -> int) = 0
2962+
member _.BindReturn(x: string, f: string -> int) = 0
2963+
let builder = Builder()
2964+
let returnBuilder = ReturnBuilder()
2965+
"""
2966+
2967+
let private bindError (call: string) (expected: (ErrorType * Line * Col * Line * Col * string) list) =
2968+
builders
2969+
|> withAdditionalSourceFile (FsSourceWithFileName "ce.fs" call)
2970+
|> asExe
2971+
|> compile
2972+
|> shouldFail
2973+
|> withDiagnostics expected
2974+
2975+
[<Fact>]
2976+
let ``Bind overload error is reported at the bound expression`` () =
2977+
bindError """
2978+
builder {
2979+
let! x = 1.0
2980+
return x
2981+
}
2982+
"""
2983+
[ (Error 41, Line 3, Col 14, Line 3, Col 17, "No overloads match for method 'Bind'.
2984+
2985+
Known types of arguments: float * (int -> int)
2986+
2987+
Available overloads:
2988+
- member Builder.Bind: x: int * f: (int -> int) -> int // Argument 'x' doesn't match
2989+
- member Builder.Bind: x: string * f: (string -> int) -> int // Argument 'x' doesn't match") ]
2990+
2991+
[<Fact>]
2992+
let ``BindReturn overload error is reported at the bound expression`` () =
2993+
bindError """
2994+
returnBuilder {
2995+
let! x = 1.0
2996+
return x
2997+
}
2998+
"""
2999+
[ (Error 41, Line 3, Col 14, Line 3, Col 17, "No overloads match for method 'BindReturn'.
3000+
3001+
Known types of arguments: float * ('a -> 'a)
3002+
3003+
Available overloads:
3004+
- member ReturnBuilder.BindReturn: x: int * f: (int -> int) -> int // Argument 'x' doesn't match
3005+
- member ReturnBuilder.BindReturn: x: string * f: (string -> int) -> int // Argument 'x' doesn't match") ]
3006+
3007+
[<Fact>]
3008+
let ``Bind2 overload error is reported at the bound expressions`` () =
3009+
bindError """
3010+
builder {
3011+
let! x = 1
3012+
and! y = 1.0
3013+
return x
3014+
}
3015+
"""
3016+
[ (Error 41, Line 3, Col 14, Line 4, Col 17, "No overloads match for method 'Bind2'.
3017+
3018+
Known types of arguments: int * float * (int * 'a -> int)
3019+
3020+
Available overloads:
3021+
- member Builder.Bind2: x: int * y: int * f: (int * int -> int) -> int // Argument 'y' doesn't match
3022+
- member Builder.Bind2: x: string * y: string * f: (string * string -> int) -> int // Argument 'x' doesn't match") ]
3023+
3024+
[<Fact>]
3025+
let ``use! Bind overload error is reported at the bound expression`` () =
3026+
bindError """
3027+
builder {
3028+
use! x = 1.0
3029+
return 0
3030+
}
3031+
"""
3032+
[ (Error 41, Line 3, Col 14, Line 3, Col 17, "No overloads match for method 'Bind'.
3033+
3034+
Known types of arguments: float * ('a -> int) when 'a :> System.IDisposable
3035+
3036+
Available overloads:
3037+
- member Builder.Bind: x: int * f: (int -> int) -> int // Argument 'x' doesn't match
3038+
- member Builder.Bind: x: string * f: (string -> int) -> int // Argument 'x' doesn't match") ]
3039+
3040+
[<Fact>]
3041+
let ``match! Bind overload error is reported at the bound expression`` () =
3042+
bindError """
3043+
builder {
3044+
match! 1.0 with
3045+
| _ -> return 0
3046+
}
3047+
"""
3048+
[ (Error 41, Line 3, Col 12, Line 3, Col 15, "No overloads match for method 'Bind'.
3049+
3050+
Known types of arguments: float * ('a -> int)
3051+
3052+
Available overloads:
3053+
- member Builder.Bind: x: int * f: (int -> int) -> int // Argument 'x' doesn't match
3054+
- member Builder.Bind: x: string * f: (string -> int) -> int // Argument 'x' doesn't match") ]

0 commit comments

Comments
 (0)