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

Check target of module/assembly level suppressions - #2129

Merged
mateoatr merged 8 commits into
dotnet:mainfrom
mateoatr:usmScopes
Jul 8, 2021
Merged

mateoatr merged 8 commits into
dotnet:mainfrom
mateoatr:usmScopes

Conversation

@mateoatr

@mateoatr mateoatr commented Jul 2, 2021

Copy link
Copy Markdown
Contributor

The Scope parameter is only relevant in the context of global suppressions (put in a module or an assembly); and the only scopes that we support are module, type and member. Whenever the scope is set to a value other than these three, we log a message stating that the used argument is not supported -- this was changed to log a warning instead.

Unconditional suppressions put on a module/assembly that specify a target are registered as if they were put on the target (e.g., on a type, field, etc.); thus, when looking if a warning is suppressed on a global level, we must make sure to only look in global suppressions that do not specify a target -- this was fixed.

Comment thread docs/error-codes.md Resolved

if (info.Target == null || info.Scope == null)
if (info.Target == null || scope == null)
continue;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should null Target or Scope be warnings too?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see why we should. Both parameters are only relevant for global suppressions, which should always look like [assembly: Unc.../[module: Unc..., and then I think it's reasonable to put the suppression on the module whenever neither are specified. Having a non-module scope without a target could be a case where I think a warning makes sense, e.g. [module: USM("", "", Scope = "type")], I haven't tried this but I think this would actually throw from DocumentationSignatureParser :/

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add tests for all these cases - to validate the current behavior. And if some of those behaviors seem broken, either fix it or file new bugs. But have them "verified" by tests regardless.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some tests and changed the DocumentationSignatureParser so that we don't throw when module or id are null.

@eerhardt

eerhardt commented Jul 6, 2021

Copy link
Copy Markdown
Member

Unconditional suppressions put on a module/assembly that specify a target are registered as if they were put on the target (e.g., on a type, field, etc.); thus, when looking if a warning is suppressed on a global level, we must make sure to only look in global suppressions that do not specify a target

I wonder if we really need to support global suppressions for a whole assembly. It feels like too easy of a trap to fall into.

Maybe if we need to support this, we could write a special rule into the Roslyn analyzer that warns for UnconditionalSuppressMessage attributes at the global level that don't have Target specified. Then someone could suppress that warning with a #pragma if they really, really wanted to suppress a warning across a whole assembly.

@vitek-karas

Copy link
Copy Markdown
Member

Maybe if we need to support this, we could write a special rule into the Roslyn analyzer that warns for UnconditionalSuppressMessage attributes at the global level that don't have Target specified. Then someone could suppress that warning with a #pragma if they really, really wanted to suppress a warning across a whole assembly.

I like this - also as a way to gather more data - if there are actual use cases for assembly level suppressions, we can always just remove this analyzer warning (or scope it better).

return _suppressions.TryGetValue (provider, out var suppressions) &&
suppressions.TryGetValue (id, out info);
suppressions.TryGetValue (id, out info) &&
(provider is ModuleDefinition || provider is AssemblyDefinition ? info.Target == null : true);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this necessary - the code in LookForModuleLevelAndGlobalSuppressions will only add the suppression to the list if

  • Target is null and provider is module or assembly
  • Target is not null, but then provider is not module or assembly

In which case do we need this additional condition?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. I was hitting this whenever a global suppression was being added from MarkStep https://github.com/mono/linker/blob/41fd293dda27c099b063f51d8917e607271a508e/src/linker/Linker.Steps/MarkStep.cs#L811-L812
Changed things there so that that path isn't followed for global suppressions, otherwise we'd hit this (having suppressions where the provider is a module/assembly and it has a target).

Add tests

@vitek-karas vitek-karas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@mateoatr
mateoatr requested a review from sbomer July 8, 2021 18:05

@sbomer sbomer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@mateoatr
mateoatr merged commit b950192 into dotnet:main Jul 8, 2021
agocke pushed a commit to dotnet/runtime that referenced this pull request Nov 16, 2022
* Check that global level suppressions do not specify any target

* Run lint

* Update test/Mono.Linker.Tests.Cases/Warnings/WarningSuppression/SuppressWarningsInModule.cs

Co-authored-by: Sven Boemer <[email protected]>

* Add code snippet with an example of 2108.

* PR feedback
Add tests

* Lint

* Don't silently suppress warnings on module if scope is missing and a target was used.

Co-authored-by: Sven Boemer <[email protected]>

Commit migrated from dotnet/linker@b950192
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants