Skip to content

partition_attribute's static asserts should show the actual types on compilation error #59

@saki7

Description

@saki7

Currently, some user code that violates the tuple partitioning conditions emits compilation error like below:

parse_sequence.hpp(123,21): error C2338: static assertion failed: 'Sequence size of the passed attribute is greater than expected.'
... very long backtrace ... very long backtrace ... very long backtrace ... very long backtrace ... very long backtrace ...
... very long backtrace ... very long backtrace ... very long backtrace ... very long backtrace ... very long backtrace ...
... very long backtrace ... very long backtrace ... very long backtrace ... very long backtrace ... very long backtrace ...
... very long backtrace ... very long backtrace ... very long backtrace ... very long backtrace ... very long backtrace ...

This is very unfriendly because the actual types are not printed in the initial lines of the compilation error.

Instead of using locally defined static constexpr std::size_t variables, the whole static_assert condition should be wrapped into some metafunction so that it can print the actual type on compilation error.

static constexpr std::size_t l_size = parser_traits<LParser>::sequence_size;
static constexpr std::size_t r_size = parser_traits<RParser>::sequence_size;
static constexpr std::size_t actual_size = alloy::tuple_size_v<Attr>;
static constexpr std::size_t expected_size = l_size + r_size;
// If you got an error here, then you are trying to pass
// a tuple-like with the wrong number of elements
// as that expected by the (sequence) parser.
static_assert(
actual_size >= expected_size,
"Sequence size of the passed attribute is less than expected."
);
static_assert(
actual_size <= expected_size,
"Sequence size of the passed attribute is greater than expected."
);

Note that we should probably not emit the whole type (i.e., LParser or RParser) given to partition_attribute, because the real-world parser type looks like this:

'iris::x4::detail::partition_attribute<iris::x4::sequence<iris::x4::sequence<agni::lang::parser::token_parser<iris::x4::literal_string<std::basic_string_view<char,std::char_traits<char>>,iris::x4::char_encoding::standard,iris::x4::unused_type>>,iris::x4::expect_directive<iris::x4::rule<agni::lang::grammar::ident_tag,agni::lang::ast::Ident,false>>>,iris::x4::expect_directive<agni::lang::parser::token_parser<iris::x4::literal_char<iris::x4::char_encoding::standard,iris::x4::unused_type>>>>,iris::x4::expect_directive<iris::x4::skip_directive<iris::x4::sequence<iris::x4::alternative<iris::x4::sequence<iris::x4::sequence<iris::x4::attr_parser<std::vector<agni::lang::ast::FuncParam,std::allocator<agni::lang::ast::FuncParam>>,void>,iris::x4::alternative<iris::x4::sequence<agni::lang::parser::token_parser<iris::x4::literal_string<std::basic_string_view<char,std::char_traits<char>>,iris::x4::char_encoding::standard,iris::x4::unused_type>>,iris::x4::attr_parser<bool,T>>,iris::x4::attr_parser<T,T>>>,agni::lang::parser::token_parser<iris::x4::literal_char<iris::x4::char_encoding::standard,iris::x4::unused_type>>>,iris::x4::sequence<iris::x4::sequence<iris::x4::list<iris::x4::rule<agni::lang::grammar::func_param_tag,agni::lang::ast::FuncParam,false>,agni::lang::parser::token_parser<iris::x4::literal_char<iris::x4::char_encoding::standard,iris::x4::unused_type>>>,iris::x4::expect_directive<iris::x4::alternative<iris::x4::sequence<iris::x4::sequence<agni::lang::parser::token_parser<iris::x4::literal_char<iris::x4::char_encoding::standard,iris::x4::unused_type>>,iris::x4::expect_directive<agni::lang::parser::token_parser<iris::x4::literal_string<std::basic_string_view<char,std::char_traits<char>>,iris::x4::char_encoding::standard,iris::x4::unused_type>>>>,iris::x4::expect_directive<iris::x4::attr_parser<T,T>>>,iris::x4::attr_parser<T,T>>>>,iris::x4::expect_directive<agni::lang::parser::token_parser<iris::x4::literal_char<iris::x4::char_encoding::standard,iris::x4::unused_type>>>>>,iris::x4::expect_directive<iris::x4::attr_parser<nullptr,nullptr>>>,iris::x4::rule<agni::lang::grammar::comment_space_tag,agni::lang::ast::Comment,false>>>,Attr>'

I believe the Attr parameter (given to partition_attribute) is sufficient for the insight.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions