Skip to content

Conversation

@DragonSA
Copy link
Contributor

@DragonSA DragonSA commented Oct 22, 2025

Description

Attributes attached to the return value of a class method (e.g. [<return: NotNull>]) were instead being attached to the method itself. This did not happen for functions (let bind). I refactored the logic out of TcNormalizedBinding (used by TcLetBinding and used that in AnalyzeAndMakeAndPublishRecursiveValue.

Fixes #19020

Checklist

  • Test cases added
  • Performance benchmarks added in case of performance changes
  • Release notes entry updated:

Sponsored by CP Dynamics

@github-actions
Copy link
Contributor

github-actions bot commented Oct 22, 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/11.0.0.md

@DragonSA DragonSA force-pushed the fix-returnattr branch 2 times, most recently from 30f5af1 to 41a384b Compare October 22, 2025 21:02
Copy link
Contributor

Choose a reason for hiding this comment

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

Please minimise the diff. So it is easier to review.

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 removed the white-space churn and also fixed a bug this introduced that caused recursive active pattern matches that return struct to fail (see test Rec struct active pattern is possible).

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks. To fix the formatting we need to restore the dotnet tools and run dotnet fantomas .

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 pushed the formatting fix. I see the AOT test is failing due to a size change. I cannot run the AOT test on my mac, so I haven't updated the expected values. Any suggestions?

Copy link
Contributor

Choose a reason for hiding this comment

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

I normally just update the expected values and wait for the review. I think the value diff reduction is related the removal of the attributes in IlxGen.fs. Maybe @vzarytovskii can give some more insight here ?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, majority of codegen changes will result in size change for AOT, size check is there on purpose, so we are conscious about these sorts of changes. If it's expected, just update the size.

let attrs =
attrs
|> List.filter (function Attrib(targetsOpt = Some flags) -> not (flags.HasFlag(AttributeTargets.ReturnValue)) | _ -> true)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Instead of filtering, one could partition and move the return attributes into ilReturn below. Then, all the changes to CheckExpressions.fs would not be required.

This would be the smaller change, but I feel it would continue the mis-propagation of attributes...

let retAttribs =
match rtyOpt with
| Some (SynBindingReturnInfo(attributes = Attributes retAttrs)) ->
rotRetAttribs @ TcAttrs cenv env attrTgt AttributeTargets.ReturnValue true retAttrs
Copy link
Member

Choose a reason for hiding this comment

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

Doesn't this mean that the return attributes will be TcAttrs'd twice, leading to type checking and diagnostics duplicates?

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 added this to fix active pattern tests that broke when the return attribute was rotated out. As far as a I recall correctly, this was caused by ActivePatternElemsOfValRef using the ValRef, instead of using the return attributes. I was not able to find an easy way to fix this.

To check, I've now created an attribute only applicable to properties and added it as a return attribute on a method. It returned two error messages, as you suspected.

A trivial alternative to this is the alternative change I suggested to src/Compiler/CodeGen/IlxGen.fs. I do feel that is an outright hack. But, based on guidance here I am happy to submit an alternative PR?

Copy link
Member

Choose a reason for hiding this comment

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

Checking is the right place, we want the attributes also to be checked for their attribute targets (e.g. an attribute only configured for the ReturnValue target such be type checked properly), ilxgen would indeed be a hack.

Copy link
Member

Choose a reason for hiding this comment

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

IMO you should first separate the attributes (return value vs rest), and then TcAttrs each group separately, with the different AttributeTargets.

Does that make sense, would it work?

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.

[<return: X>] not respected on class members

4 participants