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

Commit5c491b2

Browse files
forkidsyme
authored andcommitted
Improve warning for ignored values in sequence expressions - fixes #4228 (#4245)
* Improve warning for ignored values in sequence expressions - fixes #4228* Adding a test* Show the type* Add a case for when we need to suggest yield!* Check sequence type* Manually copy resx and fs from txt conversion* fix rebase issue* Only report yield! for sequences
1 parentac2d8d5 commit5c491b2

25 files changed

+1478
-1253
lines changed

‎src/buildfromsource/FSharp.Compiler.Private/FSComp.fs‎

Lines changed: 1243 additions & 1239 deletions
Large diffs are not rendered by default.

‎src/buildfromsource/FSharp.Compiler.Private/FSComp.resx‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -597,9 +597,6 @@
597597
<dataname="typrelCannotResolveImplicitGenericInstantiation"xml:space="preserve">
598598
<value>The implicit instantiation of a generic construct at or near this point could not be resolved because it could resolve to multiple unrelated types, e.g. '{0}' and '{1}'. Consider using type annotations to resolve the ambiguity</value>
599599
</data>
600-
<dataname="typrelCannotResolveAmbiguityInOverloadedOperator"xml:space="preserve">
601-
<value>Could not resolve the ambiguity inherent in the use of the operator '{0}' at or near this program point. Consider using type annotations to resolve the ambiguity.</value>
602-
</data>
603600
<dataname="typrelCannotResolveAmbiguityInPrintf"xml:space="preserve">
604601
<value>Could not resolve the ambiguity inherent in the use of a 'printf'-style format string</value>
605602
</data>
@@ -4306,4 +4303,10 @@
43064303
<dataname="tcTupleMemberNotNormallyUsed"xml:space="preserve">
43074304
<value>This method or property is not normally used from F# code, use an explicit tuple pattern for deconstruction instead.</value>
43084305
</data>
4306+
<dataname="implicitlyDiscardedInSequenceExpression"xml:space="preserve">
4307+
<value>This expression returns a value of type '{0}' but is implicitly discarded. Consider using 'let' to bind the result to a name, e.g. 'let result = expression'. If you intended to use the expression as a value in the sequence then use an explicit 'yield'.</value>
4308+
</data>
4309+
<dataname="implicitlyDiscardedSequenceInSequenceExpression"xml:space="preserve">
4310+
<value>This expression returns a value of type '{0}' but is implicitly discarded. Consider using 'let' to bind the result to a name, e.g. 'let result = expression'. If you intended to use the expression as a value in the sequence then use an explicit 'yield!'.</value>
4311+
</data>
43094312
</root>

‎src/fsharp/CompileOps.fs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ let OutputPhasedErrorR (os:StringBuilder) (err:PhasedDiagnostic) =
699699
os.Append(System.Environment.NewLine+ FSComp.SR.derefInsteadOfNot())|> ignore
700700
|_-> os.Append(ErrorFromAddingTypeEquation1E().Format t2 t1 tpcs)|> ignore
701701

702-
| ErrorFromAddingTypeEquation(_,_,_,_,((ConstraintSolverTypesNotInEqualityRelation(_,_,_,_,_, contextInfo))as e),_)when contextInfo<> ContextInfo.NoContext->
702+
| ErrorFromAddingTypeEquation(_,_,_,_,((ConstraintSolverTypesNotInEqualityRelation(_,_,_,_,_, contextInfo))as e),_)when(matchcontextInfowith ContextInfo.NoContext->false|_->true)->
703703
OutputExceptionR os e
704704

705705
| ErrorFromAddingTypeEquation(_,_,_,_,((ConstraintSolverTypesNotInSubsumptionRelation_| ConstraintSolverError_)as e),_)->

‎src/fsharp/ConstraintSolver.fs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ type ContextInfo =
142142
| FollowingPatternMatchClauseofrange
143143
/// The type equation comes from a pattern match guard.
144144
| PatternMatchGuardofrange
145+
/// The type equation comes from a sequence expression.
146+
| SequenceExpressionofTType
145147

146148
exception ConstraintSolverTupleDiffLengthsofDisplayEnv*TTypelist*TTypelist*range*range
147149
exception ConstraintSolverInfiniteTypesofContextInfo*DisplayEnv*TType*TType*range*range

‎src/fsharp/ConstraintSolver.fsi‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ type ContextInfo =
7070
| FollowingPatternMatchClauseofrange
7171
/// The type equation comes from a pattern match guard.
7272
| PatternMatchGuardofrange
73-
73+
/// The type equation comes from a sequence expression.
74+
| SequenceExpressionofTType
7475

7576
exception ConstraintSolverTupleDiffLengthsofDisplayEnv*TTypelist*TTypelist*range*range
7677
exception ConstraintSolverInfiniteTypesofContextInfo*DisplayEnv*TType*TType*range*range

‎src/fsharp/FSComp.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,3 +1422,5 @@ notAFunctionButMaybeDeclaration,"This value is not a function and cannot be appl
14221422
3218,ArgumentsInSigAndImplMismatch,"The argument names in the signature '%s' and implementation '%s' do not match. The argument name from the signature file will be used. This may cause problems when debugging or profiling."
14231423
3219,pickleUnexpectedNonZero,"An error occurred while reading the F# metadata of assembly '%s'. A reserved construct was utilized. You may need to upgrade your F# compiler or use an earlier version of the assembly that doesn't make use of a specific construct."
14241424
3220,tcTupleMemberNotNormallyUsed,"This method or property is not normally used from F# code, use an explicit tuple pattern for deconstruction instead."
1425+
3221,implicitlyDiscardedInSequenceExpression,"This expression returns a value of type '%s' but is implicitly discarded. Consider using 'let' to bind the result to a name, e.g. 'let result = expression'. If you intended to use the expression as a value in the sequence then use an explicit 'yield'."
1426+
3222,implicitlyDiscardedSequenceInSequenceExpression,"This expression returns a value of type '%s' but is implicitly discarded. Consider using 'let' to bind the result to a name, e.g. 'let result = expression'. If you intended to use the expression as a value in the sequence then use an explicit 'yield!'."

‎src/fsharp/TypeChecker.fs‎

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,8 @@ let ShrinkContext env oldRange newRange =
717717
| ContextInfo.ReturnInComputationExpression
718718
| ContextInfo.YieldInComputationExpression
719719
| ContextInfo.RuntimeTypeTest _
720-
| ContextInfo.DowncastUsedInsteadOfUpcast _ ->
720+
| ContextInfo.DowncastUsedInsteadOfUpcast _
721+
| ContextInfo.SequenceExpression _ ->
721722
env
722723
| ContextInfo.CollectionElement (b,m) ->
723724
if m <> oldRange then env else
@@ -830,19 +831,35 @@ let ReportImplicitlyIgnoredBoolExpression denv m ty expr =
830831
extractNext inner
831832
| expr -> checkExpr m expr
832833

833-
let UnifyUnitType cenv denv m ty expr =
834+
let UnifyUnitType cenv (env:TcEnv) m ty expr =
835+
let denv = env.DisplayEnv
834836
if AddCxTypeEqualsTypeUndoIfFailed denv cenv.css m ty cenv.g.unit_ty then
835837
true
836838
else
837839
let domainTy = NewInferenceType ()
838840
let resultTy = NewInferenceType ()
839841
if AddCxTypeEqualsTypeUndoIfFailed denv cenv.css m ty (domainTy --> resultTy) then
840842
warning (FunctionValueUnexpected(denv, ty, m))
841-
else
842-
if not (typeEquiv cenv.g cenv.g.bool_ty ty) then
843-
warning (UnitTypeExpected (denv, ty, m))
844-
else
845-
warning (ReportImplicitlyIgnoredBoolExpression denv m ty expr)
843+
else
844+
let reportImplicitlyDiscardError() =
845+
if typeEquiv cenv.g cenv.g.bool_ty ty then
846+
warning (ReportImplicitlyIgnoredBoolExpression denv m ty expr)
847+
else
848+
warning (UnitTypeExpected (denv, ty, m))
849+
850+
match env.eContextInfo with
851+
| ContextInfo.SequenceExpression seqTy ->
852+
let lifted = mkSeqTy cenv.g ty
853+
if typeEquiv cenv.g seqTy lifted then
854+
warning (Error (FSComp.SR.implicitlyDiscardedInSequenceExpression(NicePrint.prettyStringOfTy denv ty), m))
855+
else
856+
if isListTy cenv.g ty || isArrayTy cenv.g ty || typeEquiv cenv.g seqTy ty then
857+
warning (Error (FSComp.SR.implicitlyDiscardedSequenceInSequenceExpression(NicePrint.prettyStringOfTy denv ty), m))
858+
else
859+
reportImplicitlyDiscardError()
860+
| _ ->
861+
reportImplicitlyDiscardError()
862+
846863
false
847864

848865
//-------------------------------------------------------------------------
@@ -5567,7 +5584,7 @@ and TcStmtThatCantBeCtorBody cenv env tpenv expr =
55675584
and TcStmt cenv env tpenv synExpr =
55685585
let expr, ty, tpenv = TcExprOfUnknownType cenv env tpenv synExpr
55695586
let m = synExpr.Range
5570-
let wasUnit = UnifyUnitType cenv env.DisplayEnv m ty expr
5587+
let wasUnit = UnifyUnitType cenv env m ty expr
55715588
if wasUnit then
55725589
expr, tpenv
55735590
else
@@ -8255,6 +8272,7 @@ and TcSequenceExpression cenv env tpenv comp overallTy m =
82558272
// seq { ...; expr } is treated as 'seq { ... ; expr; yield! Seq.empty }'
82568273
// Note this means seq { ...; () } is treated as 'seq { ... ; (); yield! Seq.empty }'
82578274
let m = comp.Range
8275+
let env = { env with eContextInfo = ContextInfo.SequenceExpression genOuterTy }
82588276
let expr, tpenv = TcStmtThatCantBeCtorBody cenv env tpenv comp
82598277
Expr.Sequential(expr, mkSeqEmpty cenv env m genOuterTy, NormalSeq, SuppressSequencePointOnStmtOfSequential, m), tpenv
82608278

@@ -10413,7 +10431,7 @@ and TcNormalizedBinding declKind (cenv:cenv) env tpenv overallTy safeThisValOpt
1041310431
else TcExprThatCantBeCtorBody cenv overallExprTy envinner tpenv rhsExpr)
1041410432

1041510433
if bkind = StandaloneExpression && not cenv.isScript then
10416-
UnifyUnitType cenv env.DisplayEnv mBinding overallPatTy rhsExprChecked |> ignore<bool>
10434+
UnifyUnitType cenv env mBinding overallPatTy rhsExprChecked |> ignore<bool>
1041710435

1041810436
// Fix up the r.h.s. expression for 'fixed'
1041910437
let rhsExprChecked =

‎src/fsharp/xlf/FSComp.txt.cs.xlf‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6972,6 +6972,16 @@
69726972
<targetstate="translated">Tato metoda nebo vlastnost se obvykle z kódu F# nepoužívá, použijte místo toho pro dekonstrukci explicitní vzor řazené kolekce členů.</target>
69736973
<note />
69746974
</trans-unit>
6975+
<trans-unitid="implicitlyDiscardedInSequenceExpression">
6976+
<source>This expression returns a value of type '{0}' but is implicitly discarded. Consider using 'let' to bind the result to a name, e.g. 'let result = expression'. If you intended to use the expression as a value in the sequence then use an explicit 'yield'.</source>
6977+
<targetstate="new">This expression returns a value of type '{0}' but is implicitly discarded. Consider using 'let' to bind the result to a name, e.g. 'let result = expression'. If you intended to use the expression as a value in the sequence then use an explicit 'yield'.</target>
6978+
<note />
6979+
</trans-unit>
6980+
<trans-unitid="implicitlyDiscardedSequenceInSequenceExpression">
6981+
<source>This expression returns a value of type '{0}' but is implicitly discarded. Consider using 'let' to bind the result to a name, e.g. 'let result = expression'. If you intended to use the expression as a value in the sequence then use an explicit 'yield!'.</source>
6982+
<targetstate="new">This expression returns a value of type '{0}' but is implicitly discarded. Consider using 'let' to bind the result to a name, e.g. 'let result = expression'. If you intended to use the expression as a value in the sequence then use an explicit 'yield!'.</target>
6983+
<note />
6984+
</trans-unit>
69756985
</body>
69766986
</file>
69776987
</xliff>

