Issue 107844
Summary Clang C++ optimiser generates incorrect code
Labels clang
Assignees
Reporter filiplukasik
    `
#include <optional>
#include <iostream>

inline std::optional<int> fail() {
    std::terminate();
}

std::optional<int> test(int index) {
    if (0 == index) {
        return std::optional<int>{};
    } else {
        return fail();
    }
}

int main(int argc, const char * argv[]) {
    // This should evaluate to an empty optional
    auto color = test(0);
        
    if (color.has_value()) {
        // With -Os set, we hit this branch on armv8
        std::cout << color.value() << std::endl;
    } else {
 // Without optimisations we hit this branch
        std::cout << "Color Empty" << std::endl;
    }

    return 0;
}
`

The code above results in "bad_optional_access" exception being thrown when compiled for armv8 in clang 18.1.0 and -Os (it also seems to crash in clang 17 and 16 according to compiler explorer + corresponding Apple clang forks - tested with Xcode 15.3 and Xcode 16 betas)

https://godbolt.org/z/r1qbfb4nE

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to