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

displayString2() loses type substitutions for constructor invocation with type arguments #56933

Open
DanTup opened this issue Oct 21, 2024 · 1 comment
Assignees
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P2 A bug or feature request we're likely to work on type-question A question about expected behavior or functionality

Comments

@DanTup
Copy link
Collaborator

DanTup commented Oct 21, 2024

I hit this issue trying to migrate some code to the new Element model, but I couldn't figure out what the fix was...

The code I'm migrating is testing the hover on some code like new A<String>(). Calling getDisplayString() returns "A<String> A()" but calling displayString2() returns "A<T> A()":

image

Stepping through getDisplayString(), it accesses ExecutableMember.type which calls _substitution.substituteType before returning the type.

However when stepping through displayString2() it accesses _element2 which accesses declaration.asElement2. I don't know if it's this use of declaration that is losing the type substitutions?

Here's a small repro test:

  @soloTest
  Future<void> test_danny() async {
    await resolveTestCode('''
class A<T> {}
void f() {
  new A<String>();
}
''');

    var element =
        findNode.instanceCreation('A<String>').constructorName.staticElement!;

    expect(
      (element as Element2).displayString2(),
      element.getDisplayString(),
    );
  }

Which fails with:

00:00 +0 -2: SearchEngineImplWithNonFunctionTypeAliasesTest | test_danny [E]

  Expected: 'A<String> A()'
    Actual: 'A<T> A()'
     Which: is different.
            Expected: A<String> A( ...
              Actual: A<T> A()
                        ^
             Differ at offset 2

@bwilkerson

@DanTup DanTup added the area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. label Oct 21, 2024
@bwilkerson
Copy link
Member

Given that you have a ConstructorMember, you're probably running the implementation of displayString2 from ExecutableMember:

  String displayString2(
      {bool multiline = false, bool preferTypeAlias = false}) {
    return _element2.displayString2(
        multiline: multiline, preferTypeAlias: preferTypeAlias);
  }

My guess is that the use of _element2 is where the type information is discarded.

That method probably needs to be rewritten to create an ElementDisplayStringBuilder and pass it to appendTo.

@bwilkerson bwilkerson added the type-question A question about expected behavior or functionality label Oct 21, 2024
@DanTup DanTup self-assigned this Oct 21, 2024
@keertip keertip added the P2 A bug or feature request we're likely to work on label Oct 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P2 A bug or feature request we're likely to work on type-question A question about expected behavior or functionality
Projects
None yet
Development

No branches or pull requests

3 participants