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

Commit9512ed1

Browse files
forkiKevinRansom
authored andcommitted
Better error message for list/array init (#3161)
* Better error message for list/array init* add comma
1 parent5989c78 commit9512ed1

File tree

9 files changed

+41
-54
lines changed

9 files changed

+41
-54
lines changed

‎src/fsharp/CompileOps.fs‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -623,15 +623,11 @@ let OutputPhasedErrorR (os:StringBuilder) (err:PhasedDiagnostic) =
623623
os.Append(ConstraintSolverMissingConstraintE().Format(NicePrint.stringOfTyparConstraint denv(tpr,tpc)))|> ignore
624624
if m.StartLine<> m2.StartLinethen
625625
os.Append(SeeAlsoE().Format(stringOfRange m))|> ignore
626-
| ConstraintSolverTypesNotInEqualityRelation(denv,(TType_measure_as t1),(TType_measure_as t2),m,m2,contextInfo)->
626+
| ConstraintSolverTypesNotInEqualityRelation(denv,(TType_measure_as t1),(TType_measure_as t2),m,m2,_)->
627627
// REVIEW: consider if we need to show _cxs (the type parameter constraints)
628628
lett1,t2,_cxs= NicePrint.minimalStringsOfTwoTypes denv t1 t2
629629

630-
match contextInfowith
631-
| ContextInfo.IfExpression rangewhen range= m-> os.Append(FSComp.SR.ifExpression(t1,t2))|> ignore
632-
| ContextInfo.OmittedElseBranch rangewhen range= m-> os.Append(FSComp.SR.missingElseBranch(t2))|> ignore
633-
| ContextInfo.ElseBranchResult rangewhen range= m-> os.Append(FSComp.SR.elseBranchHasWrongType(t1,t2))|> ignore
634-
|_-> os.Append(ConstraintSolverTypesNotInEqualityRelation1E().Format t1 t2)|> ignore
630+
os.Append(ConstraintSolverTypesNotInEqualityRelation1E().Format t1 t2)|> ignore
635631

636632
if m.StartLine<> m2.StartLinethen
637633
os.Append(SeeAlsoE().Format(stringOfRange m))|> ignore
@@ -641,6 +637,11 @@ let OutputPhasedErrorR (os:StringBuilder) (err:PhasedDiagnostic) =
641637

642638
match contextInfowith
643639
| ContextInfo.IfExpression rangewhen range= m-> os.Append(FSComp.SR.ifExpression(t1,t2))|> ignore
640+
| ContextInfo.CollectionElement(isArray,range)when range= m->
641+
if isArraythen
642+
os.Append(FSComp.SR.arrayElementHasWrongType(t1,t2))|> ignore
643+
else
644+
os.Append(FSComp.SR.listElementHasWrongType(t1,t2))|> ignore
644645
| ContextInfo.OmittedElseBranch rangewhen range= m-> os.Append(FSComp.SR.missingElseBranch(t2))|> ignore
645646
| ContextInfo.ElseBranchResult rangewhen range= m-> os.Append(FSComp.SR.elseBranchHasWrongType(t1,t2))|> ignore
646647
|_-> os.Append(ConstraintSolverTypesNotInEqualityRelation2E().Format t1 t2)|> ignore
@@ -667,6 +668,11 @@ let OutputPhasedErrorR (os:StringBuilder) (err:PhasedDiagnostic) =
667668
lett1,t2,tpcs= NicePrint.minimalStringsOfTwoTypes denv t1 t2
668669
match contextInfowith
669670
| ContextInfo.IfExpression rangewhen range= m-> os.Append(FSComp.SR.ifExpression(t1,t2))|> ignore
671+
| ContextInfo.CollectionElement(isArray,range)when range= m->
672+
if isArraythen
673+
os.Append(FSComp.SR.arrayElementHasWrongType(t1,t2))|> ignore
674+
else
675+
os.Append(FSComp.SR.listElementHasWrongType(t1,t2))|> ignore
670676
| ContextInfo.OmittedElseBranch rangewhen range= m-> os.Append(FSComp.SR.missingElseBranch(t2))|> ignore
671677
| ContextInfo.ElseBranchResult rangewhen range= m-> os.Append(FSComp.SR.elseBranchHasWrongType(t1,t2))|> ignore
672678
| ContextInfo.TupleInRecordFields->

‎src/fsharp/ConstraintSolver.fs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ type ContextInfo =
127127
| RecordFields
128128
/// The type equation comes from the verification of a tuple in record fields.
129129
| TupleInRecordFields
130+
/// The type equation comes from a list or array constructor
131+
| CollectionElementofbool*range
130132
/// The type equation comes from a return in a computation expression.
131133
| ReturnInComputationExpression
132134
/// The type equation comes from a yield in a computation expression.

‎src/fsharp/ConstraintSolver.fsi‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ type ContextInfo =
6262
| RecordFields
6363
/// The type equation comes from the verification of a tuple in record fields.
6464
| TupleInRecordFields
65+
/// The type equation comes from a list or array constructor
66+
| CollectionElementofbool*range
6567
/// The type equation comes from a return in a computation expression.
6668
| ReturnInComputationExpression
6769
/// The type equation comes from a yield in a computation expression.

‎src/fsharp/FSComp.txt‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ undefinedNameSuggestionsIntro,"Maybe you want one of the following:"
1616
undefinedNameTypeParameter,"The type parameter %s is not defined."
1717
undefinedNamePatternDiscriminator,"The pattern discriminator '%s' is not defined."
1818
replaceWithSuggestion,"Replace with '%s'"
19+
listElementHasWrongType,"All elements of a list constructor expression must have the same type. This expression was expected to have type '%s', but here has type '%s'."
20+
arrayElementHasWrongType,"All elements of an array constructor expression must have the same type. This expression was expected to have type '%s', but here has type '%s'."
1921
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."
2022
ifExpression,"The 'if' expression needs to have type '%s' to satisfy context type requirements. It currently has type '%s'."
21-
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'."
23+
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'."
2224
commaInsteadOfSemicolonInRecord,"A ';' is used to separate field values in records. Consider replacing ',' with ';'."
2325
derefInsteadOfNot,"The '!' operator is used to dereference a ref cell. Consider using 'not expr' here."
2426
buildUnexpectedTypeArgs,"The non-generic type '%s' does not expect any type arguments, but here is given %d type argument(s)"

‎src/fsharp/TypeChecker.fs‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5715,7 +5715,15 @@ and TcExprUndelayed cenv overallTy env tpenv (expr: SynExpr) =
57155715

57165716
// Always allow subsumption if a nominal type is known prior to type checking any arguments
57175717
let flex = not (isTyparTy cenv.g argty)
5718-
let args',tpenv = List.mapFold (TcExprFlex cenv flex argty env) tpenv args
5718+
let first = ref true
5719+
let getInitEnv m =
5720+
if !first then
5721+
first := false
5722+
env
5723+
else
5724+
{ env with eContextInfo = ContextInfo.CollectionElement (isArray,m) }
5725+
5726+
let args',tpenv = List.mapFold (fun tpenv (x:SynExpr) -> TcExprFlex cenv flex argty (getInitEnv x.Range) tpenv x) tpenv args
57195727

57205728
let expr =
57215729
if isArray then Expr.Op(TOp.Array, [argty],args',m)

‎tests/fsharp/typecheck/sigs/neg20.bsl‎

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -69,35 +69,17 @@ neg20.fs(53,38,53,39): typecheck error FS0001: This expression was expected to h
6969
but here has type
7070
'int'
7171

72-
neg20.fs(60,26,60,33): typecheck error FS0001: This expression was expectedto have type
73-
'B'
74-
but here has type
75-
'A'
72+
neg20.fs(60,26,60,33): typecheck error FS0001: All elements of a list constructor expression must have the same type. This expression was expectedto have type 'B', but here has type 'A'.
7673

77-
neg20.fs(61,27,61,35): typecheck error FS0001: This expression was expectedto have type
78-
'B1'
79-
but here has type
80-
'B2'
74+
neg20.fs(61,27,61,35): typecheck error FS0001: All elements of a list constructor expression must have the same type. This expression was expectedto have type 'B1', but here has type 'B2'.
8175

82-
neg20.fs(62,26,62,33): typecheck error FS0001: This expression was expectedto have type
83-
'C'
84-
but here has type
85-
'B'
76+
neg20.fs(62,26,62,33): typecheck error FS0001: All elements of a list constructor expression must have the same type. This expression was expectedto have type 'C', but here has type 'B'.
8677

87-
neg20.fs(66,25,66,32): typecheck error FS0001: This expression was expectedto have type
88-
'A'
89-
but here has type
90-
'B'
78+
neg20.fs(66,25,66,32): typecheck error FS0001: All elements of a list constructor expression must have the same type. This expression was expectedto have type 'A', but here has type 'B'.
9179

92-
neg20.fs(67,27,67,34): typecheck error FS0001: This expression was expectedto have type
93-
'B'
94-
but here has type
95-
'C'
80+
neg20.fs(67,27,67,34): typecheck error FS0001: All elements of a list constructor expression must have the same type. This expression was expectedto have type 'B', but here has type 'C'.
9681

97-
neg20.fs(70,31,70,38): typecheck error FS0001: This expression was expectedto have type
98-
'B'
99-
but here has type
100-
'C'
82+
neg20.fs(70,31,70,38): typecheck error FS0001: All elements of a list constructor expression must have the same type. This expression was expectedto have type 'B', but here has type 'C'.
10183

10284
neg20.fs(71,34,71,42): typecheck error FS0001: Type mismatch. Expecting a
10385
'A list'
@@ -128,7 +110,7 @@ but given a
128110
'B list'
129111
The type 'A' doesnot match the type 'B'
130112

131-
neg20.fs(83,47,83,54): typecheck error FS0001: All branches of an 'if' expression mustreturn the same type. This expression was expectedto have type 'B' but here has type 'C'.
113+
neg20.fs(83,47,83,54): typecheck error FS0001: All branches of an 'if' expression mustreturn the same type. This expression was expectedto have type 'B', but here has type 'C'.
132114

133115
neg20.fs(87,54,87,61): typecheck error FS0001: This expression was expectedto have type
134116
'B'
@@ -150,10 +132,7 @@ neg20.fs(97,26,97,33): typecheck error FS0001: This expression was expected to h
150132
but here has type
151133
'B'
152134

153-
neg20.fs(99,26,99,33): typecheck error FS0001: This expression was expectedto have type
154-
'B'
155-
but here has type
156-
'A'
135+
neg20.fs(99,26,99,33): typecheck error FS0001: All elements of a list constructor expression must have the same type. This expression was expectedto have type 'B', but here has type 'A'.
157136

158137
neg20.fs(108,12,108,16): typecheck error FS0001: Type mismatch. Expecting a
159138
'B* B-> 'a'

‎tests/fsharp/typecheck/sigs/neg21.bsl‎

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,11 @@ but given a
1717
'float<s ^4>'
1818
The unit of measure 's ^3' doesnot match the unit of measure 's ^4'
1919

20-
neg21.fs(19,59,19,67): typecheck error FS0001: Type mismatch. Expecting a
21-
'area'
22-
but given a
23-
'float<m ^3>'
24-
The unit of measure 'sqrm' doesnot match the unit of measure 'm ^3'
20+
neg21.fs(19,59,19,67): typecheck error FS0001: The unit of measure 'sqrm' doesnot match the unit of measure 'm ^3'
2521

26-
neg21.fs(20,36,20,44): typecheck error FS0001: Type mismatch. Expecting a
27-
'area'
28-
but given a
29-
'float<m ^3>'
30-
The unit of measure 'sqrm' doesnot match the unit of measure 'm ^3'
22+
neg21.fs(20,36,20,44): typecheck error FS0001: The unit of measure 'sqrm' doesnot match the unit of measure 'm ^3'
3123

32-
neg21.fs(21,53,21,59): typecheck error FS0001: Type mismatch. Expecting a
33-
'float<s ^2>'
34-
but given a
35-
'float<m>'
36-
The unit of measure 's ^2' doesnot match the unit of measure 'm'
24+
neg21.fs(21,53,21,59): typecheck error FS0001: The unit of measure 's ^2' doesnot match the unit of measure 'm'
3725

3826
neg21.fs(22,17,22,21): typecheck error FS0001: The unit of measure 's ^4' doesnot match the unit of measure 'sqrm'
3927

‎tests/fsharpqa/Source/Warnings/ElseBranchHasWrongType.fs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// #Warnings
2-
//<Expects status="Error" span="(7,10)">All branches of an 'if' expression must return the same type. This expression was expected to have type 'string' but here has type 'int'.</Expects>
2+
//<Expects status="Error" span="(7,10)">All branches of an 'if' expression must return the same type. This expression was expected to have type 'string', but here has type 'int'.</Expects>
33

44
lettest=100
55
lety=

‎tests/fsharpqa/Source/Warnings/ElseBranchHasWrongType2.fs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// #Warnings
2-
//<Expects status="Error">All branches of an 'if' expression must return the same type. This expression was expected to have type 'string' but here has type 'int'.</Expects>
2+
//<Expects status="Error">All branches of an 'if' expression must return the same type. This expression was expected to have type 'string', but here has type 'int'.</Expects>
33

44
lettest=100
55
letf x= test

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp