dotnet/roslyn

Reported conversion type

Open

#1832 aperta il 7 apr 2015

Vedi su GitHub
 (0 commenti) (0 reazioni) (0 assegnatari)C# (4257 fork)batch import
Area-CompilersConcept-APIInvestigation RequiredQuestionhelp wanted

Metriche repository

Star
 (20.414 star)
Metriche merge PR
 (Merge medio 6g 17h) (256 PR mergiate in 30 g)

Descrizione

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

Guida contributor