Skip to content

Commit dc44561

Browse files
authored
Type checker: use inner expr range in upcast constraints errors (#18850)
1 parent 616530c commit dc44561

File tree

6 files changed

+26
-14
lines changed

6 files changed

+26
-14
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
### Changed
2626
* Use `errorR` instead of `error` in `CheckDeclarations.fs` when possible. ([PR #18645](https://github.com/dotnet/fsharp/pull/18645))
27+
* Type checker: use inner expr range in upcast constraints errors ([PR #18850](https://github.com/dotnet/fsharp/pull/18850))
2728

2829
### Breaking Changes
2930

src/Compiler/Checking/Expressions/CheckExpressions.fs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3004,20 +3004,19 @@ let TcRuntimeTypeTest isCast isOperator (cenv: cenv) denv m tgtTy srcTy =
30043004
warning(Error(FSComp.SR.tcTypeTestLossy(NicePrint.minimalStringOfTypeWithNullness denv ety, NicePrint.minimalStringOfType denv (stripTyEqnsWrtErasure EraseAll g ety)), m))
30053005

30063006
/// Checks, warnings and constraint assertions for upcasts
3007-
let TcStaticUpcast (cenv: cenv) denv m tgtTy srcTy =
3007+
let TcStaticUpcast (cenv: cenv) denv mSourceExpr mUpcastExpr tgtTy srcTy =
30083008
let g = cenv.g
30093009
if isTyparTy g tgtTy then
30103010
if not (destTyparTy g tgtTy).IsCompatFlex then
3011-
error(IndeterminateStaticCoercion(denv, srcTy, tgtTy, m))
3012-
//else warning(UpcastUnnecessary m)
3011+
error(IndeterminateStaticCoercion(denv, srcTy, tgtTy, mUpcastExpr))
30133012

30143013
if isSealedTy g tgtTy && not (isTyparTy g tgtTy) then
3015-
warning(CoercionTargetSealed(denv, tgtTy, m))
3014+
warning(CoercionTargetSealed(denv, tgtTy, mUpcastExpr))
30163015

30173016
if typeEquiv g srcTy tgtTy then
3018-
warning(UpcastUnnecessary m)
3017+
warning(UpcastUnnecessary mUpcastExpr)
30193018

3020-
AddCxTypeMustSubsumeType ContextInfo.NoContext denv cenv.css m NoTrace tgtTy srcTy
3019+
AddCxTypeMustSubsumeType ContextInfo.NoContext denv cenv.css mSourceExpr NoTrace tgtTy srcTy
30213020

30223021
let BuildPossiblyConditionalMethodCall (cenv: cenv) env isMutable m isProp minfo valUseFlags minst objArgs args staticTyOpt =
30233022

@@ -6139,7 +6138,7 @@ and TcExprUpcast (cenv: cenv) overallTy env tpenv (synExpr, synInnerExpr, m) =
61396138
| SynExpr.InferredUpcast _ ->
61406139
overallTy.Commit, tpenv
61416140
| _ -> failwith "upcast"
6142-
TcStaticUpcast cenv env.DisplayEnv m tgtTy srcTy
6141+
TcStaticUpcast cenv env.DisplayEnv synInnerExpr.Range m tgtTy srcTy
61436142
let expr = mkCoerceExpr(innerExpr, tgtTy, m, srcTy)
61446143
expr, tpenv
61456144

tests/FSharp.Compiler.ComponentTests/Conformance/Expressions/ControlFlowExpressions/Type-relatedExpressions/Type-relatedExpressions.fs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,16 @@ module TyperelatedExpressions =
7474
(Warning 20, Line 9, Col 1, Line 9, Col 22, "The result of this expression has type 'obj' and is implicitly ignored. Consider using 'ignore' to discard this value explicitly, e.g. 'expr |> ignore', or 'let' to bind the result to a name, e.g. 'let result = expr'.")
7575
]
7676

77+
[<Fact>]
78+
let ``Upcast 01`` () =
79+
FSharp """
80+
module Module
7781
82+
type A() =
83+
class end
84+
85+
not true :> A |> ignore
86+
"""
87+
|> compile
88+
|> shouldFail
89+
|> withDiagnostics [(Error 193, Line 7, Col 1, Line 7, Col 9, "Type constraint mismatch. The type \n 'bool' \nis not compatible with type\n 'A' \n")]

tests/FSharp.Compiler.ComponentTests/ErrorMessages/UpcastDowncastTests.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ let c = orig :> Dictionary<obj,obj>
1717
"""
1818
|> typecheck
1919
|> shouldFail
20-
|> withSingleDiagnostic (Error 193, Line 5, Col 9, Line 5, Col 36,
20+
|> withSingleDiagnostic (Error 193, Line 5, Col 9, Line 5, Col 13,
2121
"Type constraint mismatch. The type \n 'IDictionary<obj,obj>' \nis not compatible with type\n 'Dictionary<obj,obj>' \n")
2222

2323
[<Fact>]

tests/fsharpqa/Source/Conformance/Expressions/Type-relatedExpressions/E_StaticCoercion_class_not_impl_iface.fsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
// Negative tests on :>
33
// Cast to an interface not implemented
44

5-
//<Expects id="FS0193" span="(25,12-25,20)" status="error">Type constraint mismatch\. The type</Expects>
5+
//<Expects id="FS0193" span="(25,19-25,20)" status="error">Type constraint mismatch\. The type</Expects>
66

7-
//<Expects id="FS0193" span="(26,12-26,18)" status="error">Type constraint mismatch\. The type</Expects>
7+
//<Expects id="FS0193" span="(26,12-26,13)" status="error">Type constraint mismatch\. The type</Expects>
88

9-
//<Expects id="FS0193" span="(27,12-27,18)" status="error">Type constraint mismatch\. The type</Expects>
9+
//<Expects id="FS0193" span="(27,12-27,13)" status="error">Type constraint mismatch\. The type</Expects>
1010

1111

1212

tests/fsharpqa/Source/Conformance/Expressions/Type-relatedExpressions/E_StaticCoercion_class_not_subclass.fsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
// Negative tests on :>
33
// Cast to a class that is not parent
44

5-
//<Expects id="FS0193" span="(20,12-20,20)" status="error">Type constraint mismatch\. The type</Expects>
5+
//<Expects id="FS0193" span="(20,19-20,20)" status="error">Type constraint mismatch\. The type</Expects>
66

7-
//<Expects id="FS0193" span="(21,12-21,18)" status="error">Type constraint mismatch\. The type</Expects>
7+
//<Expects id="FS0193" span="(21,12-21,13)" status="error">Type constraint mismatch\. The type</Expects>
88

9-
//<Expects id="FS0193" span="(22,12-22,19)" status="error">Type constraint mismatch\. The type</Expects>
9+
//<Expects id="FS0193" span="(22,12-22,13)" status="error">Type constraint mismatch\. The type</Expects>
1010

1111

1212

0 commit comments

Comments
 (0)