dotnet/roslyn

SemanticModel.GetTypeInfo(decl) doesn't work for an erroneous declaration expression in global code

Open

#17,377 opened on Feb 24, 2017

View on GitHub
 (1 comment) (0 reactions) (0 assignees)C# (4,257 forks)batch import
Area-CompilersBughelp wanted

Repository metrics

Stars
 (20,414 stars)
PR merge metrics
 (Avg merge 6d 17h) (256 merged PRs in 30d)

Description

The following test (in OutVarTests.cs) does not pass because GetTypeInfo(decl) returns null for the (erroneous) declaration of x1.

        [Fact(Skip = "https://github.com/dotnet/roslyn/issues/17377")]
        public void GlobalCode_InferenceFailure_07()
        {
            string source =
@"
H.M((var x1, int x2));
H.M(x1);

class H
{
    void M(object a) {}
}
";
            var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.ReleaseExe.WithScriptClassName("Script"), parseOptions: TestOptions.Script);

            compilation.GetDeclarationDiagnostics().Verify(
                // (2,10): error CS7019: Type of 'x1' cannot be inferred since its initializer directly or indirectly refers to the definition.
                // H.M((var x1, int x2));
                Diagnostic(ErrorCode.ERR_RecursivelyTypedVariable, "x1").WithArguments("x1").WithLocation(2, 10)
                );

            var tree = compilation.SyntaxTrees.Single();
            var model = compilation.GetSemanticModel(tree);

            var x1Decl = tree.GetRoot().DescendantNodes().OfType<DeclarationExpressionSyntax>()
                    .Where(p => p.Identifier().ValueText == "x1").Single();
            var x1Ref = GetReferences(tree, "x1").ToArray();
            Assert.Equal(1, x1Ref.Length);
            VerifyModelForOutField(model, x1Decl, x1Ref);
            var x1 = (FieldSymbol)model.GetDeclaredSymbol(x1Decl.VariableDesignation());
            Assert.Equal("var", x1.Type.ToTestDisplayString());
            Assert.True(x1.Type.IsErrorType());
        }

Contributor guide