Skip to main content

rustfmt_nightly/
lib.rs

1#![feature(rustc_private)]
2#![deny(rust_2018_idioms)]
3#![warn(unreachable_pub)]
4#![recursion_limit = "256"]
5#![allow(clippy::match_like_matches_macro)]
6
7// N.B. these crates are loaded from the sysroot, so they need extern crate.
8extern crate rustc_ast;
9extern crate rustc_ast_pretty;
10extern crate rustc_data_structures;
11extern crate rustc_errors;
12extern crate rustc_expand;
13extern crate rustc_feature;
14extern crate rustc_parse;
15extern crate rustc_session;
16extern crate rustc_span;
17extern crate thin_vec;
18
19// Necessary to pull in object code as the rest of the rustc crates are shipped only as rmeta
20// files.
21#[allow(unused_extern_crates)]
22extern crate rustc_driver;
23
24use std::cell::RefCell;
25use std::cmp::min;
26use std::collections::HashMap;
27use std::fmt;
28use std::io::{self, Write};
29use std::mem;
30use std::panic;
31use std::path::PathBuf;
32use std::rc::Rc;
33
34use rustc_ast::ast;
35use rustc_span::symbol;
36use thiserror::Error;
37
38use crate::comment::LineClasses;
39use crate::emitter::Emitter;
40use crate::formatting::{FormatErrorMap, FormattingError, ReportedErrors, SourceFile};
41use crate::modules::ModuleResolutionError;
42use crate::parse::parser::DirectoryOwnership;
43use crate::shape::Indent;
44use crate::utils::indent_next_line;
45
46pub use crate::config::{
47    CliOptions, Color, Config, Edition, EmitMode, FileLines, FileName, NewlineStyle, Range,
48    StyleEdition, Verbosity, Version, load_config,
49};
50
51pub use crate::format_report_formatter::{FormatReportFormatter, FormatReportFormatterBuilder};
52
53pub use crate::rustfmt_diff::{ModifiedChunk, ModifiedLines};
54
55#[macro_use]
56mod utils;
57
58macro_rules! static_regex {
59    ($re:literal) => {{
60        static RE: ::std::sync::OnceLock<::regex::Regex> = ::std::sync::OnceLock::new();
61        RE.get_or_init(|| ::regex::Regex::new($re).unwrap())
62    }};
63}
64
65mod attr;
66mod chains;
67mod closures;
68mod comment;
69pub(crate) mod config;
70mod coverage;
71mod emitter;
72mod expr;
73mod format_report_formatter;
74pub(crate) mod formatting;
75pub(crate) mod header;
76mod ignore_path;
77mod imports;
78mod items;
79mod lists;
80mod macros;
81mod matches;
82mod missed_spans;
83pub(crate) mod modules;
84mod overflow;
85mod pairs;
86mod parse;
87mod patterns;
88mod range;
89mod release_channel;
90mod reorder;
91mod rewrite;
92pub(crate) mod rustfmt_diff;
93mod shape;
94mod skip;
95mod sort;
96pub(crate) mod source_file;
97pub(crate) mod source_map;
98mod spanned;
99mod stmt;
100mod string;
101#[cfg(test)]
102mod test;
103mod types;
104mod vertical;
105pub(crate) mod visitor;
106
107/// The various errors that can occur during formatting. Note that not all of
108/// these can currently be propagated to clients.
109#[derive(Error, Debug)]
110pub enum ErrorKind {
111    /// Line has exceeded character limit (found, maximum).
112    #[error(
113        "line formatted, but exceeded maximum width \
114         (maximum: {1} (see `max_width` option), found: {0})"
115    )]
116    LineOverflow(usize, usize),
117    /// Line ends in whitespace.
118    #[error("left behind trailing whitespace")]
119    TrailingWhitespace,
120    /// Used deprecated skip attribute.
121    #[error("`rustfmt_skip` is deprecated; use `rustfmt::skip`")]
122    DeprecatedAttr,
123    /// Used a rustfmt:: attribute other than skip or skip::macros.
124    #[error("invalid attribute")]
125    BadAttr,
126    /// An io error during reading or writing.
127    #[error("io error: {0}")]
128    IoError(io::Error),
129    /// Error during module resolution.
130    #[error("{0}")]
131    ModuleResolutionError(#[from] ModuleResolutionError),
132    /// Parse error occurred when parsing the input.
133    #[error("parse error")]
134    ParseError,
135    /// The user mandated a version and the current version of Rustfmt does not
136    /// satisfy that requirement.
137    #[error("version mismatch")]
138    VersionMismatch,
139    /// If we had formatted the given node, then we would have lost a comment.
140    #[error("not formatted because a comment would be lost")]
141    LostComment,
142    /// Invalid glob pattern in `ignore` configuration option.
143    #[error("Invalid glob pattern found in ignore list: {0}")]
144    InvalidGlobPattern(ignore::Error),
145}
146
147impl ErrorKind {
148    fn is_comment(&self) -> bool {
149        matches!(self, ErrorKind::LostComment)
150    }
151}
152
153impl From<io::Error> for ErrorKind {
154    fn from(e: io::Error) -> ErrorKind {
155        ErrorKind::IoError(e)
156    }
157}
158
159/// Result of formatting a snippet of code along with ranges of lines that didn't get formatted,
160/// i.e., that got returned as they were originally.
161#[derive(Debug)]
162struct FormattedSnippet {
163    snippet: String,
164    non_formatted_ranges: Vec<(usize, usize)>,
165}
166
167impl FormattedSnippet {
168    /// In case the snippet needed to be wrapped in a function, this shifts down the ranges of
169    /// non-formatted code.
170    fn unwrap_code_block(&mut self) {
171        self.non_formatted_ranges
172            .iter_mut()
173            .for_each(|(low, high)| {
174                *low -= 1;
175                *high -= 1;
176            });
177    }
178
179    /// Returns `true` if the line n did not get formatted.
180    fn is_line_non_formatted(&self, n: usize) -> bool {
181        self.non_formatted_ranges
182            .iter()
183            .any(|(low, high)| *low <= n && n <= *high)
184    }
185}
186
187/// Reports on any issues that occurred during a run of Rustfmt.
188///
189/// Can be reported to the user using the `Display` impl on [`FormatReportFormatter`].
190#[derive(Clone)]
191pub struct FormatReport {
192    // Maps stringified file paths to their associated formatting errors.
193    internal: Rc<RefCell<(FormatErrorMap, ReportedErrors)>>,
194    non_formatted_ranges: Vec<(usize, usize)>,
195}
196
197impl FormatReport {
198    fn new() -> FormatReport {
199        FormatReport {
200            internal: Rc::new(RefCell::new((HashMap::new(), ReportedErrors::default()))),
201            non_formatted_ranges: Vec::new(),
202        }
203    }
204
205    fn add_non_formatted_ranges(&mut self, mut ranges: Vec<(usize, usize)>) {
206        self.non_formatted_ranges.append(&mut ranges);
207    }
208
209    fn append(&self, f: FileName, mut v: Vec<FormattingError>) {
210        self.track_errors(&v);
211        self.internal
212            .borrow_mut()
213            .0
214            .entry(f)
215            .and_modify(|fe| fe.append(&mut v))
216            .or_insert(v);
217    }
218
219    fn track_errors(&self, new_errors: &[FormattingError]) {
220        let errs = &mut self.internal.borrow_mut().1;
221        if !new_errors.is_empty() {
222            errs.has_formatting_errors = true;
223        }
224        if errs.has_operational_errors && errs.has_check_errors && errs.has_unformatted_code_errors
225        {
226            return;
227        }
228        for err in new_errors {
229            match err.kind {
230                ErrorKind::LineOverflow(..) => {
231                    errs.has_operational_errors = true;
232                }
233                ErrorKind::TrailingWhitespace => {
234                    errs.has_operational_errors = true;
235                    errs.has_unformatted_code_errors = true;
236                }
237                ErrorKind::LostComment => {
238                    errs.has_unformatted_code_errors = true;
239                }
240                ErrorKind::DeprecatedAttr | ErrorKind::BadAttr | ErrorKind::VersionMismatch => {
241                    errs.has_check_errors = true;
242                }
243                _ => {}
244            }
245        }
246    }
247
248    fn add_diff(&mut self) {
249        self.internal.borrow_mut().1.has_diff = true;
250    }
251
252    fn add_macro_format_failure(&mut self) {
253        self.internal.borrow_mut().1.has_macro_format_failure = true;
254    }
255
256    fn add_parsing_error(&mut self) {
257        self.internal.borrow_mut().1.has_parsing_errors = true;
258    }
259
260    fn warning_count(&self) -> usize {
261        self.internal
262            .borrow()
263            .0
264            .values()
265            .map(|errors| errors.len())
266            .sum()
267    }
268
269    /// Whether any warnings or errors are present in the report.
270    pub fn has_warnings(&self) -> bool {
271        self.internal.borrow().1.has_formatting_errors
272    }
273
274    /// Print the report to a terminal using colours and potentially other
275    /// fancy output.
276    #[deprecated(note = "Use FormatReportFormatter with colors enabled instead")]
277    pub fn fancy_print(
278        &self,
279        mut t: Box<dyn term::Terminal<Output = io::Stderr>>,
280    ) -> Result<(), term::Error> {
281        writeln!(
282            t,
283            "{}",
284            FormatReportFormatterBuilder::new(self)
285                .enable_colors(true)
286                .build()
287        )?;
288        Ok(())
289    }
290}
291
292/// Deprecated - Use FormatReportFormatter instead
293// https://github.com/rust-lang/rust/issues/78625
294// https://github.com/rust-lang/rust/issues/39935
295impl fmt::Display for FormatReport {
296    // Prints all the formatting errors.
297    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
298        write!(fmt, "{}", FormatReportFormatterBuilder::new(self).build())?;
299        Ok(())
300    }
301}
302
303/// Format the given snippet. The snippet is expected to be *complete* code.
304/// When we cannot parse the given snippet, this function returns `None`.
305fn format_snippet(snippet: &str, config: &Config, is_macro_def: bool) -> Option<FormattedSnippet> {
306    let mut config = config.clone();
307    panic::catch_unwind(|| {
308        let mut out: Vec<u8> = Vec::with_capacity(snippet.len() * 2);
309        config.set().emit_mode(config::EmitMode::Stdout);
310        config.set().verbose(Verbosity::Quiet);
311        config.set().show_parse_errors(false);
312        if is_macro_def {
313            config.set().error_on_unformatted(true);
314        }
315
316        let (formatting_error, result) = {
317            let input = Input::Text(snippet.into());
318            let mut session = Session::new(config, Some(&mut out));
319            let result = session.format_input_inner(input, is_macro_def);
320            (
321                session.errors.has_macro_format_failure
322                    || session.out.as_ref().unwrap().is_empty() && !snippet.is_empty()
323                    || result.is_err()
324                    || (is_macro_def && session.has_unformatted_code_errors()),
325                result,
326            )
327        };
328        if formatting_error {
329            None
330        } else {
331            String::from_utf8(out).ok().map(|snippet| FormattedSnippet {
332                snippet,
333                non_formatted_ranges: result.unwrap().non_formatted_ranges,
334            })
335        }
336    })
337    // Discard panics encountered while formatting the snippet
338    // The ? operator is needed to remove the extra Option
339    .ok()?
340}
341
342/// Format the given code block. Mainly targeted for code block in comment.
343/// The code block may be incomplete (i.e., parser may be unable to parse it).
344/// To avoid panic in parser, we wrap the code block with a dummy function.
345/// The returned code block does **not** end with newline.
346fn format_code_block(
347    code_snippet: &str,
348    config: &Config,
349    is_macro_def: bool,
350) -> Option<FormattedSnippet> {
351    const FN_MAIN_PREFIX: &str = "fn main() {\n";
352
353    fn enclose_in_main_block(s: &str, config: &Config) -> String {
354        let indent = Indent::from_width(config, config.tab_spaces());
355        let mut result = String::with_capacity(s.len() * 2);
356        result.push_str(FN_MAIN_PREFIX);
357        let mut need_indent = true;
358        for (kind, line) in LineClasses::new(s) {
359            if need_indent {
360                result.push_str(&indent.to_string(config));
361            }
362            result.push_str(&line);
363            result.push('\n');
364            need_indent = indent_next_line(kind, &line, config);
365        }
366        result.push('}');
367        result
368    }
369
370    // Wrap the given code block with `fn main()` if it does not have one.
371    let snippet = enclose_in_main_block(code_snippet, config);
372    let mut result = String::with_capacity(snippet.len());
373    let mut is_first = true;
374
375    // While formatting the code, ignore the config's newline style setting and always use "\n"
376    // instead of "\r\n" for the newline characters. This is ok because the output here is
377    // not directly outputted by rustfmt command, but used by the comment formatter's input.
378    // We have output-file-wide "\n" ==> "\r\n" conversion process after here if it's necessary.
379    let mut config_with_unix_newline = config.clone();
380    config_with_unix_newline
381        .set()
382        .newline_style(NewlineStyle::Unix);
383    let mut formatted = format_snippet(&snippet, &config_with_unix_newline, is_macro_def)?;
384    // Remove wrapping main block
385    formatted.unwrap_code_block();
386
387    // Trim "fn main() {" on the first line and "}" on the last line,
388    // then unindent the whole code block.
389    let block_len = formatted
390        .snippet
391        .rfind('}')
392        // trim whitespace when using `fn_single_line=true`
393        // ```
394        //                        | `block_len` starts at the index of `}`
395        // fn main { some_func(); }
396        //                      | remove whitespace to move it to the index of `;`
397        // ```
398        // aka, strip off any whitespace between the end of the code and the `}`
399        .map(|i| {
400            formatted.snippet[..i]
401                .strip_suffix(char::is_whitespace)
402                .map_or(i, str::len)
403        })
404        .unwrap_or_else(|| formatted.snippet.len());
405
406    // It's possible that `block_len < FN_MAIN_PREFIX.len()`. This can happen if the code block was
407    // formatted into the empty string, leading to the enclosing `fn main() {\n}` being formatted
408    // into `fn main() {}`. In this case no unindentation is done.
409    let block_start = min(FN_MAIN_PREFIX.len(), block_len);
410
411    let mut is_indented = true;
412    let indent_str = Indent::from_width(config, config.tab_spaces()).to_string(config);
413    for (kind, ref line) in LineClasses::new(&formatted.snippet[block_start..block_len]) {
414        if !is_first {
415            result.push('\n');
416        } else {
417            is_first = false;
418        }
419        let trimmed_line = if !is_indented {
420            line
421        } else if line.len() > config.max_width() {
422            // If there are lines that are larger than max width, we cannot tell
423            // whether we have succeeded but have some comments or strings that
424            // are too long, or we have failed to format code block. We will be
425            // conservative and just return `None` in this case.
426            return None;
427        } else if line.len() > indent_str.len() {
428            // Make sure that the line has leading whitespaces.
429            if line.starts_with(indent_str.as_ref()) {
430                let offset = if config.hard_tabs() {
431                    1
432                } else {
433                    config.tab_spaces()
434                };
435                &line[offset..]
436            } else {
437                line
438            }
439        } else {
440            line
441        };
442        result.push_str(trimmed_line);
443        is_indented = indent_next_line(kind, line, config);
444    }
445    Some(FormattedSnippet {
446        snippet: result,
447        non_formatted_ranges: formatted.non_formatted_ranges,
448    })
449}
450
451/// A session is a run of rustfmt across a single or multiple inputs.
452pub struct Session<'b, T: Write> {
453    pub config: Config,
454    pub out: Option<&'b mut T>,
455    pub(crate) errors: ReportedErrors,
456    source_file: SourceFile,
457    emitter: Box<dyn Emitter + 'b>,
458}
459
460impl<'b, T: Write + 'b> Session<'b, T> {
461    pub fn new(config: Config, mut out: Option<&'b mut T>) -> Session<'b, T> {
462        let emitter = create_emitter(&config);
463
464        if let Some(ref mut out) = out {
465            let _ = emitter.emit_header(out);
466        }
467
468        Session {
469            config,
470            out,
471            emitter,
472            errors: ReportedErrors::default(),
473            source_file: SourceFile::new(),
474        }
475    }
476
477    /// The main entry point for Rustfmt. Formats the given input according to the
478    /// session's config.
479    pub fn format(&mut self, input: Input) -> Result<FormatReport, ErrorKind> {
480        self.format_input_inner(input, false)
481    }
482
483    pub fn override_config<F, U>(&mut self, mut config: Config, f: F) -> U
484    where
485        F: FnOnce(&mut Session<'b, T>) -> U,
486    {
487        mem::swap(&mut config, &mut self.config);
488        let result = f(self);
489        mem::swap(&mut config, &mut self.config);
490        result
491    }
492
493    pub fn add_operational_error(&mut self) {
494        self.errors.has_operational_errors = true;
495    }
496
497    pub fn has_operational_errors(&self) -> bool {
498        self.errors.has_operational_errors
499    }
500
501    pub fn has_parsing_errors(&self) -> bool {
502        self.errors.has_parsing_errors
503    }
504
505    pub fn has_formatting_errors(&self) -> bool {
506        self.errors.has_formatting_errors
507    }
508
509    pub fn has_check_errors(&self) -> bool {
510        self.errors.has_check_errors
511    }
512
513    pub fn has_diff(&self) -> bool {
514        self.errors.has_diff
515    }
516
517    pub fn has_unformatted_code_errors(&self) -> bool {
518        self.errors.has_unformatted_code_errors
519    }
520
521    pub fn has_no_errors(&self) -> bool {
522        !(self.has_operational_errors()
523            || self.has_parsing_errors()
524            || self.has_formatting_errors()
525            || self.has_check_errors()
526            || self.has_diff()
527            || self.has_unformatted_code_errors()
528            || self.errors.has_macro_format_failure)
529    }
530}
531
532pub(crate) fn create_emitter<'a>(config: &Config) -> Box<dyn Emitter + 'a> {
533    match config.emit_mode() {
534        EmitMode::Files if config.make_backup() => {
535            Box::new(emitter::FilesWithBackupEmitter::default())
536        }
537        EmitMode::Files => Box::new(emitter::FilesEmitter::new(
538            config.print_misformatted_file_names(),
539        )),
540        EmitMode::Stdout | EmitMode::Coverage => {
541            Box::new(emitter::StdoutEmitter::new(config.verbose()))
542        }
543        EmitMode::Json => Box::new(emitter::JsonEmitter::default()),
544        EmitMode::ModifiedLines => Box::new(emitter::ModifiedLinesEmitter::default()),
545        EmitMode::Checkstyle => Box::new(emitter::CheckstyleEmitter::default()),
546        EmitMode::Diff => Box::new(emitter::DiffEmitter::new(config.clone())),
547    }
548}
549
550impl<'b, T: Write + 'b> Drop for Session<'b, T> {
551    fn drop(&mut self) {
552        if let Some(ref mut out) = self.out {
553            let _ = self.emitter.emit_footer(out);
554        }
555    }
556}
557
558#[derive(Debug)]
559pub enum Input {
560    File(PathBuf),
561    Text(String),
562}
563
564impl Input {
565    fn file_name(&self) -> FileName {
566        match *self {
567            Input::File(ref file) => FileName::Real(file.clone()),
568            Input::Text(..) => FileName::Stdin,
569        }
570    }
571
572    fn to_directory_ownership(&self) -> Option<DirectoryOwnership> {
573        match self {
574            Input::File(ref file) => {
575                // If there exists a directory with the same name as an input,
576                // then the input should be parsed as a sub module.
577                let file_stem = file.file_stem()?;
578                if file.parent()?.to_path_buf().join(file_stem).is_dir() {
579                    Some(DirectoryOwnership::Owned {
580                        relative: file_stem.to_str().map(symbol::Ident::from_str),
581                    })
582                } else {
583                    None
584                }
585            }
586            _ => None,
587        }
588    }
589}
590
591#[cfg(test)]
592mod unit_tests {
593    use super::*;
594
595    #[test]
596    fn test_no_panic_on_format_snippet_and_format_code_block() {
597        // `format_snippet()` and `format_code_block()` should not panic
598        // even when we cannot parse the given snippet.
599        let snippet = "let";
600        assert!(format_snippet(snippet, &Config::default(), false).is_none());
601        assert!(format_code_block(snippet, &Config::default(), false).is_none());
602    }
603
604    fn test_format_inner<F>(formatter: F, input: &str, expected: &str) -> bool
605    where
606        F: Fn(&str, &Config, bool) -> Option<FormattedSnippet>,
607    {
608        let output = formatter(input, &Config::default(), false);
609        output.is_some() && output.unwrap().snippet == expected
610    }
611
612    #[test]
613    fn test_format_snippet() {
614        let snippet = "fn main() { println!(\"hello, world\"); }";
615        #[cfg(not(windows))]
616        let expected = "fn main() {\n    \
617                        println!(\"hello, world\");\n\
618                        }\n";
619        #[cfg(windows)]
620        let expected = "fn main() {\r\n    \
621                        println!(\"hello, world\");\r\n\
622                        }\r\n";
623        assert!(test_format_inner(format_snippet, snippet, expected));
624    }
625
626    #[test]
627    fn test_format_code_block_fail() {
628        #[rustfmt::skip]
629        let code_block = "this_line_is_100_characters_long_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(x, y, z);";
630        assert!(format_code_block(code_block, &Config::default(), false).is_none());
631    }
632
633    #[test]
634    fn test_format_code_block() {
635        // simple code block
636        let code_block = "let x=3;";
637        let expected = "let x = 3;";
638        assert!(test_format_inner(format_code_block, code_block, expected));
639
640        // more complex code block, taken from chains.rs.
641        let code_block =
642"let (nested_shape, extend) = if !parent_rewrite_contains_newline && is_continuable(&parent) {
643(
644chain_indent(context, shape.add_offset(parent_rewrite.len())),
645context.config.indent_style() == IndentStyle::Visual || is_small_parent,
646)
647} else if is_block_expr(context, &parent, &parent_rewrite) {
648match context.config.indent_style() {
649// Try to put the first child on the same line with parent's last line
650IndentStyle::Block => (parent_shape.block_indent(context.config.tab_spaces()), true),
651// The parent is a block, so align the rest of the chain with the closing
652// brace.
653IndentStyle::Visual => (parent_shape, false),
654}
655} else {
656(
657chain_indent(context, shape.add_offset(parent_rewrite.len())),
658false,
659)
660};
661";
662        let expected =
663"let (nested_shape, extend) = if !parent_rewrite_contains_newline && is_continuable(&parent) {
664    (
665        chain_indent(context, shape.add_offset(parent_rewrite.len())),
666        context.config.indent_style() == IndentStyle::Visual || is_small_parent,
667    )
668} else if is_block_expr(context, &parent, &parent_rewrite) {
669    match context.config.indent_style() {
670        // Try to put the first child on the same line with parent's last line
671        IndentStyle::Block => (parent_shape.block_indent(context.config.tab_spaces()), true),
672        // The parent is a block, so align the rest of the chain with the closing
673        // brace.
674        IndentStyle::Visual => (parent_shape, false),
675    }
676} else {
677    (
678        chain_indent(context, shape.add_offset(parent_rewrite.len())),
679        false,
680    )
681};";
682        assert!(test_format_inner(format_code_block, code_block, expected));
683    }
684}