-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Help compiler enforce nullability annotations #32090
Changes from 3 commits
ae5f683
cf34100
1282234
d65f2d7
3e5405f
4ee4cea
011b7b7
3e6f259
ca6adaa
b25040f
f7175ba
1feaf72
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -221,7 +221,7 @@ public ImmutableHashSet<T> Remove(T item) | |
[Pure] | ||
public bool TryGetValue(T equalValue, out T actualValue) | ||
{ | ||
int hashCode = _equalityComparer.GetHashCode(equalValue); | ||
int hashCode = _equalityComparer.GetHashCode(equalValue!); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📝 should we remove the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In general |
||
HashBucket bucket; | ||
if (_root.TryGetValue(hashCode, out bucket)) | ||
{ | ||
|
@@ -639,7 +639,7 @@ private static bool IsSupersetOf(IEnumerable<T> other, MutationInput origin) | |
private static MutationResult Add(T item, MutationInput origin) | ||
{ | ||
OperationResult result; | ||
int hashCode = origin.EqualityComparer.GetHashCode(item); | ||
int hashCode = origin.EqualityComparer.GetHashCode(item!); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But now it seems like that would actually not just be for TryGetValue, but really all operations on the hash set, at which point we'd be better off using a notnull constraint? Maybe instead of changing the annotations, open an issue about it and keep the ! changes you have here. #Resolved There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (And actually, the right answer is probably to do a null check prior to calling GetHashCode, as we do in |
||
HashBucket bucket = origin.Root.GetValueOrDefault(hashCode); | ||
var newBucket = bucket.Add(item, origin.EqualityComparer, out result); | ||
if (result == OperationResult.NoChangeRequired) | ||
|
@@ -658,7 +658,7 @@ private static MutationResult Add(T item, MutationInput origin) | |
private static MutationResult Remove(T item, MutationInput origin) | ||
{ | ||
var result = OperationResult.NoChangeRequired; | ||
int hashCode = origin.EqualityComparer.GetHashCode(item); | ||
int hashCode = origin.EqualityComparer.GetHashCode(item!); | ||
HashBucket bucket; | ||
var newRoot = origin.Root; | ||
if (origin.Root.TryGetValue(hashCode, out bucket)) | ||
|
@@ -680,7 +680,7 @@ private static MutationResult Remove(T item, MutationInput origin) | |
/// </summary> | ||
private static bool Contains(T item, MutationInput origin) | ||
{ | ||
int hashCode = origin.EqualityComparer.GetHashCode(item); | ||
int hashCode = origin.EqualityComparer.GetHashCode(item!); | ||
HashBucket bucket; | ||
if (origin.Root.TryGetValue(hashCode, out bucket)) | ||
{ | ||
|
@@ -701,7 +701,7 @@ private static MutationResult Union(IEnumerable<T> other, MutationInput origin) | |
var newRoot = origin.Root; | ||
foreach (var item in other.GetEnumerableDisposable<T, Enumerator>()) | ||
{ | ||
int hashCode = origin.EqualityComparer.GetHashCode(item); | ||
int hashCode = origin.EqualityComparer.GetHashCode(item!); | ||
HashBucket bucket = newRoot.GetValueOrDefault(hashCode); | ||
OperationResult result; | ||
var newBucket = bucket.Add(item, origin.EqualityComparer, out result); | ||
|
@@ -812,7 +812,7 @@ private static MutationResult Except(IEnumerable<T> other, IEqualityComparer<T> | |
var newRoot = root; | ||
foreach (var item in other.GetEnumerableDisposable<T, Enumerator>()) | ||
{ | ||
int hashCode = equalityComparer.GetHashCode(item); | ||
int hashCode = equalityComparer.GetHashCode(item!); | ||
HashBucket bucket; | ||
if (newRoot.TryGetValue(hashCode, out bucket)) | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -140,10 +140,12 @@ internal virtual bool TryGetExports(ImportDefinition definition, [NotNullWhen(tr | |
if (multipleExports != null) | ||
{ | ||
multipleMatches = multipleExports; | ||
// TODO2 singleMatch dould be null when returning true | ||
singleMatch = null!; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📝 the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, please remove it. Thanks. #Resolved |
||
} | ||
else | ||
{ | ||
singleMatch = singleExport; | ||
singleMatch = singleExport!; | ||
} | ||
return true; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -172,14 +172,15 @@ private static bool IsLazyGenericType(Type genericType) | |
|
||
private static bool TryGetCastFunction(Type genericType, bool isOpenGeneric, Type[] arguments, [NotNullWhen(true)] out Func<Export, object>? castFunction) | ||
{ | ||
castFunction = null; | ||
castFunction = null!; // TODO2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📝 the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, please remove it. The dangers of the compiler not having checked those annotations ;) #Resolved |
||
|
||
if (genericType == LazyOfTType) | ||
{ | ||
if (!isOpenGeneric) | ||
{ | ||
castFunction = ExportServices.CreateStronglyTypedLazyFactory(arguments[0].UnderlyingSystemType, null); | ||
} | ||
// castFunction | ||
return true; | ||
} | ||
|
||
|
@@ -189,6 +190,7 @@ private static bool TryGetCastFunction(Type genericType, bool isOpenGeneric, Typ | |
{ | ||
castFunction = ExportServices.CreateStronglyTypedLazyFactory(arguments[0].UnderlyingSystemType, arguments[1].UnderlyingSystemType); | ||
} | ||
// castFunction | ||
return true; | ||
} | ||
|
||
|
@@ -200,6 +202,7 @@ private static bool TryGetCastFunction(Type genericType, bool isOpenGeneric, Typ | |
{ | ||
castFunction = new ExportFactoryCreator(genericType).CreateStronglyTypedExportFactoryFactory(arguments[0].UnderlyingSystemType, null); | ||
} | ||
// castFunction | ||
return true; | ||
} | ||
else if (arguments.Length == 2) | ||
|
@@ -208,6 +211,7 @@ private static bool TryGetCastFunction(Type genericType, bool isOpenGeneric, Typ | |
{ | ||
castFunction = new ExportFactoryCreator(genericType).CreateStronglyTypedExportFactoryFactory(arguments[0].UnderlyingSystemType, arguments[1].UnderlyingSystemType); | ||
} | ||
// castFunction | ||
return true; | ||
} | ||
else | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -154,11 +154,11 @@ public override ComposablePart CreatePart() | |
return null; | ||
} | ||
|
||
internal override bool TryGetExports(ImportDefinition definition, out Tuple<ComposablePartDefinition, ExportDefinition>? singleMatch, out IEnumerable<Tuple<ComposablePartDefinition, ExportDefinition>>? multipleMatches) | ||
internal override bool TryGetExports(ImportDefinition definition, [NotNullWhen(true)] out Tuple<ComposablePartDefinition, ExportDefinition>? singleMatch, out IEnumerable<Tuple<ComposablePartDefinition, ExportDefinition>>? multipleMatches) | ||
{ | ||
if (this.IsGeneric()) | ||
{ | ||
singleMatch = null; | ||
singleMatch = null!; // TODO2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📝 the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto #Resolved |
||
multipleMatches = null; | ||
|
||
List<Tuple<ComposablePartDefinition, ExportDefinition>>? exports = null; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,7 +108,7 @@ internal bool HasHandler(InterpretedFrame frame, Exception exception, [NotNullWh | |
// Unreachable. | ||
// Want to assert that this case isn't hit, but an assertion failure here will be eaten because | ||
// we are in an exception filter. Therefore return true here and assert in the catch block. | ||
handler = null; | ||
handler = null!; | ||
stephentoub marked this conversation as resolved.
Show resolved
Hide resolved
|
||
unwrappedException = exception; | ||
return true; | ||
} | ||
|
@@ -215,9 +215,9 @@ internal sealed class DebugInfo | |
private class DebugInfoComparer : IComparer<DebugInfo> | ||
{ | ||
//We allow comparison between int and DebugInfo here | ||
int IComparer<DebugInfo>.Compare(DebugInfo d1, DebugInfo d2) | ||
int IComparer<DebugInfo>.Compare(DebugInfo? d1, DebugInfo? d2) | ||
{ | ||
if (d1.Index > d2.Index) return 1; | ||
if (d1!.Index > d2!.Index) return 1; // TODO2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📝 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's only ever used with non-null values: https://source.dot.net/#System.Linq.Expressions/System/Linq/Expressions/Interpreter/LightCompiler.cs,233 #Resolved |
||
else if (d1.Index == d2.Index) return 0; | ||
else return -1; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since
T
is constraint to class, can we instead annotateT
asT?
? #ResolvedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed. I'd missed that. Thanks #Resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hum, it looks like this file is compiled into more than one project. One with nullability enabled and one without. Reverted to use the attribute. #Resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you use the annotation and just add
#nullable enable
at the top of the file as well? That's what we've done in other cases like this, where we've not yet gotten to every project that includes it. #Resolved