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

Nullable annotations part 4 (final) - #2300

Merged
sbomer merged 9 commits into
dotnet:mainfrom
sbomer:nullableAnnotations4
Oct 19, 2021
Merged

sbomer merged 9 commits into
dotnet:mainfrom
sbomer:nullableAnnotations4

Conversation

@sbomer

@sbomer sbomer commented Sep 30, 2021

Copy link
Copy Markdown
Member

Enable nullable annotations for the entire linker project. With this I think we will see fewer nullref exceptions in the future, though there might still be some due to Cecil being null-oblivious.

One pattern I used often was to turn fields which are initialized after the constructor into nullable fields with a non-nullable getter that throws if the underlying field is null. Let me know if there are any concerns with that approach - it's used all over MarkStep for example.

@sbomer
sbomer requested a review from marek-safar as a code owner September 30, 2021 19:01

if (typeReference.IsNested) {
VisitTypeReference (typeReference.GetInflatedDeclaringType (resolver), builder, resolver);
VisitTypeReference (typeReference.GetInflatedDeclaringType (resolver)!, builder, resolver);

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.

Calling out this use of the null-forgiving operator - I'd appreciate more eyes on this one.

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.

At the end of GetInflatedDeclaringType method we never check if the resolved is null, the declaring type would be null if the resolved type is null

var resolved = resolver.TryResolve (type);
System.Diagnostics.Debug.Assert (resolved == type);
return resolved?.DeclaringType;

If resolved cannot be null then the return statement needs a null forgiving operator return resolved!.DeclaringType;
I'm not sure what is a PinnedType or a RequiredModifierType which I guess is the part that needs more attention though

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 think when we get to that line it will always be a TypeDefinition. I changed the code a bit to make this more clear. Maybe there is a way to get here for sentinel types (I believe they're used to encode vararg signatures) but this likely isn't the only thing that doesn't work in that case.

Comment thread src/linker/Linker/MarkingHelpers.cs Resolved
Comment thread src/linker/Linker/MethodBodyScanner.cs Resolved
- Clean up extra variables introduced as
  nullable versions of existing variables.
Comment thread src/linker/Linker.Steps/MarkStep.cs Resolved
Comment thread src/linker/Linker/Driver.cs Resolved

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

Other than existing comments, this looks good!

public static bool GetMatchingExportedType (this ModuleDefinition module, TypeDefinition typeDefinition, [NotNullWhen (true)] out ExportedType? exportedType)
{
exportedType = null;
if (!module.HasExportedTypes || typeDefinition == null)

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.

Do we need the nullcheck if it's non-nullable (typeDefinition)

@sbomer sbomer Oct 2, 2021 •

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.

We don't need it any more. Generally I haven't been removing the redundant null checks in case some null-oblivious code still passes null, but we could probably remove most of them since the whole project is annotated. We should be careful about any null checks in APIs called from custom steps.

@agocke is there an analyzer that will catch these? :)

This reminded me that I forgot to update the reference assembly APIs. I just gave it a try but had issues because of how NullableContextAttribute is emitted, so I'll leave it unannotated for now (see #2302 for details).

Comment thread src/linker/Linker/TypeNameResolver.cs Outdated
return null;

TypeDefinition genericType = _context.TryResolve (genericTypeRef);
TypeDefinition? genericType = _context.TryResolve (genericTypeRef);

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.

Should we check for null here?

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.

Good catch - this is the kind of case that unfortunately won't be caught by the analysis. Looking at it I think the TryResolve call is actually unnecessary since resolving the generic type above should always return a TypeDefinition, so I replaced it with an assert. PTAL @tlakollo and @mateoatr

sbomer added 3 commits October 2, 2021 01:20
- TryGetMethodStubValue may return null when true
- GetReturnType and GetParameterType may return null in some cases
- Don't resolve a generic typeref that should already be a typedef
- Remove some redundant null checks
- Turn on nullable annotations for ref assembly
- Remove #nullable disable section
- Remove null check
- Replace TryResolve call with an assert
- Add comment and asserts justifying null-forgiving operator
Comment thread src/linker/Linker.Steps/BaseStep.cs Resolved
@sbomer sbomer self-assigned this Oct 6, 2021
@sbomer
sbomer requested a review from marek-safar October 7, 2021 16:38
var resourceManager = SharedStrings.ResourceManager;
_titleFormat = resourceManager.GetString ($"{diagnosticId}Title");
_messageFormat = resourceManager.GetString ($"{diagnosticId}Message");
_titleFormat = resourceManager.GetString ($"{diagnosticId}Title") ?? string.Empty;

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.

I would throw here. This looks like it might hide a bug.

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.

Note that not all message strings have a title counterpart, take for example BaseRequiresMismatchMessage (there's no BRMTitle string).

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.

I made it an exception to use a diagnostic without a message, but left it as-is for titles per @mateoatr's comment.

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.

Never mind - looks like not all diagnostics have fixed messages, because we use different strings depending on the context - see here:

public static string FormatRequiresAttributeMismatch (bool memberHasAttribute, bool isInterface, params object[] args)
{
string format = (memberHasAttribute, isInterface) switch {
(false, true) => SharedStrings.InterfaceRequiresMismatchMessage,
(true, true) => SharedStrings.ImplementationRequiresMismatchMessage,
(false, false) => SharedStrings.BaseRequiresMismatchMessage,
(true, false) => SharedStrings.DerivedRequiresMismatchMessage
};
return string.Format (format, args);
}

Comment thread src/ILLink.Shared/DiagnosticString.cs Resolved
- Add parens to improve readability
- Throw on diagnostic without message
- Remove unnecessary string.empty
- Some DiagnosticIds intentionally have no fixed format strings because
  we generate different strings in different circumstances.
@sbomer sbomer closed this Oct 18, 2021
@sbomer sbomer reopened this Oct 18, 2021
@sbomer sbomer closed this Oct 19, 2021
@sbomer sbomer reopened this Oct 19, 2021
@sbomer
sbomer merged commit 4c9de67 into dotnet:main Oct 19, 2021
agocke pushed a commit to dotnet/runtime that referenced this pull request Nov 16, 2022
* Nullable annotations part 4 (final)

* PR feedback

- Clean up extra variables introduced as
  nullable versions of existing variables.

* Fix a few annotations

- TryGetMethodStubValue may return null when true
- GetReturnType and GetParameterType may return null in some cases

* More annotations and PR feedback

- Don't resolve a generic typeref that should already be a typedef
- Remove some redundant null checks
- Turn on nullable annotations for ref assembly
- Remove #nullable disable section

* Clean up GetInflatedDeclaringType

- Remove null check
- Replace TryResolve call with an assert
- Add comment and asserts justifying null-forgiving operator

* Replace exception with assert in accessors

* PR feedback

- Add parens to improve readability
- Throw on diagnostic without message
- Remove unnecessary string.empty

* Undo DiagnosticString change

- Some DiagnosticIds intentionally have no fixed format strings because
  we generate different strings in different circumstances.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants