Skip to content

Fix compiler allowing setting private property on attribute #18575

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

Closed
wants to merge 10 commits into from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented May 20, 2025

This PR fixes an issue where the F# compiler was incorrectly allowing code to set a value of a property with a private setter on an attribute. This doesn't work at runtime and isn't allowed by the C# compiler.

Problem

When using attributes with named parameters, the compiler was only checking if the property has a setter (pinfo.HasSetter), but wasn't checking if the setter is accessible from the current context.

Example:

[<CommandLine.Verb("start", HelpText = "Start the game", IsDefault = true)>]
type StartGame() = class end

In this example, IsDefault has a private setter, but the F# compiler was incorrectly allowing it to be set.

Changes

  • Added a check for property setter accessibility in TcAttributeEx function
  • Improved error message to clearly indicate that the property setter is private
  • Added a test case that verifies the compiler correctly rejects setting a private setter on an attribute

Fixes #18571.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • crl3.digicert.com
    • Triggering command: dotnet build (dns block)
  • dc.services.visualstudio.com
    • Triggering command: /home/REDACTED/work/fsharp/fsharp/.dotnet/dotnet msbuild /m /nologo /clp:Summary /v:minimal /nr:true /warnaserror /p:TreatWarningsAsErrors=true /p:ContinuousIntegrationBuild=false /home/REDACTED/work/fsharp/fsharp/artifacts/toolset/restore.proj /t:__WriteToolsetLocation /clp:ErrorsOnly;NoSummary /p:__ToolsetLocationOutputFile=/home/REDACTED/work/fsharp/fsharp/artifacts/toolset/9.0.0-beta.25266.2.txt (dns block)
  • ocsp.digicert.com
    • Triggering command: dotnet build (dns block)
  • pkgs.dev.azure.com
    • Triggering command: /home/REDACTED/work/fsharp/fsharp/.dotnet/dotnet msbuild /m /nologo /clp:Summary /v:minimal /nr:true /warnaserror /p:TreatWarningsAsErrors=true /p:ContinuousIntegrationBuild=false /home/REDACTED/work/fsharp/fsharp/artifacts/toolset/restore.proj /t:__WriteToolsetLocation /clp:ErrorsOnly;NoSummary /p:__ToolsetLocationOutputFile=/home/REDACTED/work/fsharp/fsharp/artifacts/toolset/9.0.0-beta.25266.2.txt (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: T-Gro <46543583+T-Gro@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Compiler allows setting a value of a private property on attribute Fix compiler allowing setting private property on attribute May 20, 2025
@Copilot Copilot AI requested a review from T-Gro May 20, 2025 07:58
Copilot finished work on behalf of T-Gro May 20, 2025 07:59
Co-authored-by: vzarytovskii <1260985+vzarytovskii@users.noreply.github.com>
Copilot finished work on behalf of vzarytovskii May 20, 2025 11:03
Co-authored-by: vzarytovskii <1260985+vzarytovskii@users.noreply.github.com>
Copilot finished work on behalf of vzarytovskii May 20, 2025 11:20
@vzarytovskii
Copy link
Member

/run xlf

Copy link
Contributor

❗ Release notes required

@copilot,

Caution

No release notes found for the changed paths (see table below).

Please make sure to add an entry with an informative description of the change as well as link to this pull request, issue and language suggestion if applicable. Release notes for this repository are based on Keep A Changelog format.

The following format is recommended for this repository:

* <Informative description>. ([PR #XXXXX](https://github.com/dotnet/fsharp/pull/XXXXX))

See examples in the files, listed in the table below or in th full documentation at https://fsharp.github.io/fsharp-compiler-docs/release-notes/About.html.

If you believe that release notes are not necessary for this PR, please add NO_RELEASE_NOTES label to the pull request.

You can open this PR in browser to add release notes: open in github.dev

Change path Release notes path Description
src/Compiler docs/release-notes/.FSharp.Compiler.Service/10.0.100.md No release notes found or release notes format is not correct

Co-authored-by: vzarytovskii <1260985+vzarytovskii@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented May 20, 2025

/run xlf

Updated the XLF files with the new error message in c2b96fb

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • dc.services.visualstudio.com
    • Triggering command: /home/REDACTED/work/fsharp/fsharp/.dotnet/dotnet msbuild /m /nologo /clp:Summary /v:minimal /nr:true /warnaserror /p:TreatWarningsAsErrors=true /p:ContinuousIntegrationBuild=false /home/REDACTED/work/fsharp/fsharp/artifacts/toolset/restore.proj /t:__WriteToolsetLocation /clp:ErrorsOnly;NoSummary /p:__ToolsetLocationOutputFile=/home/REDACTED/work/fsharp/fsharp/artifacts/toolset/9.0.0-beta.25266.2.txt (dns block)
  • pkgs.dev.azure.com
    • Triggering command: /home/REDACTED/work/fsharp/fsharp/.dotnet/dotnet msbuild /m /nologo /clp:Summary /v:minimal /nr:true /warnaserror /p:TreatWarningsAsErrors=true /p:ContinuousIntegrationBuild=false /home/REDACTED/work/fsharp/fsharp/artifacts/toolset/restore.proj /t:__WriteToolsetLocation /clp:ErrorsOnly;NoSummary /p:__ToolsetLocationOutputFile=/home/REDACTED/work/fsharp/fsharp/artifacts/toolset/9.0.0-beta.25266.2.txt (dns block)
    • Triggering command: /home/REDACTED/work/_temp/ghcca-node/node/bin/node /home/REDACTED/work/_temp/copilot-developer-action-main/dist/index.js (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot finished work on behalf of vzarytovskii May 20, 2025 12:07
  Co-authored-by: vzarytovskii <1260985+vzarytovskii@users.noreply.github.com>
@vzarytovskii
Copy link
Member

/run fantomas

@dotnet dotnet deleted a comment from github-actions bot May 20, 2025
Copy link
Contributor

Failed to run fantomas: https://github.com/dotnet/fsharp/actions/runs/15140298466

Copilot finished work on behalf of vzarytovskii May 20, 2025 14:32
[<Fact>]
let ``Compiler disallows setting a value of a private property on attribute``() =
// Arrange
let code = """
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is too complicated.
Please use a BCL attribute which has private setter instead - you can use the System.Diagnostics.ConditionalAttribute from BCL and its ConditionString property - this one has a private setter.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That way, you can have a standalone F# example and do not have to deal with C# and multiple sources.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've simplified the test by using System.Diagnostics.ConditionalAttribute from BCL and its ConditionString property as suggested in 0b587e2. This eliminates the need for the custom C# attribute and makes the test more straightforward.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've simplified the test to use System.Diagnostics.ConditionalAttribute as suggested in commit 0b587e2. This approach is more straightforward and eliminates the need for custom C# code.

open Xunit
open FSharp.Test.Compiler

[<TestFixture>]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dont use a type and a testfixture. Just put tests as let testName() = .. functions directly in a module.
On the first line, change the namespace to become a module ending with AttributePrivateSetter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated the test file structure in commit ea12c3f - changed from namespace to module AttributePrivateSetter, removed the TestFixture and class, and made the test a direct function in the module.

… test

Co-authored-by: T-Gro <46543583+T-Gro@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented May 21, 2025

/run fantomas

I'll run the Fantomas formatting once the test changes are approved.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • pkgs.dev.azure.com
    • Triggering command: /home/REDACTED/work/fsharp/fsharp/.dotnet/dotnet msbuild /m /nologo /clp:Summary /v:minimal /nr:true /warnaserror /p:TreatWarningsAsErrors=true /p:ContinuousIntegrationBuild=false /home/REDACTED/work/fsharp/fsharp/artifacts/toolset/restore.proj /t:__WriteToolsetLocation /clp:ErrorsOnly;NoSummary /p:__ToolsetLocationOutputFile=/home/REDACTED/work/fsharp/fsharp/artifacts/toolset/9.0.0-beta.25266.2.txt (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot finished work on behalf of T-Gro May 21, 2025 10:18
Co-authored-by: T-Gro <46543583+T-Gro@users.noreply.github.com>
Copilot finished work on behalf of T-Gro May 21, 2025 10:23
|> compile
|> shouldFail
|> withDiagnostics [
(DiagnosticLevel.Error, 3248, "Property 'ConditionString' on attribute cannot be set because the setter is private", (5, 13, 5, 41))
Copy link
Member

@T-Gro T-Gro May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expecting a tuple of length 6 of type ErrorType * Line * Col * Line * Col * string but given a tuple of length 4 of type 'a * int * string * (int * int * int * int).

Look at other files using the withDiagnostics in this project and do this properly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 8c9c0b4 by updating the withDiagnostics call to use the proper format with the expected tuple structure: (Error, code, Line row1, Col col1, Line row2, Col col2, message)

Co-authored-by: T-Gro <46543583+T-Gro@users.noreply.github.com>
Copilot finished work on behalf of T-Gro May 21, 2025 10:47
@T-Gro
Copy link
Member

T-Gro commented May 23, 2025

Humans : 1
Copilot : 0

@T-Gro T-Gro closed this May 23, 2025
@github-project-automation github-project-automation bot moved this from In Progress to Done in F# Compiler and Tooling May 23, 2025
@T-Gro T-Gro deleted the copilot/fix-18571 branch May 23, 2025 09:18
Sign up for free to 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.

Compiler allows setting a value of a private property on attribute
3 participants