Skip to content

Commit

Permalink
Accept PE Operator Symbols having in parameters (#23508)
Browse files Browse the repository at this point in the history
* Accept PE Operator Symbols having in parameters

* Address PR Comments
  • Loading branch information
OmarTawfik authored Dec 10, 2017
1 parent 919b606 commit 1688be0
Show file tree
Hide file tree
Showing 3 changed files with 362 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.Threading;
using System.Reflection;
using System.Reflection.Metadata;
using System.Threading;
using Microsoft.CodeAnalysis.CSharp.DocumentationComments;
using Microsoft.CodeAnalysis.CSharp.Emit;
using Microsoft.CodeAnalysis.PooledObjects;
using Roslyn.Utilities;
using Microsoft.CodeAnalysis.CSharp.Emit;

namespace Microsoft.CodeAnalysis.CSharp.Symbols.Metadata.PE
{
Expand Down Expand Up @@ -803,13 +803,35 @@ private bool IsValidExtensionMethodSignature()
}
}

private bool IsValidUserDefinedOperatorSignature(int parameterCount) =>
!this.ReturnsVoid &&
!this.IsGenericMethod &&
!this.IsVararg &&
this.ParameterCount == parameterCount &&
this.ParameterRefKinds.IsDefault && // No 'ref' or 'out'
!this.IsParams();
private bool IsValidUserDefinedOperatorSignature(int parameterCount)
{
if (this.ReturnsVoid || this.IsGenericMethod || this.IsVararg || this.ParameterCount != parameterCount || this.IsParams())
{
return false;
}

if (this.ParameterRefKinds.IsDefault)
{
return true;
}

foreach (var kind in this.ParameterRefKinds)
{
switch (kind)
{
case RefKind.None:
case RefKind.In:
continue;
case RefKind.Out:
case RefKind.Ref:
return false;
default:
throw ExceptionUtilities.UnexpectedValue(kind);
}
}

return true;
}

private MethodKind ComputeMethodKind()
{
Expand Down
158 changes: 153 additions & 5 deletions src/Compilers/CSharp/Test/Emit/CodeGen/CodeGenInParametersTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
// Copyright (c) Microsoft Open Technologies, Inc. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp.Test.Utilities;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
Expand Down Expand Up @@ -1918,5 +1913,158 @@ .maxstack 1
IL_002d: ret
}");
}

[Fact]
[WorkItem(530136, "https://devdiv.visualstudio.com/DevDiv/_workitems?id=530136")]
public void OperatorsWithInParametersFromMetadata_Binary()
{
var reference = CreateStandardCompilation(@"
public class Test
{
public int Value { get; set; }
public static int operator +(in Test a, in Test b)
{
return a.Value + b.Value;
}
}");

var code = @"
class Program
{
static void Main(string[] args)
{
var a = new Test { Value = 3 };
var b = new Test { Value = 6 };
System.Console.WriteLine(a + b);
}
}";

CompileAndVerify(code, additionalRefs: new[] { reference.ToMetadataReference() }, expectedOutput: "9");
CompileAndVerify(code, additionalRefs: new[] { reference.EmitToImageReference() }, expectedOutput: "9");
}

[Fact]
[WorkItem(530136, "https://devdiv.visualstudio.com/DevDiv/_workitems?id=530136")]
public void OperatorsWithInParametersFromMetadata_Binary_Right()
{
var reference = CreateStandardCompilation(@"
public class Test
{
public int Value { get; set; }
public static int operator +(Test a, in Test b)
{
return a.Value + b.Value;
}
}");

var code = @"
class Program
{
static void Main(string[] args)
{
var a = new Test { Value = 3 };
var b = new Test { Value = 6 };
System.Console.WriteLine(a + b);
}
}";

CompileAndVerify(code, additionalRefs: new[] { reference.ToMetadataReference() }, expectedOutput: "9");
CompileAndVerify(code, additionalRefs: new[] { reference.EmitToImageReference() }, expectedOutput: "9");
}

[Fact]
[WorkItem(530136, "https://devdiv.visualstudio.com/DevDiv/_workitems?id=530136")]
public void OperatorsWithInParametersFromMetadata_Binary_Left()
{
var reference = CreateStandardCompilation(@"
public class Test
{
public int Value { get; set; }
public static int operator +(in Test a, Test b)
{
return a.Value + b.Value;
}
}");

var code = @"
class Program
{
static void Main(string[] args)
{
var a = new Test { Value = 3 };
var b = new Test { Value = 6 };
System.Console.WriteLine(a + b);
}
}";

CompileAndVerify(code, additionalRefs: new[] { reference.ToMetadataReference() }, expectedOutput: "9");
CompileAndVerify(code, additionalRefs: new[] { reference.EmitToImageReference() }, expectedOutput: "9");
}

[Fact]
[WorkItem(530136, "https://devdiv.visualstudio.com/DevDiv/_workitems?id=530136")]
public void OperatorsWithInParametersFromMetadata_Unary()
{
var reference = CreateStandardCompilation(@"
public class Test
{
public bool Value { get; set; }
public static bool operator !(in Test a)
{
return !a.Value;
}
}");

var code = @"
class Program
{
static void Main(string[] args)
{
var a = new Test { Value = true };
System.Console.WriteLine(!a);
}
}";

CompileAndVerify(code, additionalRefs: new[] { reference.ToMetadataReference() }, expectedOutput: "False");
CompileAndVerify(code, additionalRefs: new[] { reference.EmitToImageReference() }, expectedOutput: "False");
}

[Fact]
[WorkItem(530136, "https://devdiv.visualstudio.com/DevDiv/_workitems?id=530136")]
public void OperatorsWithInParametersFromMetadata_Conversion()
{
var reference = CreateStandardCompilation(@"
public class Test
{
public bool Value { get; set; }
public static explicit operator int(in Test a)
{
return a.Value ? 3 : 5;
}
}");

var code = @"
class Program
{
static void Main(string[] args)
{
var a = new Test { Value = true };
System.Console.WriteLine((int)a);
}
}";

CompileAndVerify(code, additionalRefs: new[] { reference.ToMetadataReference() }, expectedOutput: "3");
CompileAndVerify(code, additionalRefs: new[] { reference.EmitToImageReference() }, expectedOutput: "3");
}
}
}
Loading

0 comments on commit 1688be0

Please sign in to comment.