Skip to content

Commit eed300d

Browse files
committed
Auto merge of #13005 - ehuss:rustfix, r=weihanglo
Migrate rustfix to the cargo repo This migrates the `rustfix` crate from https://github.com/rust-lang/rustfix/ to the cargo repo. The cargo team has been responsible for the client-side of `cargo fix`, and it can make it easier to maintain with all our tooling and tracking here. This crate is used by some external parties (like the compiler), so it will need to be maintained like an "ecosystem" package, but hopefully there shouldn't be any outside requirements (I haven't seen any in several years). After merging, I'll follow up with some things to address in the future, such as: - Migrating issues from the other repo. - Opening new issues for some cleanup tasks, such as adding documentation, fixing the `#[ignore]` annotations, fixing testing on windows, maybe migrating the test code to use different dependencies, various code cleanup. - Archiving the repo.
2 parents 63df867 + 47c7ff0 commit eed300d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+2211
-10
lines changed

Cargo.lock

Lines changed: 6 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pulldown-cmark = { version = "0.9.3", default-features = false }
7575
rand = "0.8.5"
7676
regex = "1.9.3"
7777
rusqlite = { version = "0.29.0", features = ["bundled"] }
78-
rustfix = "0.6.1"
78+
rustfix = { version = "0.6.2", path = "crates/rustfix" }
7979
same-file = "1.0.6"
8080
security-framework = "2.9.2"
8181
semver = { version = "1.0.20", features = ["serde"] }

crates/rustfix/Cargo.toml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
[package]
2+
name = "rustfix"
3+
version = "0.6.2"
4+
authors = [
5+
"Pascal Hertleif <killercup@gmail.com>",
6+
"Oliver Schneider <oli-obk@users.noreply.github.com>",
7+
]
8+
rust-version = "1.70.0" # MSRV:3
9+
edition.workspace = true
10+
license.workspace = true
11+
homepage = "https://github.com/rust-lang/cargo"
12+
repository = "https://github.com/rust-lang/cargo"
13+
description = "Automatically apply the suggestions made by rustc"
14+
documentation = "https://docs.rs/rustfix"
15+
exclude = [
16+
"examples/*",
17+
"tests/*",
18+
]
19+
20+
[dependencies]
21+
anyhow.workspace = true
22+
serde = { workspace = true, features = ["derive"] }
23+
serde_json.workspace = true
24+
tracing.workspace = true
25+
26+
[dev-dependencies]
27+
proptest.workspace = true
28+
similar = "2.2.1"
29+
tempfile.workspace = true
30+
tracing-subscriber.workspace = true
31+
32+
[lints]
33+
workspace = true

crates/rustfix/Changelog.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
## [Unreleased]
9+
10+
## [0.4.6] - 2019-07-16
11+
12+
### Changed
13+
14+
Internal changes:
15+
16+
- Change example to automatically determine filename
17+
- Migrate to Rust 2018
18+
- use `derive` feature over `serde_derive` crate
19+
20+
## [0.4.5] - 2019-03-26
21+
22+
### Added
23+
24+
- Implement common traits for Diagnostic and related types
25+
26+
### Fixed
27+
28+
- Fix out of bounds access in parse_snippet
29+
30+
## [0.4.4] - 2018-12-13
31+
32+
### Added
33+
34+
- Make Diagnostic::rendered public.
35+
36+
### Changed
37+
38+
- Revert faulty "Allow multiple solutions in a suggestion"
39+
40+
## [0.4.3] - 2018-12-09 - *yanked!*
41+
42+
### Added
43+
44+
- Allow multiple solutions in a suggestion
45+
46+
### Changed
47+
48+
- use `RUSTC` environment var if present
49+
50+
## [0.4.2] - 2018-07-31
51+
52+
### Added
53+
54+
- Expose an interface to apply fixes on-by-one
55+
56+
### Changed
57+
58+
- Handle invalid snippets instead of panicking
59+
60+
## [0.4.1] - 2018-07-26
61+
62+
### Changed
63+
64+
- Ignore duplicate replacements
65+
66+
## [0.4.0] - 2018-05-23
67+
68+
### Changed
69+
70+
- Filter by machine applicability by default
71+
72+
[Unreleased]: https://github.com/rust-lang-nursery/rustfix/compare/rustfix-0.4.6...HEAD
73+
[0.4.6]: https://github.com/rust-lang-nursery/rustfix/compare/rustfix-0.4.5...rustfix-0.4.6
74+
[0.4.5]: https://github.com/rust-lang-nursery/rustfix/compare/rustfix-0.4.4...rustfix-0.4.5
75+
[0.4.4]: https://github.com/rust-lang-nursery/rustfix/compare/rustfix-0.4.3...rustfix-0.4.4
76+
[0.4.3]: https://github.com/rust-lang-nursery/rustfix/compare/rustfix-0.4.2...rustfix-0.4.3
77+
[0.4.2]: https://github.com/rust-lang-nursery/rustfix/compare/rustfix-0.4.1...rustfix-0.4.2
78+
[0.4.1]: https://github.com/rust-lang-nursery/rustfix/compare/rustfix-0.4.0...rustfix-0.4.1
79+
[0.4.0]: https://github.com/rust-lang-nursery/rustfix/compare/rustfix-0.4.0

