Skip to content

Handle Const.Zero for primitive value types in custom attribute codegen - #19484

Merged
T-Gro merged 2 commits into
mainfrom
copilot/fix-internal-error-custom-attribute
May 4, 2026
Merged

Handle Const.Zero for primitive value types in custom attribute codegen#19484
T-Gro merged 2 commits into
mainfrom
copilot/fix-internal-error-custom-attribute

Conversation

CopilotAI commented Mar 24, 2026

Copy link
Copy Markdown
Contributor

GenAttribArg in IlxGen.fs handles Const.Zero (the default for [<Optional>] params without [<DefaultParameterValue>]) for System.Object, System.String, and System.Type, but not for primitive value types. This causes an internal error for code like:

typeFooAttribute(foo: string,bar: bool)=inherit Attribute()new([<Optional>] bar: bool)= FooAttribute("", bar)[<Foo>]// FS0073: internal error: The type 'System.Boolean' may not be used as a custom attribute valuetypeBar()=classend

Changes

  • Added Const.Zero → default value mappings for all 12 primitive types valid in custom attributes: bool, sbyte, int16, int32, int64, byte, uint16, uint32, uint64, single, double, char
  • Added regression test exercising [<Optional>] attribute constructor params for all primitive value types
Original prompt

This section details on the original issue you should resolve

<issue_title>Internal error when using custom attribute and omitting optional argument</issue_title>
<issue_description>There is an internal compiler error when you use a custom attribute and omit an optional argument tagged with System.Runtime.InteropServices.OptionalAttribute

Repro steps

moduleTestCaseopenSystemopenSystem.Runtime.InteropServicestypeFooAttribute(foo: string,bar: bool)=inherit Attribute()new([<Optional>] bar)= FooAttribute("", bar)[<Foo>]typeBar()=classend

Expected behavior

This should compile

Actual behavior

TestCase.fs(10,3): error FS0073: internal error: The type 'System.Boolean' may not be used as a custom attribute value

Known workarounds

Add a DefaultParameterValue attribute.

Related information

Provide any related information (optional):

  • Mac OS 10.15.1
  • Mono JIT compiler version 6.6.0.155 (2019-08/296a9afdb24 Thu Nov 14 06:12:25 EST 2019)
  • Microsoft (R) F# Compiler version 10.2.3 for F# 4.5
    </issue_description>

<agent_instructions>See the latest comment, a fix is possible.
Careful, the comment has escaped angle brackets - you will need to figure that out.
Make sure to execute all codegen and optional related tests!

</agent_instructions>

Comments on the Issue (you are @copilot in this section)

Custom agent used: F# agent
Generic agent for F# coding following the coding guidelines of F# from MsLearn


⚡ Quickly spin up Copilot coding agent tasks from anywhere on your macOS or Windows machine with Raycast.

CopilotAI changed the title [WIP] Fix internal error when using custom attribute without optional argumentHandle Const.Zero for primitive value types in custom attribute codegenMar 24, 2026
CopilotAI requested a review from T-GroMarch 24, 2026 13:28
@T-Gro
T-Gro marked this pull request as ready for review March 26, 2026 21:34
@T-Gro
T-Gro requested a review from a team as a code ownerMarch 26, 2026 21:34
@T-Gro
T-Gro requested a review from abonieMarch 26, 2026 21:34
@github-actions

github-actionsBot commented Mar 26, 2026

Copy link
Copy Markdown
Contributor

❗ Release notes required


✅ Found changes and release notes in following paths:

Change pathRelease notes pathDescription
src/Compilerdocs/release-notes/.FSharp.Compiler.Service/11.0.100.md

@T-Gro
T-Gro enabled auto-merge (squash) April 1, 2026 10:17
@T-Gro
T-Groforce-pushed the copilot/fix-internal-error-custom-attribute branch 5 times, most recently from 78029a5 to 9cac358CompareApril 16, 2026 06:03
@T-Gro
T-Groforce-pushed the copilot/fix-internal-error-custom-attribute branch from 9cac358 to 61d2b72CompareApril 21, 2026 06:02
Comment threadsrc/Compiler/CodeGen/IlxGen.fs
@T-Gro
T-Groforce-pushed the copilot/fix-internal-error-custom-attribute branch 2 times, most recently from b451e8c to 1f64db5CompareApril 28, 2026 09:25
@T-Gro
T-Gro requested a review from abonieApril 28, 2026 10:36
@github-project-automationgithub-project-automationBot moved this from New to In Progress in F# Compiler and ToolingApr 28, 2026
@T-Gro
T-Groforce-pushed the copilot/fix-internal-error-custom-attribute branch from 1f64db5 to 38da75cCompareMay 4, 2026 07:17
…pe argument (#8353)
GenAttribArg in IlxGen.fs handles Const.Zero (the default for [<Optional>] params
without [<DefaultParameterValue>]) for System.Object, System.String, and System.Type,
but not for primitive value types. This causes an internal error (FS0073) when
applying an attribute with an [<Optional>] value type parameter and no default.
Add Const.Zero -> default value mappings for all 12 primitive types valid in custom
attributes: bool, sbyte, int16, int32, int64, byte, uint16, uint32, uint64, single,
double, char.
Fixes#8353
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Address review feedback: when a custom attribute has an [<Optional>] parameter
of an enum type (without [<DefaultParameterValue>]), Const.Zero is now resolved
by looking up the enum's underlying primitive type and recursing into GenAttribArg.
Also adds test coverage for enum types (int-based and byte-based).
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Internal error when using custom attribute and omitting optional argument

3 participants

@abonie@T-Gro