Accessibility of protected members of attribute classes #3948
Replies: 2 comments 1 reply
-
It's very likely that the rules the C# compiler implements here do not match the spec exactly, due do the native compiler's behavior. Name lookup especially was a place where the native compiler often played a bit loose with the rules, and in these cases we often matched Roslyn's behavior to the native compiler in order to allow people to move forward. I do agree that a reading of the spec suggests these cases should be illegal, but that's the likely reason for the behavior. |
Beta Was this translation helpful? Give feedback.
1 reply
-
Time to close this :) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
From the current spec, sections on accessibility and attributes do not mention how protected members of an attribute and protected attributes are scoped w.r.t. attribute usages.
Furthermore, it is unclear or at least under-specified, whether protected members of classes are accessible from within an attribute declaration of an inheriting class. In fact, the spec appears to prohibit such use as it talks about program text of a type:
Here,
type_declarations
are within the type and do not include the class declaration itself, afaik.Example #1
This example is taken from this discussion: dotnet/fsharp#3172.
The following is valid as per the status quo, but appears to be invalid per the spec:
Example #2
The following is valid, though again the spec appears to prohibit this, in particular by this statement in the spec:
Example #3
If I add a
protected int TestProp;
then suddenly the rules of the spec seem to apply properly and indeed, it is not accessible. Which suggests that the protected constant above has crept in the status quo, but does not align with the spec. This rule is actually in the spec:(the spec has this as a runtime error, which is somewhat surprising...)
I do not propose to suddenly start prohibiting this, but I do think we can do better in the section an accessibility to include whether or not the attribute declaration of a type or member are part of the program text of the type or not, and in the section on attributes, what we mean by public, as clearly attributes can be used when they are protected as well.
Beta Was this translation helpful? Give feedback.
All reactions