Skip to content

Conversation

@oinoom
Copy link
Contributor

@oinoom oinoom commented Nov 11, 2025

Several transforms (sink_lets, fold_let_assign, remove_redundant_let_types) were panicking whenever the rewrite engine had to reparse snippets that no longer looked like full Rust statements.

Two cases showed up when testing:

  • while relocating #[derive] lines, the transform temporarily splices only the attribute text (for example just #[derive(Copy, Clone)]) while the enclosing struct body is rewritten at a higher level. Rust’s parser refuses that input, so PrintParse<Stmt> panicked before recovery finished
  • newly cloned let bindings inherit the builder’s default DUMMY_SP, so pretty-printing emits an empty string even though the original code had a real initializer. The AST/text round-trip fails and subsequent rewrites abort

This PR:

  • detects attribute-only snippets in PrintParse<Stmt> up front, reparses them by temporarily appending a dummy struct, and returns a StmtKind::Empty that carries the combined attribute span, which keeps the rewriter from panicking while the surrounding struct/item is printed elsewhere
  • adds a helper to the AST builder that reuses the child node’s span whenever a synthesized statement would otherwise inherit DUMMY_SP. Newly cloned locals/exprs retain their original source ranges, so rewrite_at_impl can pull real snippets instead of an empty buffer

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.

2 participants