dotnet/roslyn

Reported conversion type

Open

#1 832 ouverte le 7 avr. 2015

Voir sur GitHub
 (0 commentaires) (0 réactions) (0 assignés)C# (4 257 forks)batch import
Area-CompilersConcept-APIInvestigation RequiredQuestionhelp wanted

Métriques du dépôt

Stars
 (20 414 stars)
Métriques de merge PR
 (Merge moyen 6j 17h) (256 PRs mergées en 30 j)

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

Guide contributeur