Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
1b8d3f3
Enforce attribute targets on let values
edgarfgp Oct 17, 2023
a4f9568
Merge branch 'main' into enforce-attribute-targets-on-let-bindings
edgarfgp Oct 17, 2023
3c54035
Merge branch 'main' into enforce-attribute-targets-on-let-bindings
edgarfgp Nov 1, 2023
2724872
Add LanguageFeatures
edgarfgp Nov 1, 2023
760247d
Add LanguageFeature preview
edgarfgp Nov 1, 2023
84eb288
Merge branch 'main' into enforce-attribute-targets-on-let-bindings
edgarfgp Nov 2, 2023
b5c9813
Merge branch 'main' into enforce-attribute-targets-on-let-bindings
edgarfgp Nov 7, 2023
78834de
Merge branch 'main' into enforce-attribute-targets-on-let-bindings
edgarfgp Nov 15, 2023
6ffe5c7
Merge branch 'main' into enforce-attribute-targets-on-let-bindings
edgarfgp Nov 29, 2023
36667c0
Check declaredTypars for type functions
edgarfgp Nov 29, 2023
8c3a8a2
Merge branch 'enforce-attribute-targets-on-let-bindings' of https://g…
edgarfgp Nov 29, 2023
816e2c1
WIP adding more tests
edgarfgp Nov 29, 2023
815b2a6
minimize diff
edgarfgp Nov 29, 2023
8c8e95b
WIP more tests
edgarfgp Nov 29, 2023
d5508e4
Merge branch 'enforce-attribute-targets-on-let-bindings' of github.co…
edgarfgp Nov 29, 2023
0f4879b
More tests
edgarfgp Nov 30, 2023
a3392f9
Fix ProjectAnalysisTests
edgarfgp Nov 30, 2023
a9854c1
Fix CompletionProviderTests
edgarfgp Nov 30, 2023
fe334b9
more tests
edgarfgp Nov 30, 2023
6d02165
Merge branch 'main' into enforce-attribute-targets-on-let-bindings
edgarfgp Nov 30, 2023
26e5779
Use AttributeTargets.FieldDecl
edgarfgp Nov 30, 2023
2adfaa0
Remove unnecessary check in TailCall CheckModuleBinding
edgarfgp Nov 30, 2023
ebe76a0
more tests
edgarfgp Nov 30, 2023
bb447de
more tests
edgarfgp Dec 1, 2023
730ce75
Merge branch 'main' into enforce-attribute-targets-on-let-bindings
edgarfgp Dec 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/Compiler/Checking/CheckExpressions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10535,7 +10535,20 @@ and TcNormalizedBinding declKind (cenv: cenv) env tpenv overallTy safeThisValOpt

let envinner = {envinner with eCallerMemberName = callerName }

let attrTgt = declKind.AllowedAttribTargets memberFlagsOpt
let attrTgt =
if g.langVersion.SupportsFeature(LanguageFeature.EnforceAttributeTargetsOnLetValues) then
match pat, rhsExpr with
| SynPat.Named _ , SynExpr.Lambda _
| SynPat.Named _ , SynExpr.App _
| SynPat.Named _ , SynExpr.MatchLambda _ -> AttributeTargets.Method ||| AttributeTargets.ReturnValue
| SynPat.Named _, _ when not declaredTypars.IsEmpty -> AttributeTargets.Method ||| AttributeTargets.ReturnValue
| SynPat.Named _ , SynExpr.Ident(ident = ident) when ident.idText = "id" -> AttributeTargets.Method ||| AttributeTargets.ReturnValue

| SynPat.Tuple _ , _
| SynPat.Named _, _ when isNil declaredTypars -> AttributeTargets.FieldDecl
| _ -> declKind.AllowedAttribTargets memberFlagsOpt
else
declKind.AllowedAttribTargets memberFlagsOpt

