Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SemanticModel is unable to infer type of a 'var' pattern variable #12996

Closed
AlekseyTs opened this issue Aug 8, 2016 · 1 comment
Closed

SemanticModel is unable to infer type of a 'var' pattern variable #12996

AlekseyTs opened this issue Aug 8, 2016 · 1 comment
Assignees
Labels
Area-Compilers Language-C# New Language Feature - Pattern Matching Pattern Matching Resolution-Fixed The bug has been fixed and/or the requested behavior has been implemented

Comments

@AlekseyTs
Copy link
Contributor

        [Fact]
        public void FailureToInferTypeOfAVarPatternVariable()
        {
            var source =
@"
class Program
{
    public static void Main(string[] args)
    {
    }

    public static void Test(int val)
    {
        if (val is var o1) 
        {
            System.Console.WriteLine(o1);
        }
    }
}
";
            var compilation = CreateCompilationWithMscorlib45(source, options: TestOptions.DebugExe);
            compilation.VerifyDiagnostics(
                );
            var tree = compilation.SyntaxTrees[0];

            var model1 = compilation.GetSemanticModel(tree);

            var declaration = tree.GetRoot().DescendantNodes().OfType<IsPatternExpressionSyntax>().Single();
            var o1 = tree.GetRoot().DescendantNodes().OfType<IdentifierNameSyntax>().Where(id => id.Identifier.ValueText == "o1").Single();

            var typeInfo1 = model1.GetTypeInfo(declaration);
            Assert.Equal(SymbolKind.NamedType, typeInfo1.Type.Kind);
            Assert.Equal("System.Boolean", typeInfo1.Type.ToTestDisplayString());

            typeInfo1 = model1.GetTypeInfo(o1);
            Assert.Equal(SymbolKind.NamedType, typeInfo1.Type.Kind);
            Assert.Equal("System.Int32", typeInfo1.Type.ToTestDisplayString());


            var model2 = compilation.GetSemanticModel(tree);

            var typeInfo2 = model2.GetTypeInfo(o1);
            Assert.Equal(SymbolKind.NamedType, typeInfo2.Type.Kind);
            Assert.Equal("System.Int32", typeInfo2.Type.ToTestDisplayString());
        }

The following assert

            Assert.Equal(SymbolKind.NamedType, typeInfo2.Type.Kind);

fails

Test 'Microsoft.CodeAnalysis.CSharp.UnitTests.PatternMatchingTests.FailureToInferTypeOfAVarPatternVariable' failed: Assert.Equal() Failure
Expected: NamedType
Actual:   ErrorType
@gafter
Copy link
Member

gafter commented Aug 18, 2016

The relevant (missing) code is this:

        protected virtual TypeSymbol InferTypeOfVarVariable(DiagnosticBag diagnostics)
        {
            // TODO: this method must be overridden for pattern variables to bind the
            // expression or statement that is the nearest enclosing to the pattern variable's
            // declaration. That will cause the type of the pattern variable to be set as a side-effect.
            return _type;
        }

@gafter gafter changed the title SemanticModel is unable to infer type of a 'var' declaration variable SemanticModel is unable to infer type of a 'var' pattern variable Aug 18, 2016
@gafter gafter modified the milestones: 2.0 (RC), 2.0 (Preview 5) Aug 19, 2016
gafter added a commit to gafter/roslyn that referenced this issue Aug 19, 2016
…tern variables.

Also eliminates some cases of cascaded definite assignment errors.
Fixes dotnet#13219
Fixes dotnet#13009
Fixes dotnet#12996
Fixes dotnet#10446
@gafter gafter added 4 - In Review A fix for the issue is submitted for review. and removed 0 - Backlog labels Aug 22, 2016
@markwilkie markwilkie removed 4 - In Review A fix for the issue is submitted for review. Bug labels Aug 30, 2016
@gafter gafter added the Resolution-Fixed The bug has been fixed and/or the requested behavior has been implemented label Aug 30, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Language-C# New Language Feature - Pattern Matching Pattern Matching Resolution-Fixed The bug has been fixed and/or the requested behavior has been implemented
Projects
None yet
Development

No branches or pull requests

3 participants