Skip to content

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

Open
Copilot wants to merge 6 commits intomainfrom
copilot/fix-internal-error-custom-attribute
Open

Handle Const.Zero for primitive value types in custom attribute codegen#19484
Copilot wants to merge 6 commits intomainfrom
copilot/fix-internal-error-custom-attribute

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 24, 2026

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:

type FooAttribute(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 value
type Bar() = class end

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

module TestCase

open System
open System.Runtime.InteropServices

type FooAttribute(foo : string, bar : bool) =
    inherit Attribute()
    new([<Optional>] bar) = FooAttribute("", bar)

[<Foo>]
type Bar() = class end

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.

Copilot AI and others added 2 commits March 24, 2026 13:24
…pe argument (fixes #8353)

Handle Const.Zero for all primitive value types in GenAttribArg, which is
used when [<Optional>] is specified without [<DefaultParameterValue>] on
attribute constructor parameters. Previously only System.Object, System.String,
and System.Type were handled, causing an internal error for bool, int, and
other value types.

Co-authored-by: T-Gro <46543583+T-Gro@users.noreply.github.com>
Agent-Logs-Url: https://github.com/dotnet/fsharp/sessions/6f81842d-a307-4b7e-9ef2-ffa78948f9c6
…onal attribute args

Co-authored-by: T-Gro <46543583+T-Gro@users.noreply.github.com>
Agent-Logs-Url: https://github.com/dotnet/fsharp/sessions/6f81842d-a307-4b7e-9ef2-ffa78948f9c6
Copilot AI changed the title [WIP] Fix internal error when using custom attribute without optional argument Handle Const.Zero for primitive value types in custom attribute codegen Mar 24, 2026
Copilot AI requested a review from T-Gro March 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 owner March 26, 2026 21:34
@T-Gro T-Gro requested a review from abonie March 26, 2026 21:34
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 26, 2026

❗ Release notes required


✅ Found changes and release notes in following paths:

Change path Release notes path Description
src/Compiler docs/release-notes/.FSharp.Compiler.Service/11.0.100.md

T-Gro and others added 2 commits March 31, 2026 12:13
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@T-Gro T-Gro enabled auto-merge (squash) April 1, 2026 10:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: New

Development

Successfully merging this pull request may close these issues.

Internal error when using custom attribute and omitting optional argument

2 participants