Skip to content

Commit 1816538

Browse files
authored
Enforce attribute targets on functions (#16692)
1 parent db6b8cd commit 1816538

27 files changed

Lines changed: 466 additions & 35 deletions

‎docs/release-notes/.FSharp.Compiler.Service/8.0.300.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* `[<CliEvent>]` member should not produce property symbol. ([Issue #16640](https://github.com/dotnet/fsharp/issues/16640), [PR #16658](https://github.com/dotnet/fsharp/pull/16658))
1212
* Fix discriminated union initialization. ([#PR 16661](https://github.com/dotnet/fsharp/pull/16661))
1313
* Allow calling method with both Optional and ParamArray. ([#PR 16688](https://github.com/dotnet/fsharp/pull/16688), [suggestions #1120](https://github.com/fsharp/fslang-suggestions/issues/1120))
14+
* Enforce AttributeTargets on let values and functions. ([PR #16692](https://github.com/dotnet/fsharp/pull/16692))
1415

1516
### Added
1617

‎docs/release-notes/.Language/preview.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
### Fixed
99

1010
* Allow extension methods without type attribute work for types from imported assemblies. ([PR #16368](https://github.com/dotnet/fsharp/pull/16368))
11+
* Enforce AttributeTargets on let values and functions. ([PR #16692](https://github.com/dotnet/fsharp/pull/16692))
1112

1213
### Changed
1314

14-
* Lower interpolated strings to string concatenation. ([PR #16556](https://github.com/dotnet/fsharp/pull/16556))
15+
* Lower interpolated strings to string concatenation. ([PR #16556](https://github.com/dotnet/fsharp/pull/16556))

‎src/Compiler/Checking/CheckExpressions.fs‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10636,8 +10636,7 @@ and TcNormalizedBinding declKind (cenv: cenv) env tpenv overallTy safeThisValOpt
1063610636
| ModuleOrMemberBinding, SynBindingKind.StandaloneExpression, _ -> Some(".cctor")
1063710637
| _, _, _ -> envinner.eCallerMemberName
1063810638

10639-
let envinner = {envinner with eCallerMemberName = callerName }
10640-
10639+
let envinner = { envinner with eCallerMemberName = callerName }
1064110640
let attrTgt = declKind.AllowedAttribTargets memberFlagsOpt
1064210641

1064310642
let isFixed, rhsExpr, overallPatTy, overallExprTy =
@@ -10873,6 +10872,16 @@ and TcNormalizedBinding declKind (cenv: cenv) env tpenv overallTy safeThisValOpt
1087310872
errorR(Error(FSComp.SR.tcLiteralCannotBeInline(), mBinding))
1087410873
if not (isNil declaredTypars) then
1087510874
errorR(Error(FSComp.SR.tcLiteralCannotHaveGenericParameters(), mBinding))
10875+
10876+
if g.langVersion.SupportsFeature(LanguageFeature.EnforceAttributeTargetsOnFunctions) && memberFlagsOpt.IsNone && not attrs.IsEmpty then
10877+
let rhsIsFunction = isFunTy g overallPatTy
10878+
let lhsIsFunction = isFunTy g overallExprTy
10879+
let attrTgt =
10880+
match rhsIsFunction, lhsIsFunction with
10881+
| false, false when declaredTypars.IsEmpty -> AttributeTargets.Field ||| AttributeTargets.Property ||| AttributeTargets.ReturnValue
10882+
| _, _ -> AttributeTargets.Method ||| AttributeTargets.ReturnValue
10883+
10884+
TcAttributesWithPossibleTargets false cenv env attrTgt attrs |> ignore
1087610885

1087710886
CheckedBindingInfo(inlineFlag, valAttribs, xmlDoc, tcPatPhase2, explicitTyparInfo, nameToPrelimValSchemeMap, rhsExprChecked, argAndRetAttribs, overallPatTy, mBinding, debugPoint, isCompGen, literalValue, isFixed), tpenv
1087810887

‎src/Compiler/Checking/TailCallChecks.fs‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -725,12 +725,7 @@ let CheckModuleBinding cenv (isRec: bool) (TBind _ as bind) =
725725

726726
// warn for non-rec functions which have the attribute
727727
if cenv.g.langVersion.SupportsFeature LanguageFeature.WarningWhenTailCallAttrOnNonRec then
728-
let isNotAFunction =
729-
match bind.Var.ValReprInfo with
730-
| Some info -> info.HasNoArgs
731-
| _ -> false
732-
733-
if (not isRec || isNotAFunction) && cenv.g.HasTailCallAttrib bind.Var.Attribs then
728+
if not isRec && cenv.g.HasTailCallAttrib bind.Var.Attribs then
734729
warning (Error(FSComp.SR.chkTailCallAttrOnNonRec (), bind.Var.Range))
735730

736731
// Check if a let binding to the result of a rec expression is not inside the rec expression

‎src/Compiler/FSComp.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,6 +1594,7 @@ featureWarningIndexedPropertiesGetSetSameType,"Indexed properties getter and set
15941594
featureChkTailCallAttrOnNonRec,"Raises warnings if the 'TailCall' attribute is used on non-recursive functions."
15951595
featureUnionIsPropertiesVisible,"Union case test properties"
15961596
featureBooleanReturningAndReturnTypeDirectedPartialActivePattern,"Boolean-returning and return-type-directed partial active patterns"
1597+
featureEnforceAttributeTargetsOnFunctions,"Enforce AttributeTargets on functions"
15971598
featureLowerInterpolatedStringToConcat,"Optimizes interpolated strings in certain cases, by lowering to concatenation"
15981599
3354,tcNotAFunctionButIndexerNamedIndexingNotYetEnabled,"This value supports indexing, e.g. '%s.[index]'. The syntax '%s[index]' requires /langversion:preview. See https://aka.ms/fsharp-index-notation."
15991600
3354,tcNotAFunctionButIndexerIndexingNotYetEnabled,"This expression supports indexing, e.g. 'expr.[index]'. The syntax 'expr[index]' requires /langversion:preview. See https://aka.ms/fsharp-index-notation."

‎src/Compiler/Facilities/LanguageFeatures.fs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ type LanguageFeature =
8585
| WarningIndexedPropertiesGetSetSameType
8686
| WarningWhenTailCallAttrOnNonRec
8787
| BooleanReturningAndReturnTypeDirectedPartialActivePattern
88+
| EnforceAttributeTargetsOnFunctions
8889
| LowerInterpolatedStringToConcat
8990

9091
/// LanguageVersion management
@@ -198,6 +199,7 @@ type LanguageVersion(versionText) =
198199
LanguageFeature.WarningWhenTailCallAttrOnNonRec, previewVersion
199200
LanguageFeature.UnionIsPropertiesVisible, previewVersion
200201
LanguageFeature.BooleanReturningAndReturnTypeDirectedPartialActivePattern, previewVersion
202+
LanguageFeature.EnforceAttributeTargetsOnFunctions, previewVersion
201203
LanguageFeature.LowerInterpolatedStringToConcat, previewVersion
202204
]
203205

@@ -342,6 +344,7 @@ type LanguageVersion(versionText) =
342344
| LanguageFeature.WarningWhenTailCallAttrOnNonRec -> FSComp.SR.featureChkTailCallAttrOnNonRec ()
343345
| LanguageFeature.BooleanReturningAndReturnTypeDirectedPartialActivePattern ->
344346
FSComp.SR.featureBooleanReturningAndReturnTypeDirectedPartialActivePattern ()
347+
| LanguageFeature.EnforceAttributeTargetsOnFunctions -> FSComp.SR.featureEnforceAttributeTargetsOnFunctions ()
345348
| LanguageFeature.LowerInterpolatedStringToConcat -> FSComp.SR.featureLowerInterpolatedStringToConcat ()
346349

347350
/// Get a version string associated with the given feature.

‎src/Compiler/Facilities/LanguageFeatures.fsi‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ type LanguageFeature =
7676
| WarningIndexedPropertiesGetSetSameType
7777
| WarningWhenTailCallAttrOnNonRec
7878
| BooleanReturningAndReturnTypeDirectedPartialActivePattern
79+
| EnforceAttributeTargetsOnFunctions
7980
| LowerInterpolatedStringToConcat
8081

8182
/// LanguageVersion management

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

Lines changed: 5 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: 5 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: 5 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)