Skip to content
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

rustfix: Support inserting new lines. #13226

Merged
merged 4 commits into from
Jan 2, 2024
Merged

Conversation

ehuss
Copy link
Contributor

@ehuss ehuss commented Dec 31, 2023

If rustfix received a suggestion which inserts new lines without replacing existing lines, it would ignore the suggestion. This is because parse_snippet would immediately return if the lines to replace was empty.

The solution here is to just drop the code which messes with the original text line. cargo fix (and compiletest) currently do not use this. This was originally added back in the days when rustfix supported an interactive UI which showed color highlighting of what it looks like with the replacement. My feeling is that when we add something like this back in, I would prefer to instead use a real diff library and display instead of trying to do various text manipulation for display. This particular code has generally been buggy, and has been a problem several times.

The included test fails without this fix because the changes do not apply, and the code cannot compile.

This also includes a little bit of cleanup for the parse_and_replace test. My feeling is that this test could use further improvements.

This is just some minor code cleanup for the parse_and_replace test,
there should not be any functional differences.
This adds an environment variable to make it easier to add new tests.
If rustfix received a suggestion which inserts new lines without
replacing existing lines, it would ignore the suggestion. This is
because `parse_snippet` would immediately return if the `lines` to
replace was empty.

The solution here is to just drop the code which messes with the
original text line. `cargo fix` (and compiletest) currently do not use
this. This was originally added back in the days when rustfix supported
an interactive UI which showed color highlighting of what it looks like
with the replacement. My feeling is that when we add something like this
back in, I would prefer to instead use a real diff library and display
instead of trying to do various text manipulation for display. This
particular code has generally been buggy, and has been a problem several
times.

The included test fails without this fix because the changes do not
apply, and the code cannot compile.
@rustbot
Copy link
Collaborator

rustbot commented Dec 31, 2023

r? @weihanglo

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added Command-fix S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 31, 2023
@ehuss ehuss changed the title Fix empty span rustfix: Support inserting new lines. Dec 31, 2023
Suggestions that come from rustc that are multi-line only use LF line
endings. But if the file is checked out on windows with CRLF
line-endings, then you end up with a mix of line endings that don't
match the "fixed.rs" file.

Tracking this at rust-lang/rust#119482.
@@ -20,6 +42,7 @@ mod settings {
pub const CHECK_JSON: &str = "RUSTFIX_TEST_CHECK_JSON";
pub const RECORD_JSON: &str = "RUSTFIX_TEST_RECORD_JSON";
pub const RECORD_FIXED_RUST: &str = "RUSTFIX_TEST_RECORD_FIXED_RUST";
pub const BLESS: &str = "RUSTFIX_TEST_BLESS";
Copy link
Member

Choose a reason for hiding this comment

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

This makes me wonder if we should use snapbox to minimize the custom logic here.

@weihanglo
Copy link
Member

Thank you. Going to merge this soon.
We can do snapbox migration later on if we want to.

@bors r+

@bors
Copy link
Contributor

bors commented Jan 2, 2024

📌 Commit 3d3e1b3 has been approved by weihanglo

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 2, 2024
@bors
Copy link
Contributor

bors commented Jan 2, 2024

⌛ Testing commit 3d3e1b3 with merge 059ec69...

@bors
Copy link
Contributor

bors commented Jan 2, 2024

☀️ Test successful - checks-actions
Approved by: weihanglo
Pushing 059ec69 to master...

@bors bors merged commit 059ec69 into rust-lang:master Jan 2, 2024
22 checks passed
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 3, 2024
Update cargo

11 commits in ac6bbb33293d8d424c17ecdb42af3aac25fb7295..add15366eaf3f3eb84717d3b8b71902ca36a7c84
2023-12-26 23:22:08 +0000 to 2024-01-02 03:24:42 +0000
- chore(deps): update gix (rust-lang/cargo#13230)
- chore(deps): update alpine docker tag to v3.19 (rust-lang/cargo#13228)
- rustfix: Support inserting new lines. (rust-lang/cargo#13226)
- Fix fix::fix_in_dependency to not rely on rustc (rust-lang/cargo#13220)
- cleanup: Remove error-format special-case in `cargo fix` (rust-lang/cargo#13224)
- `cargo fix`: always inherit the jobserver (rust-lang/cargo#13225)
- Bump cargo-credential to 0.4.3 (rust-lang/cargo#13221)
- `cargo add` - fix for adding features from repository with multiple packages. (rust-lang/cargo#13213)
- Remove repetitive words (rust-lang/cargo#13216)
- Add cargo:rustc-cdylib-link-arg into RESERVED_PREFIXES list (rust-lang/cargo#13212)
- chore(doc): doc for custom subcommands look up. (rust-lang/cargo#13203)
@rustbot rustbot added this to the 1.77.0 milestone Jan 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Command-fix S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants