Restore fix_unused_unsafe transform and fix AST printing bug#1595
Open
randomPoison wants to merge 4 commits intomasterfrom
Open
Restore fix_unused_unsafe transform and fix AST printing bug#1595randomPoison wants to merge 4 commits intomasterfrom
randomPoison wants to merge 4 commits intomasterfrom
Conversation
We were incorrectly using `pprust::to_string` for methods that directly returned the string, causing us to generate empty strings. For the various `x_to_string` methods we want to construct a `pprust::State` directly and then call the method
Contributor
|
So the tl;dr of this PR is that we're switching from the |
ahomescu
approved these changes
Feb 14, 2026
| _ => pprust::to_string(|s| { | ||
| s.stmt_to_string(self); | ||
| }), | ||
| _ => pprust::State::new().stmt_to_string(self), |
Contributor
There was a problem hiding this comment.
Reading the callee here, it seems to me that the difference is we're switching from calling s.stmt_to_string() to s.print_stmt() instead. Is that correct?
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Add a test case for the fix_unused_unsafe transform, and fix an issue with how we were converting AST nodes to strings that was causing the transform to panic.
The issue with converting AST nodes to strings came from a subtle issue with how the pretty printer in
pprustis meant to be used.pprust::to_stringconstructs apprust::State, gives you thatStateobject to interact with, and then extracts the printed string from theStateat the end. This is meant to be used in combination with someprint_foomethods onStatethat write into the state's internal buffer. However, we were using thefoo_to_stringmethods, which directly return the printed string without updating theState's buffer (or at least resets the state after the string is generated). This causedpprust::to_stringto return an empty string, which then caused a panic in the rewriting process. Thefoo_to_stringmethods are what we want to use, and we can bypasspprust::to_stringand construct aStateobject directly in order to use these methods. I looked at all the places where we were usingpprust::to_stringand fixed the ones that were using the API wrong.The test reflects the current functionality of the transform, which only removes the
unsafekeyword but leaves the block in place. I would like the further extend the transform pass to fully remove the block where safe to do so, but I'd like to do that in a separate PR to keep this one simple. I've added #1596 to track that pending work.