Skip to content

Commit 2751d6e

Browse files
T-GroCopilot
andcommitted
Narrow FS0041 'No overloads match' error range to method name (#14284)
Narrows the error range for FS0041 (No overloads match for method) from covering the entire expression to just the method name. For example, T.Instance.Method(""") now highlights only 'Method' instead of the whole 'T.Instance.Method(""")'. The narrowing is applied in TcMethodApplication after overload resolution fails. It computes the method-name-only range from the end of mItem, with guards for multi-line expressions and generic constructors whose internal names may be longer than the source text. Includes: - New OverloadResolutionErrorRangeTests with 4 test cases - Updated 35+ baseline files for narrowed FS0041 error ranges - Release notes entry Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent ead77d1 commit 2751d6e

File tree

38 files changed

+299
-118
lines changed

38 files changed

+299
-118
lines changed

docs/release-notes/.FSharp.Compiler.Service/11.0.100.md

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

3+
* Narrow 'No overloads match for method' error range to method name only instead of covering the entire expression. ([Issue #14284](https://github.com/dotnet/fsharp/issues/14284), [PR #19505](https://github.com/dotnet/fsharp/pull/19505))
34
* Fix DU case names matching IWSAM member names no longer cause duplicate property entries. (Issue [#14321](https://github.com/dotnet/fsharp/issues/14321), [PR #19341](https://github.com/dotnet/fsharp/pull/19341))
45
* Fix DefaultAugmentation(false) duplicate entry in method table. (Issue [#16565](https://github.com/dotnet/fsharp/issues/16565), [PR #19341](https://github.com/dotnet/fsharp/pull/19341))
56
* Fix abstract event accessors now have SpecialName flag. (Issue [#5834](https://github.com/dotnet/fsharp/issues/5834), [PR #19341](https://github.com/dotnet/fsharp/pull/19341))

src/Compiler/Checking/Expressions/CheckExpressions.fs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10401,6 +10401,26 @@ 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/14284.
10408+
let errors =
10409+
match errors with
10410+
| ErrorResult(warns, UnresolvedOverloading(denvErr, callerArgsErr, failure, _mWide)) ->
10411+
let mMethodName =
10412+
let itemWidth = mItem.EndColumn - mItem.StartColumn
10413+
// Only narrow when the range is single-line and the method name fits within it.
10414+
// Generic constructors may have internal names longer than the source text
10415+
// (e.g., "ImmutableStack`1" vs source "ImmutableStack").
10416+
if mItem.StartLine = mItem.EndLine && methodName.Length < itemWidth then
10417+
let startPos = mkPos mItem.EndLine (mItem.EndColumn - methodName.Length)
10418+
withStart startPos mItem
10419+
else
10420+
mItem
10421+
ErrorResult(warns, UnresolvedOverloading(denvErr, callerArgsErr, failure, mMethodName))
10422+
| other -> other
10423+
1040410424
match afterResolution, result with
1040510425
| AfterResolution.DoNothing, _ -> ()
1040610426

tests/FSharp.Compiler.ComponentTests/ConstraintSolver/neg_invalid_constructor.fs.err.bsl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
neg_invalid_constructor.fs (3,29)-(3,56) typecheck error A unique overload for method 'ImmutableStack`1' could not be determined based on type information prior to this program point. A type annotation may be needed.
1+
neg_invalid_constructor.fs (3,29)-(3,43) typecheck error A unique overload for method 'ImmutableStack`1' could not be determined based on type information prior to this program point. A type annotation may be needed.
22

33
Known type of argument: 'a list
44

55
Candidates:
66
- new: col: 'b -> ImmutableStack<'a>
77
- private new: items: 'a list -> ImmutableStack<'a>
8-
neg_invalid_constructor.fs (4,93)-(4,111) typecheck error A unique overload for method 'ImmutableStack`1' could not be determined based on type information prior to this program point. A type annotation may be needed.
8+
neg_invalid_constructor.fs (4,93)-(4,107) typecheck error A unique overload for method 'ImmutableStack`1' could not be determined based on type information prior to this program point. A type annotation may be needed.
99

1010
Known type of argument: 'a list
1111

1212
Candidates:
1313
- new: col: 'b -> ImmutableStack<'a>
1414
- private new: items: 'a list -> ImmutableStack<'a>
15-
neg_invalid_constructor.fs (7,30)-(7,60) typecheck error A unique overload for method 'ImmutableStack`1' could not be determined based on type information prior to this program point. A type annotation may be needed.
15+
neg_invalid_constructor.fs (7,30)-(7,44) typecheck error A unique overload for method 'ImmutableStack`1' could not be determined based on type information prior to this program point. A type annotation may be needed.
1616

1717
Known type of argument: 'a list
1818

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/14284
7+
[<Fact>]
8+
let ``Issue 14284 - 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 14284 - 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 14284 - 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 14284 - 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
@@ -302,6 +302,7 @@
302302
<Compile Include="ErrorMessages\MissingExpressionTests.fs" />
303303
<Compile Include="ErrorMessages\ModuleTests.fs" />
304304
<Compile Include="ErrorMessages\DiagnosticRegressionTests.fs" />
305+
<Compile Include="ErrorMessages\OverloadResolutionErrorRangeTests.fs" />
305306
<Compile Include="ErrorMessages\NameResolutionTests.fs" />
306307
<Compile Include="ErrorMessages\SuggestionsTests.fs" />
307308
<Compile Include="ErrorMessages\TypeMismatchTests.fs" />

tests/FSharp.Compiler.Service.Tests/TypeChecker/TypeCheckerRecoveryTests.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Math.Max(a,)
3939
dumpDiagnosticNumbers checkResults |> shouldEqual [
4040
"(4,10--4,11)", 3100
4141
"(4,9--4,10)", 39
42-
"(4,0--4,12)", 41
42+
"(4,5--4,8)", 41
4343
]
4444

4545
assertHasSymbolUsages ["Max"] checkResults

tests/fsharp/Compiler/Language/TypeDirectedConversionTests.fs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ let test2(x: 'T) =
329329
[|
330330
(FSharpDiagnosticSeverity.Error,
331331
41,
332-
(11, 5, 11, 11),
332+
(11, 7, 11, 8),
333333
"""A unique overload for method 'A' could not be determined based on type information prior to this program point. A type annotation may be needed.
334334
335335
Known type of argument: 'T
@@ -341,7 +341,7 @@ Candidates:
341341
- static member M.A: n: int -> unit""")
342342
(FSharpDiagnosticSeverity.Error,
343343
41,
344-
(19, 5, 19, 12),
344+
(19, 8, 19, 9),
345345
"""A unique overload for method 'A' could not be determined based on type information prior to this program point. A type annotation may be needed.
346346
347347
Known type of argument: 'T
@@ -368,7 +368,7 @@ let test(x: 'T) =
368368
"""
369369
FSharpDiagnosticSeverity.Error
370370
41
371-
(10, 5, 10, 11)
371+
(10, 7, 10, 8)
372372
"""A unique overload for method 'A' could not be determined based on type information prior to this program point. A type annotation may be needed.
373373
374374
Known type of argument: 'T
@@ -495,7 +495,7 @@ let test() = M.A(System.DateTime.UtcNow, 1)
495495
"""
496496
FSharpDiagnosticSeverity.Error
497497
41
498-
(6, 14, 6, 44)
498+
(6, 16, 6, 17)
499499
"""A unique overload for method 'A' could not be determined based on type information prior to this program point. A type annotation may be needed.
500500
501501
Known types of arguments: System.DateTime * int

tests/fsharp/conformance/expressions/syntacticsugar/E_Slices01.bsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
E_Slices01.fsx(15,9,15,19): typecheck error FS0041: A unique overload for method 'GetSlice' could not be determined based on type information prior to this program point. A type annotation may be needed.
2+
E_Slices01.fsx(15,11,15,19): typecheck error FS0041: A unique overload for method 'GetSlice' could not be determined based on type information prior to this program point. A type annotation may be needed.
33

44
Known types of arguments: int * int option * 'a option
55

@@ -15,7 +15,7 @@ Candidates:
1515
- member Foo.GetSlice: x: int * y1: int option * y2: float option -> unit
1616
- member Foo.GetSlice: x: int * y1: int option * y2: int option -> unit
1717

18-
E_Slices01.fsx(17,9,17,19): typecheck error FS0041: A unique overload for method 'GetSlice' could not be determined based on type information prior to this program point. A type annotation may be needed.
18+
E_Slices01.fsx(17,11,17,19): typecheck error FS0041: A unique overload for method 'GetSlice' could not be determined based on type information prior to this program point. A type annotation may be needed.
1919

2020
Known types of arguments: 'a option * int option * int
2121

tests/fsharp/conformance/expressions/type-relatedexpressions/E_RigidTypeAnnotation03.bsl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ E_RigidTypeAnnotation03.fsx(17,13,17,16): typecheck error FS0001: This expressio
44
but here has type
55
'byte'
66

7-
E_RigidTypeAnnotation03.fsx(17,9,17,25): typecheck error FS0041: No overloads match for method 'M'.
7+
E_RigidTypeAnnotation03.fsx(17,11,17,12): typecheck error FS0041: No overloads match for method 'M'.
88

99
Known type of argument: sbyte
1010

@@ -20,7 +20,7 @@ E_RigidTypeAnnotation03.fsx(18,13,18,19): typecheck error FS0001: This expressio
2020
but here has type
2121
'float<'u>'
2222

23-
E_RigidTypeAnnotation03.fsx(18,9,18,30): typecheck error FS0041: No overloads match for method 'M'.
23+
E_RigidTypeAnnotation03.fsx(18,11,18,12): typecheck error FS0041: No overloads match for method 'M'.
2424

2525
Known type of argument: float32
2626

@@ -42,7 +42,7 @@ but given a
4242
'decimal<Kg>'
4343
The unit of measure 'N s ^ 2' does not match the unit of measure 'Kg'
4444

45-
E_RigidTypeAnnotation03.fsx(20,9,20,39): typecheck error FS0041: No overloads match for method 'M'.
45+
E_RigidTypeAnnotation03.fsx(20,11,20,12): typecheck error FS0041: No overloads match for method 'M'.
4646

4747
Known type of argument: decimal<N s ^ 2>
4848

@@ -58,7 +58,7 @@ E_RigidTypeAnnotation03.fsx(21,14,21,18): typecheck error FS0001: This expressio
5858
but here has type
5959
'string'
6060

61-
E_RigidTypeAnnotation03.fsx(21,9,21,27): typecheck error FS0041: No overloads match for method 'M'.
61+
E_RigidTypeAnnotation03.fsx(21,11,21,12): typecheck error FS0041: No overloads match for method 'M'.
6262

6363
Known type of argument: char
6464

tests/fsharp/conformance/inference/E_LeftToRightOverloadResolution01.bsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
E_LeftToRightOverloadResolution01.fsx(7,23,7,51): typecheck error FS0041: A unique overload for method 'WriteLine' could not be determined based on type information prior to this program point. A type annotation may be needed.
2+
E_LeftToRightOverloadResolution01.fsx(7,38,7,47): typecheck error FS0041: A unique overload for method 'WriteLine' could not be determined based on type information prior to this program point. A type annotation may be needed.
33

44
Known type of argument: 'a
55

@@ -18,7 +18,7 @@ Candidates:
1818
- System.Console.WriteLine(value: uint32) : unit
1919
- System.Console.WriteLine(value: uint64) : unit
2020

21-
E_LeftToRightOverloadResolution01.fsx(9,23,9,51): typecheck error FS0041: A unique overload for method 'WriteLine' could not be determined based on type information prior to this program point. A type annotation may be needed.
21+
E_LeftToRightOverloadResolution01.fsx(9,38,9,47): typecheck error FS0041: A unique overload for method 'WriteLine' could not be determined based on type information prior to this program point. A type annotation may be needed.
2222

2323
Known type of argument: 'a
2424

0 commit comments

Comments
 (0)