From 1816538b7f664d36c673e1fe6f6b36bb6d06aa64 Mon Sep 17 00:00:00 2001 From: Edgar Gonzalez Date: Mon, 26 Feb 2024 11:18:40 +0000 Subject: [PATCH 1/4] Enforce attribute targets on functions (#16692) --- .../.FSharp.Compiler.Service/8.0.300.md | 1 + docs/release-notes/.Language/preview.md | 3 +- src/Compiler/Checking/CheckExpressions.fs | 13 +- src/Compiler/Checking/TailCallChecks.fs | 7 +- src/Compiler/FSComp.txt | 1 + src/Compiler/Facilities/LanguageFeatures.fs | 3 + src/Compiler/Facilities/LanguageFeatures.fsi | 1 + src/Compiler/xlf/FSComp.txt.cs.xlf | 5 + src/Compiler/xlf/FSComp.txt.de.xlf | 5 + src/Compiler/xlf/FSComp.txt.es.xlf | 5 + src/Compiler/xlf/FSComp.txt.fr.xlf | 5 + src/Compiler/xlf/FSComp.txt.it.xlf | 5 + src/Compiler/xlf/FSComp.txt.ja.xlf | 5 + src/Compiler/xlf/FSComp.txt.ko.xlf | 5 + src/Compiler/xlf/FSComp.txt.pl.xlf | 5 + src/Compiler/xlf/FSComp.txt.pt-BR.xlf | 5 + src/Compiler/xlf/FSComp.txt.ru.xlf | 5 + src/Compiler/xlf/FSComp.txt.tr.xlf | 5 + src/Compiler/xlf/FSComp.txt.zh-Hans.xlf | 5 + src/Compiler/xlf/FSComp.txt.zh-Hant.xlf | 5 + .../AttributeUsage/AttributeUsage.fs | 141 ++++++++++++++++++ .../E_AttributeTargetIsField01.fs | 67 +++++++++ .../E_AttributeTargetIsField02.fs | 69 +++++++++ .../E_AttributeTargetIsMethod02.fs | 40 +++++ .../E_AttributeTargetIsMethod03.fs | 54 +++++++ .../ErrorMessages/TailCallAttribute.fs | 24 +-- tests/service/ProjectAnalysisTests.fs | 12 +- 27 files changed, 466 insertions(+), 35 deletions(-) create mode 100644 tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/E_AttributeTargetIsField01.fs create mode 100644 tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/E_AttributeTargetIsField02.fs create mode 100644 tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/E_AttributeTargetIsMethod02.fs create mode 100644 tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/E_AttributeTargetIsMethod03.fs diff --git a/docs/release-notes/.FSharp.Compiler.Service/8.0.300.md b/docs/release-notes/.FSharp.Compiler.Service/8.0.300.md index 80d327e5d9e..786a741dbfa 100644 --- a/docs/release-notes/.FSharp.Compiler.Service/8.0.300.md +++ b/docs/release-notes/.FSharp.Compiler.Service/8.0.300.md @@ -11,6 +11,7 @@ * `[]` member should not produce property symbol. ([Issue #16640](https://github.com/dotnet/fsharp/issues/16640), [PR #16658](https://github.com/dotnet/fsharp/pull/16658)) * Fix discriminated union initialization. ([#PR 16661](https://github.com/dotnet/fsharp/pull/16661)) * 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)) +* Enforce AttributeTargets on let values and functions. ([PR #16692](https://github.com/dotnet/fsharp/pull/16692)) ### Added diff --git a/docs/release-notes/.Language/preview.md b/docs/release-notes/.Language/preview.md index d1880b4dd85..f39dd172561 100644 --- a/docs/release-notes/.Language/preview.md +++ b/docs/release-notes/.Language/preview.md @@ -8,7 +8,8 @@ ### Fixed * Allow extension methods without type attribute work for types from imported assemblies. ([PR #16368](https://github.com/dotnet/fsharp/pull/16368)) +* Enforce AttributeTargets on let values and functions. ([PR #16692](https://github.com/dotnet/fsharp/pull/16692)) ### Changed -* Lower interpolated strings to string concatenation. ([PR #16556](https://github.com/dotnet/fsharp/pull/16556)) \ No newline at end of file +* Lower interpolated strings to string concatenation. ([PR #16556](https://github.com/dotnet/fsharp/pull/16556)) diff --git a/src/Compiler/Checking/CheckExpressions.fs b/src/Compiler/Checking/CheckExpressions.fs index 89268e1ee6e..3e3e90a72fe 100644 --- a/src/Compiler/Checking/CheckExpressions.fs +++ b/src/Compiler/Checking/CheckExpressions.fs @@ -10636,8 +10636,7 @@ and TcNormalizedBinding declKind (cenv: cenv) env tpenv overallTy safeThisValOpt | ModuleOrMemberBinding, SynBindingKind.StandaloneExpression, _ -> Some(".cctor") | _, _, _ -> envinner.eCallerMemberName - let envinner = {envinner with eCallerMemberName = callerName } - + let envinner = { envinner with eCallerMemberName = callerName } let attrTgt = declKind.AllowedAttribTargets memberFlagsOpt let isFixed, rhsExpr, overallPatTy, overallExprTy = @@ -10873,6 +10872,16 @@ and TcNormalizedBinding declKind (cenv: cenv) env tpenv overallTy safeThisValOpt errorR(Error(FSComp.SR.tcLiteralCannotBeInline(), mBinding)) if not (isNil declaredTypars) then errorR(Error(FSComp.SR.tcLiteralCannotHaveGenericParameters(), mBinding)) + + if g.langVersion.SupportsFeature(LanguageFeature.EnforceAttributeTargetsOnFunctions) && memberFlagsOpt.IsNone && not attrs.IsEmpty then + let rhsIsFunction = isFunTy g overallPatTy + let lhsIsFunction = isFunTy g overallExprTy + let attrTgt = + match rhsIsFunction, lhsIsFunction with + | false, false when declaredTypars.IsEmpty -> AttributeTargets.Field ||| AttributeTargets.Property ||| AttributeTargets.ReturnValue + | _, _ -> AttributeTargets.Method ||| AttributeTargets.ReturnValue + + TcAttributesWithPossibleTargets false cenv env attrTgt attrs |> ignore CheckedBindingInfo(inlineFlag, valAttribs, xmlDoc, tcPatPhase2, explicitTyparInfo, nameToPrelimValSchemeMap, rhsExprChecked, argAndRetAttribs, overallPatTy, mBinding, debugPoint, isCompGen, literalValue, isFixed), tpenv diff --git a/src/Compiler/Checking/TailCallChecks.fs b/src/Compiler/Checking/TailCallChecks.fs index c26683e3da9..ce6ab702bdc 100644 --- a/src/Compiler/Checking/TailCallChecks.fs +++ b/src/Compiler/Checking/TailCallChecks.fs @@ -725,12 +725,7 @@ let CheckModuleBinding cenv (isRec: bool) (TBind _ as bind) = // warn for non-rec functions which have the attribute if cenv.g.langVersion.SupportsFeature LanguageFeature.WarningWhenTailCallAttrOnNonRec then - let isNotAFunction = - match bind.Var.ValReprInfo with - | Some info -> info.HasNoArgs - | _ -> false - - if (not isRec || isNotAFunction) && cenv.g.HasTailCallAttrib bind.Var.Attribs then + if not isRec && cenv.g.HasTailCallAttrib bind.Var.Attribs then warning (Error(FSComp.SR.chkTailCallAttrOnNonRec (), bind.Var.Range)) // Check if a let binding to the result of a rec expression is not inside the rec expression diff --git a/src/Compiler/FSComp.txt b/src/Compiler/FSComp.txt index 1fef46084d0..a1086629fbe 100644 --- a/src/Compiler/FSComp.txt +++ b/src/Compiler/FSComp.txt @@ -1594,6 +1594,7 @@ featureWarningIndexedPropertiesGetSetSameType,"Indexed properties getter and set featureChkTailCallAttrOnNonRec,"Raises warnings if the 'TailCall' attribute is used on non-recursive functions." featureUnionIsPropertiesVisible,"Union case test properties" featureBooleanReturningAndReturnTypeDirectedPartialActivePattern,"Boolean-returning and return-type-directed partial active patterns" +featureEnforceAttributeTargetsOnFunctions,"Enforce AttributeTargets on functions" featureLowerInterpolatedStringToConcat,"Optimizes interpolated strings in certain cases, by lowering to concatenation" 3354,tcNotAFunctionButIndexerNamedIndexingNotYetEnabled,"This value supports indexing, e.g. '%s.[index]'. The syntax '%s[index]' requires /langversion:preview. See https://aka.ms/fsharp-index-notation." 3354,tcNotAFunctionButIndexerIndexingNotYetEnabled,"This expression supports indexing, e.g. 'expr.[index]'. The syntax 'expr[index]' requires /langversion:preview. See https://aka.ms/fsharp-index-notation." diff --git a/src/Compiler/Facilities/LanguageFeatures.fs b/src/Compiler/Facilities/LanguageFeatures.fs index 66547a12e29..6e2d91bc6cd 100644 --- a/src/Compiler/Facilities/LanguageFeatures.fs +++ b/src/Compiler/Facilities/LanguageFeatures.fs @@ -85,6 +85,7 @@ type LanguageFeature = | WarningIndexedPropertiesGetSetSameType | WarningWhenTailCallAttrOnNonRec | BooleanReturningAndReturnTypeDirectedPartialActivePattern + | EnforceAttributeTargetsOnFunctions | LowerInterpolatedStringToConcat /// LanguageVersion management @@ -198,6 +199,7 @@ type LanguageVersion(versionText) = LanguageFeature.WarningWhenTailCallAttrOnNonRec, previewVersion LanguageFeature.UnionIsPropertiesVisible, previewVersion LanguageFeature.BooleanReturningAndReturnTypeDirectedPartialActivePattern, previewVersion + LanguageFeature.EnforceAttributeTargetsOnFunctions, previewVersion LanguageFeature.LowerInterpolatedStringToConcat, previewVersion ] @@ -342,6 +344,7 @@ type LanguageVersion(versionText) = | LanguageFeature.WarningWhenTailCallAttrOnNonRec -> FSComp.SR.featureChkTailCallAttrOnNonRec () | LanguageFeature.BooleanReturningAndReturnTypeDirectedPartialActivePattern -> FSComp.SR.featureBooleanReturningAndReturnTypeDirectedPartialActivePattern () + | LanguageFeature.EnforceAttributeTargetsOnFunctions -> FSComp.SR.featureEnforceAttributeTargetsOnFunctions () | LanguageFeature.LowerInterpolatedStringToConcat -> FSComp.SR.featureLowerInterpolatedStringToConcat () /// Get a version string associated with the given feature. diff --git a/src/Compiler/Facilities/LanguageFeatures.fsi b/src/Compiler/Facilities/LanguageFeatures.fsi index 582c191ac8a..4888479d867 100644 --- a/src/Compiler/Facilities/LanguageFeatures.fsi +++ b/src/Compiler/Facilities/LanguageFeatures.fsi @@ -76,6 +76,7 @@ type LanguageFeature = | WarningIndexedPropertiesGetSetSameType | WarningWhenTailCallAttrOnNonRec | BooleanReturningAndReturnTypeDirectedPartialActivePattern + | EnforceAttributeTargetsOnFunctions | LowerInterpolatedStringToConcat /// LanguageVersion management diff --git a/src/Compiler/xlf/FSComp.txt.cs.xlf b/src/Compiler/xlf/FSComp.txt.cs.xlf index 27098c6bd1e..d4142d783fa 100644 --- a/src/Compiler/xlf/FSComp.txt.cs.xlf +++ b/src/Compiler/xlf/FSComp.txt.cs.xlf @@ -292,6 +292,11 @@ literál float32 bez tečky + + Enforce AttributeTargets on functions + Enforce AttributeTargets on functions + + Raises errors for non-virtual members overrides Vyvolá chyby pro přepsání jiných než virtuálních členů diff --git a/src/Compiler/xlf/FSComp.txt.de.xlf b/src/Compiler/xlf/FSComp.txt.de.xlf index c89e0988f8e..fa0904ef50f 100644 --- a/src/Compiler/xlf/FSComp.txt.de.xlf +++ b/src/Compiler/xlf/FSComp.txt.de.xlf @@ -292,6 +292,11 @@ punktloses float32-Literal + + Enforce AttributeTargets on functions + Enforce AttributeTargets on functions + + Raises errors for non-virtual members overrides Löst Fehler für Außerkraftsetzungen nicht virtueller Member aus. diff --git a/src/Compiler/xlf/FSComp.txt.es.xlf b/src/Compiler/xlf/FSComp.txt.es.xlf index de54c17c1f3..2d5ada63574 100644 --- a/src/Compiler/xlf/FSComp.txt.es.xlf +++ b/src/Compiler/xlf/FSComp.txt.es.xlf @@ -292,6 +292,11 @@ literal float32 sin punto + + Enforce AttributeTargets on functions + Enforce AttributeTargets on functions + + Raises errors for non-virtual members overrides Genera errores para invalidaciones de miembros no virtuales diff --git a/src/Compiler/xlf/FSComp.txt.fr.xlf b/src/Compiler/xlf/FSComp.txt.fr.xlf index 17807523602..da3c6f4b9b6 100644 --- a/src/Compiler/xlf/FSComp.txt.fr.xlf +++ b/src/Compiler/xlf/FSComp.txt.fr.xlf @@ -292,6 +292,11 @@ littéral float32 sans point + + Enforce AttributeTargets on functions + Enforce AttributeTargets on functions + + Raises errors for non-virtual members overrides Déclenche des erreurs pour les remplacements de membres non virtuels diff --git a/src/Compiler/xlf/FSComp.txt.it.xlf b/src/Compiler/xlf/FSComp.txt.it.xlf index 1661484a63c..010e34a81a2 100644 --- a/src/Compiler/xlf/FSComp.txt.it.xlf +++ b/src/Compiler/xlf/FSComp.txt.it.xlf @@ -292,6 +292,11 @@ valore letterale float32 senza punti + + Enforce AttributeTargets on functions + Enforce AttributeTargets on functions + + Raises errors for non-virtual members overrides Genera errori per gli override dei membri non virtuali diff --git a/src/Compiler/xlf/FSComp.txt.ja.xlf b/src/Compiler/xlf/FSComp.txt.ja.xlf index a5a04b7c1b0..8a92ac2ea9e 100644 --- a/src/Compiler/xlf/FSComp.txt.ja.xlf +++ b/src/Compiler/xlf/FSComp.txt.ja.xlf @@ -292,6 +292,11 @@ ドットなしの float32 リテラル + + Enforce AttributeTargets on functions + Enforce AttributeTargets on functions + + Raises errors for non-virtual members overrides 仮想メンバー以外のオーバーライドに対してエラーを発生させます diff --git a/src/Compiler/xlf/FSComp.txt.ko.xlf b/src/Compiler/xlf/FSComp.txt.ko.xlf index d419bd0d99b..51778bd4145 100644 --- a/src/Compiler/xlf/FSComp.txt.ko.xlf +++ b/src/Compiler/xlf/FSComp.txt.ko.xlf @@ -292,6 +292,11 @@ 점이 없는 float32 리터럴 + + Enforce AttributeTargets on functions + Enforce AttributeTargets on functions + + Raises errors for non-virtual members overrides 비가상 멤버 재정의에 대한 오류 발생 diff --git a/src/Compiler/xlf/FSComp.txt.pl.xlf b/src/Compiler/xlf/FSComp.txt.pl.xlf index 65d582f3796..a8a3f05ba9a 100644 --- a/src/Compiler/xlf/FSComp.txt.pl.xlf +++ b/src/Compiler/xlf/FSComp.txt.pl.xlf @@ -292,6 +292,11 @@ bezkropkowy literał float32 + + Enforce AttributeTargets on functions + Enforce AttributeTargets on functions + + Raises errors for non-virtual members overrides Zgłasza błędy w przypadku przesłonięć elementów innych niż wirtualne diff --git a/src/Compiler/xlf/FSComp.txt.pt-BR.xlf b/src/Compiler/xlf/FSComp.txt.pt-BR.xlf index 93af3d22e75..1f62f9a3d04 100644 --- a/src/Compiler/xlf/FSComp.txt.pt-BR.xlf +++ b/src/Compiler/xlf/FSComp.txt.pt-BR.xlf @@ -292,6 +292,11 @@ literal float32 sem ponto + + Enforce AttributeTargets on functions + Enforce AttributeTargets on functions + + Raises errors for non-virtual members overrides Gera erros para substituições de membros não virtuais diff --git a/src/Compiler/xlf/FSComp.txt.ru.xlf b/src/Compiler/xlf/FSComp.txt.ru.xlf index b13f061163b..64ce92b6125 100644 --- a/src/Compiler/xlf/FSComp.txt.ru.xlf +++ b/src/Compiler/xlf/FSComp.txt.ru.xlf @@ -292,6 +292,11 @@ литерал float32 без точки + + Enforce AttributeTargets on functions + Enforce AttributeTargets on functions + + Raises errors for non-virtual members overrides Вызывает ошибки при переопределениях невиртуальных элементов diff --git a/src/Compiler/xlf/FSComp.txt.tr.xlf b/src/Compiler/xlf/FSComp.txt.tr.xlf index ad43bfe169b..d77dfd672ec 100644 --- a/src/Compiler/xlf/FSComp.txt.tr.xlf +++ b/src/Compiler/xlf/FSComp.txt.tr.xlf @@ -292,6 +292,11 @@ noktasız float32 sabit değeri + + Enforce AttributeTargets on functions + Enforce AttributeTargets on functions + + Raises errors for non-virtual members overrides Sanal olmayan üyelerde geçersiz kılmalar için hatalar oluştur diff --git a/src/Compiler/xlf/FSComp.txt.zh-Hans.xlf b/src/Compiler/xlf/FSComp.txt.zh-Hans.xlf index d7013ad94e7..5393d25202a 100644 --- a/src/Compiler/xlf/FSComp.txt.zh-Hans.xlf +++ b/src/Compiler/xlf/FSComp.txt.zh-Hans.xlf @@ -292,6 +292,11 @@ 无点 float32 文本 + + Enforce AttributeTargets on functions + Enforce AttributeTargets on functions + + Raises errors for non-virtual members overrides 引发非虚拟成员替代的错误 diff --git a/src/Compiler/xlf/FSComp.txt.zh-Hant.xlf b/src/Compiler/xlf/FSComp.txt.zh-Hant.xlf index 902612c1b64..f75fc155fde 100644 --- a/src/Compiler/xlf/FSComp.txt.zh-Hant.xlf +++ b/src/Compiler/xlf/FSComp.txt.zh-Hant.xlf @@ -292,6 +292,11 @@ 無點號的 float32 常值 + + Enforce AttributeTargets on functions + Enforce AttributeTargets on functions + + Raises errors for non-virtual members overrides 引發非虛擬成員覆寫的錯誤 diff --git a/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/AttributeUsage.fs b/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/AttributeUsage.fs index cddee9f80ab..c7861ee3e56 100644 --- a/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/AttributeUsage.fs +++ b/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/AttributeUsage.fs @@ -94,6 +94,147 @@ module CustomAttributes_AttributeUsage = (Error 842, Line 24, Col 7, Line 24, Col 36, "This attribute is not valid for use on this language element") (Error 842, Line 29, Col 15, Line 29, Col 47, "This attribute is not valid for use on this language element") ] + + // SOURCE=E_AttributeTargetIsField01.fs # E_AttributeTargetIsField01.fs + [] + let ``E_AttributeTargetIsField01_fs`` compilation = + compilation + |> withLangVersion80 + |> withOptions ["--nowarn:25"] + |> verifyCompile + |> shouldSucceed + + // SOURCE=E_AttributeTargetIsField01.fs # E_AttributeTargetIsField01.fs + [] + let ``E_AttributeTargetIsField01_fs preview`` compilation = + compilation + |> withLangVersionPreview + |> withOptions ["--nowarn:25"] + |> verifyCompile + |> shouldFail + |> withDiagnostics [ + (Error 842, Line 9, Col 3, Line 9, Col 12, "This attribute is not valid for use on this language element") + (Error 842, Line 12, Col 3, Line 12, Col 12, "This attribute is not valid for use on this language element") + (Error 842, Line 15, Col 3, Line 15, Col 12, "This attribute is not valid for use on this language element") + (Error 842, Line 18, Col 3, Line 18, Col 12, "This attribute is not valid for use on this language element") + (Error 842, Line 21, Col 3, Line 21, Col 12, "This attribute is not valid for use on this language element") + (Error 842, Line 24, Col 3, Line 24, Col 12, "This attribute is not valid for use on this language element") + (Error 842, Line 27, Col 3, Line 27, Col 12, "This attribute is not valid for use on this language element") + (Error 842, Line 30, Col 3, Line 30, Col 12, "This attribute is not valid for use on this language element") + (Error 842, Line 33, Col 3, Line 33, Col 12, "This attribute is not valid for use on this language element") + (Error 842, Line 36, Col 3, Line 36, Col 12, "This attribute is not valid for use on this language element") + (Error 842, Line 39, Col 3, Line 39, Col 12, "This attribute is not valid for use on this language element") + (Error 842, Line 42, Col 3, Line 42, Col 12, "This attribute is not valid for use on this language element") + (Error 842, Line 45, Col 3, Line 45, Col 12, "This attribute is not valid for use on this language element") + (Error 842, Line 49, Col 3, Line 49, Col 12, "This attribute is not valid for use on this language element") + (Error 842, Line 56, Col 3, Line 56, Col 12, "This attribute is not valid for use on this language element") + (Error 842, Line 64, Col 3, Line 64, Col 12, "This attribute is not valid for use on this language element") + (Error 842, Line 66, Col 7, Line 66, Col 16, "This attribute is not valid for use on this language element") + ] + + // SOURCE=E_AttributeTargetIsField02.fs # E_AttributeTargetIsField02.fs + [] + let ``E_AttributeTargetIsField02_fs`` compilation = + compilation + |> withLangVersion80 + |> withOptions ["--nowarn:25"] + |> verifyCompile + |> shouldSucceed + + // SOURCE=E_AttributeTargetIsField02.fs # E_AttributeTargetIsField02.fs + [] + let ``E_AttributeTargetIsField02_fs preview`` compilation = + compilation + |> withLangVersionPreview + |> withOptions ["--nowarn:25"] + |> verifyCompile + |> shouldFail + |> withDiagnostics [ + (Error 842, Line 11, Col 6, Line 11, Col 15, "This attribute is not valid for use on this language element") + (Error 842, Line 14, Col 6, Line 14, Col 15, "This attribute is not valid for use on this language element") + (Error 842, Line 17, Col 6, Line 17, Col 15, "This attribute is not valid for use on this language element") + (Error 842, Line 19, Col 10, Line 19, Col 19, "This attribute is not valid for use on this language element") + (Error 842, Line 21, Col 6, Line 21, Col 15, "This attribute is not valid for use on this language element") + (Error 842, Line 24, Col 6, Line 24, Col 15, "This attribute is not valid for use on this language element") + (Error 842, Line 27, Col 6, Line 27, Col 15, "This attribute is not valid for use on this language element") + (Error 842, Line 30, Col 6, Line 30, Col 15, "This attribute is not valid for use on this language element") + (Error 842, Line 33, Col 6, Line 33, Col 15, "This attribute is not valid for use on this language element") + (Error 842, Line 36, Col 6, Line 36, Col 15, "This attribute is not valid for use on this language element") + (Error 842, Line 39, Col 6, Line 39, Col 15, "This attribute is not valid for use on this language element") + (Error 842, Line 42, Col 6, Line 42, Col 15, "This attribute is not valid for use on this language element") + (Error 842, Line 45, Col 6, Line 45, Col 15, "This attribute is not valid for use on this language element") + (Error 842, Line 49, Col 6, Line 49, Col 15, "This attribute is not valid for use on this language element") + (Error 842, Line 56, Col 6, Line 56, Col 15, "This attribute is not valid for use on this language element") + (Error 842, Line 64, Col 6, Line 64, Col 15, "This attribute is not valid for use on this language element") + (Error 842, Line 66, Col 10, Line 66, Col 19, "This attribute is not valid for use on this language element") + (Error 842, Line 68, Col 6, Line 68, Col 15, "This attribute is not valid for use on this language element") + ] + + // SOURCE=E_AttributeTargetIsMethod02.fs # E_AttributeTargetIsMethod02.fs + [] + let ``E_AttributeTargetIsMethod02_fs`` compilation = + compilation + |> withLangVersion80 + |> withOptions ["--nowarn:25"] + |> verifyCompile + |> shouldSucceed + + // SOURCE=E_AttributeTargetIsMethod02.fs # E_AttributeTargetIsMethod02.fs + [] + let ``E_AttributeTargetIsMethod02_fs preview`` compilation = + compilation + |> withLangVersionPreview + |> withOptions ["--nowarn:25"] + |> verifyCompile + |> shouldFail + |> withDiagnostics [ + (Error 842, Line 9, Col 3, Line 9, Col 13, "This attribute is not valid for use on this language element") + (Error 842, Line 12, Col 3, Line 12, Col 13, "This attribute is not valid for use on this language element") + (Error 842, Line 15, Col 3, Line 15, Col 13, "This attribute is not valid for use on this language element") + (Error 842, Line 18, Col 3, Line 18, Col 13, "This attribute is not valid for use on this language element") + (Error 842, Line 21, Col 3, Line 21, Col 13, "This attribute is not valid for use on this language element") + (Error 842, Line 24, Col 3, Line 24, Col 13, "This attribute is not valid for use on this language element") + (Error 842, Line 26, Col 7, Line 26, Col 17, "This attribute is not valid for use on this language element") + (Error 842, Line 28, Col 3, Line 28, Col 13, "This attribute is not valid for use on this language element") + (Error 842, Line 31, Col 3, Line 31, Col 13, "This attribute is not valid for use on this language element") + (Error 842, Line 34, Col 3, Line 34, Col 13, "This attribute is not valid for use on this language element") + (Error 842, Line 39, Col 3, Line 39, Col 13, "This attribute is not valid for use on this language element") + ] + + // SOURCE=E_AttributeTargetIsMethod03.fs # E_AttributeTargetIsMethod03.fs + [] + let ``E_AttributeTargetIsMethod03_fs`` compilation = + compilation + |> withLangVersion80 + |> withOptions ["--nowarn:25"] + |> verifyCompile + |> shouldSucceed + + // SOURCE=E_AttributeTargetIsMethod03.fs # E_AttributeTargetIsMethod03.fs + [] + let ``E_AttributeTargetIsMethod03_fs preview`` compilation = + compilation + |> withLangVersionPreview + |> withOptions ["--nowarn:25"] + |> verifyCompile + |> shouldFail + |> withDiagnostics [ + (Error 842, Line 12, Col 6, Line 12, Col 16, "This attribute is not valid for use on this language element") + (Error 842, Line 15, Col 6, Line 15, Col 16, "This attribute is not valid for use on this language element") + (Error 842, Line 18, Col 6, Line 18, Col 16, "This attribute is not valid for use on this language element") + (Error 842, Line 20, Col 10, Line 20, Col 20, "This attribute is not valid for use on this language element") + (Error 842, Line 22, Col 6, Line 22, Col 16, "This attribute is not valid for use on this language element") + (Error 842, Line 25, Col 6, Line 25, Col 16, "This attribute is not valid for use on this language element") + (Error 842, Line 28, Col 6, Line 28, Col 16, "This attribute is not valid for use on this language element") + (Error 842, Line 31, Col 6, Line 31, Col 16, "This attribute is not valid for use on this language element") + (Error 842, Line 34, Col 6, Line 34, Col 16, "This attribute is not valid for use on this language element") + (Error 842, Line 37, Col 6, Line 37, Col 16, "This attribute is not valid for use on this language element") + (Error 842, Line 39, Col 10, Line 39, Col 20, "This attribute is not valid for use on this language element") + (Error 842, Line 41, Col 6, Line 41, Col 16, "This attribute is not valid for use on this language element") + (Error 842, Line 44, Col 6, Line 44, Col 16, "This attribute is not valid for use on this language element") + (Error 842, Line 47, Col 6, Line 47, Col 16, "This attribute is not valid for use on this language element") + (Error 842, Line 52, Col 6, Line 52, Col 16, "This attribute is not valid for use on this language element") + ] // SOURCE=E_ConditionalAttribute.fs SCFLAGS="--test:ErrorRanges" # E_ConditionalAttribute.fs [] diff --git a/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/E_AttributeTargetIsField01.fs b/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/E_AttributeTargetIsField01.fs new file mode 100644 index 00000000000..1e3e48516c9 --- /dev/null +++ b/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/E_AttributeTargetIsField01.fs @@ -0,0 +1,67 @@ +// This tests that AttributeTargets.Field is not allowed in let function bindings + +open System + +[] +type FieldOnlyAttribute() = + inherit Attribute() + +[] // Should fail +let func1 () = "someFunction" + +[] // Should fail +let func2 a = a + 1 + +[] // Should fail +let func3 (a, b) = a + b + +[] // Should fail +let func4 (a: int) : int = a + 1 + +[] // Should fail +let func5 a b = [ a; b ] + +[] // Should fail +let func6<'a> = "someTypedFunction" + +[] // Should fail +let func7<'a> (x : 'a) = "someTypedFunction2" + +[] // Should fail +let func8 = fun x -> x + +[] // Should fail +let func9 = id + +[] // Should fail +let __func10<'a> = false + +[] // Should fail +let __func11<'a> : bool = false + +[] // Should fail +let (|Bool|_|) = function "true" -> Some true | "false" -> Some false | _ -> None + +[] // Should fail +[] +let (|BoolExpr2|_|) = function "true" -> ValueSome true | "false" -> ValueSome false | _ -> ValueNone + +[] // Should fail +let (|BoolExpr3|_|) x = + match x with + | "true" -> Some true + | "false" -> Some false + | _ -> None + +[] // Should fail +[] +let (|BoolExpr4|_|) x = + match x with + | "true" -> ValueSome true + | "false" -> ValueSome false + | _ -> ValueNone + +[] // Should fail +let rec func12() = 0 +and [] func13() = [] // Should fail + diff --git a/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/E_AttributeTargetIsField02.fs b/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/E_AttributeTargetIsField02.fs new file mode 100644 index 00000000000..c7ba6dda24f --- /dev/null +++ b/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/E_AttributeTargetIsField02.fs @@ -0,0 +1,69 @@ +// This tests that AttributeTargets.Field is not allowed in class let function bindings + +open System +open System.Diagnostics + +[] +type FieldOnlyAttribute() = + inherit Attribute() + +type TestClass() = + [] // Should fail + static let func1() = "someFunction" + + [] // Should fail + static let rec func2() = "someFunction" + + []// Should fail + static let rec func3() = "someFunction" + and [] fun4() = "someFunction" // Should fail + + [] // Should fail + let func5 () = "someFunction" + + [] // Should fail + let func6 a = a + 1 + + [] // Should fail + let func7 (a, b) = a + b + + [] // Should fail + let func8 (a: int) : int = a + 1 + + [] // Should fail + let func9 a b = [ a; b ] + + [] // Should fail + let func10 = fun x -> x + + [] // Should fail + let func11 = id + + [] // Should fail + let (|Bool|_|) = function "true" -> Some true | "false" -> Some false | _ -> None + + [] // Should fail + [] + let (|BoolExpr2|_|) = function "true" -> ValueSome true | "false" -> ValueSome false | _ -> ValueNone + + [] // Should fail + let (|BoolExpr3|_|) x = + match x with + | "true" -> Some true + | "false" -> Some false + | _ -> None + + [] // Should fail + [] + let (|BoolExpr4|_|) x = + match x with + | "true" -> ValueSome true + | "false" -> ValueSome false + | _ -> ValueNone + + [] // Should fail + let rec func12() = 0 + and [] func13() = [] // Should fail + + [] // Should fail + let rec func14() = 0 \ No newline at end of file diff --git a/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/E_AttributeTargetIsMethod02.fs b/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/E_AttributeTargetIsMethod02.fs new file mode 100644 index 00000000000..8687a9b1d0c --- /dev/null +++ b/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/E_AttributeTargetIsMethod02.fs @@ -0,0 +1,40 @@ +// This tests that AttributeTargets.Method is not allowed in let bound values + +open System + +[] +type MethodOnlyAttribute() = + inherit Attribute() + +[] // Should fail +let val1 = "someValue" + +[] // Should fail +let i, j, k = (1, 2, 3) + +[] // Should fail +let val2 = nameof(MethodOnlyAttribute) + +[] // Should fail +let rec val3 = nameof(val2) + +[] // Should fail +let ``val4`` = "someValue" + +[] // Should fail +let rec val5 = 0 +and [] val6 = [] // Should fail + +[] // Should fail +let (a :: _) = [] + +[] // Should fail +let (d, e) as val7 = 1, 2 + +[] // Should fail +let 1 = 0 + +type X = { X: int } + +[] // Should fail +let { X = _ } = { X = 1 } diff --git a/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/E_AttributeTargetIsMethod03.fs b/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/E_AttributeTargetIsMethod03.fs new file mode 100644 index 00000000000..c60ecae32b2 --- /dev/null +++ b/tests/FSharp.Compiler.ComponentTests/Conformance/BasicGrammarElements/CustomAttributes/AttributeUsage/E_AttributeTargetIsMethod03.fs @@ -0,0 +1,54 @@ +// This tests that AttributeTargets.Method is not allowed in class let bound values + +open System +open System.Diagnostics + +[] +type MethodOnlyAttribute() = + inherit Attribute() + +type TestClass() = + + [] // Should fail + static let val1 = "someValue" + + [] // Should fail + static let rec val2 = "someValue" + + [] // Should fail + static let rec val3 = "someValue" + and [] val4 = "someValue" // Should fail + + [] // Should fail + let val5 = "someValue" + + [] // Should fail + let i, j, k = (1, 2, 3) + + [] // Should fail + let val5 = nameof(MethodOnlyAttribute) + + [] // Should fail + let rec val6 = nameof(val5) + + [] // Should fail + let ``val7`` = "someValue" + + [] // Should fail + let rec val8 = 0 + and [] val9 = [] // Should fail + + [] // Should fail + let (a :: _) = [] + + [] // Should fail + let (d, e) as foo = 1, 2 + + [] // Should fail + let 1 = 0 + + type X = { X: int } + + [] // Should fail + let { X = _ } = { X = 1 } + diff --git a/tests/FSharp.Compiler.ComponentTests/ErrorMessages/TailCallAttribute.fs b/tests/FSharp.Compiler.ComponentTests/ErrorMessages/TailCallAttribute.fs index 56572879715..9d34b2466e6 100644 --- a/tests/FSharp.Compiler.ComponentTests/ErrorMessages/TailCallAttribute.fs +++ b/tests/FSharp.Compiler.ComponentTests/ErrorMessages/TailCallAttribute.fs @@ -1440,7 +1440,7 @@ namespace N ] [] - let ``Warn about attribute on non-recursive let-bound value`` () = + let ``Error about attribute on non-recursive let-bound value`` () = """ namespace N @@ -1453,18 +1453,10 @@ namespace N |> withLangVersionPreview |> compile |> shouldFail - |> withResults [ - { Error = Warning 3861 - Range = { StartLine = 7 - StartColumn = 13 - EndLine = 7 - EndColumn = 18 } - Message = - "The TailCall attribute should only be applied to recursive functions." } - ] + |> withSingleDiagnostic (Error 842, Line 6, Col 11, Line 6, Col 19, "This attribute is not valid for use on this language element") [] - let ``Warn about attribute on recursive let-bound value`` () = + let ``Error about attribute on recursive let-bound value`` () = """ namespace N @@ -1477,15 +1469,7 @@ namespace N |> withLangVersionPreview |> compile |> shouldFail - |> withResults [ - { Error = Warning 3861 - Range = { StartLine = 7 - StartColumn = 17 - EndLine = 7 - EndColumn = 37 } - Message = - "The TailCall attribute should only be applied to recursive functions." } - ] + |> withSingleDiagnostic (Error 842, Line 6, Col 11, Line 6, Col 19, "This attribute is not valid for use on this language element") [] let ``Warn about self-defined attribute`` () = // is the analysis available for users of older FSharp.Core versions diff --git a/tests/service/ProjectAnalysisTests.fs b/tests/service/ProjectAnalysisTests.fs index 0336edf78bc..c375f1c0392 100644 --- a/tests/service/ProjectAnalysisTests.fs +++ b/tests/service/ProjectAnalysisTests.fs @@ -4751,17 +4751,17 @@ type TestRecord = { B : int } module Test = [)>] - let withType = 0 + let withType() = 0 [>)>] - let withGenericType = 0 + let withGenericType() = 0 [)>] - let withTupleType = 0 + let withTupleType() = 0 [ int>)>] - let withFuncType = 0 + let withFuncType() = 0 [; typeof |])>] - let withTypeArray = 0 + let withTypeArray() = 0 [] - let withIntArray = 0 + let withIntArray() = 0 module NestedModule = type NestedRecordType = { B : int } From 4e1542aff8c14d6af748dd878c2c073e3c778108 Mon Sep 17 00:00:00 2001 From: Vlad Zarytovskii Date: Mon, 26 Feb 2024 14:07:57 +0100 Subject: [PATCH 2/4] Disable fuzzing tests (#16763) They get in the way of insertions too often --- .../FSharpChecker/TransparentCompiler.fs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/FSharp.Compiler.ComponentTests/FSharpChecker/TransparentCompiler.fs b/tests/FSharp.Compiler.ComponentTests/FSharpChecker/TransparentCompiler.fs index 0b6daf96062..3a8730043a3 100644 --- a/tests/FSharp.Compiler.ComponentTests/FSharpChecker/TransparentCompiler.fs +++ b/tests/FSharp.Compiler.ComponentTests/FSharpChecker/TransparentCompiler.fs @@ -646,10 +646,12 @@ let fuzzingTest seed (project: SyntheticProject) = task { } +(* This gets in the way of insertions too often now, uncomment when stable. [] [] [] [] +*) let Fuzzing signatureFiles = let seed = System.Random().Next() From beb01a048ce69126f4517936630e9c3d528defbf Mon Sep 17 00:00:00 2001 From: "dotnet-maestro[bot]" <42748379+dotnet-maestro[bot]@users.noreply.github.com> Date: Mon, 26 Feb 2024 09:40:01 -0800 Subject: [PATCH 3/4] [main] Update dependencies from dotnet/source-build-reference-packages (#16757) * Update dependencies from https://github.com/dotnet/source-build-reference-packages build 20240223.3 Microsoft.SourceBuild.Intermediate.source-build-reference-packages From Version 9.0.0-alpha.1.24119.1 -> To Version 9.0.0-alpha.1.24123.3 * Update dependencies from https://github.com/dotnet/source-build-reference-packages build 20240223.3 Microsoft.SourceBuild.Intermediate.source-build-reference-packages From Version 9.0.0-alpha.1.24119.1 -> To Version 9.0.0-alpha.1.24123.3 * Update dependencies from https://github.com/dotnet/source-build-reference-packages build 20240223.3 Microsoft.SourceBuild.Intermediate.source-build-reference-packages From Version 9.0.0-alpha.1.24119.1 -> To Version 9.0.0-alpha.1.24123.3 --------- Co-authored-by: dotnet-maestro[bot] Co-authored-by: Vlad Zarytovskii --- eng/Version.Details.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/eng/Version.Details.xml b/eng/Version.Details.xml index 19f3a32a345..c30f8e1630b 100644 --- a/eng/Version.Details.xml +++ b/eng/Version.Details.xml @@ -1,9 +1,9 @@ - + https://github.com/dotnet/source-build-reference-packages - 539af5d8ae183d4fe61e8b2f8f4a8505c8a765a7 + 62fb9a85e5c4af657b0014fd6d6588c139d0bb4f From bc3dd9ffaaad7898a68de4828aa0a08e1bb1461b Mon Sep 17 00:00:00 2001 From: Vlad Zarytovskii Date: Mon, 26 Feb 2024 19:53:33 +0100 Subject: [PATCH 4/4] Disable flaky transparent compiler tests (#16765) --- tests/service/ProjectAnalysisTests.fs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/service/ProjectAnalysisTests.fs b/tests/service/ProjectAnalysisTests.fs index c375f1c0392..901484b6a1c 100644 --- a/tests/service/ProjectAnalysisTests.fs +++ b/tests/service/ProjectAnalysisTests.fs @@ -4655,7 +4655,7 @@ let callToOverload = B(5).Overload(4) let args = mkProjectCommandLineArgs (dllName, []) [] -[] +// [] // Flaky, reenable when stable [] let ``Test project36 FSharpMemberOrFunctionOrValue.IsBaseValue`` useTransparentCompiler = let keepAssemblyContentsChecker = FSharpChecker.Create(keepAssemblyContents=true, useTransparentCompiler=useTransparentCompiler) @@ -4672,7 +4672,7 @@ let ``Test project36 FSharpMemberOrFunctionOrValue.IsBaseValue`` useTransparentC |> fun baseSymbol -> shouldEqual true baseSymbol.IsBaseValue [] -[] +// [] // Flaky, reenable when stable [] let ``Test project36 FSharpMemberOrFunctionOrValue.IsConstructorThisValue & IsMemberThisValue`` useTransparentCompiler = let keepAssemblyContentsChecker = FSharpChecker.Create(keepAssemblyContents=true, useTransparentCompiler=useTransparentCompiler) @@ -4711,7 +4711,7 @@ let ``Test project36 FSharpMemberOrFunctionOrValue.IsConstructorThisValue & IsMe |> shouldEqual true [] -[] +// [] // Flaky, reenable when stable [] let ``Test project36 FSharpMemberOrFunctionOrValue.LiteralValue`` useTransparentCompiler = let keepAssemblyContentsChecker = FSharpChecker.Create(keepAssemblyContents=true, useTransparentCompiler=useTransparentCompiler) @@ -5329,7 +5329,7 @@ let foo (a: Foo): bool = let options = { checker.GetProjectOptionsFromCommandLineArgs (projFileName, args) with SourceFiles = fileNames } [] -[] +// [] // Flaky, reenable when stable [] let ``Test typed AST for struct unions`` useTransparentCompiler = // See https://github.com/fsharp/FSharp.Compiler.Service/issues/756 let keepAssemblyContentsChecker = FSharpChecker.Create(keepAssemblyContents=true, useTransparentCompiler=useTransparentCompiler) @@ -5419,7 +5419,7 @@ let ``Test diagnostics with line directives ignored`` () = //------------------------------------------------------ [] -[] +// [] // Flaky, reenable when stable [] let ``ParseAndCheckFileResults contains ImplFile list if FSharpChecker is created with keepAssemblyContent flag set to true`` useTransparentCompiler = @@ -5463,7 +5463,7 @@ type A(i:int) = | None -> failwith "declaration list is empty" [] -[] +// [] // Flaky, reenable when stable [] let ``TryGetRecentCheckResultsForFile called with snapshot returns cached result after ParseAndCheckFile`` useTransparentCompiler = let fileName1 = Path.ChangeExtension(tryCreateTemporaryFileName (), ".fs") @@ -5538,7 +5538,7 @@ let ``#4030, Incremental builder creation warnings`` (args, errorSeverities) = //------------------------------------------------------ [] -[] +// [] // Flaky, reenable when stable [] let ``Unused opens in rec module smoke test 1`` useTransparentCompiler = @@ -5613,7 +5613,7 @@ type UseTheThings(i:int) = unusedOpensData |> shouldEqual expected [] -[] +// [] // Flaky, reenable when stable [] let ``Unused opens in non rec module smoke test 1`` useTransparentCompiler = @@ -5688,7 +5688,7 @@ type UseTheThings(i:int) = unusedOpensData |> shouldEqual expected [] -[] +// [] // Flaky, reenable when stable [] let ``Unused opens smoke test auto open`` useTransparentCompiler = @@ -5861,4 +5861,4 @@ module internal EmptyProject = let ``Empty source list produces error FS0207`` () = let results = checker.ParseAndCheckProject(EmptyProject.options) |> Async.RunImmediate results.Diagnostics.Length |> shouldEqual 1 - results.Diagnostics[0].ErrorNumber |> shouldEqual 207 \ No newline at end of file + results.Diagnostics[0].ErrorNumber |> shouldEqual 207