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.

Inconsistent behavior for Requires on attribute constructor #2529

Closed

Description

@sbomer

When an attribute ctor has RequiresUnreferencedCode we normally warn on attribute instances. If an attributed member has RequiresUnreferencedCode, this normally silences the warning. However, the behavior is inconsistent for RUC on type:

In the analyzer, RUC on type silences warnings from attributes on the type, type but not for attributes on the type's members.
In the linker, RUC on type silences warnings from attributes on the type's members, but not for attributes on the type itself.

I think the linker behavior makes sense here because RUC on type is supposed to allow the type's members to require unreferenced code without producing a bunch of warnings, instead producing warnings on dynamic access to the RUC members. I think these warnings should cover all ways to get to the members' attributes too. The attribute on the type itself should warn because it's possible to get at the type (and its attributes) with reflection, and this doesn't produce any warning. @vitek-karas do you agree?

Activity

  1. vitek-karas commented on Jan 21, 2022

    @vitek-karas
    Member

    Short version: Yes - I agree.

    This forced me to finally merge a writeup on a related topic I had for a long time: https://github.com/dotnet/linker/blob/main/docs/design/DAM-on-type-and-RUC-interaction.md

    The important bits:

    RUC on type externally behaves as if all static members and constructors have RUC
    RUC on type suppresses warnings as if all methods in the type have RUC on them (so method bodies and attributes)
    RUC on type does NOT suppress warnings about/on nested types in any way

    In the linker RUC on type is implemented by code which only reacts to accessed to members - accessing a type is not even noticed by the linker. We should implement something similar to this in the analyzer.

    Note that this should apply to all warnings produced by the attributes, not just having RUC on the .ctor. Attribute can have DAM annotations which can also produce warnings and so on.

  2. self-assigned this
    on Jan 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions