Skip to content

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

@DanTup

Description

@DanTup

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

Metadata

Metadata

Assignees

Labels

P2A bug or feature request we're likely to work onlegacy-area-analyzerUse area-devexp instead.type-questionA question about expected behavior or functionality

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions