Repository navigation
Nullable annotations part 4 (final) - #2300
Conversation
|
|
||
| if (typeReference.IsNested) { | ||
| VisitTypeReference (typeReference.GetInflatedDeclaringType (resolver), builder, resolver); | ||
| VisitTypeReference (typeReference.GetInflatedDeclaringType (resolver)!, builder, resolver); |
There was a problem hiding this comment.
Calling out this use of the null-forgiving operator - I'd appreciate more eyes on this one.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
- Clean up extra variables introduced as nullable versions of existing variables.
vitek-karas
left a comment
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
Do we need the nullcheck if it's non-nullable (typeDefinition)
There was a problem hiding this comment.
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).
| return null; | ||
|
|
||
| TypeDefinition genericType = _context.TryResolve (genericTypeRef); | ||
| TypeDefinition? genericType = _context.TryResolve (genericTypeRef); |
There was a problem hiding this comment.
Should we check for null here?
There was a problem hiding this comment.
- 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
| var resourceManager = SharedStrings.ResourceManager; | ||
| _titleFormat = resourceManager.GetString ($"{diagnosticId}Title"); | ||
| _messageFormat = resourceManager.GetString ($"{diagnosticId}Message"); | ||
| _titleFormat = resourceManager.GetString ($"{diagnosticId}Title") ?? string.Empty; |
There was a problem hiding this comment.
I would throw here. This looks like it might hide a bug.
There was a problem hiding this comment.
Note that not all message strings have a title counterpart, take for example BaseRequiresMismatchMessage (there's no BRMTitle string).
There was a problem hiding this comment.
I made it an exception to use a diagnostic without a message, but left it as-is for titles per @mateoatr's comment.
There was a problem hiding this comment.
Never mind - looks like not all diagnostics have fixed messages, because we use different strings depending on the context - see here:
linker/src/ILLink.Shared/MessageFormat.cs
Lines 23 to 33 in 3418bcd
- 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.
* 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
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.