dotnet/roslyn

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

Open

#17,377 建立於 2017年2月24日

在 GitHub 查看
 (1 留言) (0 反應) (0 負責人)C# (4,257 fork)batch import
Area-CompilersBughelp wanted

倉庫指標

Star
 (20,414 star)
PR 合併指標
 (平均合併 6天 17小時) (30 天內合併 256 個 PR)

描述

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

貢獻者指南