Skip to content

Commit aa4c1a9

Browse files
Copilotabonie
andauthored
Fix #14190: Narrow 'No matching overload' error range to method name only
In TcMethodApplication, remap UnresolvedOverloading error range from the whole expression (mMethExpr) to just the method name identifier. This prevents the error from covering the entire expression including object access chains and arguments. Add regression tests for instance calls, static calls, chained expressions, and lambda arguments. Agent-Logs-Url: https://github.com/dotnet/fsharp/sessions/d3913698-2bf7-4b33-8c76-1079e067156b Co-authored-by: abonie <20281641+abonie@users.noreply.github.com>
1 parent 1680128 commit aa4c1a9

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

src/Compiler/Checking/Expressions/CheckExpressions.fs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10401,6 +10401,19 @@ and TcMethodApplication
1040110401

1040210402
let result, errors = ResolveOverloadingForCall denv cenv.css mMethExpr methodName callerArgs ad postArgumentTypeCheckingCalledMethGroup true returnTy
1040310403

10404+
// Narrow the error range for unresolved overloading from the whole expression (mMethExpr)
10405+
// to just the method name. For instance calls like T.Instance.Method(""), mItem covers
10406+
// the entire "T.Instance.Method" range, so we compute the method-name-only range from
10407+
// the end of mItem and the method name length. See https://github.com/dotnet/fsharp/issues/14190.
10408+
let errors =
10409+
match errors with
10410+
| ErrorResult(warns, UnresolvedOverloading(denvErr, callerArgsErr, failure, _mWide)) ->
10411+
let mMethodName =
10412+
let startPos = mkPos mItem.EndLine (mItem.EndColumn - methodName.Length)
10413+
withStart startPos mItem
10414+
ErrorResult(warns, UnresolvedOverloading(denvErr, callerArgsErr, failure, mMethodName))
10415+
| other -> other
10416+
1040410417
match afterResolution, result with
1040510418
| AfterResolution.DoNothing, _ -> ()
1040610419

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
module ErrorMessages.OverloadResolutionErrorRangeTests
2+
3+
open Xunit
4+
open FSharp.Test.Compiler
5+
6+
// https://github.com/dotnet/fsharp/issues/14190
7+
[<Fact>]
8+
let ``Issue 14190 - overload error should cover only method name, not full expression`` () =
9+
FSharp
10+
"""
11+
type T() =
12+
static member Instance = T()
13+
14+
member _.Method(_: double) = ()
15+
member _.Method(_: int) = ()
16+
17+
T.Instance.Method("")
18+
"""
19+
|> typecheck
20+
|> shouldFail
21+
|> withDiagnostics
22+
[ (Error 41, Line 8, Col 12, Line 8, Col 18, "No overloads match for method 'Method'.
23+
24+
Known type of argument: string
25+
26+
Available overloads:
27+
- member T.Method: double -> unit // Argument at index 1 doesn't match
28+
- member T.Method: int -> unit // Argument at index 1 doesn't match") ]
29+
30+
// Verify that the error range is narrow also for simple direct method calls
31+
[<Fact>]
32+
let ``Issue 14190 - overload error for simple static method`` () =
33+
FSharp
34+
"""
35+
type T() =
36+
static member Method(_: double) = ()
37+
static member Method(_: int) = ()
38+
39+
T.Method("")
40+
"""
41+
|> typecheck
42+
|> shouldFail
43+
|> withDiagnostics
44+
[ (Error 41, Line 6, Col 3, Line 6, Col 9, "No overloads match for method 'Method'.
45+
46+
Known type of argument: string
47+
48+
Available overloads:
49+
- static member T.Method: double -> unit // Argument at index 1 doesn't match
50+
- static member T.Method: int -> unit // Argument at index 1 doesn't match") ]
51+
52+
// Verify that a long expression before the method doesn't widen the error range
53+
[<Fact>]
54+
let ``Issue 14190 - overload error on chained expression`` () =
55+
FSharp
56+
"""
57+
type T() =
58+
static member Instance = T()
59+
60+
member _.Next = T()
61+
member _.Method(_: double) = ()
62+
member _.Method(_: int) = ()
63+
64+
T.Instance.Next.Next.Method("")
65+
"""
66+
|> typecheck
67+
|> shouldFail
68+
|> withDiagnostics
69+
[ (Error 41, Line 9, Col 22, Line 9, Col 28, "No overloads match for method 'Method'.
70+
71+
Known type of argument: string
72+
73+
Available overloads:
74+
- member T.Method: double -> unit // Argument at index 1 doesn't match
75+
- member T.Method: int -> unit // Argument at index 1 doesn't match") ]
76+
77+
// Verify error range with lambda argument
78+
[<Fact>]
79+
let ``Issue 14190 - overload error with lambda argument`` () =
80+
FSharp
81+
"""
82+
type T() =
83+
static member Instance = T()
84+
85+
member _.Method(_: double) = ()
86+
member _.Method(_: int) = ()
87+
88+
T.Instance.Method(fun () -> "")
89+
"""
90+
|> typecheck
91+
|> shouldFail
92+
|> withDiagnostics
93+
[ (Error 41, Line 8, Col 12, Line 8, Col 18, "No overloads match for method 'Method'.
94+
95+
Known type of argument: (unit -> string)
96+
97+
Available overloads:
98+
- member T.Method: double -> unit // Argument at index 1 doesn't match
99+
- member T.Method: int -> unit // Argument at index 1 doesn't match") ]

tests/FSharp.Compiler.ComponentTests/FSharp.Compiler.ComponentTests.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@
301301
<Compile Include="ErrorMessages\MissingExpressionTests.fs" />
302302
<Compile Include="ErrorMessages\ModuleTests.fs" />
303303
<Compile Include="ErrorMessages\DiagnosticRegressionTests.fs" />
304+
<Compile Include="ErrorMessages\OverloadResolutionErrorRangeTests.fs" />
304305
<Compile Include="ErrorMessages\NameResolutionTests.fs" />
305306
<Compile Include="ErrorMessages\SuggestionsTests.fs" />
306307
<Compile Include="ErrorMessages\TypeMismatchTests.fs" />

0 commit comments

Comments
 (0)