dotnet/roslyn

Reported conversion type

Open

#1.832 aberto em 7 de abr. de 2015

Ver no GitHub
 (0 comments) (0 reactions) (0 assignees)C# (4.257 forks)batch import
Area-CompilersConcept-APIInvestigation RequiredQuestionhelp wanted

Métricas do repositório

Stars
 (20.414 stars)
Métricas de merge de PR
 (Mesclagem média 6d 17h) (256 fundiu PRs em 30d)

Description

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);
        }
    }
}

Guia do colaborador