Skip to content
This repository was archived by the owner on Sep 25, 2026. It is now read-only.
This repository was archived by the owner on Sep 25, 2026. It is now read-only.

Linker will suggest the wrong place to use ExpectedWarning #2575

Closed

Description

@tlakollo

If you have diagnostics coming from similar member names, the linker might report the warning in a completely different place

class AnnotatedProperty
{
	[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicNestedTypes)]
	public static Type PropertyWithAnnotation { get; set; }

        [DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicNestedTypes)]
	public virtual Type VirtualPropertyWithAnnotation { get => null; set { value.ToString(); } }

	class AttributeWithPropertyWithAnnotation : Attribute
	{
		public AttributeWithPropertyWithAnnotation () { }

		[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicProperties)]
		public Type PropertyWithAnnotation { get; set; }
	}

	static void DynamicallyAccessedMembersAll ()
	{
		typeof (AnnotatedProperty).RequiresAll ();
	}
}

The set of expected warnings should be:

[ExpectedWarning ("IL2111", nameof (AnnotatedProperty.PropertyWithAnnotation) + ".set")]
[ExpectedWarning ("IL2111", nameof (AnnotatedProperty.PropertyWithAnnotation) + ".set")]
[ExpectedWarning ("IL2111", nameof (AttributeWithPropertyWithAnnotation.PropertyWithAnnotation) + ".set")]
[ExpectedWarning ("IL2111", nameof (AttributeWithPropertyWithAnnotation.PropertyWithAnnotation) + ".set")]
[ExpectedWarning ("IL2111", nameof (VirtualPropertyWithAnnotation) + ".get")]
[ExpectedWarning ("IL2111", nameof (VirtualPropertyWithAnnotation) + ".get")]
[ExpectedWarning ("IL2111", nameof (VirtualPropertyWithAnnotation) + ".set")]
[ExpectedWarning ("IL2111", nameof (VirtualPropertyWithAnnotation) + ".set")]

And linker might end up generating

[ExpectedWarning ("IL2111", nameof (AnnotatedProperty.PropertyWithAnnotation) + ".set")]
[ExpectedWarning ("IL2111", nameof (AnnotatedProperty.PropertyWithAnnotation) + ".set")]
+ [ExpectedWarning ("IL2111", nameof (AnnotatedProperty.PropertyWithAnnotation) + ".set")]
[ExpectedWarning ("IL2111", nameof (AttributeWithPropertyWithAnnotation.PropertyWithAnnotation) + ".set")]
[ExpectedWarning ("IL2111", nameof (AttributeWithPropertyWithAnnotation.PropertyWithAnnotation) + ".set")]
+ [ExpectedWarning ("IL2111", nameof (AttributeWithPropertyWithAnnotation.PropertyWithAnnotation) + ".set")]
[ExpectedWarning ("IL2111", nameof (VirtualPropertyWithAnnotation) + ".get")]
[ExpectedWarning ("IL2111", nameof (VirtualPropertyWithAnnotation) + ".get")]
- [ExpectedWarning ("IL2111", nameof (VirtualPropertyWithAnnotation) + ".set")]
- [ExpectedWarning ("IL2111", nameof (VirtualPropertyWithAnnotation) + ".set")]

Notice that nameof(AnnotatedProperty.PropertyWithAnnotation) will resolve to the string 'PropertyWithAnnotation' and therefore is a subset of the name 'VirtualPropertyWithAnnotation'. This means that if you use [ExpectedWarning ("IL2111", nameof (AnnotatedProperty.PropertyWithAnnotation) + ".set")] you can suppress the VirtualPropertyWithAnnotation.set diagnostic since it will match PropertyWithAnnotation.set inside the string VirtualPropertyWithAnnotation.set.
Problem is that the generation of the diagnostic does come from PropertyWithAnnotation.set, meaning that if you use the correct ExpectedWarning linker will fail the test.

There is a place where linker should be using string matching incorrectly since changing the name of the virtual property to VirtualPropertyWithAnnotationBothAccessors fixes the problem.

The analyzer does not have this problem, it will suggest the correct expected warning

Activity

  1. vitek-karas commented on Feb 2, 2022

    @vitek-karas
    Member

    THis is kind of by design - the strings were supposed to be just substrings... we might want to change the semantics..

  2. tlakollo commented on Feb 2, 2022

    @tlakollo
    ContributorAuthor

    Ah got it, the problem is that the whole set of warnings is right, but the ordering makes VirtualProperty... ones to appear first disappear the ExpectedWarning for Property... ones, and then linker will complain about not having enough ExpectedWarnings for Property... warnings or fail because VirtualProperty was never generated. I think I will close it I don't see ourselves changing this behavior

  3. vitek-karas commented on Feb 2, 2022

    @vitek-karas
    Member

    I think the right way to fix this is to convert all diagnostics to use the (Id + Args) in the linker (the analyzer already does this basically). And to change the linker->test interface to pass along just the Id + Args, not the actual text message. And then the meaning of the args in ExptectedWarning would be to match the args passed to the diagnostic - exactly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions