boostorg / spirit

Boost.org spirit module
http://boost.org/libs/spirit
383 stars 159 forks source link

X3: Incorrect attribute propagation for alternatives with equal attributes #771

Closed Krzmbrzl closed 9 months ago

Krzmbrzl commented 10 months ago

The docs state

a: A, b: A --> (a | b): A

meaning that an alternative between two rules/parsers that have the same attribute type will have an attribute that is the same as that common type (instead of a variant of the individual types).

However, this is not the case. Alternatives such as the above still result in an attribute of type variant< A, A >.

While (I think?) automatic propagation rules can correctly propagate this to an attribute of type A, this is a problem/inconvenience for e.g. semantic actions as they get the immediate attribute type and thus have to work with the variant.

MWE (Godbolt):

#include <boost/spirit/home/x3.hpp>

#include <type_traits>
#include <string>

namespace x3 = boost::spirit::x3;

int main() {
    auto validator = [](auto &ctx) {
        static_assert(std::is_same_v<std::remove_cvref_t<decltype(x3::_attr(ctx))>, int>);
    };
    auto parser = (x3::int_ | x3::int_)[validator];

    std::string input;
    x3::phrase_parse(input.begin(), input.end(), parser, x3::space);
}
cppljevans commented 10 months ago

think this issue has already been raised in issue 722.

Krzmbrzl commented 9 months ago

Yes indeed.