Roslyn还出现这么低级的错误,不应该呀!

前几天对Dora.Interception作了简单的重构,想提供C#脚本来定义Interception Policy,毫无疑问微软提供的编译平台Roslyn使C#脚本化提供了支持。但是没有想到随便尝试了一个简单的功能就出现了问题,我个人觉得这应该是Roslyn的Bug。但是Roslyn经历了这么多次版本的迭代还出现如此低级的错误,实在有点说不过去。

闲话少说,我们现在通过几行简单的代码来重现这个Bug。我们创建了一个.NET Core 2.1的控制台应用,并添加针对NuGet包”Microsoft.CodeAnalysis.CSharp.Scripting“(版本2.8.2)的依赖。如下所示的是.csproj的完整定义。

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<AssemblyName>App</AssemblyName>
<RootNamespace>App</RootNamespace>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<LangVersion>7.3</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="2.8.2" />
</ItemGroup>
</Project>

接下来我们编写了一段简单的程序。如下面的代码片段所示,我们只是采用脚本的形式创建了一个Foo对象并利用它创建出Bar对象,然后调用后者的Print方法而已。

namespace Scripting
{
public class Foo { }
public class Bar
{
public Foo Foo { get; }
public Bar(Foo foo) => Foo = foo;
public void Print() => Console.WriteLine(Foo);
} class Program
{
static async Task Main(string[] args)
{
var options = ScriptOptions.Default
.AddReferences(typeof(Program).Assembly)
.AddImports("Scripting"); //Success
await CSharpScript
.Create("var bar = new Bar(new Foo());", options)
.ContinueWith("bar.Print()")
.RunAsync();
}
}
}

这段代码是没有问题的,运行之后Print方法输出的结果可以正常出现在入下所示的控制台上。

Roslyn还出现这么低级的错误,不应该呀!

接下来我们将“var bar = new Bar(new Foo());”这行代码按照如下的方式拆分成两行执行。

class Program
{
static async Task Main(string[] args)
{
//Fail
await CSharpScript
.Create("var foo = new Foo();", options)
.ContinueWith("var bar = new Bar(foo);")
.ContinueWith("bar.Print()")
.RunAsync();
}
}

在此运行该程序后出现如下所示的“Microsoft.CodeAnalysis.Scripting.CompilationErrorException”异常,并提示“cannot convert from 'Scripting.Foo [App, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]' to 'Scripting.Foo [c:\users\jinnan\Source\Repos\App\App\bin\Debug\netcoreapp2.1\App.dll]”。

Roslyn还出现这么低级的错误,不应该呀!

如下所示的是完整的错误信息:

Unhandled Exception: Microsoft.CodeAnalysis.Scripting.CompilationErrorException: (1,19): error CS1503: Argument 1: cannot convert from 'Scripting.Foo [App, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]' to 'Scripting.Foo [c:\users\jinnan\Source\Repos\App\App\bin\Debug\netcoreapp2.1\App.dll]'
at Microsoft.CodeAnalysis.Scripting.ScriptBuilder.ThrowIfAnyCompilationErrors(DiagnosticBag diagnostics, DiagnosticFormatter formatter) in /_/src/Scripting/Core/ScriptBuilder.cs:line 104
at Microsoft.CodeAnalysis.Scripting.ScriptBuilder.CreateExecutor[T](ScriptCompiler compiler, Compilation compilation, Boolean emitDebugInformation, CancellationToken cancellationToken) in /_/src/Scripting/Core/ScriptBuilder.cs:line 89
at Microsoft.CodeAnalysis.Scripting.Script`1.GetExecutor(CancellationToken cancellationToken) in /_/src/Scripting/Core/Script.cs:line 359
at Microsoft.CodeAnalysis.Scripting.Script`1.CommonGetExecutor(CancellationToken cancellationToken) in /_/src/Scripting/Core/Script.cs:line 343
at Microsoft.CodeAnalysis.Scripting.Script`1.TryGetPrecedingExecutors(Script lastExecutedScriptInChainOpt, CancellationToken cancellationToken) in /_/src/Scripting/Core/Script.cs:line 407
at Microsoft.CodeAnalysis.Scripting.Script`1.GetPrecedingExecutors(CancellationToken cancellationToken) in /_/src/Scripting/Core/Script.cs:line 370
at Microsoft.CodeAnalysis.Scripting.Script`1.RunAsync(Object globals, Func`2 catchException, CancellationToken cancellationToken) in /_/src/Scripting/Core/Script.cs:line 459
at Scripting.Program.Main(String[] args) in c:\users\jinnan\Source\Repos\App\App\Program.cs:line 31
at Scripting.Program.<Main>(String[] args)

这是我提交的issue链接:https://github.com/dotnet/roslyn/issues/28803#issuecomment-407894956

上一篇:《阿里巴巴Android编码规范》阅读纪要(二)


下一篇:POJ2115 - C Looooops(扩展欧几里得)