https://github.com/a-tarasyuk updated https://github.com/llvm/llvm-project/pull/115786
>From da1b3982e84114cb1214ca5c3d8ed520d1589b83 Mon Sep 17 00:00:00 2001 From: Oleksandr T <oleksandr.taras...@outlook.com> Date: Tue, 12 Nov 2024 00:59:37 +0200 Subject: [PATCH 1/3] [Clang] enhance diagnostic by attaching source location to deduced type in trailing return without auto --- clang/docs/ReleaseNotes.rst | 1 + clang/lib/Sema/SemaType.cpp | 15 ++++++++++++--- .../CXX/dcl.decl/dcl.meaning/dcl.fct/p2-cxx0x.cpp | 13 ++++++++++++- 3 files changed, 25 insertions(+), 4 deletions(-) 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'}} +}; +} >From 0c5001a6f9473411003abae86e1ea06ba6ba5988 Mon Sep 17 00:00:00 2001 From: Oleksandr T <oleksandr.taras...@outlook.com> Date: Tue, 12 Nov 2024 14:05:05 +0200 Subject: [PATCH 2/3] update release notes --- clang/docs/ReleaseNotes.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index f4cd7f99cdce14..4378ccbb398599 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -652,7 +652,8 @@ 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) +- Clang now uses valid deduced type locations when diagnosing functions with trailing return type + missing placeholder return type. (#GH78694) Bug Fixes to AST Handling ^^^^^^^^^^^^^^^^^^^^^^^^^ >From 54d225c557b6b6eb1a2cc0c3cf35e59dddcf3582 Mon Sep 17 00:00:00 2001 From: Oleksandr T <oleksandr.taras...@outlook.com> Date: Tue, 12 Nov 2024 14:06:25 +0200 Subject: [PATCH 3/3] eliminate redundant condition and add clarification comment --- clang/lib/Sema/SemaType.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp index c50d15a3adc442..fd4bd1208d3f99 100644 --- a/clang/lib/Sema/SemaType.cpp +++ b/clang/lib/Sema/SemaType.cpp @@ -4887,16 +4887,17 @@ static TypeSourceInfo *GetFullTypeForDeclarator(TypeProcessingState &state, cast<AutoType>(T)->getKeyword() != AutoTypeKeyword::Auto || cast<AutoType>(T)->isConstrained())) { + // 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.s 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(); - } + TypeLoc TSILoc = TSI->getTypeLoc(); + Loc = TSILoc.getBeginLoc(); + SR = TSILoc.getSourceRange(); } S.Diag(Loc, diag::err_trailing_return_without_auto) << T << SR; D.setInvalidType(true); _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits