Skip to content

Commit 43f84e7

Browse files
authored
[Clang] Enhance diagnostic by attaching source location to deduced type in trailing return without auto (#115786)
Fixes #78694
1 parent 4bd982d commit 43f84e7

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,8 @@ Bug Fixes to C++ Support
666666
- Name independent data members were not correctly initialized from default member initializers. (#GH114069)
667667
- Fixed expression transformation for ``[[assume(...)]]``, allowing using pack indexing expressions within the
668668
assumption if they also occur inside of a dependent lambda. (#GH114787)
669+
- Clang now uses valid deduced type locations when diagnosing functions with trailing return type
670+
missing placeholder return type. (#GH78694)
669671

670672
Bug Fixes to AST Handling
671673
^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/SemaType.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4882,9 +4882,17 @@ static TypeSourceInfo *GetFullTypeForDeclarator(TypeProcessingState &state,
48824882
cast<AutoType>(T)->getKeyword() !=
48834883
AutoTypeKeyword::Auto ||
48844884
cast<AutoType>(T)->isConstrained())) {
4885-
S.Diag(D.getDeclSpec().getTypeSpecTypeLoc(),
4886-
diag::err_trailing_return_without_auto)
4887-
<< T << D.getDeclSpec().getSourceRange();
4885+
// Attach a valid source location for diagnostics on functions with
4886+
// trailing return types missing 'auto'. Attempt to get the location
4887+
// from the declared type; if invalid, fall back to the trailing
4888+
// return type's location.
4889+
SourceLocation Loc = D.getDeclSpec().getTypeSpecTypeLoc();
4890+
SourceRange SR = D.getDeclSpec().getSourceRange();
4891+
if (Loc.isInvalid()) {
4892+
Loc = FTI.getTrailingReturnTypeLoc();
4893+
SR = D.getSourceRange();
4894+
}
4895+
S.Diag(Loc, diag::err_trailing_return_without_auto) << T << SR;
48884896
D.setInvalidType(true);
48894897
// FIXME: recover and fill decls in `TypeLoc`s.
48904898
AreDeclaratorChunksValid = false;
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1-
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
1+
// RUN: %clang_cc1 -fsyntax-only -std=c++11 -verify %s
2+
// 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
23

34
auto a() -> int; // ok
45
const auto b() -> int; // expected-error {{function with trailing return type must specify return type 'auto', not 'const auto'}}
56
auto *c() -> int; // expected-error {{function with trailing return type must specify return type 'auto', not 'auto *'}}
67
auto (d() -> int); // expected-error {{trailing return type may not be nested within parentheses}}
78
auto e() -> auto (*)() -> auto (*)() -> void; // ok: same as void (*(*e())())();
9+
10+
namespace GH78694 {
11+
12+
template <typename T> struct B {
13+
// CHECK: error: function with trailing return type must specify return type 'auto', not 'void'
14+
// CHECK-NEXT: {{^}} template <class U> B(U) -> B<int>;
15+
// CHECK-NEXT: {{^}} ~~~~~~~~^~~~~~{{$}}
16+
template <class U> B(U) -> B<int>; // expected-error {{function with trailing return type must specify return type 'auto', not 'void'}}
17+
};
18+
}

0 commit comments

Comments
 (0)