Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Test and optimise TypeEqual and TypeIs behaviour for pointer types.
Browse files Browse the repository at this point in the history
Contributes to #8081, but since the current behaviour is to safely
always return false, continue to allow pointer type operands.

There is already code that examines these expressions for always-false
or always-true behaviour, so have pointer types also result in the
always-false path being taken.
  • Loading branch information
JonHanna committed Feb 18, 2017
1 parent 07eea8d commit 7f887e9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private static AnalyzeTypeIsResult AnalyzeTypeIs(Expression operand, Type testTy
return testType == typeof(void) ? AnalyzeTypeIsResult.KnownTrue : AnalyzeTypeIsResult.KnownFalse;
}

if (testType == typeof(void))
if (testType == typeof(void) || testType.IsPointer)
{
return AnalyzeTypeIsResult.KnownFalse;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ internal Expression ReduceTypeEqual()
{
Type cType = Expression.Type;

if (cType.GetTypeInfo().IsValueType)
if (cType.GetTypeInfo().IsValueType || TypeOperand.IsPointer)
{
if (cType.IsNullableType())
{
Expand Down
11 changes: 11 additions & 0 deletions src/System.Linq.Expressions/tests/TypeBinary/TypeEqual.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ public void TypeByRef()
Assert.Throws<ArgumentException>("type", () => Expression.TypeEqual(exp, byRef));
}

[Theory, ClassData(typeof(CompilationTypes))]
public void TypePointer(bool useInterpreter)
{
Expression exp = Expression.Constant(0);
Type pointer = typeof(int*);
var test = Expression.TypeEqual(exp, pointer);
var lambda = Expression.Lambda<Func<bool>>(test);
var func = lambda.Compile(useInterpreter);
Assert.False(func());
}

[Fact]
public void UnreadableExpression()
{
Expand Down
11 changes: 11 additions & 0 deletions src/System.Linq.Expressions/tests/TypeBinary/TypeIs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ public void TypeByRef()
Assert.Throws<ArgumentException>("type", () => Expression.TypeIs(exp, byRef));
}

[Theory, ClassData(typeof(CompilationTypes))]
public void TypePointer(bool useInterpreter)
{
Expression exp = Expression.Constant(0);
Type pointer = typeof(int*);
var test = Expression.TypeIs(exp, pointer);
var lambda = Expression.Lambda<Func<bool>>(test);
var func = lambda.Compile(useInterpreter);
Assert.False(func());
}

[Fact]
public void UnreadableExpression()
{
Expand Down

0 comments on commit 7f887e9

Please sign in to comment.