Browse Source

TypeInference: add support for nullability annotated types

pull/1730/head
Daniel Grunwald 6 years ago
parent
commit
5d337aa655
  1. 30
      ICSharpCode.Decompiler/CSharp/Resolver/TypeInference.cs
  2. 3
      ICSharpCode.Decompiler/TypeSystem/Implementation/NullabilityAnnotatedType.cs
  3. 3
      ICSharpCode.Decompiler/TypeSystem/TupleType.cs
  4. 5
      ICSharpCode.Decompiler/TypeSystem/TypeSystemExtensions.cs

30
ICSharpCode.Decompiler/CSharp/Resolver/TypeInference.cs

@ -395,11 +395,10 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver @@ -395,11 +395,10 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver
static IMethod GetDelegateOrExpressionTreeSignature(IType t)
{
ParameterizedType pt = t as ParameterizedType;
if (pt != null && pt.TypeParameterCount == 1 && pt.Name == "Expression"
&& pt.Namespace == "System.Linq.Expressions")
if (t.TypeParameterCount == 1 && t.Name == "Expression"
&& t.Namespace == "System.Linq.Expressions")
{
t = pt.GetTypeArgument(0);
t = t.TypeArguments[0];
}
return t.GetDelegateInvokeMethod();
}
@ -571,8 +570,9 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver @@ -571,8 +570,9 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver
{
Log.WriteLine("MakeExactInference from " + U + " to " + V);
if (V is NullabilityAnnotatedTypeParameter nullableTP) {
V = nullableTP.OriginalTypeParameter;
if (U.Nullability == V.Nullability) {
U = U.WithoutNullability();
V = V.WithoutNullability();
}
// If V is one of the unfixed Xi then U is added to the set of bounds for Xi.
@ -613,8 +613,10 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver @@ -613,8 +613,10 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver
TP GetTPForType(IType v)
{
ITypeParameter p = v as ITypeParameter;
if (p != null) {
if (v is NullabilityAnnotatedTypeParameter natp) {
v = natp.OriginalTypeParameter;
}
if (v is ITypeParameter p) {
int index = p.Index;
if (index < typeParameters.Length && typeParameters[index].TypeParameter == p)
return typeParameters[index];
@ -631,7 +633,11 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver @@ -631,7 +633,11 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver
void MakeLowerBoundInference(IType U, IType V)
{
Log.WriteLine(" MakeLowerBoundInference from " + U + " to " + V);
if (U.Nullability == V.Nullability) {
U = U.WithoutNullability();
V = V.WithoutNullability();
}
// If V is one of the unfixed Xi then U is added to the set of bounds for Xi.
TP tp = GetTPForType(V);
if (tp != null && tp.IsFixed == false) {
@ -728,7 +734,11 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver @@ -728,7 +734,11 @@ namespace ICSharpCode.Decompiler.CSharp.Resolver
void MakeUpperBoundInference(IType U, IType V)
{
Log.WriteLine(" MakeUpperBoundInference from " + U + " to " + V);
if (U.Nullability == V.Nullability) {
U = U.WithoutNullability();
V = V.WithoutNullability();
}
// If V is one of the unfixed Xi then U is added to the set of bounds for Xi.
TP tp = GetTPForType(V);
if (tp != null && tp.IsFixed == false) {

3
ICSharpCode.Decompiler/TypeSystem/Implementation/NullabilityAnnotatedType.cs

@ -14,7 +14,8 @@ namespace ICSharpCode.Decompiler.TypeSystem.Implementation @@ -14,7 +14,8 @@ namespace ICSharpCode.Decompiler.TypeSystem.Implementation
internal NullabilityAnnotatedType(IType type, Nullability nullability)
: base(type)
{
Debug.Assert(nullability != type.Nullability);
Debug.Assert(type.Nullability == Nullability.Oblivious);
Debug.Assert(nullability != Nullability.Oblivious);
// Due to IType -> concrete type casts all over the type system, we can insert
// the NullabilityAnnotatedType wrapper only in some limited places.
Debug.Assert(type is ITypeDefinition

3
ICSharpCode.Decompiler/TypeSystem/TupleType.cs

@ -373,7 +373,8 @@ namespace ICSharpCode.Decompiler.TypeSystem @@ -373,7 +373,8 @@ namespace ICSharpCode.Decompiler.TypeSystem
{
public static IType TupleUnderlyingTypeOrSelf(this IType type)
{
return (type as TupleType)?.UnderlyingType ?? type;
var t = (type as TupleType)?.UnderlyingType ?? type;
return t.WithoutNullability();
}
}
}

5
ICSharpCode.Decompiler/TypeSystem/TypeSystemExtensions.cs

@ -530,5 +530,10 @@ namespace ICSharpCode.Decompiler.TypeSystem @@ -530,5 +530,10 @@ namespace ICSharpCode.Decompiler.TypeSystem
{
return KnownAttributes.IsKnownAttributeType(type);
}
public static IType WithoutNullability(this IType type)
{
return type.ChangeNullability(Nullability.Oblivious);
}
}
}

Loading…
Cancel
Save