llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Conflicting declaration error with `__using_if_exists__` in libc++

Open petrhosek opened this issue 2 years ago • 10 comments

When trying to use libc++ with LLVM libc on a baremetal platform, we encountered an issue with the __using_if_exists__ attribute which was introduced in https://reviews.llvm.org/D90188:

In file included from /llvm_libcxx/include/algorithm:1973:                                                                                                                                                                                                                                                                                                                             
/llvm_libcxx/include/__algorithm/remove.h:28:1: error: declaration conflicts with target of using declaration already in scope                                                                                                                                                                                                                                                         
   28 | remove(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value) {                                                                                                                                                                                                                                                                                                                                                   
      | ^                                                                                                                                                                                                                                                                                                                                                                                                                                 
/llvm_libcxx/include/cstdio:155:1: note: target of using declaration                                                                                                                                                                                                                                                                                                                   
  155 | using ::remove _LIBCPP_USING_IF_EXISTS;                                                                                                                                                                                                                                                                                                                                                                                           
      | ^                                                                                                                                                                                                                                                                                                                                                                                                                                 
/llvm_libcxx/include/cstdio:155:9: note: using declaration                                                                                                                                                                                                                                                                                                                             
  155 | using ::remove _LIBCPP_USING_IF_EXISTS;                                                                                                                                                                                                                                                                                                                                                                                           
      |         ^                                 

This is because baremetal version of the stdio.h libc header doesn't include remove (since there's no filesystem support).

A more minimal example of this issue is:

void foo();

namespace N {
  void bar();

  using ::foo __attribute__((__using_if_exists__));
  using ::bar __attribute__((__using_if_exists__));
}

void baz() {
  N::bar();
}

