-
Notifications
You must be signed in to change notification settings - Fork 281
refactor: preserve spans for rewrites and add empty-print fallback to avoid sink_lets and remove_redundant_let_types #1469
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
Open
oinoom
wants to merge
3
commits into
master
Choose a base branch
from
fix.spans
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -252,7 +252,24 @@ impl Splice for Ty { | |
|
|
||
| impl Splice for Stmt { | ||
| fn splice_span(&self) -> Span { | ||
| self.span | ||
| // Extend statement span to include attributes on the inner node. | ||
| // | ||
| // Transforms like sink_lets set stmt.span = local.span, but attributes are | ||
| // stored on the inner Local/Item/Expr, not the Stmt wrapper. The pretty-printer | ||
| // outputs attributes, but if stmt.span doesn't cover them, the rewrite system | ||
| // can't extract the complete source text. | ||
| // | ||
| // extend_span_attrs() expands the span backward to include attributes that | ||
| // appear before the node in source, checking syntax contexts to avoid crossing | ||
| // macro boundaries. | ||
| let attrs = match &self.kind { | ||
| StmtKind::Local(local) => &local.attrs[..], | ||
| StmtKind::Item(item) => &item.attrs[..], | ||
| StmtKind::Expr(expr) | StmtKind::Semi(expr) => &expr.attrs[..], | ||
| StmtKind::MacCall(mac) => &mac.attrs[..], | ||
| StmtKind::Empty => &[], | ||
| }; | ||
| extend_span_attrs(self.span, attrs) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -674,7 +691,29 @@ fn rewrite_at_impl<T>(old_span: Span, new: &T, mut rcx: RewriteCtxtRef) -> bool | |
| where | ||
| T: PrintParse + RecoverChildren + Splice + MaybeGetNodeId, | ||
| { | ||
| let printed = add_comments(new.to_string(), new, &rcx); | ||
| let mut printed = add_comments(new.to_string(), new, &rcx); | ||
|
|
||
| // Fallback: extract source snippet when pretty-printing produces empty output. | ||
| // | ||
| // When transforms move macro-expanded code (e.g., from #[derive]), the span may | ||
| // point to generated code with no source file location. Pretty-printing such nodes | ||
| // can produce empty output, which causes problems during the reparse/recovery phase. | ||
| // | ||
| // If the printed text is empty, try extracting the source directly using | ||
| // splice_span() (which includes attributes). If source is available, use that | ||
| // for reparsing instead of the empty pretty-printed text. | ||
|
||
| if printed.trim().is_empty() { | ||
| if let Ok(snippet) = rcx | ||
| .session() | ||
| .source_map() | ||
| .span_to_snippet(new.splice_span()) | ||
| { | ||
| if !snippet.trim().is_empty() { | ||
| printed = snippet; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| let reparsed = T::parse(rcx.session(), &printed); | ||
| let reparsed = reparsed.ast_deref(); | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're fixing
sink_letsbelow, is this also needed?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My understanding is that the attributes need to sink as well, without this the extracted source code span would just be the let statement but with this we cover the attribute as well and so we'll get a match to the pretty printed code (which includes the attribute)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We create a bunch of new
mk()...local_stmt(...)below, couldn't we copy the attributes there?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not exactly sure how that would be done, can you put attributes inside a statement? I thought that was contained at a higher level.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can put attributes on a statement, wouldn't that work here?