Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support interpolated $:path followed by macro call or struct literal #1542

Merged
merged 3 commits into from
Dec 11, 2023

Conversation

dtolnay
Copy link
Owner

@dtolnay dtolnay commented Dec 11, 2023

Example of an input that previously behaved incorrectly:

let nested = Group::new(Delimiter::None, quote!(a::b || true));
let tokens = quote!(if #nested && false {});
syn::parse2::<syn::Expr>(tokens).unwrap()

Before: this has the wrong precedence. a::b || (true && false)

Expr::If {
    cond: Expr::Binary {
        left: Expr::Path {
            path: Path {
                segments: [
                    PathSegment {
                        ident: "a",
                    },
                    PathSegment {
                        ident: "b",
                    },
                ],
            },
        },
        op: BinOp::Or,
        right: Expr::Binary {
            left: Expr::Lit {
                lit: Lit::Bool {
                    value: true,
                },
            },
            op: BinOp::And,
            right: Expr::Lit {
                lit: Lit::Bool {
                    value: false,
                },
            },
        },
    },
    then_branch: Block {
        stmts: [],
    },
}

After: correct precedence. (a::b || true) && false

Expr::If {
    cond: Expr::Binary {
        left: Expr::Group {
            expr: Expr::Binary {
                left: Expr::Path {
                    path: Path {
                        segments: [
                            PathSegment {
                                ident: "a",
                            },
                            PathSegment {
                                ident: "b",
                            },
                        ],
                    },
                },
                op: BinOp::Or,
                right: Expr::Lit {
                    lit: Lit::Bool {
                        value: true,
                    },
                },
            },
        },
        op: BinOp::And,
        right: Expr::Lit {
            lit: Lit::Bool {
                value: false,
            },
        },
    },
    then_branch: Block {
        stmts: [],
    },
}

@dtolnay dtolnay merged commit 1c54651 into master Dec 11, 2023
28 checks passed
@dtolnay dtolnay deleted the exprgroup branch December 11, 2023 01:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant