dotnet/roslyn

Reported conversion type

Open

#1,832 opened on Apr 7, 2015

View on GitHub
 (0 comments) (0 reactions) (0 assignees)C# (4,257 forks)batch import
Area-CompilersConcept-APIInvestigation RequiredQuestionhelp wanted

Repository metrics

Stars
 (20,414 stars)
PR merge metrics
 (Avg merge 6d 17h) (256 merged PRs in 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);
        }
    }
}

Contributor guide