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

GetTypeInfo returns incorrectly null-annotated types for deconstruction #68572

Open
DoctorKrolic opened this issue Jun 13, 2023 · 0 comments
Open
Assignees
Milestone

Comments

@DoctorKrolic
Copy link
Contributor

Version Used:
4.7.0-1.final

Steps to Reproduce:

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

var code = """
    #nullable enable

    class C
    {
        public void Deconstruct(out string? s1, out string? s2)
        {
            s1 = null;
            s2 = null;
        }

        void M(C[] items)
        {
            var item = items[0];
            var (s1, s2) = item;

            foreach (var (s3, s4) in items)
            {

            }
        }
    }
    """;

var syntaxTree = CSharpSyntaxTree.ParseText(code);

var compilation = CSharpCompilation.Create("Test", new[] { syntaxTree }, new[] { MetadataReference.CreateFromFile(typeof(object).Assembly.Location) });
var semanticModel = compilation.GetSemanticModel(syntaxTree);

var root = syntaxTree.GetRoot();

var assignmentExpression = root.DescendantNodes().OfType<AssignmentExpressionSyntax>().ElementAt(2);
var typeSyntax = ((DeclarationExpressionSyntax)assignmentExpression.Left).Type;
var typeInfo = semanticModel.GetTypeInfo(typeSyntax);

Console.WriteLine(typeInfo.Type);

var foreachStatement = root.DescendantNodes().OfType<ForEachVariableStatementSyntax>().Single();
typeSyntax = ((DeclarationExpressionSyntax)foreachStatement.Variable).Type;
typeInfo = semanticModel.GetTypeInfo(typeSyntax);

Console.WriteLine(typeInfo.Type);

Expected Output:

(string? s1, string? s2)
(string? s3, string? s4)

Actual Output:

(string s1, string s2)
(string s3, string s4)

This is the root cause of #47038. Fixing it should automatically fix #47038 as well.

This is also observable in IDE. If you hover over any s variable, the quick info will show correct string? type, but if you hover over var, it will show tuple without nullability annotations:
devenv_AHFuU8PZZT

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Jun 13, 2023
@jcouv jcouv self-assigned this Jun 20, 2023
@jcouv jcouv added Bug New Language Feature - Nullable Semantic Model Nullable Semantic Model Issues and removed untriaged Issues and PRs which have not yet been triaged by a lead labels Jun 20, 2023
@jcouv jcouv added this to the 17.7 milestone Jun 20, 2023
@jaredpar jaredpar modified the milestones: 17.7, 17.9 Jul 19, 2023
@jaredpar jaredpar modified the milestones: 17.9, Backlog Nov 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants