Skip to content

[concepts] improve diagnostics for range concept failures #51260

@ldalessa

Description

@ldalessa
Bugzilla Link 51918
Version trunk
OS All
CC @zygoloid

Extended Description

Clang produces some concepts diagnostics that are of limited use to the programmer, particularly with respect to the ranges library.

In this case, I was playing around with proxy iterators and ran into https://godbolt.org/z/e9M9T3z5j, which tells me that my iterator type fails the borrowed_iterator_t requirement, however it gives me absolutely no information about why.

The borrowed_iterator_t is not a trivial concept, when I google for it I get https://en.cppreference.com/w/cpp/ranges/borrowed_iterator_t which is split into two cases as:

1) `std::ranges::iterator_t<R>` if `R` models `borrowed_range`, `std::ranges::dangling` otherwise.
2) `std::ranges::subrange<std::ranges::iterator_t<R>>` if `R` models `borrowed_range`, `std::ranges::dangling` otherwise.

As a user I have no idea what to do at this point.

The reduced code I was looking at was

struct Broken {
    struct iterator {
        int i;
        auto operator*() const -> decltype(auto) {
            return std::tie(i);
        }
        auto operator++() -> decltype(auto) {
            ++i;
            return *this;
        }
        friend bool operator==(iterator const&, iterator const&) = default;
        friend auto operator<=>(iterator const&, iterator const&) = default;
    };

    auto begin() const -> iterator { return {}; }
    auto end() const -> iterator { return {}; }
};

int foo() {
    Broken r;
    std::ranges::for_each(r, [](auto t) { 
        auto [i] = t;
        printf("%d\n", i);
    });
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugzillaIssues migrated from bugzillac++20clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerclang:frontendLanguage frontend issues, e.g. anything involving "Sema"conceptsC++20 concepts

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions