Skip to content

[clang] friend declaration does not apply during constraint validation #145505

Open
@yalavrinenko

Description

@yalavrinenko

I discovered that with the latest clang-21 the following code stop compiling:

#include <concepts>

template <typename T>
concept FooType = requires (T *v){
    {v->bar()} -> std::same_as<int>;
};

template <typename T>
struct Bar {
    Bar(T *owner): owner_{owner}{}

    bool func() {
        return owner()->bar() == 10;
    }

    auto owner() requires FooType<T> {
        return owner_;
    }

    T *owner_;
};

class Owner{
    public:
        auto& obj() { return obj_; }

    private:
        friend Bar<Owner>;

        int bar() { return 10; }

        Bar<Owner> obj_{this};
};

int main(){
    Owner o;
    return o.obj().func();
}

The compilation error is following:

<source>:14:16: error: invalid reference to function 'owner': constraints not satisfied
   14 |         return owner()->bar() == 10;
      |                ^
<source>:38:20: note: in instantiation of member function 'Bar<Owner>::func' requested here
   38 |     return o.obj().func();
      |                    ^
<source>:17:27: note: because 'Owner' does not satisfy 'FooType'
   17 |     auto owner() requires FooType<T> {
      |                           ^
<source>:6:9: note: because 'v->bar()' would be invalid: 'bar' is a private member of 'Owner'
    6 |     {v->bar()} -> std::same_as<int>;

Link to godbolt: https://godbolt.org/z/PasxrhaEa

Compiler info:

> clang --version
Ubuntu clang version 21.0.0 (++20250623082046+1c78d8d9d7bc-1~exp1~20250623082206.2496)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/lib/llvm-21/bin

The code above compiles with gcc as well as with clang-20 and early version of clang-21. Unfortunately I can not provide the last working revision of clang-21.

Please clarify whether this behavior is expected?

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"conceptsC++20 conceptsdiverges-from:gccDoes the clang frontend diverge from gcc on this issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions