nix-community / rnix-parser

A Nix parser written in Rust [maintainer=@oberblastmeister]
MIT License
366 stars 44 forks source link

Typed api doesn't handle lambdas properly #121

Closed darichey closed 2 years ago

darichey commented 2 years ago

The typed api gives the same node for both the argument and body of a lambda like x: x.

fn main() {
    let nix_expr = "x: x";
    if let rnix::ast::Expr::Lambda(lambda) =
        rnix::Root::parse(&nix_expr).ok().unwrap().expr().unwrap()
    {
        println!("{:?}", lambda.param());
        println!("{:?}", lambda.body());
    }
}
Some(Ident(Ident(NODE_IDENT@0..1)))
Some(Ident(Ident(NODE_IDENT@0..1)))

I think this is because both getters are using nth(0) here: https://github.com/nix-community/rnix-parser/blob/38c0f687c08b0f6eb1f5b9c7bf565a36c2911b20/src/ast/nodes.rs#L283-L287 since a NODE_IDENT is both a Param and Expr.

Changing the body getter to use nth(1) just shifts the problem such that expressions like { x }: x aren't handled properly, since here the param is a Pattern and there isn't a 1th Expr.

oberblastmeister commented 2 years ago

This needs to be handled in the parser be creating a new node, maybe NODE_PARAM instead of NODE_IDENT

darichey commented 2 years ago

Ah ok that makes sense. I'll give that a shot