Skip to content

AttributeTargets better error message #18641

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 40 commits into
base: main
Choose a base branch
from

Conversation

edgarfgp
Copy link
Contributor

@edgarfgp edgarfgp commented May 30, 2025

Description

BEFORE

We had one message to fit all the invalid attribute targets messages.

This attribute is not valid for use on this language element

AFTER

We have now: This attribute cannot be applied to (<one or more targets>). Valid targets are: (<one or more targets>) e.g.

  • This attribute cannot be applied to struct. Valid targets are: class
  • This attribute cannot be applied to property, field, return value. Valid targets are: method
  • This attribute cannot be applied to class. Valid targets are: interface
  • This attribute cannot be applied to method, property. Valid targets are: field

For cases here F# does not support attributes or still not a valid analysis for attribute targets that we want to enforce. We will still fallback to This attribute is not valid for use on this language element.

Checklist

  • Test cases added
  • Release notes entry updated

Copy link
Contributor

github-actions bot commented May 30, 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/10.0.100.md

@edgarfgp edgarfgp force-pushed the attr-targets-better-error-message branch 2 times, most recently from 65634b0 to 39b5f4d Compare May 30, 2025 18:13
@edgarfgp edgarfgp force-pushed the attr-targets-better-error-message branch from 39b5f4d to 049472b Compare May 30, 2025 22:01
@edgarfgp edgarfgp force-pushed the attr-targets-better-error-message branch from 75543b9 to 0c55b19 Compare May 31, 2025 20:29
@edgarfgp edgarfgp marked this pull request as ready for review June 1, 2025 18:41
@edgarfgp edgarfgp requested a review from a team as a code owner June 1, 2025 18:41
if Array.isEmpty currentTargets then
os.AppendString(InvalidAttributeTargetForLanguageElement2E().Format)
else
let currentTarget = String.concat ", " currentTargets
Copy link
Member

Choose a reason for hiding this comment

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

Why is the current target an array and not a single element? That part I find confusing in the error message.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So:

  • currentTarget represents the specific targets that the compiler wants to enforce e.g. let FieldDecl = AttributeTargets.Field ||| AttributeTargets.Property hence the reason been a list.
  • validTargets all the targets defined in the attribute declaration.

Alternatively we an maybe pass through the type of declaration where this attribute is been used as string. like "functions" etc. But this will differ from what the AttributeTargets naming is using.

Copy link
Member

Choose a reason for hiding this comment

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

Are the currentTargets a "and" or "or" combination?
i.e. do all of them have to be met, or just one?

(I would like that to be apparent from the naming if possible, also then in the resolution of the error message)

Copy link
Contributor Author

@edgarfgp edgarfgp Jun 9, 2025

Choose a reason for hiding this comment

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

@T-Gro This is an OR . What about something like e.g. Invalid attribute target. Expected: class, struct, parameter. Actual: method, property, field, return value.

@edgarfgp edgarfgp requested a review from T-Gro June 6, 2025 12:48
@github-project-automation github-project-automation bot moved this from New to In Progress in F# Compiler and Tooling Jun 19, 2025
@T-Gro T-Gro enabled auto-merge (squash) June 19, 2025 12:33
auto-merge was automatically disabled June 19, 2025 20:06

Head branch was pushed to by a user without write access

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

3 participants