Clang error: redeclaration of deduction guide (err_deduction_guide_redeclared)

From emmtrix Wiki
Jump to navigation Jump to search
Text error: redeclaration of deduction guide (since 9.0)
Type Error
Category Semantic Issue (since 9.0)
Internal Id err_deduction_guide_redeclared (since 9.0)
Internal Message redeclaration of deduction guide (since 9.0)
Regular Expression (?:error|fatal error)\: redeclaration of deduction guide
First Commit 2019-05-04 5fe2ddbdf47d [clang] adding explicit(bool) from c++2a

Description

The error is issued by the Clang compiler when it encounters multiple deduction guide declarations for the same template within a single translation unit (source file) that possess identical signatures. In C++17 and later, deduction guides assist the compiler in determining the template arguments from constructor arguments when using class template argument deduction (CTAD). A deduction guide specifies how to deduce the template arguments for a class template from the types of constructor arguments. According to the C++ standard, each class template may have multiple deduction guides, but they must have unique parameter-declaration-clauses to guide the compiler correctly. Redefining a deduction guide with the same parameter types as a previous declaration in the same translation unit is not permitted, as it causes ambiguity in template argument deduction and contravenes the C++ standard rules. This violation prompts the issuance of the "error: redeclaration of deduction guide" to inform the developer of the redundant and conflicting declarations.  
AI Generated

Example

In the following example, a class template named S is defined to be initialized with a single argument of type T. To facilitate class template argument deduction (CTAD) in C++17 and later, a deduction guide is provided immediately after the class definition. A deduction guide is a special function template advising the compiler on deducing the class template parameters from the constructor arguments. Here, the goal is to allow the compiler to deduce the type T for the class template S directly from the type of the argument passed to the constructor. However, an error arises from declaring a second, identical deduction guide for the same class template with the same signature, breaching the rules for deduction guides in C++. According to the C++ standards, each deduction guide for a given class template must have a unique set of parameters. The compiler error message highlights this violation by indicating the redeclaration of an already defined deduction guide, specifying the location of both the redundant declaration and its original declaration within the source code.  
AI Generated


Flags -xc++

[Try out in Compiler Explorer]

Source
// Deduction guide redeclared

template <class T>
struct S {
    S(T);
};

template <class T>
S(T) -> S<T>;

template <class T>
S(T) -> S<T>; // Error Here
Compiler Output
<source>:12:1: error: redeclaration of deduction guide
<source>:9:1: note: previous declaration is here


Clang Internals (17.0.6)

Git Commit Message

[clang] adding explicit(bool) from c++2a

this patch adds support for the explicit bool specifier.

Changes:
- The parsing for the explicit(bool) specifier was added in ParseDecl.cpp.
- The storage of the explicit specifier was changed. the explicit specifier was stored as a boolean value in the FunctionDeclBitfields and in the DeclSpec class. now it is stored as a PointerIntPair<Expr*, 2> with a flag and a potential expression in CXXConstructorDecl, CXXDeductionGuideDecl, CXXConversionDecl and in the DeclSpec class.
- Following the AST change, Serialization, ASTMatchers, ASTComparator and ASTPrinter were adapted.
- Template instantiation was adapted to instantiate the potential expressions of the explicit(bool) specifier When instantiating their associated declaration.
- The Add*Candidate functions were adapted, they now take a Boolean indicating if the context allowing explicit constructor or conversion function and this boolean is used to remove invalid overloads that required template instantiation to be detected.
- Test for Semantic and Serialization were added.

This patch is not yet complete. I still need to check that interaction with CTAD and deduction guides is correct. and add more tests for AST operations. But I wanted first feedback.
Perhaps this patch should be spited in smaller patches, but making each patch testable as a standalone may be tricky.

Patch by Tyker

Differential Revision: https://reviews.llvm.org/D60934

llvm-svn: 359949

Used in Clang Sources

This section lists all occurrences of the diagnostic within the Clang's codebase. For each occurrence, an auto-extracted snipped from the source code is listed including key elements like control structures, functions, or classes. It should illustrate the conditions under which the diagnostic is activated.

clang/lib/Sema/SemaDeclCXX.cpp (line 697)

/// MergeCXXFunctionDecl - Merge two declarations of the same C++
/// function, once we already know that they have the same
/// type. Subroutine of MergeFunctionDecl. Returns true if there was an
/// error, false otherwise.
bool Sema::MergeCXXFunctionDecl(FunctionDecl *New, FunctionDecl *Old, Scope *S) {
  // ...
  // C++17 [temp.deduct.guide]p3:
  //   Two deduction guide declarations in the same translation unit
  //   for the same class template shall not have equivalent
  //   parameter-declaration-clauses.
  if (isa<CXXDeductionGuideDecl>(New) && !New->isFunctionTemplateSpecialization() && isVisible(Old)) {
    Diag(New->getLocation(), diag::err_deduction_guide_redeclared);

Triggered in Clang Tests

This section lists all internal Clang test cases that trigger the diagnostic.

clang/test/CXX/temp/temp.deduct.guide/p3.cpp

  • clang/test/CXX/temp/temp.deduct.guide/p3.cpp:9:1: error: redeclaration of deduction guide
  • clang/test/CXX/temp/temp.deduct.guide/p3.cpp:12:1: error: redeclaration of deduction guide