]> git.lizzy.rs Git - rust.git/blobdiff - src/lib.rs
Trigger an internal error if we skip formatting due to a lost comment
[rust.git] / src / lib.rs
index 114e72c4847beaf5a4bd1fc6aeb9fe2e9613890c..820134c2085d4e6efaf50ddfefbf03282d4a2f47 100644 (file)
@@ -33,6 +33,7 @@
 extern crate serde_derive;
 extern crate serde_json;
 extern crate syntax;
+extern crate syntax_pos;
 extern crate toml;
 extern crate unicode_segmentation;
 
@@ -85,6 +86,7 @@
 mod missed_spans;
 pub(crate) mod modules;
 mod overflow;
+mod pairs;
 mod patterns;
 mod reorder;
 mod rewrite;
@@ -138,8 +140,20 @@ pub enum ErrorKind {
     ParseError,
     /// The user mandated a version and the current version of Rustfmt does not
     /// satisfy that requirement.
-    #[fail(display = "Version mismatch")]
+    #[fail(display = "version mismatch")]
     VersionMismatch,
+    /// If we had formatted the given node, then we would have lost a comment.
+    #[fail(display = "not formatted because a comment would be lost")]
+    LostComment,
+}
+
+impl ErrorKind {
+    fn is_comment(&self) -> bool {
+        match self {
+            ErrorKind::LostComment => true,
+            _ => false,
+        }
+    }
 }
 
 impl From<io::Error> for ErrorKind {
@@ -160,8 +174,8 @@ impl FormattingError {
     fn from_span(span: &Span, codemap: &CodeMap, kind: ErrorKind) -> FormattingError {
         FormattingError {
             line: codemap.lookup_char_pos(span.lo()).line,
+            is_comment: kind.is_comment(),
             kind,
-            is_comment: false,
             is_string: false,
             line_buffer: codemap
                 .span_to_lines(*span)
@@ -179,7 +193,8 @@ fn msg_prefix(&self) -> &str {
             ErrorKind::LineOverflow(..)
             | ErrorKind::TrailingWhitespace
             | ErrorKind::IoError(_)
-            | ErrorKind::ParseError => "internal error:",
+            | ErrorKind::ParseError
+            | ErrorKind::LostComment => "internal error:",
             ErrorKind::LicenseCheck | ErrorKind::BadAttr | ErrorKind::VersionMismatch => "error:",
             ErrorKind::BadIssue(_) | ErrorKind::DeprecatedAttr => "warning:",
         }
@@ -198,7 +213,10 @@ fn msg_suffix(&self) -> &str {
     fn format_len(&self) -> (usize, usize) {
         match self.kind {
             ErrorKind::LineOverflow(found, max) => (max, found - max),
-            ErrorKind::TrailingWhitespace | ErrorKind::DeprecatedAttr | ErrorKind::BadAttr => {
+            ErrorKind::TrailingWhitespace
+            | ErrorKind::DeprecatedAttr
+            | ErrorKind::BadAttr
+            | ErrorKind::LostComment => {
                 let trailing_ws_start = self
                     .line_buffer
                     .rfind(|c: char| !c.is_whitespace())
@@ -499,7 +517,7 @@ fn should_report_error(
     is_string: bool,
     error_kind: &ErrorKind,
 ) -> bool {
-    let allow_error_report = if char_kind.is_comment() || is_string {
+    let allow_error_report = if char_kind.is_comment() || is_string || error_kind.is_comment() {
         config.error_on_unformatted()
     } else {
         true
@@ -507,7 +525,7 @@ fn should_report_error(
 
     match error_kind {
         ErrorKind::LineOverflow(..) => config.error_on_line_overflow() && allow_error_report,
-        ErrorKind::TrailingWhitespace => allow_error_report,
+        ErrorKind::TrailingWhitespace | ErrorKind::LostComment => allow_error_report,
         _ => true,
     }
 }
@@ -793,6 +811,7 @@ fn format_input_inner<T: Write>(
     config: &Config,
     mut out: Option<&mut T>,
 ) -> Result<(Summary, FileMap, FormatReport), (ErrorKind, Summary)> {
+    syntax_pos::hygiene::set_default_edition(config.edition().to_libsyntax_pos_edition());
     let mut summary = Summary::default();
     if config.disable_all_formatting() {
         // When the input is from stdin, echo back the input.
@@ -927,7 +946,7 @@ fn duration_to_f32(d: Duration) -> f32 {
     }
 }
 
-pub fn replace_with_system_newlines(text: &mut String, config: &Config) -> () {
+fn replace_with_system_newlines(text: &mut String, config: &Config) -> () {
     let style = if config.newline_style() == NewlineStyle::Native {
         if cfg!(windows) {
             NewlineStyle::Windows
@@ -1038,9 +1057,14 @@ fn test_format_inner<F>(formatter: F, input: &str, expected: &str) -> bool
     #[test]
     fn test_format_snippet() {
         let snippet = "fn main() { println!(\"hello, world\"); }";
+        #[cfg(not(windows))]
         let expected = "fn main() {\n    \
                         println!(\"hello, world\");\n\
                         }\n";
+        #[cfg(windows)]
+        let expected = "fn main() {\r\n    \
+                        println!(\"hello, world\");\r\n\
+                        }\r\n";
         assert!(test_format_inner(format_snippet, snippet, expected));
     }