Repository navigation
Linker will suggest the wrong place to use ExpectedWarning #2575
Description
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
vitek-karas commentedon Feb 2, 2022
THis is kind of by design - the strings were supposed to be just substrings... we might want to change the semantics..
tlakollo commentedon Feb 2, 2022
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
vitek-karas commentedon Feb 2, 2022
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.