[clangd] Lambda-pattern completion for std::function target disappears with 2+ overloads
Summary
When completion is requested at a position whose expected type is a std::function<Sig>-based type alias, clangd offers a Pattern-kind
completion item with a matching lambda skeleton ([=](ParamType parameter, ...) { body }), but only if the target is reached through a single, unambiguous function/variable declaration. As soon as a second overload is introduced that also takes a std::function-based parameter, the lambda-pattern item disappears entirely from the completion list, even though the first overload's target type hasn't changed.
Minimal reproduction
Case 1: single overload (lambda pattern appears):
#include <functional>
typedef std::function<void(int, double)> func;
void execute(func f) { f(1, 1.); }
int main() {
execute(
}
Completing at execute(^) offers, among others:
[=](int parameter, double parameter) { body } (Snippet)
Case 2: same code, one overload added (lambda pattern disappears):
#include <functional>
typedef std::function<void(int, double)> func;
typedef std::function<void(int)> func_alternative;
void execute(func f) { f(1, 1.); }
void execute(func_alternative f) { f(1); }
int main() {
execute(
}
Completing at the same position now returns the regular symbol list (execute, main, func, func_alternative, std, keywords, ...) but no lambda-pattern snippet for either overload.
Expected behavior
Ideally, one lambda-pattern item per viable overload would be offered (similar to how signature help lists one entry per overload). At minimum, falling back to a pattern (e.g. for the first/best-ranked overload) would be preferable to offering none.
Additional notes
- The underlying pattern-generation itself works correctly for a single target type, including references and
const-qualifiers, once resolved (tested manually with e.g.std::function<double(const std::string&, float, int)>). - Confirmed identically in:
- clangd 18.1.3 (Ubuntu 24.04)
- clangd 23.x (openSUSE Tumbleweed)
- Both via Emacs (lsp-mode + company) and VS Code (llvm-vs-code-extensions.vscode-clangd, Copilot/IntelliCode disabled)
@llvm/issue-subscribers-clangd
Author: max-winkler
When completion is requested at a position whose expected type is a std::function<Sig>-based type alias, clangd offers a Pattern-kind
completion item with a matching lambda skeleton ([=](ParamType parameter, ...) { body }), but only if the target is reached through a single, unambiguous function/variable declaration. As soon as a second overload is introduced that also takes a std::function-based parameter, the lambda-pattern item disappears entirely from the completion list, even though the first overload's target type hasn't changed.
Minimal reproduction
Case 1: single overload (lambda pattern appears):
#include <functional>
typedef std::function<void(int, double)> func;
void execute(func f) { f(1, 1.); }
int main() {
execute(
}
Completing at execute(^) offers, among others:
[=](int parameter, double parameter) { body } (Snippet)
<img width="1275" height="691" alt="Image" src="https://github.com/user-attachments/assets/a5e86151-52cc-4239-8e7a-0e0df021d3be" />
Case 2: same code, one overload added (lambda pattern disappears):
#include <functional>
typedef std::function<void(int, double)> func;
typedef std::function<void(int)> func_alternative;
void execute(func f) { f(1, 1.); }
void execute(func_alternative f) { f(1); }
int main() {
execute(
}
Completing at the same position now returns the regular symbol list (execute, main, func, func_alternative, std, keywords, ...) but no lambda-pattern snippet for either overload.
Expected behavior
Ideally, one lambda-pattern item per viable overload would be offered (similar to how signature help lists one entry per overload). At minimum, falling back to a pattern (e.g. for the first/best-ranked overload) would be preferable to offering none.
Additional notes
- The underlying pattern-generation itself works correctly for a single target type, including references and
const-qualifiers, once resolved (tested manually with e.g.std::function<double(const std::string&, float, int)>). - Confirmed identically in:
- clangd 18.1.3 (Ubuntu 24.04)
- clangd 23.x (openSUSE Tumbleweed)
- Both via Emacs (lsp-mode + company) and VS Code (llvm-vs-code-extensions.vscode-clangd, Copilot/IntelliCode disabled)
I poked at this a bit to understand how the current feature works:
- The lambda-pattern completion is added in the function
AddLambdaCompletioncalled here - The call is conditioned on a piece of code-completion state called
PreferredTypebeing set, and it being a function type.- As I understand it, the interpretation of
PreferredTypeis basically "the code completion engine has identified that at this location, a value of this type is expected"
- As I understand it, the interpretation of
PreferredTypeis set in many different places in the code completion implementation corresponding to different contexts, but for function arguments I believe it's here.- The implementation of the logic to compute the preferred type in that context goes through several other helpers but ends up here, which takes an array of overload candidates and a parameter position, and computes an expected type for an argument in that position.
- Notably, the logic bails if two overloads have different parameter types in that position.
So, to implement the feature you're asking for, what would need to be done is roughly:
- Extend the code completion engine's
PreferredTypeabstraction to support the notion of multiple preferred types. - Extend places that populate
PreferredType(in particular the one linked above) to populate multiple types where appropriate. - Extend places that consume
PreferredTypeto handle multiple types where appropriate, such as inAddLambdaCompletion. Other places can bail if there is more than one type.
@llvm/issue-subscribers-clang-frontend
Author: max-winkler
When completion is requested at a position whose expected type is a std::function<Sig>-based type alias, clangd offers a Pattern-kind
completion item with a matching lambda skeleton ([=](ParamType parameter, ...) { body }), but only if the target is reached through a single, unambiguous function/variable declaration. As soon as a second overload is introduced that also takes a std::function-based parameter, the lambda-pattern item disappears entirely from the completion list, even though the first overload's target type hasn't changed.
Minimal reproduction
Case 1: single overload (lambda pattern appears):
#include <functional>
typedef std::function<void(int, double)> func;
void execute(func f) { f(1, 1.); }
int main() {
execute(
}
Completing at execute(^) offers, among others:
[=](int parameter, double parameter) { body } (Snippet)
<img width="1275" height="691" alt="Image" src="https://github.com/user-attachments/assets/a5e86151-52cc-4239-8e7a-0e0df021d3be" />
Case 2: same code, one overload added (lambda pattern disappears):
#include <functional>
typedef std::function<void(int, double)> func;
typedef std::function<void(int)> func_alternative;
void execute(func f) { f(1, 1.); }
void execute(func_alternative f) { f(1); }
int main() {
execute(
}
Completing at the same position now returns the regular symbol list (execute, main, func, func_alternative, std, keywords, ...) but no lambda-pattern snippet for either overload.
Expected behavior
Ideally, one lambda-pattern item per viable overload would be offered (similar to how signature help lists one entry per overload). At minimum, falling back to a pattern (e.g. for the first/best-ranked overload) would be preferable to offering none.
Additional notes
- The underlying pattern-generation itself works correctly for a single target type, including references and
const-qualifiers, once resolved (tested manually with e.g.std::function<double(const std::string&, float, int)>). - Confirmed identically in:
- clangd 18.1.3 (Ubuntu 24.04)
- clangd 23.x (openSUSE Tumbleweed)
- Both via Emacs (lsp-mode + company) and VS Code (llvm-vs-code-extensions.vscode-clangd, Copilot/IntelliCode disabled)
@HighCommander4 Thanks for the pointers. I traced this with a Debug build and can confirm the exact mechanism you described.
The relevant spot is getParamType in SemaCodeComplete.cpp here
if (!SemaRef.Context.hasSameUnqualifiedType(
ParamType.getNonReferenceType(),
CandidateParamType.getNonReferenceType()))
// Two conflicting types, give up.
return QualType();
With a single process(std::function<void(int, double)>) overload, this loop never reaches the conflicting-types branch, a non-null ParamType comes out and AddLambdaCompletion is called as expected.
With a second overload added (process(std::function<void(int)>)), the second candidate's param type at that position differs, this branch returns QualType(), and that empty PreferredType propagates all the way up through ProduceCallSignatureHelp -> PreferredTypeBuilder::get -> ParseAssignmentExpression, so AddLambdaCompletion is skipped.
So this confirms it's exactly the deliberate "give up on conflict" bail-out you pointed to, not something else going wrong further down.