Skip to content

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

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 7 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,8 @@ Bug Fixes to C++ Support
- Name independent data members were not correctly initialized from default member initializers. (#GH114069)
- Fixed expression transformation for ``[[assume(...)]]``, allowing using pack indexing expressions within the
assumption if they also occur inside of a dependent lambda. (#GH114787)
- Clang now uses valid deduced type locations when diagnosing functions with trailing return type
missing placeholder return type. (#GH78694)

Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
14 changes: 11 additions & 3 deletions clang/lib/Sema/SemaType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4887,9 +4887,17 @@ 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();
// Attach a valid source location for diagnostics on functions with
// trailing return types missing 'auto'. Attempt to get the location
// from the declared type; if invalid, fall back to the trailing
// return type's location.
SourceLocation Loc = D.getDeclSpec().getTypeSpecTypeLoc();
SourceRange SR = D.getDeclSpec().getSourceRange();
if (Loc.isInvalid()) {
Loc = FTI.getTrailingReturnTypeLoc();
SR = D.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;
Expand Down
13 changes: 12 additions & 1 deletion clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p2-cxx0x.cpp
Original file line number Diff line number Diff line change
@@ -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'}}
};
}
Loading