Skip to content

Conversation

@oinoom
Copy link
Contributor

@oinoom oinoom commented Nov 11, 2025

Several transforms (sink_lets, 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

@oinoom oinoom marked this pull request as ready for review November 17, 2025 19:36
Copy link
Contributor

@fw-immunant fw-immunant left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable. Is there an easy way to observe the change in behavior? It would be nice to have a test case if possible.

Copy link
Contributor

@ahomescu ahomescu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you try this together with #1433?

/// Pick the span we should attach to a freshly minted statement.
///
/// We want new statements produced by transforms such as `sink_lets` or
/// `fold_let_assign` to retain the source span of the code they were cloned
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we just fix the transforms then? Why couldn't they pass in the spans?

@oinoom
Copy link
Contributor Author

oinoom commented Nov 21, 2025

Did you try this together with #1433?

@ahomescu I didn't know this existed, should I have tried that?

@oinoom oinoom force-pushed the fix.printparse-attr-spans branch from 86b24c2 to f294de7 Compare November 21, 2025 19:41
@oinoom
Copy link
Contributor Author

oinoom commented Nov 21, 2025

@ahomescu @fw-immunant I'm trying a different approach in #1469 based on this comment. If that looks good, I'll merge it and close this one

Could we just fix the transforms then? Why couldn't they pass in the spans?

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.

4 participants