Skip to content

Adding attributes to extension block has uninformative error message #82444

@Rekkonnect

Description

@Rekkonnect

Summary

Adding attributes to extension block does not specify that extension blocks are not valid targets for attributes.

Steps to Reproduce

Type the following:

public static class FluentExtensions
{
    [MyAttribute]
    extension(string s)
    {
    }
}

Notice the MyAttribute attribute on the extension block. This will be invalid as extension blocks are not valid attribute targets.

Expected Result

Any of the following error variants:

  • Error CS7014 - Attributes are not valid in this context.
    This error is being reported when for example trying to add attributes on a statement
  • Error CS1671 - A namespace declaration cannot have modifiers or attributes
    This is an error specific to namespaces, as it would be common to assume namespaces can be attributed

Actual

When the attribute name is bound:
Error CS0592 - Attribute 'MyAttribute' is not valid on this declaration type. It is only valid on 'assembly, module, class, struct, enum, constructor, method, property, indexer, field, event, interface, parameter, delegate, return, type parameter' declarations.

When the attribute name is something random (e.g. gsdf):
Error CS0246 - The type or namespace name 'gsdf' could not be found (are you missing a using directive or an assembly reference?)
Error CS0246 - The type or namespace name 'gsdfAttribute' could not be found (are you missing a using directive or an assembly reference?)

Metadata

Metadata

Assignees

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions