tcbrindle / flux

A C++20 library for sequence-orientated programming
https://tristanbrindle.com/flux/
Boost Software License 1.0
472 stars 29 forks source link

Dropping from an empty sequence asserts #132

Closed JohelEGP closed 9 months ago

JohelEGP commented 9 months ago

An assertion is triggered due to the drop in intersperse. See https://cpp1.godbolt.org/z/ff99sqcK5:

#include "https://raw.githubusercontent.com/tcbrindle/flux/main/single_include/flux.hpp"

using namespace flux;

auto intersperse(auto&& r, auto&& e) -> auto { return FLUX_FWD(r).map([_0 = e](auto const& x) -> auto { return std::vector{_0, x};  }).flatten().drop(1);  }

auto sfml_argument(std::string_view) -> std::string { return "abc";  }

auto sfml_argument_list(std::span<std::string_view> mf) -> std::string { 
    return "(" + intersperse(drop(mf, 1).map(sfml_argument), std::string(", ")).flatten().to<std::string>() + ")";  }

auto main() -> int{
  std::vector<std::string_view> v {"point"}; 
  (void)sfml_argument_list(v);
}
/app/raw.githubusercontent.com/tcbrindle/flux/main/single_include/flux.hpp:1136: Fatal error: assertion 'self.has_value()' failed
terminate called without an active exception
Program terminated with signal: SIGSEGV

The same happens here (https://cpp1.godbolt.org/z/s4xGzzK3j):

#include "https://raw.githubusercontent.com/tcbrindle/flux/main/single_include/flux.hpp"

#include <array>
#include <iostream>
#include <iterator>

int main()
{
    auto result = flux::from(std::array{1, 2})
                                .filter(flux::pred::even)
                                .drop(2)
                                .drop(1);
    result.output_to(std::ostream_iterator<int>{std::cout});
}
tcbrindle commented 9 months ago

Hi @JohelEGP, thanks for the bug report. Something definitely seems to be going wrong here. I'll look into it.

tcbrindle commented 9 months ago

This should now be fixed. Thanks again for the bug report!

JohelEGP commented 9 months ago

Thank you! I can confirm it works now.