Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/release-notes/.FSharp.Compiler.Service/10.0.100.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

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

### Breaking Changes

Expand Down
13 changes: 6 additions & 7 deletions src/Compiler/Checking/Expressions/CheckExpressions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3004,20 +3004,19 @@ let TcRuntimeTypeTest isCast isOperator (cenv: cenv) denv m tgtTy srcTy =
warning(Error(FSComp.SR.tcTypeTestLossy(NicePrint.minimalStringOfTypeWithNullness denv ety, NicePrint.minimalStringOfType denv (stripTyEqnsWrtErasure EraseAll g ety)), m))

/// Checks, warnings and constraint assertions for upcasts
let TcStaticUpcast (cenv: cenv) denv m tgtTy srcTy =
let TcStaticUpcast (cenv: cenv) denv mSourceExpr mUpcastExpr tgtTy srcTy =
let g = cenv.g
if isTyparTy g tgtTy then
if not (destTyparTy g tgtTy).IsCompatFlex then
error(IndeterminateStaticCoercion(denv, srcTy, tgtTy, m))
//else warning(UpcastUnnecessary m)
error(IndeterminateStaticCoercion(denv, srcTy, tgtTy, mUpcastExpr))

if isSealedTy g tgtTy && not (isTyparTy g tgtTy) then
warning(CoercionTargetSealed(denv, tgtTy, m))
warning(CoercionTargetSealed(denv, tgtTy, mUpcastExpr))

if typeEquiv g srcTy tgtTy then
warning(UpcastUnnecessary m)
warning(UpcastUnnecessary mUpcastExpr)

AddCxTypeMustSubsumeType ContextInfo.NoContext denv cenv.css m NoTrace tgtTy srcTy
AddCxTypeMustSubsumeType ContextInfo.NoContext denv cenv.css mSourceExpr NoTrace tgtTy srcTy

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

Expand Down Expand Up @@ -6139,7 +6138,7 @@ and TcExprUpcast (cenv: cenv) overallTy env tpenv (synExpr, synInnerExpr, m) =
| SynExpr.InferredUpcast _ ->
overallTy.Commit, tpenv
| _ -> failwith "upcast"
TcStaticUpcast cenv env.DisplayEnv m tgtTy srcTy
TcStaticUpcast cenv env.DisplayEnv synInnerExpr.Range m tgtTy srcTy
let expr = mkCoerceExpr(innerExpr, tgtTy, m, srcTy)
expr, tpenv

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,16 @@ module TyperelatedExpressions =
(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'.")
]

[<Fact>]
let ``Upcast 01`` () =
FSharp """
module Module

type A() =
class end

not true :> A |> ignore
"""
|> compile
|> shouldFail
|> 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")]
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ let c = orig :> Dictionary<obj,obj>
"""
|> typecheck
|> shouldFail
|> withSingleDiagnostic (Error 193, Line 5, Col 9, Line 5, Col 36,
|> withSingleDiagnostic (Error 193, Line 5, Col 9, Line 5, Col 13,
"Type constraint mismatch. The type \n 'IDictionary<obj,obj>' \nis not compatible with type\n 'Dictionary<obj,obj>' \n")

[<Fact>]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
// Negative tests on :>
// Cast to an interface not implemented

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

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

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



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
// Negative tests on :>
// Cast to a class that is not parent

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

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

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



Expand Down
Loading