-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[clangd] Show type hint for simple cases of dependent 'auto' #156284
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
Merged
Merged
+247
−145
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
… getApproximateType() in HeuristicResolver After this change, HeuristicResolver should be able to do everything that SemaCodeComplete's getApproximateType() can do (and more).
… with calls to HeuristicResolver
67b81f5
to
6791b7a
Compare
@llvm/pr-subscribers-clang-tools-extra @llvm/pr-subscribers-clangd Author: Nathan Ridge (HighCommander4) ChangesFixes clangd/clangd#2275 Full diff: https://github.com/llvm/llvm-project/pull/156284.diff 2 Files Affected:
diff --git a/clang-tools-extra/clangd/InlayHints.cpp b/clang-tools-extra/clangd/InlayHints.cpp
index cd479e1b7c9bc..d56b93e5f36dc 100644
--- a/clang-tools-extra/clangd/InlayHints.cpp
+++ b/clang-tools-extra/clangd/InlayHints.cpp
@@ -633,13 +633,30 @@ class InlayHintVisitor : public RecursiveASTVisitor<InlayHintVisitor> {
}
if (auto *AT = D->getType()->getContainedAutoType()) {
- if (AT->isDeduced() && !D->getType()->isDependentType()) {
- // Our current approach is to place the hint on the variable
- // and accordingly print the full type
- // (e.g. for `const auto& x = 42`, print `const int&`).
- // Alternatively, we could place the hint on the `auto`
- // (and then just print the type deduced for the `auto`).
- addTypeHint(D->getLocation(), D->getType(), /*Prefix=*/": ");
+ if (AT->isDeduced()) {
+ QualType T;
+ // If the type is dependent, HeuristicResolver *may* be able to
+ // resolve it to something that's useful to print. In other
+ // cases, it can't, and the resultng type would just be printed
+ // as "<dependent type>", in which case don't hint it at all.
+ if (D->getType()->isDependentType()) {
+ if (D->hasInit()) {
+ QualType Resolved = Resolver->resolveExprToType(D->getInit());
+ if (Resolved != AST.DependentTy) {
+ T = Resolved;
+ }
+ }
+ } else {
+ T = D->getType();
+ }
+ if (!T.isNull()) {
+ // Our current approach is to place the hint on the variable
+ // and accordingly print the full type
+ // (e.g. for `const auto& x = 42`, print `const int&`).
+ // Alternatively, we could place the hint on the `auto`
+ // (and then just print the type deduced for the `auto`).
+ addTypeHint(D->getLocation(), T, /*Prefix=*/": ");
+ }
}
}
diff --git a/clang-tools-extra/clangd/unittests/InlayHintTests.cpp b/clang-tools-extra/clangd/unittests/InlayHintTests.cpp
index 99e728c40063d..feb4404b3d2bf 100644
--- a/clang-tools-extra/clangd/unittests/InlayHintTests.cpp
+++ b/clang-tools-extra/clangd/unittests/InlayHintTests.cpp
@@ -1441,7 +1441,8 @@ TEST(TypeHints, DependentType) {
void bar(T arg) {
auto [a, b] = arg;
}
- )cpp");
+ )cpp",
+ ExpectedHint{": T", "var2"});
}
TEST(TypeHints, LongTypeName) {
|
zyn0217
approved these changes
Sep 2, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks
Base automatically changed from
users/HighCommander4/issue-2431-review
to
main
September 16, 2025 19:21
kimsh02
pushed a commit
to kimsh02/llvm-project
that referenced
this pull request
Sep 19, 2025
itzexpoexpo
pushed a commit
to itzexpoexpo/llvm-project
that referenced
this pull request
Sep 21, 2025
SeongjaeP
pushed a commit
to SeongjaeP/llvm-project
that referenced
this pull request
Sep 23, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes clangd/clangd#2275