-
Notifications
You must be signed in to change notification settings - Fork 790
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
Generic Attributes #17258
base: main
Are you sure you want to change the base?
Generic Attributes #17258
Conversation
❗ Release notes required
|
Adding of test cases, and fullfilment of minutia (documentation, various "feature" tags, general cleanup, etc) still WIP. Currently, [<SomeAttribute<int>()>] compiles down to the expected IL. I plan to explore what will be required for inferencing before marking as ready. Drafting now while I complete the minor details in case anyone wants to provide feedback in the meantime. |
…ign with apparent standard
…SecurityAttribute (ILxGen)
@dotnet-policy-service agree company="Hillcrest Research and Development LLC" |
Hi @mflibby, this looks very interesting! In your original description, can you link to the reported issue that this is fixing? And if it is a language change (i.e., a new feature that is being added), can you link to the RFC (in https://github.com/fsharp/fslang-design) and the language proposal or discussion chat (https://github.com/fsharp/fslang-suggestions)? If none exist, can you update the OP with a summary of what this change is doing and why? |
@abelbraaksma I've updated the description with the desired information. Please let me know if this is satisfactory, or if more information will be helpful :) |
I'm not sure why, but commit 64607ef passed all checks, yet after a few cleanup/a minor change/merges to other changes on main, a large number of the checks have been failing (the only substantive change I made since the passing commit was a minor change to the LexFilter that allowed the type arguments immediately adjacent to the close of an attribute list to be properly parsed). I'm wondering why I am all of a sudden getting a bunch of seemingly unrelated tests failing? Additionally, windows defender seems to be flagging a bunch of tests, and occasionally quarantining innocuous tests like "Conformance/../E_Literals02.fs". The investigation continues. |
|
||
module Attribute = | ||
|
||
let verifyCompilation compilation = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please add test cases(s) with mutual recursion (either via and
or via a rec module
) between the attribute declaration, the type argument and the type it is being applied on?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have a particular point of failure we are looking for here? Off the top of my head this is what comes to mind:
(in a module rec)
type MyAttribute<^T>() =
inherit Attribute()
[<MyAttribute<AnActualType>>]
let x = 1
type AnActualType = class end
(Note: it appears that in a rec module, the usage of an attribute on a binding needs to occur after the definition, but on a type it can occur before the definition.)
and maybe something like
(in a regular module)
[<MyAttribute<AnActualType>>]
type SomeClass(arg) =
member _.AGoodProperty = arg
and MyAttribute<^T>() =
inherit Attribute()
and AnActualType = int
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh great call, both rec and and fail right now (made sure to check they pass when there are no type args and both do)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Glad I could suggest a point of failure - I did not have a particular one in mind, I just know that rec modules are a common point of failure for new typechecking features.
Maybe also a type applying "itself" as the type argument to the attribute?
[<MyAttribute<SomeClass<SomeClass<SomeClass<MyAttribute>>>>>]
type SomeClass<'a>(arg) =
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still need to figure out why the rec/and cases failed, but I implemented the self referencing case (great case btw, I know there must be some cases where it will be useful but this in particular made me laugh quite hard), and was surprised to find it working right out of the gate!
Hey @mflibby, excellent, thanks, now we know where it's coming from and that it was approved 👍. We still need an RFC, though. I may be able to help with that. Language changes require an RFC for documentation and consensus. They capture the what, how and scope of any new feature. The go here: https://github.com/fsharp/fslang-design. It is also used to set up a discussion thread to deal with any unresolved questions. I can help with the process. |
@abelbraaksma Oh well the order in which I've done things certainly should make drawing up the RFC easier, I will get on that ASAP - thank you for the heads up! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mflibby Thanks for this!
Could we please have more tests for various error cases?
Please also add tests for cases where type inference is important, like the one from the RFC:
type MyAttribute<^T>(context : ^T)=
inherit Attribute()
[<MyAttribute(24)>]
let x = 1
@@ -2252,7 +2258,7 @@ and u_attribkind st = | |||
|
|||
and u_attrib st : Attrib = | |||
let a, b, c, d, e, f = u_tup6 u_tcref u_attribkind (u_list u_attrib_expr) (u_list u_attrib_arg) u_bool u_dummy_range st | |||
Attrib(a, b, c, d, e, None, f) // AttributeTargets are not preserved | |||
Attrib(a, b, [], c, d, e, None, f) // AttributeTargets are not preserved (u_list u_ty) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What will happen if an older compiler sees metadata containing type arguments?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I understand your question correctly, this relates to a situation where in an assembly that actually uses generic attributes (i.e. actually applying attributes to "things") is referenced by another assembly being built by an older version of fsc. In that case, I think the type arguments of used attributes will need to be digested into the "TyConRef" field of the typed Attrib; i.e., it would need to become a new type definition?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I've read this change wrong. Not keeping the data is sad, but at the same time changing the metadata format is probably a no-go here for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would that kind of change just be another RFC/PR, or are there other complexities that need to be dealt with?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem is we try to maintain the metadata compatibility with the older compilers, and currently there's no preferred way to add new data to it without breaking existing compilers.
@vzarytovskii Do you know if there been any changes in this direction?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The precedence we used to take before:
- Absolutely maintain backwards compatibility for existing source code (i.e. code not using the feature)
- Have a compiler error if this is used in FSharp.Core
- New code using this feature will force consumers of such code to upgrade their tooling (IDE,SDK).
There is an alternative approach of not breaking the pickle format and instead loading the needed info from .NET IL metadata ;; but I do not think this path has been taken before (even though it would be safer from a compatibility perspective, because we do not need to know how to load it for interop scenarios anyway)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would assume that the purpose of the pickled resources is for the sake of performance improvements (correct me if I'm wrong), in which case I would think that there are some kind of benchmarks concerning the gain of using pickling as opposed to loading the same resources from the IL? Assuming that the cost in this case wouldn't be great, and that it is feasible, loading the info from the IL and not breaking backward compatibility sounds like the better option.
@@ -1506,26 +1506,52 @@ attributeListElements: | |||
|
|||
|
|||
/* One custom attribute */ | |||
attribute: | |||
attribute: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be possible to add recovery for unfinished attributes? For example, consider typing a new attribute and then adding type arguments to it. We want the parser (and the rest of the analysis) to be able to parse the rest of the file while the attribute is being typed, if that is possible with the language grammar.
[<Attr<>]
()
[<Attr<>>]
()
[<Attr<arg>]
()
[<Attr<_>]
()
[<Attr<arg; Attr2>]
()
[<Attr<(LiteralArg)>]
()
[<Attr< LiteralArg>]
()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not entirely certain how this would be done at the moment, but I will begin playing the idea now.
There is a complication with the lexer and typars being immediately adjacent to a closing >]
that I think might end up being an obstacle in this case - I include in case I am missing something and someone can enlighten me on the inner workings on the lexer:
In order to allow for the closing >]
to be immediately adjacent to a typar, e.g.:
[<Attr<_>>]
We need to deal with the fact that the lexer is going to collect the whole series of greater-thans. Based on my current understanding of how the lexer works, I think the only place we can deal with this is in the LexFilterImpl.peekAdjacentTypars
function in Compiler\SyntaxTree\LexFilter.fs
, in particular when the stack is being constructed via scan ahead (I assume the scan ahead section is the best place as we need to know that a ]
is immediately adjacent in order to be sure we can smash the last gt off the end of what the lexer collects).
@@ -353,7 +353,7 @@ type UnsharedRow(elems: RowElement[]) = | |||
type ILTypeWriterEnv = { EnclosingTyparCount: int } | |||
let envForTypeDef (tdef: ILTypeDef) = { EnclosingTyparCount=tdef.GenericParams.Length } | |||
let envForMethodRef env (ty: ILType) = { EnclosingTyparCount=(match ty with ILType.Array _ -> env.EnclosingTyparCount | _ -> ty.GenericArgs.Length) } | |||
let envForNonGenericMethodRef _mref = { EnclosingTyparCount=Int32.MaxValue } | |||
//let envForNonGenericMethodRef _mref = { EnclosingTyparCount=Int32.MaxValue } do we need this for attributes? It doesn't appear so |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I realize it's generally a "no-no" to do this to obscure functionality (i.e. I dont yet understand what this was for), but it seems that with the introduction of generic attributes, we will need to fully qualify the environment for attribute usage. Since this function was only used in the generation of attributes, my current position is that this function can be deleted - though I am still curious why the count was being maxed for the non-generic case, if anyone has that wisdom on hand.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rule of thumb is usually:
We have one more case - when we introduced another resource for nullability data, it has multiple problems unfortunately:
We probably want to think of such mechanism in future, but it will require a very thorough design and will be a huge chunk of work.
Not necessary, main purpose is to have/store additional metadata, which we want to use cross-assembly (mostly). I don't really think it will affect perf significatly.
Yeah, but please, refer to part of this comment below - not all the info we would like to have is in the IL metadata. TL;DR - I would personally go with second point in the beginning of my comment if possible. We have done it before. We will likely do it in future. And at the moment we, unfortunately, don't have a better mechanism for it. |
I would like to see more interop tests,
There probably needs to be work to ensure that the IDE experience of Generic Attributes works OK, but I think that can be a separate PR. Needs a language flag so at least we have about a year to work through the kinks. Needs a much more detailed writeup of what's enabled, why, and what the interop experience is supposed to be. This looks like a good start, thanks for taking care of it. |
We'll convert this to draft for now, due to low activity. @mflibby ping us if you need help :) |
It would be nice to finally have them |
Yeah my apologies for letting this drift a little, I have a project at work that really ramped up at the end of May so this fell by the wayside. I fully intend on returning to this and busting it out the rest of the way starting January. I will definitely take you up on that offer as I know that there are several things that are confusing me in terms of both the build process and general path-forward development things.
I look forward to getting back to this :) |
@mflibby no need to apologize, we all get overwhelmed by stuff sometimes :) Happy you're going to be back in a game! On that note, seeing that it's your first PR in F#, I would like to suggest you joining the Amplifying F# initiative. Folks in AF# can be happy and fast to answer all (or most of) the questions you've put here. Moreover, recently we've started doing community PR reviews and I thought this one can be also a good case for such a session - generic attributes are a rather hard yet interesting topic and this can be a great learning exercise for the community (and maintainers as well, me for sure :D) @edgarfgp et al will guide through the process then. |
Just adding a feature for language version
Anywhere under
One important thing to remember, is that you want to run tests (not debug them) in the Release configuration, since it's what we care codegen-wise the most, I think this is what you were facing. Otherwise, we can solve it as it comes.
Not Kevin, but I think either here or in the suggestion/RFC would be a good place for it to be. |
Description
Resolves FSLang 965
RFC FS-1143
Intention:
The intent of this PR is to implement Generic Attribute support, e.g.:
Checklist