Clang error: 'using_if_exists' attribute cannot be applied to an inheriting constructor (err_using_if_exists_on_ctor)

From emmtrix Wiki
Jump to navigation Jump to search
Text error: 'using_if_exists' attribute cannot be applied to an inheriting constructor (since 13.0)
Type Error
Category Semantic Issue (since 13.0)
Internal Id err_using_if_exists_on_ctor (since 13.0)
Internal Message 'using_if_exists' attribute cannot be applied to an inheriting constructor (since 13.0)
Regular Expression (?:error|fatal error)\: 'using_if_exists' attribute cannot be applied to an inheriting constructor
First Commit 2021-03-10 3dbcea8b957a Reland [clang] Check unsupported types in expressions

Description

The error is issued by the Clang compiler when the using_if_exists attribute is improperly applied to an inheriting constructor in a C++ program. In C++, constructors can be inherited from a base class to a derived class using a using-declaration. However, the using_if_exists attribute, which conditionally introduces a using-declaration only if a name exists in the base class, is not supported for inheriting constructors. This attribute is aimed at mitigating compilation errors when code is compiled conditionally with different versions of libraries or different build configurations, but its application is restricted to certain contexts only. The compiler generates this error to enforce the correct use of attributes in C++ code, specifically concerning inheriting constructors, ensuring that attributes are applied only where appropriate and semantically valid according to the C++ standard and Clang's extensions.  
AI Generated

Example

In the following example, a structure named D is derived from another structure named B. The example attempts to inherit the constructor from B into D using a using-declaration. The using-declaration is additionally annotated with the [[clang::using_if_exists]] attribute, which is intended to conditionally introduce a using-declaration if a name exists in the base class. However, applying the using_if_exists attribute to an inheriting constructor is invalid, which results in the Clang compiler generating an error message. This illustrates the compiler's enforcement of attribute usage rules and the correct application of attributes in the context of inheriting constructors.  
AI Generated


Flags -xc++

[Try out in Compiler Explorer]

Source
#include <utility>

struct B {};

struct D : B {
    // Invalid use of 'using_if_exists' on inheriting constructor
    using B::B [[clang::using_if_exists]];
};
Compiler Output
<source>:7:18: warning: ISO C++ does not allow an attribute list to appear here [-Wcxx-attribute-extension]
<source>:7:5: error: 'using_if_exists' attribute cannot be applied to an inheriting constructor


Clang Internals (17.0.6)

Git Commit Message

Reland [clang] Check unsupported types in expressions

This was committed as ec6c847179fd, but then reverted after a failure
in: https://lab.llvm.org/buildbot/#/builders/84/builds/13983

I was not able to reproduce the problem, but I added an extra check
for a NULL QualType just in case.

Original comit message:

The patch adds missing diagnostics for cases like:

  float F3 = ((__float128)F1 * (__float128)F2) / 2.0f;

Sema::checkDeviceDecl (renamed to checkTypeSupport) is changed to work
with a type without the corresponding ValueDecl. It is also refactored
so that host diagnostics for unsupported types can be added here as
well.

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

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 12624)

/// Builds a using declaration.
///
/// \param IsInstantiation - Whether this call arises from an
///   instantiation of an unresolved using declaration.  We treat
///   the lookup differently for these declarations.
NamedDecl *Sema::BuildUsingDeclaration(Scope *S, AccessSpecifier AS, SourceLocation UsingLoc, bool HasTypenameKeyword, SourceLocation TypenameLoc, CXXScopeSpec &SS, DeclarationNameInfo NameInfo, SourceLocation EllipsisLoc, const ParsedAttributesView &AttrList, bool IsInstantiation, bool IsUsingIfExists) {
  // ...
  // 'using_if_exists' doesn't make sense on an inherited constructor.
  if (IsUsingIfExists && UsingName.getName().getNameKind() == DeclarationName::CXXConstructorName) {
    Diag(UsingLoc, diag::err_using_if_exists_on_ctor);

Triggered in Clang Tests

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

clang/test/SemaCXX/using-if-exists.cpp

  • clang/test/SemaCXX/using-if-exists.cpp:66:3: error: 'using_if_exists' attribute cannot be applied to an inheriting constructor