This results in the following error (you can also see it at https://godbolt.org/z/dTxqnc1oP):

<source>:7:11: error: target of using declaration conflicts with declaration already in scope
    7 |   using ::bar __attribute__((__using_if_exists__));
      |           ^
<source>:7:3: note: target of using declaration
    7 |   using ::bar __attribute__((__using_if_exists__));
      |   ^
<source>:4:8: note: conflicting declaration
    4 |   void bar();
      |        ^
1 error generated.
Compiler returned: 1

Normally, remove is declared in stdio.h, which corresponds to the foo case.

In our case, there's no remove symbol in stdio.h and as such there's no such symbol in the global namespace, but there's remove in the std:: namespace introduced by algorithm corresponding to the bar case.

Is this an issue with the __using_if_exists__ implementation or intended behavior?

This is related to issue https://github.com/llvm/llvm-project/issues/84879.

CC @ldionne @zygoloid @AaronBallman @epilk

petrhosek avatar Mar 14 '24 23:03 petrhosek

Thanks for filing this issue! I've come across this several times on various platforms and I believe this is a Clang bug in the way it implements the attribute. Basically the intent is that if there is no ::bar declaration, the using ::bar __attribute__((using_if_exists)); declaration shouldn't declare anything. But the way this is implemented in Clang IIRC is that we actually do create a special kind of declaration representing "no declaration at all". And then that conflicts with the existing declaration for N::bar.

ldionne avatar Mar 27 '24 16:03 ldionne

Hi!

This issue may be a good introductory issue for people new to working on LLVM. If you would like to work on this issue, your first steps are:

  1. Check that no other contributor is working on this issue. If someone is assigned to the issue or claimed to be working on it, ping the person. After one week without a response, the assignee may be changed.
  2. Leave a comment indicating that you are working on the issue, or just create a pull request after following the steps below. Mention this issue in the description of the pull request.
  3. Fix the issue locally.
  4. Run the test suite locally. Remember that the subdirectories under test/ create fine-grained testing targets, so you can e.g. use make check-clang-ast to only run Clang's AST tests.
  5. Create a Git commit.
  6. Run git clang-format HEAD~1 to format your changes.
  7. Open a pull request to the upstream repository on GitHub. Detailed instructions can be found in GitHub's documentation. Mention this issue in the description of the pull request.

If you have any further questions about this issue, don't hesitate to ask via a comment in the thread below.

llvmbot avatar Jun 25 '25 22:06 llvmbot

@llvm/issue-subscribers-good-first-issue

Author: Petr Hosek (petrhosek)

When trying to use libc++ with LLVM libc on a baremetal platform, we encountered an issue with the `__using_if_exists__` attribute which was introduced in https://reviews.llvm.org/D90188:
In file included from /llvm_libcxx/include/algorithm:1973:                                                                                                                                                                                                                                                                                                                             
/llvm_libcxx/include/__algorithm/remove.h:28:1: error: declaration conflicts with target of using declaration already in scope                                                                                                                                                                                                                                                         
   28 | remove(_ForwardIterator __first, _ForwardIterator __last, const _Tp&amp; __value) {                                                                                                                                                                                                                                                                                                                                                   
      | ^                                                                                                                                                                                                                                                                                                                                                                                                                                 
/llvm_libcxx/include/cstdio:155:1: note: target of using declaration                                                                                                                                                                                                                                                                                                                   
  155 | using ::remove _LIBCPP_USING_IF_EXISTS;                                                                                                                                                                                                                                                                                                                                                                                           
      | ^                                                                                                                                                                                                                                                                                                                                                                                                                                 
/llvm_libcxx/include/cstdio:155:9: note: using declaration                                                                                                                                                                                                                                                                                                                             
  155 | using ::remove _LIBCPP_USING_IF_EXISTS;                                                                                                                                                                                                                                                                                                                                                                                           
      |         ^                                 

This is because baremetal version of the stdio.h libc header doesn't include remove (since there's no filesystem support).

A more minimal example of this issue is:

void foo();

namespace N {
  void bar();

  using ::foo __attribute__((__using_if_exists__));
  using ::bar __attribute__((__using_if_exists__));
}

void baz() {
  N::bar();
}

This results in the following error (you can also see it at https://godbolt.org/z/dTxqnc1oP):

&lt;source&gt;:7:11: error: target of using declaration conflicts with declaration already in scope
    7 |   using ::bar __attribute__((__using_if_exists__));
      |           ^
&lt;source&gt;:7:3: note: target of using declaration
    7 |   using ::bar __attribute__((__using_if_exists__));
      |   ^
&lt;source&gt;:4:8: note: conflicting declaration
    4 |   void bar();
      |        ^
1 error generated.
Compiler returned: 1

Normally, remove is declared in stdio.h, which corresponds to the foo case.

In our case, there's no remove symbol in stdio.h and as such there's no such symbol in the global namespace, but there's remove in the std:: namespace introduced by algorithm corresponding to the bar case.

Is this an issue with the __using_if_exists__ implementation or intended behavior?

This is related to issue https://github.com/llvm/llvm-project/issues/84879.

CC @ldionne @zygoloid @AaronBallman @epilk

llvmbot avatar Jun 25 '25 22:06 llvmbot

I see what looks like another manifestation of it, but here a previous using-if-exists declaration seem to conflict with the regular declaration (that's when building libc++ against llvm-libc headers), which seems to be the inverse example to the one @petrhosek mentioned:

In file included from src/libcxxabi/src/cxa_demangle.cpp:17:
In file included from src/libcxxabi/src/demangle/ItaniumDemangle.h:20:
In file included from src/libcxxabi/src/demangle/StringViewExtras.h:20:
In file included from src/libcxx/include/string_view:235:
In file included from src/libcxx/include/__string/char_traits.h:29:
src/libcxx/include/cstdio:120:9: error: target of using declaration conflicts with declaration already in scope
  120 | using ::FILE _LIBCPP_USING_IF_EXISTS;
      |         ^
libc/include/llvm-libc-macros/../llvm-libc-types/FILE.h:12:21: note: target of using declaration
   12 | typedef struct FILE FILE;
      |                     ^
src/libcxx/include/cwchar:137:1: note: conflicting declaration
  137 | using ::FILE _LIBCPP_USING_IF_EXISTS;
      | ^
1 error generated.

vonosmas avatar Oct 21 '25 23:10 vonosmas

Note that this, and similar error messages should typically be fixed by actually providing the real declaration that is needed by using-decl with __using_if_exists__ in libc++. On a regular linux-x86_64 I haven't yet encountered an issue which is not fixed by adding a proper decl, but the error messages surely make it confusing.

vonosmas avatar Oct 22 '25 18:10 vonosmas

Hello developers, I want to contribute in resolving this issue

jimmy2683 avatar Nov 01 '25 17:11 jimmy2683

Note that this, and similar error messages should typically be fixed by actually providing the real declaration that is needed by using-decl with using_if_exists in libc++. On a regular linux-x86_64 I haven't yet encountered an issue which is not fixed by adding a proper decl, but the error messages surely make it confusing.

I'm assuming you're talking about patches like #165795? That might be the fix in some cases, but for the case that Petr mentioned, they can't add a declaration for remove because the functionality isn't implemented.

boomanaiden154 avatar Nov 02 '25 22:11 boomanaiden154

Note that this, and similar error messages should typically be fixed by actually providing the real declaration that is needed by using-decl with using_if_exists in libc++. On a regular linux-x86_64 I haven't yet encountered an issue which is not fixed by adding a proper decl, but the error messages surely make it confusing.

I'm assuming you're talking about patches like #165795? That might be the fix in some cases, but for the case that Petr mentioned, they can't add a declaration for remove because the functionality isn't implemented.

Correct, that's also why I also mention linux-x86 systems, which generally should have all the functionality libc++ "optionally" depends on with using_if_exists.

vonosmas avatar Nov 03 '25 16:11 vonosmas

Hi, I would love to work on this issue

shivam-dhir avatar Jan 22 '26 06:01 shivam-dhir

We believe this issue should be addressed by https://github.com/llvm/llvm-project/pull/167646, we're just waiting for reviews from the Clang maintainers.

petrhosek avatar Jan 22 '26 06:01 petrhosek