Skip to content
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

No associated IFieldSymbol is provided for a field-like event #36259

Closed
jnm2 opened this issue Jun 8, 2019 · 5 comments
Closed

No associated IFieldSymbol is provided for a field-like event #36259

jnm2 opened this issue Jun 8, 2019 · 5 comments
Labels
Area-Compilers Concept-API This issue involves adding, removing, clarification, or modification of an API. Feature Request
Milestone

Comments

@jnm2
Copy link
Contributor

jnm2 commented Jun 8, 2019

If you create a compilation with the following syntax:

public class Foo
{
    public int X { get; }

    private event EventHandler Y;
}

And you get the ITypeSymbol for the type declaration from the symbolic model, it contains an IFieldSymbol representing the backing field for the property but none for the backing field of the event:

Why is an associated IFieldSymbol surfaced for an autoprop but not an auto event?
Is there any chance that an associated IFieldSymbol might not be surfaced for an autoprop in some circumstances if I got the containing ITypeSymbol from a TypeDeclarationSyntax?

@CyrusNajmabadi reproduced this and asked me to file since this doesn't seem right to him.

/// <summary>
/// If this field serves as a backing variable for an automatically generated
/// property or a field-like event, returns that
/// property/event. Otherwise returns null.
/// Note, the set of possible associated symbols might be expanded in the future to
/// reflect changes in the languages.
/// </summary>
ISymbol AssociatedSymbol { get; }

// NOTE: if there's an initializer in source, we'd better create a backing field, regardless of
// whether or not the initializer is legal.
if (hasInitializer || !(this.IsExtern || this.IsAbstract))
{
_associatedField = MakeAssociatedField(declaratorSyntax);
// Don't initialize this.type - we'll just use the type of the field (which is lazy and handles var)
}

@CyrusNajmabadi
Copy link
Member

Tagging @jcouv and @gafter

@jcouv jcouv added Area-Compilers Concept-API This issue involves adding, removing, clarification, or modification of an API. labels Jun 9, 2019
@gafter gafter added the Bug label Jun 10, 2019
@gafter gafter added Feature Request and removed Bug labels Jul 9, 2019
@gafter gafter added this to the Compiler.Next milestone Jul 9, 2019
@JohanLarsson
Copy link

Also AssociatedField on SourceFieldLikeEventSymbol is internal and hence not exposed in IEventSymbol.

http://sourceroslyn.io/#Microsoft.CodeAnalysis.CSharp/Symbols/Source/SourceFieldLikeEventSymbol.cs,114

@jnm2
Copy link
Contributor Author

jnm2 commented Dec 24, 2019

15 hours in, PR is up at #40572.

@jnm2
Copy link
Contributor Author

jnm2 commented Dec 26, 2019

My original use case: https://gitter.im/dotnet/roslyn/archives/2019/06/07?at=5cfb12aa3dcdab40030b066c (keep in mind that my HasBackingField extension method was forced to use reflection)

Another use case: DotNetAnalyzers/ReflectionAnalyzers#206

@jnm2
Copy link
Contributor Author

jnm2 commented Mar 12, 2020

After discussion in #40572, closing in favor of #40103.

@jnm2 jnm2 closed this as completed Mar 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Concept-API This issue involves adding, removing, clarification, or modification of an API. Feature Request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants