Browse Source

Add support for "where T : notnull" constraint.

pull/1641/head
Daniel Grunwald 6 years ago
parent
commit
7afa86d90c
  1. 4
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/NullableRefTypes.cs
  2. 4
      ICSharpCode.Decompiler/CSharp/Syntax/TypeSystemAstBuilder.cs
  3. 8
      ICSharpCode.Decompiler/TypeSystem/Implementation/MetadataTypeParameter.cs
  4. 5
      ILSpy/Languages/CSharpHighlightingTokenWriter.cs

4
ICSharpCode.Decompiler.Tests/TestCases/Pretty/NullableRefTypes.cs

@ -48,6 +48,10 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -48,6 +48,10 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
ByRef(ref a).ToString();
ByRef(ref b)!.ToString();
}
public void Constraints<UC, C, CN, NN, S, SN, D, DN, NND>() where C : class where CN : class? where NN : notnull where S : struct where D : IDisposable where DN : IDisposable? where NND : notnull, IDisposable
{
}
}
public class T02_EverythingIsNullableInHere

4
ICSharpCode.Decompiler/CSharp/Syntax/TypeSystemAstBuilder.cs

@ -1785,7 +1785,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax @@ -1785,7 +1785,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
Constraint ConvertTypeParameterConstraint(ITypeParameter tp)
{
if (!tp.HasDefaultConstructorConstraint && !tp.HasReferenceTypeConstraint && !tp.HasValueTypeConstraint && tp.DirectBaseTypes.All(IsObjectOrValueType)) {
if (!tp.HasDefaultConstructorConstraint && !tp.HasReferenceTypeConstraint && !tp.HasValueTypeConstraint && tp.NullabilityConstraint != Nullability.NotNullable && tp.DirectBaseTypes.All(IsObjectOrValueType)) {
return null;
}
Constraint c = new Constraint();
@ -1802,6 +1802,8 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax @@ -1802,6 +1802,8 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
} else {
c.BaseTypes.Add(new PrimitiveType("struct"));
}
} else if (tp.NullabilityConstraint == Nullability.NotNullable) {
c.BaseTypes.Add(new PrimitiveType("notnull"));
}
foreach (IType t in tp.DirectBaseTypes) {
if (!IsObjectOrValueType(t))

8
ICSharpCode.Decompiler/TypeSystem/Implementation/MetadataTypeParameter.cs

@ -161,7 +161,13 @@ namespace ICSharpCode.Decompiler.TypeSystem.Implementation @@ -161,7 +161,13 @@ namespace ICSharpCode.Decompiler.TypeSystem.Implementation
}
}
}
return Nullability.Oblivious;
if (Owner is MetadataMethod method) {
return method.NullableContext;
} else if (Owner is ITypeDefinition td) {
return td.NullableContext;
} else {
return Nullability.Oblivious;
}
}
public override IEnumerable<IType> DirectBaseTypes {

5
ILSpy/Languages/CSharpHighlightingTokenWriter.cs

@ -272,7 +272,9 @@ namespace ICSharpCode.ILSpy @@ -272,7 +272,9 @@ namespace ICSharpCode.ILSpy
HighlightingColor color = null;
switch (type) {
case "new":
color = typeKeywordsColor;
case "notnull":
// Not sure if reference type or value type
color = referenceTypeKeywordsColor;
break;
case "bool":
case "byte":
@ -289,6 +291,7 @@ namespace ICSharpCode.ILSpy @@ -289,6 +291,7 @@ namespace ICSharpCode.ILSpy
case "uint":
case "ushort":
case "ulong":
case "unmanaged":
color = valueTypeKeywordsColor;
break;
case "class":

Loading…
Cancel
Save