You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Include parameter names in function types/method tear-offs in hovers, code completion, etc. #63792
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:
classA {
voidmyFunc1(int x, int y) {}
}
voidf(A a) {
var tearoff = a.myFunc1; // Hover over `tearoff` and `myFunc` here
}
The hovers look like this:
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.
I just noticed that when I try to invoke tearoff here, I do actually see the parameter names:
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)?
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.
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
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):
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.
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.
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.
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.
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).
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.
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:
classX {
finalvoidFunction(int x, int y) myFunc;
// lots of other codevoidf() {
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).
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.
voidpoint(int x, int y) {}
voiddimensions(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.
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.
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):
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.
Activity
DanTup commentedon Jul 9, 2026
I just noticed that when I try to invoke
tearoffhere, I do actually see the parameter names:I suspect this may be related to @bwilkerson's comment here:
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)?
bwilkerson commentedon Jul 9, 2026
Yes, probably so. I didn't realize that we had found a way to get the information we need.
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.
[-]Preserve parameter names in function types/method tear-offs[/-][+]Include parameter names in function types/method tear-offs in hovers, code completion, etc.[/+]DanTup commentedon Jul 10, 2026
I took a quick look at this, and I think the easiest (and most consistent) way to do this would be to remove this
ifcheck here inElementDisplayStringBuilderso that we always write a parameters name (keeping the innerifto ensure they have one):sdk/pkg/analyzer/lib/src/dart/element/display_string_builder.dart
Lines 537 to 541 in 82471c3
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 likealwaysIncludeParameterNamesthat 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.bwilkerson commentedon Jul 10, 2026
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
indexfield 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.
scheglov commentedon Jul 13, 2026
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
FunctionTypepure type - stop usingFormalParameterElement, and useFunctionTypeFormalParameterinstead - positional or named, probably as subtypes.bwilkerson commentedon Jul 13, 2026
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.
DanTup commentedon Jul 14, 2026
You're right, it is:
So I think we probably would need to pass some kind of flag to
ElementDisplayStringBuilderto 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).
scheglov commentedon Jul 14, 2026
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 aFunctionType. I don't agree with the other package argument, tear-offs are (always?) local, and the signature is one mouse movement away.DanTup commentedon Jul 14, 2026
In the original request there was this example (as top-level code):
The variable
InitWindowcould 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 toInitWindow, you'd need to navigate to the definition, and then hover overproxy.InitWindow, and then go back.But I think there are other cases, where you might provide parameter names in a function type for convenience:
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).scheglov commentedon Jul 14, 2026
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 (andTypeParameterElements) inFunctionType. Keeping names is fairly easy.We still might get something weird and not universal.
What are names in
callback?And theoretically, we could de-duplicate
FunctionTypeinstances more often if we did not have to have distinct positional formal parameter names.bwilkerson commentedon Jul 14, 2026
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.
DanTup commentedon Jul 15, 2026
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.
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):
DanTup commentedon Aug 25, 2026
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.
[analyzer][analysis_server] Include all parameter names in hovers and…