Skip to content

const generics can't be parsed in element tags #65

Description

@gbj

Consider the following test:

#[test]
fn test_const_generics() -> Result<()> {
    let tokens = quote! {
        <foo<1>/>
    };
    let nodes = parse2(tokens)?;
    let element = get_element(&nodes, 0);

    assert_eq!(element.name().to_string(), "foo");
    let param: TypeParam = parse_quote!(1);
    assert_eq!(element.open_tag.generics.type_params().next(), Some(&param));

    Ok(())
}

This follows the same form as the existing test_generics(), but uses a const generic.

This fails to parse, because rstml currently tries to parse these generics into [syn::Generics]
(https://docs.rs/syn/latest/syn/struct.Generics.html):

pub struct OpenTag {
pub token_lt: Token![<],
pub name: NodeName,
pub generics: syn::Generics,

But syn::Generics is the syntax for generics "lifetimes and type parameters attached to a declaration of a function, enum, trait, etc." i.e., for a const generic its GenericParam is a ConstParam as in a declaration, not an invocation: i.e., it's const LENGTH: usize, not 3.

I made some progress (66e8e6f) in moving this over to parse the generics as generic arguments rather than declarations, but other tests are written to rely on access to some methods from syn::Generics. So I thought I'd stop here and ask your thoughts!

(Or, if const generics are intended to be supported in another way, let me know!)


   --> rstml/tests/test.rs:755:42
    |
755 |     assert_eq!(element.open_tag.generics.lifetimes().next(), Some(&param));
    |                                          ^^^^^^^^^ method not found in `TagGenerics`

error[E0599]: no method named `type_params` found for struct `TagGenerics` in the current scope
   --> rstml/tests/test.rs:777:14
    |
772 | /         element
773 | |             .close_tag
774 | |             .as_ref()
775 | |             .unwrap()
776 | |             .generics
777 | |             .type_params()
    | |             -^^^^^^^^^^^ method not found in `TagGenerics`
    | |_____________|
    |

error[E0599]: no method named `lifetimes` found for struct `TagGenerics` in the current scope
   --> rstml/tests/test.rs:787:14
    |
782 | /         element
783 | |             .close_tag
784 | |             .as_ref()
785 | |             .unwrap()
786 | |             .generics
787 | |             .lifetimes()
    | |             -^^^^^^^^^ method not found in `TagGenerics`
    | |_____________|

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions