Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit24ea91b

Browse files
forkiKevinRansom
authored andcommitted
Give better error message when IF Statement has context requirements (#3150)
* Give better error message when IF Statement has context requirements* simplify code* Update TypeChecker.fs* something has a type* Adding a test* Transitive IF context* Another test - fixes #3146* fix wording
1 parent4f222ac commit24ea91b

File tree

8 files changed

+41
-4
lines changed

8 files changed

+41
-4
lines changed

‎src/fsharp/CompileOps.fs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,7 @@ let OutputPhasedErrorR (os:StringBuilder) (err:PhasedDiagnostic) =
628628
lett1,t2,_cxs= NicePrint.minimalStringsOfTwoTypes denv t1 t2
629629

630630
match contextInfowith
631+
| ContextInfo.IfExpression rangewhen range= m-> os.Append(FSComp.SR.ifExpression(t1,t2))|> ignore
631632
| ContextInfo.OmittedElseBranch rangewhen range= m-> os.Append(FSComp.SR.missingElseBranch(t2))|> ignore
632633
| ContextInfo.ElseBranchResult rangewhen range= m-> os.Append(FSComp.SR.elseBranchHasWrongType(t1,t2))|> ignore
633634
|_-> os.Append(ConstraintSolverTypesNotInEqualityRelation1E().Format t1 t2)|> ignore
@@ -639,6 +640,7 @@ let OutputPhasedErrorR (os:StringBuilder) (err:PhasedDiagnostic) =
639640
lett1,t2,_cxs= NicePrint.minimalStringsOfTwoTypes denv t1 t2
640641

641642
match contextInfowith
643+
| ContextInfo.IfExpression rangewhen range= m-> os.Append(FSComp.SR.ifExpression(t1,t2))|> ignore
642644
| ContextInfo.OmittedElseBranch rangewhen range= m-> os.Append(FSComp.SR.missingElseBranch(t2))|> ignore
643645
| ContextInfo.ElseBranchResult rangewhen range= m-> os.Append(FSComp.SR.elseBranchHasWrongType(t1,t2))|> ignore
644646
|_-> os.Append(ConstraintSolverTypesNotInEqualityRelation2E().Format t1 t2)|> ignore
@@ -664,6 +666,7 @@ let OutputPhasedErrorR (os:StringBuilder) (err:PhasedDiagnostic) =
664666
&& typeEquiv g t2 t2'->
665667
lett1,t2,tpcs= NicePrint.minimalStringsOfTwoTypes denv t1 t2
666668
match contextInfowith
669+
| ContextInfo.IfExpression rangewhen range= m-> os.Append(FSComp.SR.ifExpression(t1,t2))|> ignore
667670
| ContextInfo.OmittedElseBranch rangewhen range= m-> os.Append(FSComp.SR.missingElseBranch(t2))|> ignore
668671
| ContextInfo.ElseBranchResult rangewhen range= m-> os.Append(FSComp.SR.elseBranchHasWrongType(t1,t2))|> ignore
669672
| ContextInfo.TupleInRecordFields->

‎src/fsharp/ConstraintSolver.fs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ let FreshenMethInfo m (minfo:MethInfo) =
117117
typeContextInfo=
118118
/// No context was given.
119119
| NoContext
120+
/// The type equation comes from an IF expression.
121+
| IfExpressionofrange
120122
/// The type equation comes from an omitted else branch.
121123
| OmittedElseBranchofrange
122124
/// The type equation comes from a type check of the result of an else branch.

‎src/fsharp/ConstraintSolver.fsi‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ val FreshenMethInfo : range -> MethInfo -> TType list
5252
type ContextInfo=
5353
/// No context was given.
5454
| NoContext
55+
/// The type equation comes from an IF expression.
56+
| IfExpressionofrange
5557
/// The type equation comes from an omitted else branch.
5658
| OmittedElseBranchofrange
5759
/// The type equation comes from a type check of the result of an else branch.

‎src/fsharp/FSComp.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ undefinedNameTypeParameter,"The type parameter %s is not defined."
1717
undefinedNamePatternDiscriminator,"The pattern discriminator '%s' is not defined."
1818
replaceWithSuggestion,"Replace with '%s'"
1919
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."
20+
ifExpression,"The 'if' expression needs to have type '%s' to satisfy context type requirements. It currently has type '%s'."
2021
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'."
2122
commaInsteadOfSemicolonInRecord,"A ';' is used to separate field values in records. Consider replacing ',' with ';'."
2223
derefInsteadOfNot,"The '!' operator is used to dereference a ref cell. Consider using 'not expr' here."

‎src/fsharp/TypeChecker.fs‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5872,12 +5872,19 @@ and TcExprUndelayed cenv overallTy env tpenv (expr: SynExpr) =
58725872
| SynExpr.IfThenElse (e1,e2,e3opt,spIfToThen,isRecovery,mIfToThen,m) ->
58735873
let e1',tpenv = TcExprThatCantBeCtorBody cenv cenv.g.bool_ty env tpenv e1
58745874
let e2',tpenv =
5875+
let env =
5876+
match env.eContextInfo with
5877+
| ContextInfo.ElseBranchResult _ -> { env with eContextInfo = ContextInfo.ElseBranchResult e2.Range }
5878+
| _ ->
5879+
match e3opt with
5880+
| None -> { env with eContextInfo = ContextInfo.OmittedElseBranch e2.Range }
5881+
| _ -> { env with eContextInfo = ContextInfo.IfExpression e2.Range }
5882+
58755883
if not isRecovery && Option.isNone e3opt then
5876-
let env = { env with eContextInfo = ContextInfo.OmittedElseBranch e2.Range}
58775884
UnifyTypes cenv env m cenv.g.unit_ty overallTy
5878-
TcExprThatCanBeCtorBody cenv overallTy env tpenv e2
5879-
else
5880-
TcExprThatCanBeCtorBody cenv overallTy env tpenv e2
5885+
5886+
TcExprThatCanBeCtorBody cenv overallTy env tpenv e2
5887+
58815888
let e3',sp2,tpenv =
58825889
match e3opt with
58835890
| None ->
@@ -5886,6 +5893,7 @@ and TcExprUndelayed cenv overallTy env tpenv (expr: SynExpr) =
58865893
let env = { env with eContextInfo = ContextInfo.ElseBranchResult e3.Range }
58875894
let e3',tpenv = TcExprThatCanBeCtorBody cenv overallTy env tpenv e3
58885895
e3',SequencePointAtTarget,tpenv
5896+
58895897
primMkCond spIfToThen SequencePointAtTarget sp2 m overallTy e1' e2' e3', tpenv
58905898

58915899
// This is for internal use in the libraries only
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// #Warnings
2+
//<Expects status="Error" id="FS0001">The 'if' expression needs to have type 'bool'</Expects>
3+
4+
letx=1
5+
lety:bool=
6+
if x=2then"A"
7+
else"B"
8+
9+
exit0
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// #Warnings
2+
//<Expects status="Error" id="FS0001">All branches of an 'if' expression must return the same type.</Expects>
3+
4+
letx=1
5+
if x=1thentrue
6+
else
7+
if x=2then"A"
8+
else"B"
9+
10+
exit0

‎tests/fsharpqa/Source/Warnings/env.lst‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
SOURCE=SuggestDoubleBacktickUnions.fs SCFLAGS="--vserrors" # SuggestDoubleBacktickUnions.fs
4646
SOURCE=ElseBranchHasWrongType.fs # ElseBranchHasWrongType.fs
4747
SOURCE=ElseBranchHasWrongType2.fs # ElseBranchHasWrongType2.fs
48+
SOURCE=NestedElseBranchHasWrongType.fs # NestedElseBranchHasWrongType.fs
49+
SOURCE=ElseBranchHasWrongContextType.fs # ElseBranchHasWrongContextType.fs
4850
SOURCE=ElseBranchContextDoesntPropagateInAppl.fs # ElseBranchContextDoesntPropagateInAppl.fs
4951
SOURCE=ElseBranchContextDoesntPropagateInAppl2.fs # ElseBranchContextDoesntPropagateInAppl2.fs
5052
SOURCE=ElseBranchContextDoesntPropagateInForLoop.fs # ElseBranchContextDoesntPropagateInForLoop.fs

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp