Skip to content

Rust-analyzer as library: potential overlap between expr()/tail_expr() and statements() in ast::MacroStmts and ast::StmtList #20485

@redsun82

Description

@redsun82

rust-analyzer version: 0.0.300

As far as I understand generated getters in ast classes take the first child that matches the required type. However since AsmExpr is now also an Item (and therefore a Stmt), this can lead to overlaps where the last element of ast::MacroStmts::statements() will be a duplicate of ast::MacroStmts::expr(). This can be reproduced by any use of asm!, for example this one coming from https://doc.rust-lang.org/reference/inline-assembly.html

code snippet to reproduce:

use std::arch::asm;

let mut x: u64 = 4;
unsafe {
    asm!(
        "mov {tmp}, {x}",
        "shl {tmp}, 1",
        "shl {x}, 2",
        "add {x}, {tmp}",
        x = inout(reg) x,
        tmp = out(reg) _,
    );
}

When expanding macros, this will result in a MacroStmts instance that exhibits this duplication. I don't know if this could cause any issues elsewhere in the library.

Theoretically this problem could also appear in StmtList, which is

  '{'
    Attr*
    statements:Stmt*
    tail_expr:Expr?
  '}'

but I think that can't happen in practice yet, as there's no other way to produce an AsmExpr (and therefore something that is both an Expr and a Stmt) than by asm! or similar inline assembly macros, so an AsmExpr won't show up as a tail_expr in any StmtList instance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions