Skip to content

Commit b4dc66c

Browse files
dawedaweT-Gro
andauthored
warn about the TailCall attribute being applied to non-rec functions or let-bound values (#16336)
Co-authored-by: Tomas Grosup <tomasgrosup@microsoft.com>
1 parent 0f713ab commit b4dc66c

18 files changed

Lines changed: 226 additions & 1 deletion

‎src/Compiler/Checking/TailCallChecks.fs‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,23 @@ and CheckBindings cenv binds =
727727
CheckBinding cenv false PermitByRefExpr.Yes bind
728728

729729
let CheckModuleBinding cenv (isRec: bool) (TBind _ as bind) =
730+
731+
// warn for non-rec functions which have the attribute
732+
if
733+
cenv.reportErrors
734+
&& cenv.g.langVersion.SupportsFeature LanguageFeature.WarningWhenTailCallAttrOnNonRec
735+
then
736+
let isNotAFunction =
737+
match bind.Var.ValReprInfo with
738+
| Some info -> info.HasNoArgs
739+
| _ -> false
740+
741+
if
742+
(not isRec || isNotAFunction)
743+
&& HasFSharpAttribute cenv.g cenv.g.attrib_TailCallAttribute bind.Var.Attribs
744+
then
745+
warning (Error(FSComp.SR.chkTailCallAttrOnNonRec (), bind.Var.Range))
746+
730747
// Check if a let binding to the result of a rec expression is not inside the rec expression
731748
// Check if a call of a rec expression is not inside a TryWith/TryFinally operation
732749
// see test ``Warn for invalid tailcalls in seq expression because of bind`` for an example

‎src/Compiler/FSComp.txt‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1590,6 +1590,7 @@ featureExtendedFixedBindings,"extended fixed bindings for byref and GetPinnableR
15901590
featurePreferStringGetPinnableReference,"prefer String.GetPinnableReference in fixed bindings"
15911591
featurePreferExtensionMethodOverPlainProperty,"prefer extension method over plain property"
15921592
featureWarningIndexedPropertiesGetSetSameType,"Indexed properties getter and setter must have the same type"
1593+
featureChkTailCallAttrOnNonRec,"Raises warnings if the 'TailCall' attribute is used on non-recursive functions."
15931594
3354,tcNotAFunctionButIndexerNamedIndexingNotYetEnabled,"This value supports indexing, e.g. '%s.[index]'. The syntax '%s[index]' requires /langversion:preview. See https://aka.ms/fsharp-index-notation."
15941595
3354,tcNotAFunctionButIndexerIndexingNotYetEnabled,"This expression supports indexing, e.g. 'expr.[index]'. The syntax 'expr[index]' requires /langversion:preview. See https://aka.ms/fsharp-index-notation."
15951596
3355,tcNotAnIndexerNamedIndexingNotYetEnabled,"The value '%s' is not a function and does not support index notation."
@@ -1731,4 +1732,5 @@ featureUnmanagedConstraintCsharpInterop,"Interop between C#'s and F#'s unmanaged
17311732
3584,tcDotLambdaAtNotSupportedExpression,"Shorthand lambda syntax is only supported for atomic expressions, such as method, property, field or indexer on the implied '_' argument. For example: 'let f = _.Length'."
17321733
3855,tcNoStaticMemberFoundForOverride,"No static abstract member was found that corresponds to this override"
17331734
3859,tcNoStaticPropertyFoundForOverride,"No static abstract property was found that corresponds to this override"
1734-
3860,chkStaticMembersOnObjectExpressions,"Object expressions cannot implement interfaces with static abstract members or declare static members."
1735+
3860,chkStaticMembersOnObjectExpressions,"Object expressions cannot implement interfaces with static abstract members or declare static members."
1736+
3861,chkTailCallAttrOnNonRec,"The TailCall attribute should only be applied to recursive functions."

‎src/Compiler/Facilities/LanguageFeatures.fs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ type LanguageFeature =
8181
| PreferStringGetPinnableReference
8282
| PreferExtensionMethodOverPlainProperty
8383
| WarningIndexedPropertiesGetSetSameType
84+
| WarningWhenTailCallAttrOnNonRec
8485

8586
/// LanguageVersion management
8687
type LanguageVersion(versionText) =
@@ -189,6 +190,7 @@ type LanguageVersion(versionText) =
189190
LanguageFeature.UnmanagedConstraintCsharpInterop, previewVersion
190191
LanguageFeature.PreferExtensionMethodOverPlainProperty, previewVersion
191192
LanguageFeature.WarningIndexedPropertiesGetSetSameType, previewVersion
193+
LanguageFeature.WarningWhenTailCallAttrOnNonRec, previewVersion
192194
]
193195

194196
static let defaultLanguageVersion = LanguageVersion("default")
@@ -327,6 +329,7 @@ type LanguageVersion(versionText) =
327329
| LanguageFeature.PreferStringGetPinnableReference -> FSComp.SR.featurePreferStringGetPinnableReference ()
328330
| LanguageFeature.PreferExtensionMethodOverPlainProperty -> FSComp.SR.featurePreferExtensionMethodOverPlainProperty ()
329331
| LanguageFeature.WarningIndexedPropertiesGetSetSameType -> FSComp.SR.featureWarningIndexedPropertiesGetSetSameType ()
332+
| LanguageFeature.WarningWhenTailCallAttrOnNonRec -> FSComp.SR.featureChkTailCallAttrOnNonRec ()
330333

331334
/// Get a version string associated with the given feature.
332335
static member GetFeatureVersionString feature =

‎src/Compiler/Facilities/LanguageFeatures.fsi‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ type LanguageFeature =
7272
/// RFC-1137
7373
| PreferExtensionMethodOverPlainProperty
7474
| WarningIndexedPropertiesGetSetSameType
75+
| WarningWhenTailCallAttrOnNonRec
7576

7677
/// LanguageVersion management
7778
type LanguageVersion =

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

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/Compiler/xlf/FSComp.txt.es.xlf‎

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/Compiler/xlf/FSComp.txt.fr.xlf‎

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/Compiler/xlf/FSComp.txt.it.xlf‎

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/Compiler/xlf/FSComp.txt.ja.xlf‎

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)