Skip to content

Clang front-end crash on compiling lambda #151521

@olivermadge

Description

@olivermadge

The following code causes clang-19 to crash with the error clang++-19: error: clang frontend command failed with exit code 139 (use -v to see invocation):

#include <utility>
#include <cstring>
#include <print>

template <typename TypeTuple>
auto count_types() {
    return []<size_t... Idx>(std::index_sequence<Idx...>) {
        return ([](std::tuple_element_t<Idx, TypeTuple> base) {return base;}(1) + ...);
    }(std::make_index_sequence<std::tuple_size_v<TypeTuple>>{});
}

int main() {
    std::println("{}", count_types<std::tuple<int>>());
    std::println("{}", count_types<std::tuple<int, char>>());
}

Interestingly, if I introduce an additional intermediate lambda to extract Idx, it compiles without error:

#include <utility>
#include <cstring>
#include <print>

template <typename TypeTuple>
auto count_types() {
    return []<size_t... Idx>(std::index_sequence<Idx...>) {
        return ([]<size_t Index>(std::integral_constant<size_t, Index>) {
            return [](std::tuple_element_t<Index, TypeTuple> base) {return base;}(1);
        }(std::integral_constant<size_t, Idx>()) + ...);
    }(std::make_index_sequence<std::tuple_size_v<TypeTuple>>{});
}

int main() {
    std::println("{}", count_types<std::tuple<int>>());
    std::println("{}", count_types<std::tuple<int, char>>());
}

Clang-19 from Arch Linux (version 19.1.7 on x86-64) invoked as clang++-19 -emit-llvm -Xclang -disable-llvm-passes --std=c++20 clang_bug.cpp -c -o clang

The same code compiles and runs with the expected result on clang++-20, g++-14 and g++-15

clang_bug-86f8f9.cpp.log
clang_bug-86f8f9.sh.log

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"crashPrefer [crash-on-valid] or [crash-on-invalid]lambdaC++11 lambda expressionsobsoleteIssues with old (unsupported) versions of LLVM

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions