cenotelie / hime

Apache License 2.0
27 stars 4 forks source link

Invalid Rust Generation for Semantic actions #108

Open UPWRD1 opened 2 months ago

UPWRD1 commented 2 months ago

I have run into an issue regarding the Rust target.

himecc v4.3.0 generates invalid Rust code when attempting to use semantic actions.

In particular, rustc emits the following errors: 1:

E0106]: missing lifetime specifier
   --> ../lang.rs:429:78
    |
429 | fn parse_text(text: Text, actions: &mut dyn Actions) -> ParseResult<'static, '_, 'static> {
    |                     ----           ----------------                          ^^ expected named lifetime parameter

2:

error[E0308]: mismatched types
   --> src/root/passes/frontend/lang.rs:420:5
    |
418 | pub fn parse_utf8_stream(input: &mut dyn Read) -> Result<ParseResult<'static, 'static, 'static>, std::io::Error> {
    |                                                   -------------------------------------------------------------- expected `Result<ParseResult<'static, 'static, 'static>, std::io::Error>` because of return type
419 |     let text = Text::from_utf8_stream(input).unwrap();
420 |     parse_text(text, &mut NoActions {})
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Result<ParseResult<'_, '_, '_>, ...>`, found `ParseResult<'_, '_, '_>`
    |
    = note: expected enum `Result<ParseResult<'_, 'static, '_>, std::io::Error>`
             found struct `ParseResult<'_, '_, '_>`
help: try wrapping the expression in `Ok`
    |
420 |     Ok(parse_text(text, &mut NoActions {}))
    |     +++                                   +

3

error: lifetime may not live long enough
   --> src/root/passes/frontend/lang.rs:430:5
    |
429 | fn parse_text(text: Text, actions: &mut dyn Actions) -> ParseResult<'static, '_, 'static> {
    |               ---- has type `Text<'1>`
430 |     parse_text_with(text, TERMINALS, VARIABLES, VIRTUALS, actions)
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ returning this value requires that `'1` must outlive `'static`

These are quite easy to fix by simply wrapping error 2 in Ok(), and setting the faulty lifetimes to 'static. (Given my relative inexperience with complicated Rust development, these are most certianly the wrong approach.)

However, it's quite a pain to do this each time, as regenerating the files destroys previous changes, and it also creates a barrier of entry, for someone who is not as experienced with Rust.

Would it be possible to fix this in the generator?

Thank you.

UPWRD1 commented 2 months ago

I believe the problem can be found here: https://github.com/cenotelie/hime/blob/e48e2179261140276a8c432fb89068e4f1961096/sdk-rust/src/output/parser_rust.rs#L385C1-L395C1