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

IntelliSense loses the type of Action<T> parameter only while typing #72571

Closed
datvm opened this issue Mar 16, 2024 · 3 comments · Fixed by #75695
Closed

IntelliSense loses the type of Action<T> parameter only while typing #72571

datvm opened this issue Mar 16, 2024 · 3 comments · Fixed by #75695
Assignees
Labels
Area-IDE Bug IDE-IntelliSense Completion, Signature Help, Quick Info IntelliSense-Completion Resolution-Fixed The bug has been fixed and/or the requested behavior has been implemented
Milestone

Comments

@datvm
Copy link

datvm commented Mar 16, 2024

Version Used:

Steps to Reproduce:

  1. Create a new C# Console App (top-level statement) and put in this code:
AddConfig(new()
{
    A = a =>
    {

    },
});

static void AddConfig(Config config) { }

class Config
{
    public Action<A>? A { get; set; }
}

class A
{
    public string Foo1 { get; set; } = "";
}
  1. Try to use a, like typing a.Foo1 = "something". IntelliSense cannot shows any member of a and the type of a is ?

image

  1. Without accessing to a, it correctly detects a as A. The moment a is access/typed however, it becomes ? again.

image

  1. Manually typing a complete statement would result in correct type assertment again, however, pressing Enter and type another statement with a and the problem happens again.

image

The behavior is similar to #68027 so I wonder if it's the same issue. This only affects IntelliSense, the code can be compiled successfully if it's typed manully.

I have also just realized it only happen to the automatic new() statement. Explicitly type new Config() on the first line would fix this issue.

Diagnostic Id: None

Expected Behavior:

a's type should be kept consistent while typing.

Actual Behavior:

a loses its type while typing.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels Mar 16, 2024
@sharwell sharwell added IDE-IntelliSense Completion, Signature Help, Quick Info IntelliSense-Completion Bug help wanted The issue is "up for grabs" - add a comment if you are interested in working on it and removed untriaged Issues and PRs which have not yet been triaged by a lead labels Apr 5, 2024
@sharwell sharwell added this to the Backlog milestone Apr 5, 2024
@sharwell
Copy link
Member

sharwell commented Apr 5, 2024

/cc @genlu @jcouv

@jcouv
Copy link
Member

jcouv commented Apr 5, 2024

The root cause is likely a compiler issue (ability to recover in the presence of an error in a lambda). I'm not sure whether that root cause can be addressed or whether it will need to be worked around from the IDE side.

    [Fact]
    public void TODO2()
    {
        var src = """
#nullable enable
using System;

AddConfig(new()
    {
        A = a =>
        {
            a
        },
    });

static void AddConfig(Config config) { }

class Config
{
    public Action<A>? A { get; set; }
}

class A
{
    public string Goo1 { get; set; } = "";
}
""";

        var comp = CreateCompilation(src, targetFramework: TargetFramework.Net70);
        comp.VerifyDiagnostics(
            // (8,14): error CS1002: ; expected
            //             a
            Diagnostic(ErrorCode.ERR_SemicolonExpected, "").WithLocation(8, 14));

        var tree = comp.SyntaxTrees.Single();
        var model = comp.GetSemanticModel(tree);
        var a = GetSyntax<IdentifierNameSyntax>(tree, "a");
        Assert.Equal("? a", model.GetSymbolInfo(a).Symbol.ToTestDisplayString()); // Should be "A a" parameter
    }

@CyrusNajmabadi
Copy link
Member

This def feels like an odd case for the compiler to not be able to get good semantics with (esp. as there are no overloads goign on).

Given:

AddConfig(new()
    {
        A = a =>
        {
            a
        },
    });

I would expect new() ... to be target typed with Config. Which means 'A' will be Action<A>. So a => will be target typed with Action<A>, So a will be a parameter with type A. So a. should properly give Goo. :)

Given the simplicity of the scenario, i would really want compiler to give reasonable results here :)

Thanks!

@github-project-automation github-project-automation bot moved this to InQueue in Small Fixes Oct 22, 2024
@jcouv jcouv added the 4 - In Review A fix for the issue is submitted for review. label Oct 31, 2024
@jcouv jcouv self-assigned this Oct 31, 2024
@jcouv jcouv modified the milestones: Backlog, 17.13 Oct 31, 2024
@jcouv jcouv removed the help wanted The issue is "up for grabs" - add a comment if you are interested in working on it label Oct 31, 2024
@github-project-automation github-project-automation bot moved this from InQueue to Completed in Small Fixes Nov 7, 2024
@jcouv jcouv added Resolution-Fixed The bug has been fixed and/or the requested behavior has been implemented and removed 4 - In Review A fix for the issue is submitted for review. labels Nov 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE Bug IDE-IntelliSense Completion, Signature Help, Quick Info IntelliSense-Completion Resolution-Fixed The bug has been fixed and/or the requested behavior has been implemented
Projects
Status: Completed
Development

Successfully merging a pull request may close this issue.

5 participants