Skip to content

Commit 4f388c0

Browse files
edgarfgpKevinRansomvzarytovskiipsfinaki
authored
Enforce AttributeTargets.Class and AttributeTargets.Struct (#16790)
* Enforce AttributeTargets.Class and AttributeTargets.Struct * Enforce AttributeTargets.Class properly in the compiler * Enforce AttributeTargets.Class properly in the compiler * update tests * Fix test * release-notes * Extend AutoOpenAttribute and RequireQualifiedAccessAttribute to use AttributeTargets.Struct * Put back AutoOpen and RequireQualifiedAccess * Update tests * Release notes * release notes * Extend attributes targets * Update prim-types.fs * Update 8.0.300.md --------- Co-authored-by: Kevin Ransom (msft) <[email protected]> Co-authored-by: Vlad Zarytovskii <[email protected]> Co-authored-by: Petr <[email protected]>
1 parent bcae924 commit 4f388c0

File tree

33 files changed

+307
-31
lines changed

33 files changed

+307
-31
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* Enforce AttributeTargets on let values and functions. ([PR #16692](https://github.com/dotnet/fsharp/pull/16692))
1717
* Enforce AttributeTargets on union case declarations. ([PR #16764](https://github.com/dotnet/fsharp/pull/16764))
1818
* Disallow using base to invoke an abstract base method. ([Issue #13926](https://github.com/dotnet/fsharp/issues/13926), [PR #16773](https://github.com/dotnet/fsharp/pull/16773))
19+
* Enforce AttributeTargets on structs and classes ([PR #16790](https://github.com/dotnet/fsharp/pull/16790))
1920
* Parser: fix pattern range for idents with trivia ([PR #16824](https://github.com/dotnet/fsharp/pull/16824))
2021
* Fix broken code completion after a record type declaration ([PR #16813](https://github.com/dotnet/fsharp/pull/16813))
2122

docs/release-notes/.FSharp.Core/8.0.300.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
### Fixed
77

88
* Preserve original stack traces in resumable state machines generated code if available. ([PR #16568](https://github.com/dotnet/fsharp/pull/16568))
9+
* Enforce AttributeTargets on structs and classes. Also update `RequireQualifiedAccessAttribute` and `AutoOpenAttribute` to use `AttributeTargets.Struct` ([PR #16790](https://github.com/dotnet/fsharp/pull/16790))

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* Allow extension methods without type attribute work for types from imported assemblies. ([PR #16368](https://github.com/dotnet/fsharp/pull/16368))
1212
* Enforce AttributeTargets on let values and functions. ([PR #16692](https://github.com/dotnet/fsharp/pull/16692))
1313
* Enforce AttributeTargets on union case declarations. ([PR #16764](https://github.com/dotnet/fsharp/pull/16764))
14+
* Enforce AttributeTargets on structs and classes ([PR #16790](https://github.com/dotnet/fsharp/pull/16790))
1415

1516
### Changed
1617

src/Compiler/Checking/CheckDeclarations.fs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2926,10 +2926,16 @@ module EstablishTypeDefinitionCores =
29262926
| _ ->
29272927
let kind =
29282928
match kind with
2929-
| SynTypeDefnKind.Class -> TFSharpClass
2929+
| SynTypeDefnKind.Class ->
2930+
if g.langVersion.SupportsFeature(LanguageFeature.EnforceAttributeTargetsOnStructAndClasses) then
2931+
TcAttributesWithPossibleTargets false cenv envinner AttributeTargets.Class synAttrs |> ignore
2932+
TFSharpClass
29302933
| SynTypeDefnKind.Interface -> TFSharpInterface
29312934
| SynTypeDefnKind.Delegate _ -> TFSharpDelegate (MakeSlotSig("Invoke", g.unit_ty, [], [], [], None))
2932-
| SynTypeDefnKind.Struct -> TFSharpStruct
2935+
| SynTypeDefnKind.Struct ->
2936+
if g.langVersion.SupportsFeature(LanguageFeature.EnforceAttributeTargetsOnStructAndClasses) then
2937+
TcAttributesWithPossibleTargets false cenv envinner AttributeTargets.Struct synAttrs |> ignore
2938+
TFSharpStruct
29332939
| _ -> error(InternalError("should have inferred tycon kind", m))
29342940

29352941
TFSharpTyconRepr (Construct.NewEmptyFSharpTyconData kind)

src/Compiler/FSComp.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,6 +1596,7 @@ featureBooleanReturningAndReturnTypeDirectedPartialActivePattern,"Boolean-return
15961596
featureEnforceAttributeTargetsOnFunctions,"Enforce AttributeTargets on functions"
15971597
featureEnforceAttributeTargetsUnionCaseDeclarations,"Enforce AttributeTargets on union case declarations"
15981598
featureLowerInterpolatedStringToConcat,"Optimizes interpolated strings in certain cases, by lowering to concatenation"
1599+
featureEnforceAttributeTargetsOnStructAndClasses,"Enforce AttributeTargets on structs and classes"
15991600
featureLowerIntegralRangesToFastLoops,"Optimizes certain uses of the integral range (..) and range-step (.. ..) operators to fast while-loops."
16001601
3354,tcNotAFunctionButIndexerNamedIndexingNotYetEnabled,"This value supports indexing, e.g. '%s.[index]'. The syntax '%s[index]' requires /langversion:preview. See https://aka.ms/fsharp-index-notation."
16011602
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
@@ -88,6 +88,7 @@ type LanguageFeature =
8888
| EnforceAttributeTargetsOnFunctions
8989
| EnforceAttributeTargetsUnionCaseDeclarations
9090
| LowerInterpolatedStringToConcat
91+
| EnforceAttributeTargetsOnStructAndClasses
9192
| LowerIntegralRangesToFastLoops
9293

9394
/// LanguageVersion management
@@ -204,6 +205,7 @@ type LanguageVersion(versionText) =
204205
LanguageFeature.EnforceAttributeTargetsOnFunctions, previewVersion
205206
LanguageFeature.EnforceAttributeTargetsUnionCaseDeclarations, previewVersion
206207
LanguageFeature.LowerInterpolatedStringToConcat, previewVersion
208+
LanguageFeature.EnforceAttributeTargetsOnStructAndClasses, previewVersion
207209
LanguageFeature.LowerIntegralRangesToFastLoops, previewVersion
208210
]
209211

@@ -351,6 +353,7 @@ type LanguageVersion(versionText) =
351353
| LanguageFeature.EnforceAttributeTargetsOnFunctions -> FSComp.SR.featureEnforceAttributeTargetsOnFunctions ()
352354
| LanguageFeature.EnforceAttributeTargetsUnionCaseDeclarations -> FSComp.SR.featureEnforceAttributeTargetsUnionCaseDeclarations ()
353355
| LanguageFeature.LowerInterpolatedStringToConcat -> FSComp.SR.featureLowerInterpolatedStringToConcat ()
356+
| LanguageFeature.EnforceAttributeTargetsOnStructAndClasses -> FSComp.SR.featureEnforceAttributeTargetsOnStructAndClasses ()
354357
| LanguageFeature.LowerIntegralRangesToFastLoops -> FSComp.SR.featureLowerIntegralRangesToFastLoops ()
355358

356359
/// 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
@@ -79,6 +79,7 @@ type LanguageFeature =
7979
| EnforceAttributeTargetsOnFunctions
8080
| EnforceAttributeTargetsUnionCaseDeclarations
8181
| LowerInterpolatedStringToConcat
82+
| EnforceAttributeTargetsOnStructAndClasses
8283
| LowerIntegralRangesToFastLoops
8384

8485
/// 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)