let isFixed, rhsExpr, overallPatTy, overallExprTy =
match rhsExpr with
Expand Down Expand Up @@ -10815,7 +10828,7 @@ and TcNonRecursiveBinding declKind cenv env tpenv ty binding =
warning(Error(FSComp.SR.tcInfoIfFunctionShadowsUnionCase(), headPatRange))
| _ -> ()
| _ -> ()

let binding = BindingNormalization.NormalizeBinding ValOrMemberBinding cenv env binding
let explicitTyparInfo, tpenv = TcNonrecBindingTyparDecls cenv env tpenv binding
TcNormalizedBinding declKind cenv env tpenv ty None NoSafeInitInfo ([], explicitTyparInfo) binding
Expand Down
7 changes: 1 addition & 6 deletions src/Compiler/Checking/TailCallChecks.fs
Original file line number Diff line number Diff line change
Expand Up @@ -733,13 +733,8 @@ let CheckModuleBinding cenv (isRec: bool) (TBind _ as bind) =
cenv.reportErrors
&& cenv.g.langVersion.SupportsFeature LanguageFeature.WarningWhenTailCallAttrOnNonRec
then
let isNotAFunction =
match bind.Var.ValReprInfo with
| Some info -> info.HasNoArgs
| _ -> false

if
(not isRec || isNotAFunction)
not isRec
&& HasFSharpAttribute cenv.g cenv.g.attrib_TailCallAttribute bind.Var.Attribs
then
warning (Error(FSComp.SR.chkTailCallAttrOnNonRec (), bind.Var.Range))
Expand Down
1 change: 1 addition & 0 deletions src/Compiler/FSComp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1588,6 +1588,7 @@ featureChkNotTailRecursive,"Raises warnings if a member or function has the 'Tai
featureWhileBang,"'while!' expression"
featureExtendedFixedBindings,"extended fixed bindings for byref and GetPinnableReference"
featurePreferStringGetPinnableReference,"prefer String.GetPinnableReference in fixed bindings"
featureEnforceAttributeTargetsOnLetValues,"Enforce AttributeTargets on let values"
featurePreferExtensionMethodOverPlainProperty,"prefer extension method over plain property"
featureWarningIndexedPropertiesGetSetSameType,"Indexed properties getter and setter must have the same type"
featureChkTailCallAttrOnNonRec,"Raises warnings if the 'TailCall' attribute is used on non-recursive functions."
Expand Down
3 changes: 3 additions & 0 deletions src/Compiler/Facilities/LanguageFeatures.fs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ type LanguageFeature =
| ReuseSameFieldsInStructUnions
| ExtendedFixedBindings
| PreferStringGetPinnableReference
| EnforceAttributeTargetsOnLetValues
| PreferExtensionMethodOverPlainProperty
| WarningIndexedPropertiesGetSetSameType
| WarningWhenTailCallAttrOnNonRec
Expand Down Expand Up @@ -189,6 +190,7 @@ type LanguageVersion(versionText) =
// F# preview
LanguageFeature.FromEndSlicing, previewVersion
LanguageFeature.UnmanagedConstraintCsharpInterop, previewVersion
LanguageFeature.EnforceAttributeTargetsOnLetValues, previewVersion
LanguageFeature.ReuseSameFieldsInStructUnions, previewVersion
LanguageFeature.PreferExtensionMethodOverPlainProperty, previewVersion
LanguageFeature.WarningIndexedPropertiesGetSetSameType, previewVersion
Expand Down Expand Up @@ -330,6 +332,7 @@ type LanguageVersion(versionText) =
| LanguageFeature.ReuseSameFieldsInStructUnions -> FSComp.SR.featureReuseSameFieldsInStructUnions ()
| LanguageFeature.ExtendedFixedBindings -> FSComp.SR.featureExtendedFixedBindings ()
| LanguageFeature.PreferStringGetPinnableReference -> FSComp.SR.featurePreferStringGetPinnableReference ()
| LanguageFeature.EnforceAttributeTargetsOnLetValues -> FSComp.SR.featureEnforceAttributeTargetsOnLetValues ()
| LanguageFeature.PreferExtensionMethodOverPlainProperty -> FSComp.SR.featurePreferExtensionMethodOverPlainProperty ()
| LanguageFeature.WarningIndexedPropertiesGetSetSameType -> FSComp.SR.featureWarningIndexedPropertiesGetSetSameType ()
| LanguageFeature.WarningWhenTailCallAttrOnNonRec -> FSComp.SR.featureChkTailCallAttrOnNonRec ()
Expand Down
1 change: 1 addition & 0 deletions src/Compiler/Facilities/LanguageFeatures.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ type LanguageFeature =
| ReuseSameFieldsInStructUnions
| ExtendedFixedBindings
| PreferStringGetPinnableReference
| EnforceAttributeTargetsOnLetValues
/// RFC-1137
| PreferExtensionMethodOverPlainProperty
| WarningIndexedPropertiesGetSetSameType
Expand Down
5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.tr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.zh-Hans.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/Compiler/xlf/FSComp.txt.zh-Hant.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// #Regression #Conformance #DeclarationElements #Attributes
// Regression test for FSharp1.0:5172
// Title: "method" attribute target is not recognized
// Descr: Verify that attribute target 'method' is recognized by F# compiler correctly.
open System

[<AttributeUsage(AttributeTargets.Method)>]
type MethodOnlyAttribute() =
inherit Attribute()

[<MethodOnly>]
let someValue = "someValue" // Should fail

[<MethodOnly>]
let i, j, k = (1, 2, 3) // Should fail

[<MethodOnly>]
let someFunction () = "someFunction"

[<MethodOnly>]
let someFunction2 a = a + 1

[<MethodOnly>]
let someFunction3 (a, b) = a + b

[<MethodOnly>]
let someFunction4 (a: int) : int = a + 1

[<MethodOnly>]
let makeList a b = [ a; b ]

[<MethodOnly>]
let someTypedFunction<'a> = "someTypedFunction"

[<MethodOnly>]
let someTypedFunction2<'a> (x : 'a) = "someTypedFunction2"

[<MethodOnly>]
let someTypedFunction3 = fun x -> x

[<MethodOnly>]
let someTypedFunction4 = id

[<MethodOnly>]
let __someTypedFunction5<'a> = false

[<MethodOnly>]
let __someTypedFunction6<'a> : bool = false

[<MethodOnly>]
let ``someValue2`` = "someValue" // Should fail

type TestConst =
| Bool of bool

[<NoComparison>]
type TestExpr =
| Const of TestConst * Type * int
| Var of string * Type * int

[<MethodOnly>]
let (|BoolExpr|_|) =
function
| TestExpr.Const (TestConst.Bool b, _, _) -> Some b
| _ -> None

[<MethodOnly>]
[<return: Struct>]
let (|BoolExpr2|_|) =
function
| TestExpr.Const(TestConst.Bool b1, _, _) -> ValueSome b1
| _ -> ValueNone

[<MethodOnly>]
let (|BoolExpr3|_|) x =
match x with
| TestExpr.Const (TestConst.Bool b, _, _) -> Some b
| _ -> None

[<MethodOnly>]
[<return: Struct>]
let (|BoolExpr4|_|) x =
match x with
| TestExpr.Const(TestConst.Bool b1, _, _) -> ValueSome b1
| _ -> ValueNone

let private dangling (target: TestExpr -> TestExpr voption) =
function
| TestExpr.Const (TestConst.Bool _, _, _) as b -> ValueSome b
| _ -> ValueNone

[<MethodOnly>]
[<return: Struct>]
let (|IfThen|_|) =
dangling (function
| TestExpr.Const(TestConst.Bool b1, _, _) as expr -> ValueSome expr
| _ -> ValueNone)

[<MethodOnly>]
let someRecLetBoundValue = nameof(MethodOnlyAttribute) // Should fail

[<MethodOnly>]
let rec someRecLetBoundValue2 = nameof(someRecLetBoundValue2) // Should fail
Loading