dotnet/roslyn

Reported conversion type

Open

#1,832 opened on 2015年4月7日

GitHub で見る
 (0 comments) (0 reactions) (0 assignees)C# (20,414 stars) (4,257 forks)batch import
Area-CompilersConcept-APIInvestigation RequiredQuestionhelp wanted

説明

It is my understanding both conversions in the following program should be null-literal conversions, but that is not what is reported (using rc-1).

using System;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace RoslynTest {
    class Program {
        static void Main(string[] args) {

            var syntaxTree = CSharpSyntaxTree.ParseText(@"class C { public C() { object x = null; x = null; } }");

            var compilation = CSharpCompilation.Create("Test", new[] { syntaxTree }, new[] { MetadataReference.CreateFromAssembly(typeof(object).Assembly) }, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
            foreach (var d in compilation.GetDiagnostics().Where(d => d.Severity == DiagnosticSeverity.Error)) {
                Console.WriteLine("Invalid test, error in compilation: "+ d);
            }

            var st = compilation.SyntaxTrees.Single();
            var sm = compilation.GetSemanticModel(st);

            var declaration = st.GetRoot().DescendantNodes().OfType<VariableDeclarationSyntax>().Single().Variables[0];
            var declarationConversion = sm.GetConversion(declaration.Initializer);
            var expr = st.GetRoot().DescendantNodes().OfType<ExpressionStatementSyntax>().Single().Expression;
            var right = ((AssignmentExpressionSyntax)expr).Right;
            var expressionConversion = sm.GetConversion(right);

            if (declarationConversion.IsNullLiteral)
                Console.WriteLine("Declaration conversion is a null-literal conversion");
            else
                Console.WriteLine("ERROR: Declaration conversion is a " + declarationConversion);

            if (expressionConversion.IsNullLiteral)
                Console.WriteLine("Expression conversion is a null-literal conversion");
            else
                Console.WriteLine("ERROR: Expression conversion is a " + expressionConversion);
        }
    }
}

コントリビューターガイド