Skip to content

Include parameter names in function types/method tear-offs in hovers, code completion, etc. #63792

Closed

Description

@DanTup

This was originally filed at Dart-Code/Dart-Code#6081 by @dzylikecode.

Currently, parameter names are not captured in function types, which leads to information in hovers and completions for a function type/tear-off being less useful that the original. For example:

class A {
  void myFunc1(int x, int y) {}
}

void f(A a) {
  var tearoff = a.myFunc1; // Hover over `tearoff` and `myFunc` here
}

The hovers look like this:

Image Image

The hover (and code completion) on the tear-off don't have the parameter names, which can be useful information when invoking the tear-off.

I don't know how large a change this would be or what the impact would be in capturing this, but here feels like the right place for this discussion rather than Dart-Code.

Activity

  1. added
    area-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.
    on Jul 9, 2026
  2. DanTup commented on Jul 9, 2026

    @DanTup
    CollaboratorAuthor

    I just noticed that when I try to invoke tearoff here, I do actually see the parameter names:

    Image

    I suspect this may be related to @bwilkerson's comment here:

    Some function types do point to a declaration from which they were derived, but using that information when it's available would lead to an inconsistent experience, so it isn't clearly the right solution.

    But I'm now wondering in what cases this wouldn't be enough. I tried some other cases (for ex. typedef FooFunc = void Function(int x, int y);) and the signature help also has the parameter names in that case.

    So if we just updated hover+completion to do whatever Signature Help is doing, would that be enough? In what cases would we lose them (where you might reasonable expect not to)?

  3. bwilkerson commented on Jul 9, 2026

    @bwilkerson
    Member

    So if we just updated hover+completion to do whatever Signature Help is doing, would that be enough?

    Yes, probably so. I didn't realize that we had found a way to get the information we need.

    In what cases would we lose them (where you might reasonable expect not to)?

    The cases I know of where this information might not be preserved are when the analyzer needs to create a function type that doesn't come from a single declaration. I think that can happen in cases where LUB is applied, such as the ?: operator, or in cases where two different function typed members are inherited from two different supertypes. My understanding is that these synthesized function types don't have all of the information of the two original types.

    @scheglov can confirm that and provide examples of code that would generate synthesized function types.

  4. changed the title [-]Preserve parameter names in function types/method tear-offs[/-] [+]Include parameter names in function types/method tear-offs in hovers, code completion, etc.[/+] on Jul 10, 2026
  5. DanTup commented on Jul 10, 2026

    @DanTup
    CollaboratorAuthor

    I took a quick look at this, and I think the easiest (and most consistent) way to do this would be to remove this if check here in ElementDisplayStringBuilder so that we always write a parameters name (keeping the inner if to ensure they have one):

    if (forElement || element.isNamed) {
    if (element.name case var name?) {
    _write(' $name');
    }
    }

    This would handle everywhere that is creating a display string for a function type. However, I don't know if there are drawbacks to this or if there are places we don't want this (I feel like names are always useful if they exist, but I don't know if we synthesize them anywhere).

    Otherwise, we could add a flag to pass in (like we have multiline: true), something like alwaysIncludeParameterNames that we just pass in places like hover/completion/etc.., though if we do this we should probably define a rule for where we do/don't want this.

  6. bwilkerson commented on Jul 10, 2026

    @bwilkerson
    Member

    However, I don't know if there are drawbacks to this or if there are places we don't want this (I feel like names are always useful if they exist, but I don't know if we synthesize them anywhere).

    I don't know why this condition was added, so I also don't know what the impact of this change would be. I suspect, though, that this is the utility we use to build diagnostic messages, and I don't think we want to include position parameter names in problem messages.

    As far as I know, we only synthesize names for synthetic elements (like the index field in an enum) or when there are errors in the code (in which case it's always an empty string).

    Adding a parameter is safe, but I'd rather only do that if it's necessary.

  7. scheglov commented on Jul 13, 2026

    @scheglov
    Contributor

    Strictly speaking, function types don't have names for positional formal parameters. The fact that they sometimes do it now, is accidental. My long term goal is to make FunctionType pure type - stop using FormalParameterElement, and use FunctionTypeFormalParameter instead - positional or named, probably as subtypes.

  8. bwilkerson commented on Jul 13, 2026

    @bwilkerson
    Member

    Maybe I've lost too much context, but it sounds to me like removing the names would degrade the UX. If that's true, then given that one of the important responsibilities of the analyzer is to provide the information needed for a good UX, dropping the names seems suboptimal.

  9. DanTup commented on Jul 14, 2026

    @DanTup
    CollaboratorAuthor

    I suspect, though, that this is the utility we use to build diagnostic messages, and I don't think we want to include position parameter names in problem messages.

    You're right, it is:

    + // [diag.invalidOverride][context 1] 'D.f' ('S Function<T, S>(T x)') isn't a valid override of 'C.f' ('T Function<T>(T x)').
    - // [diag.invalidOverride][context 1] 'D.f' ('S Function<T, S>(T)') isn't a valid override of 'C.f' ('T Function<T>(T)').
    

    So I think we probably would need to pass some kind of flag to ElementDisplayStringBuilder to indicate it should include parameter names for positional parameter to do this.

    I'll wait for @scheglov's response before doing anything here, though I do also think removing the names from the places where we have them would make a worse experience. I understand they're not part of the type and that we should be careful not to make them seem like they are (for example I agree they shouldn't be in diagnostic messages), they are useful information (moreso if the source is a package you didn't write and might not be as familiar with).

  10. scheglov commented on Jul 14, 2026

    @scheglov
    Contributor

    We almost always know exact names, e.g. when we invoke a constructor, or a method - in these cases we have ExecutableElement, which we can ask for its signature. But when you use tear-off, there is no element anymore, this is just a FunctionType. I don't agree with the other package argument, tear-offs are (always?) local, and the signature is one mouse movement away.

  11. DanTup commented on Jul 14, 2026

    @DanTup
    CollaboratorAuthor

    I don't agree with the other package argument, tear-offs are (always?) local, and the signature is one mouse movement away.

    In the original request there was this example (as top-level code):

    const InitWindow = proxy.InitWindow;
    

    The variable InitWindow could be used anywhere in the file (or even in other files), but does not have the parameter names when you hover over it. To get to the signature from a call to InitWindow, you'd need to navigate to the definition, and then hover over proxy.InitWindow, and then go back.

    But I think there are other cases, where you might provide parameter names in a function type for convenience:

    class X {
      final void Function(int x, int y) myFunc;
    
      // lots of other code
    
      void f() {
        myFunc(); // What are the parameters here?
      }
    }

    We have the parameter names available here at the call to myFunc() (and we do currently show them in the signature help), but again, you'd need to jump to the definition to see the paramter names if we don't do this (and if we remove them entirely, we'd also lose them in signature help).

  12. scheglov commented on Jul 14, 2026

    @scheglov
    Contributor

    You are right, it can be non-local.

    In principle my view is not so much against the positional formal parameter names, as much against using FormalParameterElements (and TypeParameterElements) in FunctionType. Keeping names is fairly easy.

    We still might get something weird and not universal.

    void point(int x, int y) {}
    void dimensions(int width, int height) {}
    
    final callback = condition ? point : dimensions;

    What are names in callback?

    And theoretically, we could de-duplicate FunctionType instances more often if we did not have to have distinct positional formal parameter names.

  13. bwilkerson commented on Jul 14, 2026

    @bwilkerson
    Member

    What are names in callback?

    You're right, there isn't any good name to use in a case like that. But I'd guess that it's fairly uncommon to have a situation where we have a computed type like this. Certainly rare enough that it's better to occasionally not have names than to never have names.

  14. DanTup commented on Jul 15, 2026

    @DanTup
    CollaboratorAuthor

    In principle my view is not so much against the positional formal parameter names, as much against using FormalParameterElements (and TypeParameterElements) in FunctionType. Keeping names is fairly easy.

    If we can keep the names, great - so it sounds like we could go ahead with this change, and if this refactor occurs in future, the names could all be preserved. I don't think implementing this will add any additional work, since the code to output the names is already there, we just need a flag to use it.

    What are names in callback?

    I think it's completely understandable for names to not be available in that case. A slightly weirder case is this one - but we're already doing this and it's also what TypeScript does. I think it's understandable (and it's very unlikely anyone is doing something like this):

    Image
  15. self-assigned this
    on Jul 15, 2026
  16. added
    P3A lower priority bug or feature request
    on Jul 22, 2026
  17. DanTup commented on Aug 25, 2026

    @DanTup
    CollaboratorAuthor

    I tidied up my change and uploaded it to https://dart-review.googlesource.com/c/sdk/+/541380

    It adds the parameter names to the element description part of the hover (but not the "Type" part) and also the details part of code completion. I think this is useful, but should avoid any confusion of whether the names are part of the actual type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P3A lower priority bug or feature requestarea-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions