Skip to content

Commit 73cfbce

Browse files
T-GroCopilot
andcommitted
Fix #12796: proper diagnostic for invalid custom attribute array element types
Previously, using an array of a user-defined type as a custom attribute argument (e.g. [<DefaultValue([||] : A[])>]) caused an internal error FS0192 in encodeCustomAttrElemType. The ECMA 335 metadata format requires the array element type to be encoded even for empty arrays, and only primitive types, enums, string, System.Type, and System.Object are valid. This change adds a validation check in GenAttribArg (IlxGen.fs) that detects unsupported element types before reaching the IL writer, and reports a proper diagnostic (FS3885) instead of crashing. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 40e815c commit 73cfbce

17 files changed

Lines changed: 124 additions & 26 deletions

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* Fixed how the source ranges of warn directives are reported (as trivia) in the parser output (by not reporting leading spaces). ([Issue #19405](https://github.com/dotnet/fsharp/issues/19405), [PR #19408]((https://github.com/dotnet/fsharp/pull/19408)))
2020
* Fix UoM value type `ToString()` returning garbage values when `--checknulls+` is enabled, caused by double address-taking in codegen. ([Issue #19435](https://github.com/dotnet/fsharp/issues/19435), [PR #19440](https://github.com/dotnet/fsharp/pull/19440))
2121
* Fix completion inconsistently showing some obsolete members (fields and events) while hiding others (methods and properties). All obsolete members are now consistently hidden by default. ([Issue #13512](https://github.com/dotnet/fsharp/issues/13512), [PR #19506](https://github.com/dotnet/fsharp/pull/19506))
22+
* Fix internal error `FS0192: encodeCustomAttrElemType` when using an array of user-defined type as a custom attribute argument. Now reports a proper diagnostic (FS3885). ([Issue #12796](https://github.com/dotnet/fsharp/issues/12796), [PR #19472](https://github.com/dotnet/fsharp/pull/19472))
2223

2324
### Added
2425

src/Compiler/CodeGen/IlxGen.fs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10223,6 +10223,18 @@ and GenAttribArg amap g eenv x (ilArgTy: ILType) =
1022310223
// Detect '[| ... |]' nodes
1022410224
| Expr.Op(TOp.Array, [ elemTy ], args, m), _ ->
1022510225
let ilElemTy = GenType amap m eenv.tyenv elemTy
10226+
10227+
// Validate element type is encodable in custom attribute metadata (ECMA 335).
10228+
// Only primitive types, enums, string, System.Type, and System.Object are valid.
10229+
match ilElemTy with
10230+
| ILType.Boxed tspec when
10231+
tspec.Name <> "System.String"
10232+
&& tspec.Name <> "System.Object"
10233+
&& tspec.Name <> "System.Type"
10234+
->
10235+
error (Error(FSComp.SR.ilCustomAttrInvalidArrayElemType (tspec.Name), m))
10236+
| _ -> ()
10237+
1022610238
ILAttribElem.Array(ilElemTy, List.map (fun arg -> GenAttribArg amap g eenv arg ilElemTy) args)
1022710239

1022810240
// Detect 'typeof<ty>' calls

src/Compiler/FSComp.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1814,3 +1814,4 @@ featurePreprocessorElif,"#elif preprocessor directive"
18141814
3882,lexHashElifMustBeFirst,"#elif directive must appear as the first non-whitespace character on a line"
18151815
3883,lexHashElifMustHaveIdent,"#elif directive should be immediately followed by an identifier"
18161816
3884,tcFunctionValueUsedAsInterpolatedStringArg,"This expression is a function value. When used in an interpolated string it will be formatted using its 'ToString' method, which is likely not the intended behavior. Consider applying the function to its arguments."
1817+
3885,ilCustomAttrInvalidArrayElemType,"The type '%s' is not a valid custom attribute argument type. Custom attribute arrays must have elements of primitive types, enums, string, System.Type, or System.Object."

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

Lines changed: 7 additions & 2 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: 7 additions & 2 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: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compiler/xlf/FSComp.txt.fr.xlf

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compiler/xlf/FSComp.txt.it.xlf

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compiler/xlf/FSComp.txt.ja.xlf

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Compiler/xlf/FSComp.txt.ko.xlf

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)