From c02aedfcafafe3d2d0fbb4e9d85b2bbbbfa9abb2 Mon Sep 17 00:00:00 2001 From: Pascal Hertleif Date: Wed, 2 May 2018 00:32:31 +0200 Subject: [PATCH] Use rustfix' suggestion fixing API Uses branch from until we publish a new release. --- src/tools/compiletest/Cargo.toml | 2 +- src/tools/compiletest/src/autofix.rs | 70 ---------------------------- src/tools/compiletest/src/main.rs | 1 - src/tools/compiletest/src/runtest.rs | 7 ++- 4 files changed, 6 insertions(+), 74 deletions(-) delete mode 100644 src/tools/compiletest/src/autofix.rs diff --git a/src/tools/compiletest/Cargo.toml b/src/tools/compiletest/Cargo.toml index 1710a44380f..476e5927a2f 100644 --- a/src/tools/compiletest/Cargo.toml +++ b/src/tools/compiletest/Cargo.toml @@ -13,7 +13,7 @@ regex = "0.2" serde = "1.0" serde_json = "1.0" serde_derive = "1.0" -rustfix = { git = "https://github.com/rust-lang-nursery/rustfix" } +rustfix = { git = "https://github.com/rust-lang-nursery/rustfix", branch = "apply_suggestion" } [target.'cfg(unix)'.dependencies] libc = "0.2" diff --git a/src/tools/compiletest/src/autofix.rs b/src/tools/compiletest/src/autofix.rs deleted file mode 100644 index 29ea5cdff8f..00000000000 --- a/src/tools/compiletest/src/autofix.rs +++ /dev/null @@ -1,70 +0,0 @@ -use rustfix::{get_suggestions_from_json, Replacement}; -use std::collections::HashSet; -use std::error::Error; - -pub fn run_rustfix(code: &str, json: &str) -> String { - let suggestions = get_suggestions_from_json(&json, &HashSet::new()) - .expect("could not load suggestions"); - - let mut fixed = code.to_string(); - - for sug in suggestions.into_iter().rev() { - for sol in sug.solutions { - for r in sol.replacements { - fixed = apply_suggestion(&mut fixed, &r) - .expect("could not apply suggestion"); - } - } - } - - fixed -} - -fn apply_suggestion( - file_content: &mut String, - suggestion: &Replacement, -) -> Result> { - use std::cmp::max; - - let mut new_content = String::new(); - - // Add the lines before the section we want to replace - new_content.push_str(&file_content - .lines() - .take(max(suggestion.snippet.line_range.start.line - 1, 0) as usize) - .collect::>() - .join("\n")); - new_content.push_str("\n"); - - // Parts of line before replacement - new_content.push_str(&file_content - .lines() - .nth(suggestion.snippet.line_range.start.line - 1) - .unwrap_or("") - .chars() - .take(suggestion.snippet.line_range.start.column - 1) - .collect::()); - - // Insert new content! Finally! - new_content.push_str(&suggestion.replacement); - - // Parts of line after replacement - new_content.push_str(&file_content - .lines() - .nth(suggestion.snippet.line_range.end.line - 1) - .unwrap_or("") - .chars() - .skip(suggestion.snippet.line_range.end.column - 1) - .collect::()); - - // Add the lines after the section we want to replace - new_content.push_str("\n"); - new_content.push_str(&file_content - .lines() - .skip(suggestion.snippet.line_range.end.line as usize) - .collect::>() - .join("\n")); - new_content.push_str("\n"); - - Ok(new_content) -} diff --git a/src/tools/compiletest/src/main.rs b/src/tools/compiletest/src/main.rs index 3e7c6500433..a7849d53c3d 100644 --- a/src/tools/compiletest/src/main.rs +++ b/src/tools/compiletest/src/main.rs @@ -53,7 +53,6 @@ pub mod common; pub mod errors; mod raise_fd_limit; mod read2; -mod autofix; fn main() { env_logger::init(); diff --git a/src/tools/compiletest/src/runtest.rs b/src/tools/compiletest/src/runtest.rs index 20456a21cb5..f0feb9b50f1 100644 --- a/src/tools/compiletest/src/runtest.rs +++ b/src/tools/compiletest/src/runtest.rs @@ -35,7 +35,6 @@ use std::path::{Path, PathBuf}; use std::process::{Child, Command, ExitStatus, Output, Stdio}; use std::str; -use autofix::run_rustfix; use extract_gdb_version; /// The name of the environment variable that holds dynamic library locations. @@ -2607,10 +2606,14 @@ impl<'test> TestCx<'test> { let fixture_path = expected_output_path(&self.testpaths, None, &None, UI_FIXED); if fixture_path.exists() { + use std::collections::HashSet; + use rustfix::{apply_suggestions, get_suggestions_from_json}; + let unfixed_code = self.load_expected_output_from_path(&self.testpaths.file) .unwrap(); let expected_fixed = self.load_expected_output_from_path(&fixture_path).unwrap(); - let fixed_code = run_rustfix(&unfixed_code, &proc_res.stderr); + let suggestions = get_suggestions_from_json(&proc_res.stderr, &HashSet::new()).unwrap(); + let fixed_code = apply_suggestions(&unfixed_code, &suggestions); let errors = self.compare_output("rs.fixed", &fixed_code, &expected_fixed); if errors > 0 { panic!("rustfix produced different fixed file!");