Skip to content
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

False positive warning warning: instantiation of variable 'declvar<A>' required here, but no definition is available, when the definition is not required #128657

Open
HolyBlackCat opened this issue Feb 25, 2025 · 3 comments
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer false-positive Warning fires when it should not

Comments

@HolyBlackCat
Copy link

HolyBlackCat commented Feb 25, 2025

Consider the following code: https://gcc.godbolt.org/z/acd5W9YMn

struct A {explicit constexpr operator bool() const {return true;}};
struct B {int x = 1; explicit constexpr operator bool() const {return x;}};

template <typename T>
extern T declvar;

template <auto T>
constexpr int dummy = 0;

template <typename T>
concept C = requires{dummy<declvar<T> ? true : false>;};

static_assert(C<A>);
static_assert(!C<B>);

This compiles correctly on all 3 bug compilers including Clang (and seems to be the only good way of doing this).

But Clang emits a useless warning:

<source>:11:28: warning: instantiation of variable 'declvar<A>' required here, but no definition is available [-Wundefined-var-template]
   11 | concept C = requires{dummy<declvar<T> ? true : false>;};
      |                            ^
<source>:5:10: note: forward declaration of template entity is here
    5 | extern T declvar;
      |          ^
<source>:11:28: note: add an explicit instantiation declaration to suppress this warning if 'declvar<A>' is explicitly instantiated in another translation unit
   11 | concept C = requires{dummy<declvar<T> ? true : false>;};
      |                            ^

The variable definition isn't needed here, so it shouldn't warn.

@frederick-vs-ja
Copy link
Contributor

I think declvar<A> & declvar<B> are still odr-used per [basic.def.odr]/7, even though the odr-uses are constant-evaluation-only.

@frederick-vs-ja frederick-vs-ja added clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer and removed new issue labels Feb 25, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 25, 2025

@llvm/issue-subscribers-clang-frontend

Author: Egor (HolyBlackCat)

Consider the following code: https://gcc.godbolt.org/z/acd5W9YMn ```cpp struct A {explicit constexpr operator bool() const {return true;}}; struct B {int x = 1; explicit constexpr operator bool() const {return x;}};

template <typename T>
extern T declvar;

template <auto T>
constexpr int dummy = 0;

template <typename T>
concept C = requires{dummy<declvar<T> ? true : false>;};

static_assert(C<A>);
static_assert(!C<B>);

This compiles correctly on all 3 bug compilers including Clang (and seems to be the only good way of doing this).

But Clang emits a useless warning:
```none
&lt;source&gt;:11:28: warning: instantiation of variable 'declvar&lt;A&gt;' required here, but no definition is available [-Wundefined-var-template]
   11 | concept C = requires{dummy&lt;declvar&lt;T&gt; ? true : false&gt;;};
      |                            ^
&lt;source&gt;:5:10: note: forward declaration of template entity is here
    5 | extern T declvar;
      |          ^
&lt;source&gt;:11:28: note: add an explicit instantiation declaration to suppress this warning if 'declvar&lt;A&gt;' is explicitly instantiated in another translation unit
   11 | concept C = requires{dummy&lt;declvar&lt;T&gt; ? true : false&gt;;};
      |                            ^

The variable definition isn't needed here, so it shouldn't warn.

@HolyBlackCat
Copy link
Author

I think declvar<A> & declvar<B> are still odr-used per [basic.def.odr]/7, even though the odr-uses are constant-evaluation-only.

Even if they technically are, I think Clang shouldn't warn if there's no linker error.

@EugeneZelenko EugeneZelenko added false-positive Warning fires when it should not and removed clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Feb 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer false-positive Warning fires when it should not
Projects
None yet
Development

No branches or pull requests

4 participants