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

Fixes a null-ref/NotSupportedException in warning infra - #2396

Merged
vitek-karas merged 1 commit into
dotnet:mainfrom
vitek-karas:FixNullRefInWarnings
Dec 1, 2021
Merged

vitek-karas merged 1 commit into
dotnet:mainfrom
vitek-karas:FixNullRefInWarnings

Conversation

@vitek-karas

Copy link
Copy Markdown
Member

If we end up marking a type due to descriptor and the type marking will cause a warning (for example a removed attribute type does this in some cases), the warning infra would fail.

This is because it requires all warnings to have a valid origin including which assembly it came from. But descriptors didn't provide this information.

Most of the change here are to fix that lack of information. When marking through annotations, we now store the message origin for the mark operation along with the target of the marking. This is then restored on the scope stack to be used when reporting warnings caused by such marking.

This required lot of changes to pass the message origin in all places which end up marking things in annotations.

This also required some small fixes as it uncovered previously unseen issues.

Additionally, it can still happen that there's really no origin (for example copy action specified on command line will mark entire assembly - there's nothing to blame, but the command line) for a message or we can't assign an assembly (descriptor file pass on the command line, we can blame the file, but we still don't have an assembly).

So this change tweaks the warning infra to allow messages without assembly of origin.

Enabled a test whcih validated the behavior around removed attribute being marked via descriptor (this now produces warning with fully valid origin).
Added another test where the warning is due to a descriptor on command line (no associated assembly for such warning).

This fixes the linker issue found in #2358.

If we end up marking a type due to descriptor and the type marking will cause a warning (for example a removed attribute type does this in some cases), the warning infra would fail.

This is because it requires all warnings to have a valid origin including which assembly it came from. But descriptors didn't provide this information.

Most of the change here are to fix that lack of information. When marking through annotations, we now store the message origin for the mark operation along with the target of the marking. This is then restored on the scope stack to be used when reporting warnings caused by such marking.

This required lot of changes to pass the message origin in all places which end up marking things in annotations.

This also required some small fixes as it uncovered previously unseen issues.

Additionally, it can still happen that there's really no origin (for example copy action specified on command line will mark entire assembly - there's nothing to blame, but the command line) for a message or we can't assign an assembly (descriptor file pass on the command line, we can blame the file, but we still don't have an assembly).

So this change tweaks the warning infra to allow messages without assembly of origin.

Enabled a test whcih validated the behavior around removed attribute being marked via descriptor (this now produces warning with fully valid origin).
Added another test where the warning is due to a descriptor on command line (no associated assembly for such warning).
Comment thread src/linker/Linker/Annotations.cs Resolved

// The assembly attribute should be specified if available as it allows assigning the diagnostic
// to a an assembly (we group based on assembly).
public MessageOrigin (string fileName, int sourceLine, int sourceColumn, AssemblyDefinition? assembly)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you make AssemblyDefinition non-nullable? I think in this advanced ctor it would make more sense.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Unfortunately not. The case where the null happens here is command line specified descriptor xml. Warnings from it have file name, and line/col numbers, but no assembly. It's a valid case and the code actually handles it. The "field" test I added tests for exactly that case.

@dotnet dotnet deleted a comment from vitek-karas Nov 26, 2021

@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.

LGTM, thank you!

Comment thread src/linker/Linker/Annotations.cs Resolved
// Add an empty record - this prevents reentrancy
// If the embedded XML itself generates warnings, trying to log such warning
// may ask for attributes (suppressions) and thus we could end up in this very place again
// So first add a dummy record and once processed we will replace it with the real data

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.

Makes sense, but this scares me 😨

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah - I'm not happy about it either. I did go through the code which can run here, and there's only very few places where we generate warnings, so it's not that bad. Maybe we should think about this some more how to solve - but this is hard, we want to be lazy about the suppressions (too expensive to do eagerly).

@vitek-karas
vitek-karas merged commit 4e77a94 into dotnet:main Dec 1, 2021
@vitek-karas
vitek-karas deleted the FixNullRefInWarnings branch December 1, 2021 21:37
agocke pushed a commit to dotnet/runtime that referenced this pull request Nov 16, 2022
…r#2396)

If we end up marking a type due to descriptor and the type marking will cause a warning (for example a removed attribute type does this in some cases), the warning infra would fail.

This is because it requires all warnings to have a valid origin including which assembly it came from. But descriptors didn't provide this information.

Most of the change here are to fix that lack of information. When marking through annotations, we now store the message origin for the mark operation along with the target of the marking. This is then restored on the scope stack to be used when reporting warnings caused by such marking.

This required lot of changes to pass the message origin in all places which end up marking things in annotations.

This also required some small fixes as it uncovered previously unseen issues.

Additionally, it can still happen that there's really no origin (for example copy action specified on command line will mark entire assembly - there's nothing to blame, but the command line) for a message or we can't assign an assembly (descriptor file pass on the command line, we can blame the file, but we still don't have an assembly).

So this change tweaks the warning infra to allow messages without assembly of origin.

Enabled a test whcih validated the behavior around removed attribute being marked via descriptor (this now produces warning with fully valid origin).
Added another test where the warning is due to a descriptor on command line (no associated assembly for such warning).

Commit migrated from dotnet/linker@4e77a94
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.

3 participants