Skip to content

Commit

Permalink
Auto merge of #13005 - ehuss:rustfix, r=weihanglo
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Nov 21, 2023
2 parents 63df867 + 47c7ff0 commit eed300d
Show file tree
Hide file tree
Showing 48 changed files with 2,211 additions and 10 deletions.
10 changes: 6 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pulldown-cmark = { version = "0.9.3", default-features = false }
rand = "0.8.5"
regex = "1.9.3"
rusqlite = { version = "0.29.0", features = ["bundled"] }
rustfix = "0.6.1"
rustfix = { version = "0.6.2", path = "crates/rustfix" }
same-file = "1.0.6"
security-framework = "2.9.2"
semver = { version = "1.0.20", features = ["serde"] }
Expand Down
33 changes: 33 additions & 0 deletions crates/rustfix/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[package]
name = "rustfix"
version = "0.6.2"
authors = [
"Pascal Hertleif <[email protected]>",
"Oliver Schneider <[email protected]>",
]
rust-version = "1.70.0" # MSRV:3
edition.workspace = true
license.workspace = true
homepage = "https://github.com/rust-lang/cargo"
repository = "https://github.com/rust-lang/cargo"
description = "Automatically apply the suggestions made by rustc"
documentation = "https://docs.rs/rustfix"
exclude = [
"examples/*",
"tests/*",
]

[dependencies]
anyhow.workspace = true
serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true
tracing.workspace = true

[dev-dependencies]
proptest.workspace = true
similar = "2.2.1"
tempfile.workspace = true
tracing-subscriber.workspace = true

[lints]
workspace = true
79 changes: 79 additions & 0 deletions crates/rustfix/Changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [0.4.6] - 2019-07-16

### Changed

Internal changes:

- Change example to automatically determine filename
- Migrate to Rust 2018
- use `derive` feature over `serde_derive` crate

## [0.4.5] - 2019-03-26

### Added

- Implement common traits for Diagnostic and related types

### Fixed

- Fix out of bounds access in parse_snippet

## [0.4.4] - 2018-12-13

### Added

- Make Diagnostic::rendered public.

### Changed

- Revert faulty "Allow multiple solutions in a suggestion"

## [0.4.3] - 2018-12-09 - *yanked!*

### Added

- Allow multiple solutions in a suggestion

### Changed

- use `RUSTC` environment var if present

## [0.4.2] - 2018-07-31

### Added

- Expose an interface to apply fixes on-by-one

### Changed

- Handle invalid snippets instead of panicking

## [0.4.1] - 2018-07-26

### Changed

- Ignore duplicate replacements

## [0.4.0] - 2018-05-23

### Changed

- Filter by machine applicability by default

[Unreleased]: https://github.com/rust-lang-nursery/rustfix/compare/rustfix-0.4.6...HEAD
[0.4.6]: https://github.com/rust-lang-nursery/rustfix/compare/rustfix-0.4.5...rustfix-0.4.6
[0.4.5]: https://github.com/rust-lang-nursery/rustfix/compare/rustfix-0.4.4...rustfix-0.4.5
[0.4.4]: https://github.com/rust-lang-nursery/rustfix/compare/rustfix-0.4.3...rustfix-0.4.4
[0.4.3]: https://github.com/rust-lang-nursery/rustfix/compare/rustfix-0.4.2...rustfix-0.4.3
[0.4.2]: https://github.com/rust-lang-nursery/rustfix/compare/rustfix-0.4.1...rustfix-0.4.2
[0.4.1]: https://github.com/rust-lang-nursery/rustfix/compare/rustfix-0.4.0...rustfix-0.4.1
[0.4.0]: https://github.com/rust-lang-nursery/rustfix/compare/rustfix-0.4.0
1 change: 1 addition & 0 deletions crates/rustfix/LICENSE-APACHE
1 change: 1 addition & 0 deletions crates/rustfix/LICENSE-MIT
29 changes: 29 additions & 0 deletions crates/rustfix/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# rustfix

[![Latest Version](https://img.shields.io/crates/v/rustfix.svg)](https://crates.io/crates/rustfix)
[![Rust Documentation](https://docs.rs/rustfix/badge.svg)](https://docs.rs/rustfix)

Rustfix is a library defining useful structures that represent fix suggestions from rustc.

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.

If you are looking for the [`cargo fix`] implementation, the core of it is located in [`cargo::ops::fix`].

[`cargo fix`]: https://doc.rust-lang.org/cargo/commands/cargo-fix.html
[`cargo::ops::fix`]: https://github.com/rust-lang/cargo/blob/master/src/cargo/ops/fix.rs

## License

Licensed under either of

- Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or <http://www.apache.org/licenses/LICENSE-2.0>)
- MIT license ([LICENSE-MIT](LICENSE-MIT) or <http://opensource.org/licenses/MIT>)

at your option.

### Contribution

Unless you explicitly state otherwise, any contribution intentionally
submitted for inclusion in the work by you, as defined in the Apache-2.0
license, shall be dual licensed as above, without any additional terms or
conditions.
44 changes: 44 additions & 0 deletions crates/rustfix/examples/fix-json.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#![allow(clippy::disallowed_methods, clippy::print_stdout, clippy::print_stderr)]

use anyhow::Error;
use std::io::{stdin, BufReader, Read};
use std::{collections::HashMap, collections::HashSet, env, fs};

fn main() -> Result<(), Error> {
let suggestions_file = env::args().nth(1).expect("USAGE: fix-json <file or -->");
let suggestions = if suggestions_file == "--" {
let mut buffer = String::new();
BufReader::new(stdin()).read_to_string(&mut buffer)?;
buffer
} else {
fs::read_to_string(&suggestions_file)?
};
let suggestions = rustfix::get_suggestions_from_json(
&suggestions,
&HashSet::new(),
rustfix::Filter::Everything,
)?;

let mut files = HashMap::new();
for suggestion in suggestions {
let file = suggestion.solutions[0].replacements[0]
.snippet
.file_name
.clone();
files.entry(file).or_insert_with(Vec::new).push(suggestion);
}

for (source_file, suggestions) in &files {
let source = fs::read_to_string(source_file)?;
let mut fix = rustfix::CodeFix::new(&source);
for suggestion in suggestions.iter().rev() {
if let Err(e) = fix.apply(suggestion) {
eprintln!("Failed to apply suggestion to {}: {}", source_file, e);
}
}
let fixes = fix.finish()?;
fs::write(source_file, fixes)?;
}

Ok(())
}
8 changes: 8 additions & 0 deletions crates/rustfix/proptest-regressions/replace.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Seeds for failure cases proptest has generated in the past. It is
# automatically read and these particular cases re-run before any
# novel cases are generated.
#
# It is recommended to check this file in to source control so that
# everyone who runs the test benefits from these saved cases.
xs 358148376 3634975642 2528447681 3675516813 # shrinks to ref s = ""
xs 3127423015 3362740891 2605681441 2390162043 # shrinks to ref data = "", ref replacements = [(0..0, [])]
89 changes: 89 additions & 0 deletions crates/rustfix/src/diagnostics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//! Rustc Diagnostic JSON Output
//!
//! The following data types are copied from [rust-lang/rust](https://github.com/rust-lang/rust/blob/de78655bca47cac8e783dbb563e7e5c25c1fae40/src/libsyntax/json.rs)
use serde::Deserialize;

#[derive(Clone, Deserialize, Debug, Hash, Eq, PartialEq)]
pub struct Diagnostic {
/// The primary error message.
pub message: String,
pub code: Option<DiagnosticCode>,
/// "error: internal compiler error", "error", "warning", "note", "help".
level: String,
pub spans: Vec<DiagnosticSpan>,
/// Associated diagnostic messages.
pub children: Vec<Diagnostic>,
/// The message as rustc would render it. Currently this is only
/// `Some` for "suggestions", but eventually it will include all
/// snippets.
pub rendered: Option<String>,
}

#[derive(Clone, Deserialize, Debug, Hash, Eq, PartialEq)]
pub struct DiagnosticSpan {
pub file_name: String,
pub byte_start: u32,
pub byte_end: u32,
/// 1-based.
pub line_start: usize,
pub line_end: usize,
/// 1-based, character offset.
pub column_start: usize,
pub column_end: usize,
/// Is this a "primary" span -- meaning the point, or one of the points,
/// where the error occurred?
pub is_primary: bool,
/// Source text from the start of line_start to the end of line_end.
pub text: Vec<DiagnosticSpanLine>,
/// Label that should be placed at this location (if any)
label: Option<String>,
/// If we are suggesting a replacement, this will contain text
/// that should be sliced in atop this span. You may prefer to
/// load the fully rendered version from the parent `Diagnostic`,
/// however.
pub suggested_replacement: Option<String>,
pub suggestion_applicability: Option<Applicability>,
/// Macro invocations that created the code at this span, if any.
expansion: Option<Box<DiagnosticSpanMacroExpansion>>,
}

#[derive(Copy, Clone, Debug, PartialEq, Deserialize, Hash, Eq)]
pub enum Applicability {
MachineApplicable,
HasPlaceholders,
MaybeIncorrect,
Unspecified,
}

#[derive(Clone, Deserialize, Debug, Eq, PartialEq, Hash)]
pub struct DiagnosticSpanLine {
pub text: String,

/// 1-based, character offset in self.text.
pub highlight_start: usize,

pub highlight_end: usize,
}

#[derive(Clone, Deserialize, Debug, Eq, PartialEq, Hash)]
struct DiagnosticSpanMacroExpansion {
/// span where macro was applied to generate this code; note that
/// this may itself derive from a macro (if
/// `span.expansion.is_some()`)
span: DiagnosticSpan,

/// name of macro that was applied (e.g., "foo!" or "#[derive(Eq)]")
macro_decl_name: String,

/// span where macro was defined (if known)
def_site_span: Option<DiagnosticSpan>,
}

#[derive(Clone, Deserialize, Debug, Eq, PartialEq, Hash)]
pub struct DiagnosticCode {
/// The code itself.
pub code: String,
/// An explanation for the code.
explanation: Option<String>,
}
Loading

0 comments on commit eed300d

Please sign in to comment.