crates/rustfix/LICENSE-APACHE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../LICENSE-APACHE

crates/rustfix/LICENSE-MIT

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../LICENSE-MIT

crates/rustfix/Readme.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# rustfix
2+
3+
[![Latest Version](https://img.shields.io/crates/v/rustfix.svg)](https://crates.io/crates/rustfix)
4+
[![Rust Documentation](https://docs.rs/rustfix/badge.svg)](https://docs.rs/rustfix)
5+
6+
Rustfix is a library defining useful structures that represent fix suggestions from rustc.
7+
8+
This is a low-level library. You pass it the JSON output from `rustc`, and you can then use it to apply suggestions to in-memory strings. This library doesn't execute commands, or read or write from the filesystem.
9+
10+
If you are looking for the [`cargo fix`] implementation, the core of it is located in [`cargo::ops::fix`].
11+
12+
[`cargo fix`]: https://doc.rust-lang.org/cargo/commands/cargo-fix.html
13+
[`cargo::ops::fix`]: https://github.com/rust-lang/cargo/blob/master/src/cargo/ops/fix.rs
14+
15+
## License
16+
17+
Licensed under either of
18+
19+
- Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or <http://www.apache.org/licenses/LICENSE-2.0>)
20+
- MIT license ([LICENSE-MIT](LICENSE-MIT) or <http://opensource.org/licenses/MIT>)
21+
22+
at your option.
23+
24+
### Contribution
25+
26+
Unless you explicitly state otherwise, any contribution intentionally
27+
submitted for inclusion in the work by you, as defined in the Apache-2.0
28+
license, shall be dual licensed as above, without any additional terms or
29+
conditions.

crates/rustfix/examples/fix-json.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#![allow(clippy::disallowed_methods, clippy::print_stdout, clippy::print_stderr)]
2+
3+
use anyhow::Error;
4+
use std::io::{stdin, BufReader, Read};
5+
use std::{collections::HashMap, collections::HashSet, env, fs};
6+
7+
fn main() -> Result<(), Error> {
8+
let suggestions_file = env::args().nth(1).expect("USAGE: fix-json <file or -->");
9+
let suggestions = if suggestions_file == "--" {
10+
let mut buffer = String::new();
11+
BufReader::new(stdin()).read_to_string(&mut buffer)?;
12+
buffer
13+
} else {
14+
fs::read_to_string(&suggestions_file)?
15+
};
16+
let suggestions = rustfix::get_suggestions_from_json(
17+
&suggestions,
18+
&HashSet::new(),
19+
rustfix::Filter::Everything,
20+
)?;
21+
22+
let mut files = HashMap::new();
23+
for suggestion in suggestions {
24+
let file = suggestion.solutions[0].replacements[0]
25+
.snippet
26+
.file_name
27+
.clone();
28+
files.entry(file).or_insert_with(Vec::new).push(suggestion);
29+
}
30+
31+
for (source_file, suggestions) in &files {
32+
let source = fs::read_to_string(source_file)?;
33+
let mut fix = rustfix::CodeFix::new(&source);
34+
for suggestion in suggestions.iter().rev() {
35+
if let Err(e) = fix.apply(suggestion) {
36+
eprintln!("Failed to apply suggestion to {}: {}", source_file, e);
37+
}
38+
}
39+
let fixes = fix.finish()?;
40+
fs::write(source_file, fixes)?;
41+
}
42+
43+
Ok(())
44+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Seeds for failure cases proptest has generated in the past. It is
2+
# automatically read and these particular cases re-run before any
3+
# novel cases are generated.
4+
#
5+
# It is recommended to check this file in to source control so that
6+
# everyone who runs the test benefits from these saved cases.
7+
xs 358148376 3634975642 2528447681 3675516813 # shrinks to ref s = ""
8+
xs 3127423015 3362740891 2605681441 2390162043 # shrinks to ref data = "", ref replacements = [(0..0, [])]

crates/rustfix/src/diagnostics.rs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
//! Rustc Diagnostic JSON Output
2+
//!
3+
//! The following data types are copied from [rust-lang/rust](https://github.com/rust-lang/rust/blob/de78655bca47cac8e783dbb563e7e5c25c1fae40/src/libsyntax/json.rs)
4+
5+
use serde::Deserialize;
6+
7+
#[derive(Clone, Deserialize, Debug, Hash, Eq, PartialEq)]
8+
pub struct Diagnostic {
9+
/// The primary error message.
10+
pub message: String,
11+
pub code: Option<DiagnosticCode>,
12+
/// "error: internal compiler error", "error", "warning", "note", "help".
13+
level: String,
14+
pub spans: Vec<DiagnosticSpan>,
15+
/// Associated diagnostic messages.
16+
pub children: Vec<Diagnostic>,
17+
/// The message as rustc would render it. Currently this is only
18+
/// `Some` for "suggestions", but eventually it will include all
19+
/// snippets.
20+
pub rendered: Option<String>,
21+
}
22+
23+
#[derive(Clone, Deserialize, Debug, Hash, Eq, PartialEq)]
24+
pub struct DiagnosticSpan {
25+
pub file_name: String,
26+
pub byte_start: u32,
27+
pub byte_end: u32,
28+
/// 1-based.
29+
pub line_start: usize,
30+
pub line_end: usize,
31+
/// 1-based, character offset.
32+
pub column_start: usize,
33+
pub column_end: usize,
34+
/// Is this a "primary" span -- meaning the point, or one of the points,
35+
/// where the error occurred?
36+
pub is_primary: bool,
37+
/// Source text from the start of line_start to the end of line_end.
38+
pub text: Vec<DiagnosticSpanLine>,
39+
/// Label that should be placed at this location (if any)
40+
label: Option<String>,
41+
/// If we are suggesting a replacement, this will contain text
42+
/// that should be sliced in atop this span. You may prefer to
43+
/// load the fully rendered version from the parent `Diagnostic`,
44+
/// however.
45+
pub suggested_replacement: Option<String>,
46+
pub suggestion_applicability: Option<Applicability>,
47+
/// Macro invocations that created the code at this span, if any.
48+
expansion: Option<Box<DiagnosticSpanMacroExpansion>>,
49+
}
50+
51+
#[derive(Copy, Clone, Debug, PartialEq, Deserialize, Hash, Eq)]
52+
pub enum Applicability {
53+
MachineApplicable,
54+
HasPlaceholders,
55+
MaybeIncorrect,
56+
Unspecified,
57+
}
58+
59+
#[derive(Clone, Deserialize, Debug, Eq, PartialEq, Hash)]
60+
pub struct DiagnosticSpanLine {
61+
pub text: String,
62+
63+
/// 1-based, character offset in self.text.
64+
pub highlight_start: usize,
65+
66+
pub highlight_end: usize,
67+
}
68+
69+
#[derive(Clone, Deserialize, Debug, Eq, PartialEq, Hash)]
70+
struct DiagnosticSpanMacroExpansion {
71+
/// span where macro was applied to generate this code; note that
72+
/// this may itself derive from a macro (if
73+
/// `span.expansion.is_some()`)
74+
span: DiagnosticSpan,
75+
76+
/// name of macro that was applied (e.g., "foo!" or "#[derive(Eq)]")
77+
macro_decl_name: String,
78+
79+
/// span where macro was defined (if known)
80+
def_site_span: Option<DiagnosticSpan>,
81+
}
82+
83+
#[derive(Clone, Deserialize, Debug, Eq, PartialEq, Hash)]
84+
pub struct DiagnosticCode {
85+
/// The code itself.
86+
pub code: String,
87+
/// An explanation for the code.
88+
explanation: Option<String>,
89+
}

0 commit comments

Comments
 (0)