‎src/fsharp/xlf/FSComp.txt.de.xlf‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6972,6 +6972,16 @@
69726972
<targetstate="translated">Diese Methode oder Eigenschaft wird normalerweise nicht im F#-Code verwendet. Nutzen Sie stattdessen zur Dekonstruktion ein explizites Tupelmuster.</target>
69736973
<note />
69746974
</trans-unit>
6975+
<trans-unitid="implicitlyDiscardedInSequenceExpression">
6976+
<source>This expression returns a value of type '{0}' but is implicitly discarded. Consider using 'let' to bind the result to a name, e.g. 'let result = expression'. If you intended to use the expression as a value in the sequence then use an explicit 'yield'.</source>
6977+
<targetstate="new">This expression returns a value of type '{0}' but is implicitly discarded. Consider using 'let' to bind the result to a name, e.g. 'let result = expression'. If you intended to use the expression as a value in the sequence then use an explicit 'yield'.</target>
6978+
<note />
6979+
</trans-unit>
6980+
<trans-unitid="implicitlyDiscardedSequenceInSequenceExpression">
6981+
<source>This expression returns a value of type '{0}' but is implicitly discarded. Consider using 'let' to bind the result to a name, e.g. 'let result = expression'. If you intended to use the expression as a value in the sequence then use an explicit 'yield!'.</source>
6982+
<targetstate="new">This expression returns a value of type '{0}' but is implicitly discarded. Consider using 'let' to bind the result to a name, e.g. 'let result = expression'. If you intended to use the expression as a value in the sequence then use an explicit 'yield!'.</target>
6983+
<note />
6984+
</trans-unit>
69756985
</body>
69766986
</file>
69776987
</xliff>

‎src/fsharp/xlf/FSComp.txt.en.xlf‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6972,6 +6972,16 @@
69726972
<targetstate="new">This method or property is not normally used from F# code, use an explicit tuple pattern for deconstruction instead.</target>
69736973
<note />
69746974
</trans-unit>
6975+
<trans-unitid="implicitlyDiscardedInSequenceExpression">
6976+
<source>This expression returns a value of type '{0}' but is implicitly discarded. Consider using 'let' to bind the result to a name, e.g. 'let result = expression'. If you intended to use the expression as a value in the sequence then use an explicit 'yield'.</source>
6977+
<targetstate="new">This expression returns a value of type '{0}' but is implicitly discarded. Consider using 'let' to bind the result to a name, e.g. 'let result = expression'. If you intended to use the expression as a value in the sequence then use an explicit 'yield'.</target>
6978+
<note />
6979+
</trans-unit>
6980+
<trans-unitid="implicitlyDiscardedSequenceInSequenceExpression">
6981+
<source>This expression returns a value of type '{0}' but is implicitly discarded. Consider using 'let' to bind the result to a name, e.g. 'let result = expression'. If you intended to use the expression as a value in the sequence then use an explicit 'yield!'.</source>
6982+
<targetstate="new">This expression returns a value of type '{0}' but is implicitly discarded. Consider using 'let' to bind the result to a name, e.g. 'let result = expression'. If you intended to use the expression as a value in the sequence then use an explicit 'yield!'.</target>
6983+
<note />
6984+
</trans-unit>
69756985
</body>
69766986
</file>
69776987
</xliff>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp