Skip to content

opt-in warning attribute not valid for union case with fields #18532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 26 commits into from
May 28, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
4e85c3a
raise an opt-in warning attribute not valid for union case with fields
edgarfgp May 5, 2025
e254386
Merge branch 'main' into attr-targets-opt-in-warning
edgarfgp May 5, 2025
8201668
Merge branch 'main' into attr-targets-opt-in-warning
edgarfgp May 14, 2025
b0922ae
Merge branch 'main' into attr-targets-opt-in-warning
edgarfgp May 15, 2025
85aa7f3
release notes
edgarfgp May 16, 2025
c6a401f
Merge branch 'main' into attr-targets-opt-in-warning
edgarfgp May 16, 2025
0288f0a
Merge branch 'attr-targets-opt-in-warning' of github.com:edgarfgp/fsh…
edgarfgp May 16, 2025
7fd3f51
Update FSComp
edgarfgp May 16, 2025
995b7b6
3878 -> false
edgarfgp May 16, 2025
d136e47
Update test
edgarfgp May 16, 2025
5507d3a
Merge branch 'main' into attr-targets-opt-in-warning
edgarfgp May 16, 2025
fdc2ea9
add opt-in warning tests
edgarfgp May 17, 2025
40b8196
extract ResolveAttributeType and CheckAttributeUsage
edgarfgp May 17, 2025
d18e934
Merge branch 'main' into attr-targets-opt-in-warning
edgarfgp May 19, 2025
57b7bf1
Merge branch 'main' into attr-targets-opt-in-warning
edgarfgp May 20, 2025
7060ff8
use bit-logic to check if the bit for Method is set
edgarfgp May 21, 2025
47c5b15
Merge branch 'main' into attr-targets-opt-in-warning
edgarfgp May 22, 2025
a33c433
PR feedback
edgarfgp May 22, 2025
dcd93ff
Merge branch 'attr-targets-opt-in-warning' of github.com:edgarfgp/fsh…
edgarfgp May 22, 2025
7f6e4d9
Merge branch 'main' into attr-targets-opt-in-warning
edgarfgp May 22, 2025
66c9e49
Merge branch 'main' into attr-targets-opt-in-warning
edgarfgp May 22, 2025
1fb5a81
Merge branch 'main' into attr-targets-opt-in-warning
edgarfgp May 22, 2025
c5be0b2
Merge branch 'main' into attr-targets-opt-in-warning
edgarfgp May 23, 2025
834a250
Merge branch 'main' into attr-targets-opt-in-warning
edgarfgp May 26, 2025
19e2c86
Merge branch 'main' into attr-targets-opt-in-warning
edgarfgp May 26, 2025
30a3dc9
Merge branch 'main' into attr-targets-opt-in-warning
edgarfgp May 26, 2025
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
43 changes: 27 additions & 16 deletions src/Compiler/Checking/CheckDeclarations.fs
Original file line number Diff line number Diff line change
Expand Up @@ -577,22 +577,33 @@ module TcRecdUnionAndEnumDeclarations =

let checkXmlDocs = cenv.diagnosticOptions.CheckXmlDocs
let xmlDoc = xmldoc.ToXmlDoc(checkXmlDocs, Some names)
let attrs =
(*
The attributes of a union case decl get attached to the generated "static factory" method.
Enforce union-cases AttributeTargets:
- AttributeTargets.Method
type SomeUnion =
| Case1 of int // Compiles down to a static method
- AttributeTargets.Property
type SomeUnion =
| Case1 // Compiles down to a static property
*)
if g.langVersion.SupportsFeature(LanguageFeature.EnforceAttributeTargets) then
let target = if rfields.IsEmpty then AttributeTargets.Property else AttributeTargets.Method
TcAttributes cenv env target synAttrs
else
TcAttributes cenv env AttributeTargets.UnionCaseDecl synAttrs
let attrs = TcAttributes cenv env AttributeTargets.UnionCaseDecl synAttrs
(*
The attributes of a union case decl get attached to the generated "static factory" method.
Enforce union-cases AttributeTargets:
- AttributeTargets.Method
type SomeUnion =
| Case1 of int // Compiles down to a static method
- AttributeTargets.Property
type SomeUnion =
| Case1 // Compiles down to a static property
*)
if g.langVersion.SupportsFeature(LanguageFeature.EnforceAttributeTargets) then
let attrTargets =
attrs
|> List.collect (fun attr ->
attr.TyconRef.Attribs
|> List.choose (fun attr ->
match attr with
| Attrib(unnamedArgs = [ AttribInt32Arg validOn ]) -> Some validOn
| _ -> None))

attrTargets
|> List.iter (fun target ->
// If the union case has fields, and the target is not AttributeTargets.Method || AttributeTargets.All. Then we raise a separate opt-in warning
let targetEnum = enum<AttributeTargets> target
if targetEnum <> AttributeTargets.Method && targetEnum <> AttributeTargets.All then
warning(Error(FSComp.SR.tcAttributeIsNotValidForUnionCaseWithFields(), id.idRange)))

Construct.NewUnionCase id rfields recordTy attrs xmlDoc vis

Expand Down
1 change: 1 addition & 0 deletions src/Compiler/Driver/CompilerDiagnostics.fs
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ type PhasedDiagnostic with
| 3579 -> false // alwaysUseTypedStringInterpolation - off by default
| 3582 -> false // infoIfFunctionShadowsUnionCase - off by default
| 3570 -> false // tcAmbiguousDiscardDotLambda - off by default
| 3875 -> false // tcAttributeIsNotValidForUnionCaseWithFields - off by default
| _ ->
match x.Exception with
| DiagnosticEnabledWithLanguageFeature(_, _, _, enabled) -> enabled
Expand Down
1 change: 1 addition & 0 deletions src/Compiler/FSComp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1795,6 +1795,7 @@ featureUseTypeSubsumptionCache,"Use type conversion cache during compilation"
featureDontWarnOnUppercaseIdentifiersInBindingPatterns,"Don't warn on uppercase identifiers in binding patterns"
3873,chkDeprecatePlacesWhereSeqCanBeOmitted,"This construct is deprecated. Sequence expressions should be of the form 'seq {{ ... }}'"
3874,tcExpectedTypeParamMarkedWithUnitOfMeasureAttribute,"Expected unit-of-measure type parameter must be marked with the [<Measure>] attribute."
3875,tcAttributeIsNotValidForUnionCaseWithFields,"This attribute is not valid for use on union cases with fields."
featureDeprecatePlacesWhereSeqCanBeOmitted,"Deprecate places where 'seq' can be omitted"
featureSupportValueOptionsAsOptionalParameters,"Support ValueOption as valid type for optional member parameters"
featureSupportWarnWhenUnitPassedToObjArg,"Warn when unit is passed to a member accepting `obj` argument, e.g. `Method(o:obj)` will warn if called via `Method()`."
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
Expand Up @@ -492,7 +492,6 @@ module CustomAttributes_AttributeUsage =
|> shouldFail
|> withDiagnostics [
(Error 842, Line 14, Col 5, Line 14, Col 15, "This attribute is not valid for use on this language element")
(Error 842, Line 15, Col 5, Line 15, Col 25, "This attribute is not valid for use on this language element")
]

// SOURCE=E_AttributeTargetIsProperty01.fs # E_AttributeTargetIsField03.fs
Expand All @@ -509,11 +508,7 @@ module CustomAttributes_AttributeUsage =
compilation
|> withLangVersionPreview
|> verifyCompile
|> shouldFail
|> withDiagnostics [
(Error 842, Line 14, Col 5, Line 14, Col 18, "This attribute is not valid for use on this language element")
(Error 842, Line 15, Col 5, Line 15, Col 25, "This attribute is not valid for use on this language element")
]
|> shouldSucceed

// SOURCE=E_AttributeTargetIsCtor01.fs # E_AttributeTargetIsCtor01.fs
[<Theory; Directory(__SOURCE_DIRECTORY__, Includes=[|"E_AttributeTargetIsCtor01.fs"|])>]
Expand Down
Loading