Skip to content

[Clang] enhance diagnostic by attaching source location to deduced type in trailing return without auto - #115786

Merged
Sirraide merged 7 commits into
llvm:mainfrom
a-tarasyuk:fix/78694
Nov 19, 2024
Merged

[Clang] enhance diagnostic by attaching source location to deduced type in trailing return without auto#115786
Sirraide merged 7 commits into
llvm:mainfrom
a-tarasyuk:fix/78694

Conversation

@a-tarasyuk

Copy link
Copy Markdown
Member

Fixes #78694

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Nov 11, 2024
@llvmbot

llvmbot commented Nov 11, 2024

Copy link
Copy Markdown
Member

@llvm/pr-subscribers-clang

Author: Oleksandr T. (a-tarasyuk)

Changes

Fixes #78694


Full diff: https://github.com/llvm/llvm-project/pull/115786.diff

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+1)
  • (modified) clang/lib/Sema/SemaType.cpp (+12-3)
  • (modified) clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p2-cxx0x.cpp (+12-1)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index e235a04f78112b..f4cd7f99cdce14 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -652,6 +652,7 @@ Bug Fixes to C++ Support
   an implicitly instantiated class template specialization. (#GH51051)
 - Fixed an assertion failure caused by invalid enum forward declarations. (#GH112208)
 - Name independent data members were not correctly initialized from default member initializers. (#GH114069)
+- Clang now uses deduced type locations in trailing return without auto diagnostics. (#GH78694)
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index 515b9f689a248a..c50d15a3adc442 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -4887,9 +4887,18 @@ static TypeSourceInfo *GetFullTypeForDeclarator(TypeProcessingState &state,
                       cast<AutoType>(T)->getKeyword() !=
                           AutoTypeKeyword::Auto ||
                       cast<AutoType>(T)->isConstrained())) {
-            S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
-                   diag::err_trailing_return_without_auto)
-                << T << D.getDeclSpec().getSourceRange();
+            SourceLocation Loc = D.getDeclSpec().getTypeSpecTypeLoc();
+            SourceRange SR = D.getDeclSpec().getSourceRange();
+            if (Loc.isInvalid()) {
+              TypeSourceInfo *TSI = nullptr;
+              S.GetTypeFromParser(FTI.getTrailingReturnType(), &TSI);
+              if (TSI) {
+                TypeLoc TSILoc = TSI->getTypeLoc();
+                Loc = TSILoc.getBeginLoc();
+                SR = TSILoc.getSourceRange();
+              }
+            }
+            S.Diag(Loc, diag::err_trailing_return_without_auto) << T << SR;
             D.setInvalidType(true);
             // FIXME: recover and fill decls in `TypeLoc`s.
             AreDeclaratorChunksValid = false;
diff --git a/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p2-cxx0x.cpp b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p2-cxx0x.cpp
index ce90728861605f..e43eb8e48c7272 100644
--- a/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p2-cxx0x.cpp
+++ b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p2-cxx0x.cpp
@@ -1,7 +1,18 @@
-// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s 
+// RUN: %clang_cc1 -fsyntax-only -std=c++11 -verify %s
+// RUN: not %clang_cc1 -fsyntax-only -std=c++11 -fno-diagnostics-show-line-numbers -fcaret-diagnostics-max-lines=1 %s 2>&1 | FileCheck %s -strict-whitespace
 
 auto a() -> int; // ok
 const auto b() -> int; // expected-error {{function with trailing return type must specify return type 'auto', not 'const auto'}}
 auto *c() -> int; // expected-error {{function with trailing return type must specify return type 'auto', not 'auto *'}}
 auto (d() -> int); // expected-error {{trailing return type may not be nested within parentheses}}
 auto e() -> auto (*)() -> auto (*)() -> void; // ok: same as void (*(*e())())();
+
+namespace GH78694 {
+
+template <typename T> struct B {
+  // CHECK:      error: function with trailing return type must specify return type 'auto', not 'void'
+  // CHECK-NEXT: {{^}}  template <class U> B(U) -> B<int>;
+  // CHECK-NEXT: {{^}}                             ^~~~~~{{$}}
+  template <class U> B(U) -> B<int>; // expected-error {{function with trailing return type must specify return type 'auto', not 'void'}}
+};
+}

Comment thread clang/lib/Sema/SemaType.cpp Outdated
Comment thread clang/lib/Sema/SemaType.cpp
Comment thread clang/lib/Sema/SemaType.cpp
Comment thread clang/docs/ReleaseNotes.rst Outdated
@github-actions

github-actions Bot commented Nov 12, 2024

Copy link
Copy Markdown

✅ With the latest revision this PR passed the C/C++ code formatter.

@a-tarasyuk
a-tarasyuk requested a review from Fznamznon November 12, 2024 12:13
@a-tarasyuk
a-tarasyuk requested a review from Sirraide November 12, 2024 16:21

@AaronBallman AaronBallman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment thread clang/lib/Sema/SemaType.cpp
Comment thread clang/lib/Sema/SemaType.cpp Outdated
@a-tarasyuk
a-tarasyuk requested a review from Fznamznon November 12, 2024 18:29

@Fznamznon Fznamznon left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This looks great

@Sirraide
Sirraide merged commit 43f84e7 into llvm:main Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

No valid source location attached to trailing_return_without_auto diagnostic for some cases

5 participants