roslyn icon indicating copy to clipboard operation
roslyn copied to clipboard

Quick info on `field` exposes generated member name, unlike on accessors

Open jnm2 opened this issue 1 year ago • 9 comments

Version Used: 17.12 Preview 3.0

The metadata name of the backing field is being displayed: Image

But accessors don't show the metadata name of the generated accessor methods: Image

Perhaps instead, this could show int C.P.field?

jnm2 avatar Oct 16 '24 15:10 jnm2

Probably the fix belongs in SymbolDisplay?

RikkiGibson avatar Oct 16 '24 19:10 RikkiGibson

Just be curious: what will be displayed in quick info for such backing field? int C.field or just int field?

kyoyama-kazusa avatar Oct 17 '24 14:10 kyoyama-kazusa

Also: Image

jnm2 avatar Oct 17 '24 14:10 jnm2

Just be curious: what will be displayed in quick info for such backing field? int C.field or just int field?

I think int C.P.field would be best for symbol display. The property is a logical container for the field. @cston in case you have any additional thoughts here.

RikkiGibson avatar Oct 17 '24 16:10 RikkiGibson

Doing a quick check for what additional behaviors need to be specified, using property accessors (e.g. int C.P.get) as a starting point for the way the field name is displayed.

  • https://github.com/dotnet/roslyn/blob/main/src/Compilers/Core/Portable/SymbolDisplay/SymbolDisplayMemberOptions.cs
  • https://github.com/dotnet/roslyn/blob/main/src/Compilers/Core/Portable/SymbolDisplay/SymbolDisplayMiscellaneousOptions.cs
  • https://github.com/dotnet/roslyn/blob/main/src/Compilers/CSharp/Test/Symbol/SymbolDisplay/SymbolDisplayTests.cs

I am not seeing any options or tests which apply customization of how a property accessor name is displayed. I don't think we should offer any option to, for example, omit the field name component from symbol display. But, let's bring the question up with API review.

I am also thinking that the symbol display behavior should be independent of LangVersion. I do not think that SymbolDisplayVisitor.Members has any existing cases of language version affecting the way symbols are displayed.

RikkiGibson avatar Oct 17 '24 17:10 RikkiGibson

I'd prefer just naming the property. Is there a reason that would not be suitable?

CyrusNajmabadi avatar Oct 17 '24 18:10 CyrusNajmabadi

That would be confusing, because the tracked-null state of field and P may differ.

jnm2 avatar Oct 17 '24 18:10 jnm2

Agree with @jnm2. The backing field is a distinct symbol, and when using the backing field, it is helpful to be clear that is what you are using and not the containing property.

RikkiGibson avatar Oct 17 '24 19:10 RikkiGibson

Ah ok. int X.field makes sense. Similar to int X.set.

CyrusNajmabadi avatar Oct 17 '24 19:10 CyrusNajmabadi

@RikkiGibson why is this marked as "api-ready-for-review". I don't see any API changes in the comments here.

jaredpar avatar Oct 22 '24 12:10 jaredpar

It was suggested in offline conversation with @cston that we should run this scenario by API review to decide if we want to introduce any symbol display options related to this feature.

RikkiGibson avatar Oct 22 '24 16:10 RikkiGibson

I think an email discussion with the api review alias, or with the owner of the api (@alekseyts and myself) is the appropriate first step here, rather than bringing it to the entire api review meeting.

333fred avatar Oct 23 '24 00:10 333fred

We discussed on the internal mailing list and got several folks on board with the suggested design.

  • Display of a backing field symbol will suffix '.field' onto the display of the containing property. This is similar to the way property accessors are handled by suffixing '.get', for example.
  • This only applies when the field itself is used, which generally only occurs within the accessors of the containing property.
  • Therefore display of an example symbol like 'field' in 'int Prop { get => field; }' will result in 'int ContainingType.Prop.field' when the appropriate options are used.
  • No new options are provided for customizing the display. Existing options which affect things like type name and member name qualification continue to be respected.
  • This will apply to all backing field symbols, including those associated with "classic" auto properties. (Such symbols have generally not been displayed in IDE scenarios to date.)
  • This doesn't apply to VB backing fields, which have speakable names (they can be referenced in source code by name).
  • Symbol display options which indicate the metadata name should be displayed, should also apply here (e.g. causing the field's unspeakable metadata name to be shown as it is today.)

RikkiGibson avatar Oct 29 '24 22:10 RikkiGibson

@RikkiGibson Quick question: is the type in string C.P.field going to have the field's nullability annotation?

jnm2 avatar Oct 30 '24 12:10 jnm2

For now, I am expecting the field to have, and show, the same type and nullable annotation as the containing property.

If we move forward with null-resilient getters, I am not sure yet whether the inferred nullability should surface in the symbol model. Basically, it seems risky to make it so accessing the TypeWithAnnotations of a symbol (or just the Type in public symbol API), causes a get accessor to be bound and flow analyzed. It could make the access unexpectedly costly, and could introduce cycles which are difficult to stamp out.

Though, FWIW, we know that work is already done and being surfaced in the Quick Info case.

RikkiGibson avatar Oct 30 '24 19:10 RikkiGibson

It didn't occur to me that changing this behavior for all backing fields would break a billion test baselines, but I will still do it.

RikkiGibson avatar Nov 21 '24 03:11 RikkiGibson

It looks like we only half-handled this for Quick Info.

Quick Info tooltip saying "'<Prop>k__BackingField' is not null here."

@jasonmalinowski do you know which API is being used to display the symbol here? Maybe just Name? I am wondering if some SymbolDisplay needs to be used instead, or if IDE needs to special case it, or if compiler needs to expose something new for the layers to work appropriately..

RikkiGibson avatar Feb 14 '25 00:02 RikkiGibson