Skip to content

Commit 178fc31

Browse files
matthidKevinRansom
authored andcommitted
forward warnings when searching for a method overload (#4481)
* forward warnings when searching for a method overload, fixes #3752 * add a new test for the bug * simplify test by using a `typecheck/sigs` tests * Update according to test * update test
1 parent 179f3c4 commit 178fc31

File tree

4 files changed

+18
-7
lines changed

4 files changed

+18
-7
lines changed

src/fsharp/ConstraintSolver.fs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ let FilterEachThenUndo f meths =
359359
trace.Undo()
360360
match CheckNoErrorsAndGetWarnings res with
361361
| None -> None
362-
| Some warns -> Some (calledMeth, warns.Length, trace))
362+
| Some warns -> Some (calledMeth, warns, trace))
363363

364364
let ShowAccessDomain ad =
365365
match ad with
@@ -2194,8 +2194,8 @@ and ResolveOverloading
21942194
(ArgsEquivInsideUndo csenv cx.IsSome)
21952195
reqdRetTyOpt
21962196
calledMeth) with
2197-
| [(calledMeth, _, _)] ->
2198-
Some calledMeth, CompleteD, NoTrace // Can't re-play the trace since ArgsEquivInsideUndo was used
2197+
| [(calledMeth, warns, _)] ->
2198+
Some calledMeth, OkResult (warns, ()), NoTrace // Can't re-play the trace since ArgsEquivInsideUndo was used
21992199

22002200
| _ ->
22012201
// Now determine the applicable methods.
@@ -2255,8 +2255,8 @@ and ResolveOverloading
22552255

22562256
None, ErrorD (failOverloading (FSComp.SR.csNoOverloadsFound methodName) errors), NoTrace
22572257

2258-
| [(calledMeth, _, t)] ->
2259-
Some calledMeth, CompleteD, WithTrace t
2258+
| [(calledMeth, warns, t)] ->
2259+
Some calledMeth, OkResult (warns, ()), WithTrace t
22602260

22612261
| applicableMeths ->
22622262

@@ -2292,7 +2292,9 @@ and ResolveOverloading
22922292
if c <> 0 then c else
22932293
0
22942294

2295-
let better (candidate:CalledMeth<_>, candidateWarnCount, _) (other:CalledMeth<_>, otherWarnCount, _) =
2295+
let better (candidate:CalledMeth<_>, candidateWarnings, _) (other:CalledMeth<_>, otherwarnings, _) =
2296+
let candidateWarnCount = List.length candidateWarnings
2297+
let otherWarnCount = List.length otherwarnings
22962298
// Prefer methods that don't give "this code is less generic" warnings
22972299
// Note: Relies on 'compare' respecting true > false
22982300
let c = compare (candidateWarnCount = 0) (otherWarnCount = 0)
@@ -2383,7 +2385,7 @@ and ResolveOverloading
23832385
else
23842386
None)
23852387
match bestMethods with
2386-
| [(calledMeth, _, t)] -> Some calledMeth, CompleteD, WithTrace t
2388+
| [(calledMeth, warns, t)] -> Some calledMeth, OkResult (warns, ()), WithTrace t
23872389
| bestMethods ->
23882390
let methodNames =
23892391
let methods =

tests/fsharp/tests.fs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2168,6 +2168,9 @@ module TypecheckTests =
21682168
[<Test>]
21692169
let ``type check neg101`` () = singleNegTest (testConfig "typecheck/sigs") "neg101"
21702170

2171+
[<Test>]
2172+
let ``type check neg_issue_3752`` () = singleNegTest (testConfig "typecheck/sigs") "neg_issue_3752"
2173+
21712174
[<Test>]
21722175
let ``type check neg_byref_1`` () = singleNegTest (testConfig "typecheck/sigs") "neg_byref_1"
21732176

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
neg_issue_3752.fs(4,19,4,46): typecheck error FS0064: This construct causes code to be less generic than indicated by the type annotations. The type variable 'a has been constrained to be type 'string'.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module Test
2+
let memoizeBy (getKey : 'a -> string) (f: 'a -> 'b) : 'a -> 'b =
3+
let cache = System.Collections.Concurrent.ConcurrentDictionary<string, 'b>()
4+
fun (x: 'a) -> cache.GetOrAdd(getKey x, f)

0 commit comments

Comments
 (0)