Skip to content

Make attribute target mismatch a warning and not an error. #18492

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

Open
wants to merge 16 commits into
base: main
Choose a base branch
from

Conversation

edgarfgp
Copy link
Contributor

@edgarfgp edgarfgp commented Apr 23, 2025

Description

To reduce the impact of the EnforceAttributeTargets feature currently in preview. we should make attribute targets mismatches a warning and not an error.

Note that the warning won't be applied to desktop tests(still remains as error).

Related: #18298

Checklist

  • Test cases added
  • Release notes entry updated

Copy link
Contributor

github-actions bot commented Apr 23, 2025

❗ 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/9.0.300.md

@edgarfgp edgarfgp changed the title Make attribute targets mismatches a warning and not an error. Make attribute target mismatch a warning and not an error. Apr 23, 2025
@edgarfgp edgarfgp marked this pull request as ready for review April 29, 2025 20:06
@edgarfgp edgarfgp requested a review from a team as a code owner April 29, 2025 20:06
@edgarfgp
Copy link
Contributor Author

This is ready

@T-Gro T-Gro enabled auto-merge (squash) April 30, 2025 20:06
auto-merge was automatically disabled April 30, 2025 20:58

Head branch was pushed to by a user without write access

@auduchinok
Copy link
Member

auduchinok commented May 5, 2025

@edgarfgp I wanted to chime in and to thank you for working on the attributes analysis!

What I also want to add here is maybe we should not change much because of the fundamental issues that these changes are going to face in almost any case. These attributes were initially designed with .NET metadata in mind, where you annotate things like fields, properties, or methods. This doesn't map one to one for F# entities, where you also have things like union cases, functions, or values. The old implementation tried to find good-enough correspondence between F# symbols and their compiled representations, but it had its issues, all coming from this design difference. Any tweaking of the analysis is going to highlight the same issue from different angles, while also breaking the tools/frameworks/etc relying on how it was recorded many years ago. But we can't change much here: we can't add F# symbols to the common .NET attribute and expect everyone to (correctly) take it all into account. We also can't change what things are being compiled to. Often we don't even know that from syntax! (So can't assume that F# users will annotate it properly.)

Consider a simple code sample:

let a = foo

Whether a is going to be a property or a method in the metadata, depends on the type inference! I don't know what the previous implementation assumed, but we can't say easily that it's either, neither, or both.

Another case:

type U1 =
    | A
    | B of int

type U2 = int

U1.A is compiled as a property, U1.B is a nested class and a constructor method. But we don't want to assume these things either, I guess.
And U2 doesn't even exist in the metadata at all...

Any attempt to map F# things to .NET metadata entities differently will have some flaws here, so maybe sticking to what has already been done could just be a good way to break less things.

What could in theory be done is having some F#-specific attribute that F# libraries could take into account, but then we'd lose support from the broader .NET ecosystem. Maybe we could map this attribute during the compilation somehow and generate the corresponding .NET attribute instances, but it also doesn't seem to be a viable solution. (While possible in theory, it would also have issues and would require a lot of work and careful design.) In any case that I try to come up with I end up thinking we should probably just not try to change this analysis in any fundamental way here.

@edgarfgp
Copy link
Contributor Author

edgarfgp commented May 5, 2025

@auduchinok Thanks for the detailed reply. Regarding in specific the change on this PR converting the error into a warning. I believe it is a better compromise allowing users to ignore it and rely in a previous behaviour in any F# construct.

In regards the DU cases with fields I think we can rollback the have the previous targets but still haven an opt-in warning would be good

@auduchinok
Copy link
Member

Regarding in specific the change on this PR converting the error into a warning

Yes, thanks @edgarfgp 🙂

I just wanted to write it somewhere where a discussion is still active (i.e. instead of choosing one of many previous PR)

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.

3 participants