Skip to content

Commit

Permalink
Merge pull request #1275 from forki/refcell
Browse files Browse the repository at this point in the history
Show better error when using ref cell instead of not
  • Loading branch information
KevinRansom authored Jun 24, 2016
2 parents 1f952d4 + 9fa8547 commit deea6a5
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/fsharp/CompileOps.fs
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ let OutputPhasedErrorR (os:System.Text.StringBuilder) (err:PhasedError) =
(if m.StartLine <> m2.StartLine then
os.Append(SeeAlsoE().Format (stringOfRange m)) |> ignore )
| ConstraintSolverTypesNotInEqualityRelation(denv,(TType_measure _ as t1),(TType_measure _ as t2),m,m2) ->
// REVIEW: consider if we need to show _cxs (the type parameter constrants)
// REVIEW: consider if we need to show _cxs (the type parameter constraints)
let t1, t2, _cxs = NicePrint.minimalStringsOfTwoTypes denv t1 t2
os.Append(ConstraintSolverTypesNotInEqualityRelation1E().Format t1 t2 ) |> ignore
(if m.StartLine <> m2.StartLine then
Expand All @@ -633,7 +633,7 @@ let OutputPhasedErrorR (os:System.Text.StringBuilder) (err:PhasedError) =
(if m.StartLine <> m2.StartLine then
os.Append(SeeAlsoE().Format (stringOfRange m)) |> ignore)
| ConstraintSolverTypesNotInSubsumptionRelation(denv,t1,t2,m,m2) ->
// REVIEW: consider if we need to show _cxs (the type parameter constrants)
// REVIEW: consider if we need to show _cxs (the type parameter constraints)
let t1, t2, cxs= NicePrint.minimalStringsOfTwoTypes denv t1 t2
os.Append(ConstraintSolverTypesNotInSubsumptionRelationE().Format t2 t1 cxs) |> ignore
(if m.StartLine <> m2.StartLine then
Expand All @@ -654,9 +654,12 @@ let OutputPhasedErrorR (os:System.Text.StringBuilder) (err:PhasedError) =
match contextInfo with
| ContextInfo.OmittedElseBranch -> os.Append(FSComp.SR.missingElseBranch(t2)) |> ignore
| ContextInfo.ElseBranch -> os.Append(FSComp.SR.elseBranchHasWrongType(t1,t2)) |> ignore
| ContextInfo.TupleInRecordFields ->
| ContextInfo.TupleInRecordFields ->
os.Append(ErrorFromAddingTypeEquation1E().Format t2 t1 tpcs) |> ignore
os.Append(System.Environment.NewLine + FSComp.SR.commaInsteadOfSemicolonInRecord()) |> ignore
| _ when t2 = "bool" && t1.EndsWith " ref" ->
os.Append(ErrorFromAddingTypeEquation1E().Format t2 t1 tpcs) |> ignore
os.Append(System.Environment.NewLine + FSComp.SR.commaInsteadOfSemicolonInRecord()) |> ignore
os.Append(System.Environment.NewLine + FSComp.SR.derefInsteadOfNot()) |> ignore
| _ -> os.Append(ErrorFromAddingTypeEquation1E().Format t2 t1 tpcs) |> ignore
| ErrorFromAddingTypeEquation(_,_,_,_,((ConstraintSolverTypesNotInSubsumptionRelation _ | ConstraintSolverError _) as e), _, _) ->
OutputExceptionR os e
Expand Down
1 change: 1 addition & 0 deletions src/fsharp/FSComp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ undefinedNamePatternDiscriminator,"The pattern discriminator '%s' is not defined
missingElseBranch,"The 'if' expression is missing an 'else' branch. The 'then' branch has type '%s'. Because 'if' is an expression, and not a statement, add an 'else' branch which returns a value of the same type."
elseBranchHasWrongType,"All branches of an 'if' expression must return the same type. This expression was expected to have type '%s' but here has type '%s'."
commaInsteadOfSemicolonInRecord,"A ';' is used to separate field values in records. Consider replacing ',' with ';'."
derefInsteadOfNot,"The '!' operator is used to dereference a ref cell. Consider using 'not expr' here."
buildUnexpectedTypeArgs,"The non-generic type '%s' does not expect any type arguments, but here is given %d type argument(s)"
returnUsedInsteadOfReturnBang,"Consider using 'return!' instead of 'return'."
yieldUsedInsteadOfYieldBang,"Consider using 'yield!' instead of 'yield'."
Expand Down
9 changes: 9 additions & 0 deletions tests/fsharpqa/Source/Warnings/RefCellInsteadOfNot.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// #Warnings
//<Expects status="Error" id="FS0001">This expression was expected to have type</Expects>
//<Expects status="success">The '!' operator is used to dereference a ref cell. Consider using 'not expr' here.</Expects>

let x = true
if !x then
printfn "hello"

exit 0
8 changes: 8 additions & 0 deletions tests/fsharpqa/Source/Warnings/RefCellInsteadOfNot2.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// #Warnings
//<Expects status="Error" id="FS0001">This expression was expected to have type</Expects>
//<Expects status="success">The '!' operator is used to dereference a ref cell. Consider using 'not expr' here.</Expects>

let x = true
let y = !x

exit 0
2 changes: 2 additions & 0 deletions tests/fsharpqa/Source/Warnings/env.lst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
SOURCE=ElseBranchHasWrongType.fs # ElseBranchHasWrongType.fs
SOURCE=MissingExpressionAfterLet.fs # MissingExpressionAfterLet.fs
SOURCE=AssignmentOnImmutable.fs # AssignmentOnImmutable.fs
SOURCE=RefCellInsteadOfNot.fs # RefCellInsteadOfNot.fs
SOURCE=RefCellInsteadOfNot2.fs # RefCellInsteadOfNot2.fs
SOURCE=UpcastInsteadOfDowncast.fs # UpcastInsteadOfDowncast.fs
SOURCE=UpcastFunctionInsteadOfDowncast.fs # UpcastFunctionInsteadOfDowncast.fs
SOURCE=DowncastInsteadOfUpcast.fs # DowncastInsteadOfUpcast.fs
Expand Down

0 comments on commit deea6a5

Please sign in to comment.