Skip to main content

rustfmt_nightly/
visitor.rs

1use std::cell::{Cell, RefCell};
2use std::rc::Rc;
3use std::sync::Arc;
4
5use rustc_ast::{ast, token::Delimiter, visit};
6use rustc_span::{BytePos, Ident, Pos, Span, symbol};
7use tracing::debug;
8
9use crate::attr::*;
10use crate::comment::{
11    CodeCharKind, CommentCodeSlices, contains_comment, recover_comment_removed, rewrite_comment,
12};
13use crate::config::{BraceStyle, Config, MacroSelector, StyleEdition};
14use crate::coverage::transform_missing_snippet;
15use crate::items::{
16    FnBraceStyle, FnSig, ItemVisitorKind, StaticParts, StructParts, format_impl, format_trait,
17    format_trait_alias, is_mod_decl, is_use_item, rewrite_extern_crate, rewrite_type_alias,
18};
19use crate::macros::{MacroPosition, macro_style, rewrite_macro, rewrite_macro_def};
20use crate::modules::Module;
21use crate::parse::session::ParseSess;
22use crate::rewrite::{Rewrite, RewriteContext};
23use crate::shape::{Indent, Shape};
24use crate::skip::{SkipContext, is_skip_attr};
25use crate::source_map::{LineRangeUtils, SpanUtils};
26use crate::spanned::Spanned;
27use crate::stmt::Stmt;
28use crate::utils::{
29    self, contains_skip, count_newlines, depr_skip_annotation, format_safety, inner_attributes,
30    last_line_width, mk_sp, ptr_vec_to_ref_vec, rewrite_ident, starts_with_newline, stmt_expr,
31};
32use crate::{ErrorKind, FormatReport, FormattingError};
33
34/// Creates a string slice corresponding to the specified span.
35pub(crate) struct SnippetProvider {
36    /// A pointer to the content of the file we are formatting.
37    big_snippet: Arc<String>,
38    /// A position of the start of `big_snippet`, used as an offset.
39    start_pos: usize,
40    /// An end position of the file that this snippet lives.
41    end_pos: usize,
42}
43
44impl SnippetProvider {
45    pub(crate) fn span_to_snippet(&self, span: Span) -> Option<&str> {
46        let start_index = span.lo().to_usize().checked_sub(self.start_pos)?;
47        let end_index = span.hi().to_usize().checked_sub(self.start_pos)?;
48        Some(&self.big_snippet[start_index..end_index])
49    }
50
51    pub(crate) fn new(start_pos: BytePos, end_pos: BytePos, big_snippet: Arc<String>) -> Self {
52        let start_pos = start_pos.to_usize();
53        let end_pos = end_pos.to_usize();
54        SnippetProvider {
55            big_snippet,
56            start_pos,
57            end_pos,
58        }
59    }
60
61    pub(crate) fn entire_snippet(&self) -> &str {
62        self.big_snippet.as_str()
63    }
64
65    pub(crate) fn start_pos(&self) -> BytePos {
66        BytePos::from_usize(self.start_pos)
67    }
68
69    pub(crate) fn end_pos(&self) -> BytePos {
70        BytePos::from_usize(self.end_pos)
71    }
72}
73
74pub(crate) struct FmtVisitor<'a> {
75    parent_context: Option<&'a RewriteContext<'a>>,
76    pub(crate) psess: &'a ParseSess,
77    pub(crate) buffer: String,
78    pub(crate) last_pos: BytePos,
79    // FIXME: use an RAII util or closure for indenting
80    pub(crate) block_indent: Indent,
81    pub(crate) config: &'a Config,
82    pub(crate) is_if_else_block: bool,
83    pub(crate) snippet_provider: &'a SnippetProvider,
84    pub(crate) line_number: usize,
85    /// List of 1-based line ranges which were annotated with skip
86    /// Both bounds are inclusive.
87    pub(crate) skipped_range: Rc<RefCell<Vec<(usize, usize)>>>,
88    pub(crate) macro_rewrite_failure: bool,
89    pub(crate) report: FormatReport,
90    pub(crate) skip_context: SkipContext,
91    pub(crate) is_macro_def: bool,
92}
93
94impl<'a> Drop for FmtVisitor<'a> {
95    fn drop(&mut self) {
96        if let Some(ctx) = self.parent_context {
97            if self.macro_rewrite_failure {
98                ctx.macro_rewrite_failure.replace(true);
99            }
100        }
101    }
102}
103
104impl<'b, 'a: 'b> FmtVisitor<'a> {
105    fn set_parent_context(&mut self, context: &'a RewriteContext<'_>) {
106        self.parent_context = Some(context);
107    }
108
109    pub(crate) fn shape(&self) -> Shape {
110        Shape::indented(self.block_indent, self.config)
111    }
112
113    fn next_span(&self, hi: BytePos) -> Span {
114        mk_sp(self.last_pos, hi)
115    }
116
117    fn visit_stmt(&mut self, stmt: &Stmt<'_>, include_empty_semi: bool) {
118        debug!("visit_stmt: {}", self.psess.span_to_debug_info(stmt.span()));
119
120        if stmt.is_empty() {
121            // If the statement is empty, just skip over it. Before that, make sure any comment
122            // snippet preceding the semicolon is picked up.
123            let snippet = self.snippet(mk_sp(self.last_pos, stmt.span().lo()));
124            let original_starts_with_newline = snippet
125                .find(|c| c != ' ')
126                .map_or(false, |i| starts_with_newline(&snippet[i..]));
127            let snippet = snippet.trim();
128            if !snippet.is_empty() {
129                // FIXME(calebcartwright 2021-01-03) - This exists strictly to maintain legacy
130                // formatting where rustfmt would preserve redundant semicolons on Items in a
131                // statement position.
132                // See comment within `walk_stmts` for more info
133                if include_empty_semi {
134                    self.format_missing(stmt.span().hi());
135                } else {
136                    if original_starts_with_newline {
137                        self.push_str("\n");
138                    }
139
140                    self.push_str(&self.block_indent.to_string(self.config));
141                    self.push_str(snippet);
142                }
143            } else if include_empty_semi {
144                self.push_str(";");
145            }
146            self.last_pos = stmt.span().hi();
147            return;
148        }
149
150        match stmt.as_ast_node().kind {
151            ast::StmtKind::Item(ref item) => {
152                self.visit_item(item);
153                self.last_pos = stmt.span().hi();
154            }
155            ast::StmtKind::Let(..) | ast::StmtKind::Expr(..) | ast::StmtKind::Semi(..) => {
156                let attrs = get_attrs_from_stmt(stmt.as_ast_node());
157                if contains_skip(attrs) {
158                    self.push_skipped_with_span(
159                        attrs,
160                        stmt.span(),
161                        get_span_without_attrs(stmt.as_ast_node()),
162                    );
163                } else {
164                    let shape = self.shape();
165                    let rewrite = self.with_context(|ctx| stmt.rewrite(ctx, shape));
166                    self.push_rewrite(stmt.span(), rewrite)
167                }
168            }
169            ast::StmtKind::MacCall(ref mac_stmt) => {
170                if self.visit_attrs(&mac_stmt.attrs, ast::AttrStyle::Outer) {
171                    self.push_skipped_with_span(
172                        &mac_stmt.attrs,
173                        stmt.span(),
174                        get_span_without_attrs(stmt.as_ast_node()),
175                    );
176                } else {
177                    self.visit_mac(&mac_stmt.mac, MacroPosition::Statement);
178                }
179                self.format_missing(stmt.span().hi());
180            }
181            ast::StmtKind::Empty => (),
182        }
183    }
184
185    /// Remove spaces between the opening brace and the first statement or the inner attribute
186    /// of the block.
187    fn trim_spaces_after_opening_brace(
188        &mut self,
189        b: &ast::Block,
190        inner_attrs: Option<&[ast::Attribute]>,
191    ) {
192        if let Some(first_stmt) = b.stmts.first() {
193            let hi = inner_attrs
194                .and_then(|attrs| inner_attributes(attrs).first().map(|attr| attr.span.lo()))
195                .unwrap_or_else(|| first_stmt.span().lo());
196            let missing_span = self.next_span(hi);
197            let snippet = self.snippet(missing_span);
198            let len = CommentCodeSlices::new(snippet)
199                .next()
200                .and_then(|(kind, _, s)| {
201                    if kind == CodeCharKind::Normal {
202                        s.rfind('\n')
203                    } else {
204                        None
205                    }
206                });
207            if let Some(len) = len {
208                self.last_pos = self.last_pos + BytePos::from_usize(len);
209            }
210        }
211    }
212
213    pub(crate) fn visit_block(
214        &mut self,
215        b: &ast::Block,
216        inner_attrs: Option<&[ast::Attribute]>,
217        has_braces: bool,
218    ) {
219        debug!("visit_block: {}", self.psess.span_to_debug_info(b.span));
220
221        // Check if this block has braces.
222        let brace_compensation = BytePos(if has_braces { 1 } else { 0 });
223
224        self.last_pos = self.last_pos + brace_compensation;
225        self.block_indent = self.block_indent.block_indent(self.config);
226        self.push_str("{");
227        self.trim_spaces_after_opening_brace(b, inner_attrs);
228
229        // Format inner attributes if available.
230        if let Some(attrs) = inner_attrs {
231            self.visit_attrs(attrs, ast::AttrStyle::Inner);
232        }
233
234        self.walk_block_stmts(b);
235
236        if !b.stmts.is_empty() {
237            if let Some(expr) = stmt_expr(&b.stmts[b.stmts.len() - 1]) {
238                if utils::semicolon_for_expr(&self.get_context(), expr) {
239                    self.push_str(";");
240                }
241            }
242        }
243
244        let rest_span = self.next_span(b.span.hi());
245        if out_of_file_lines_range!(self, rest_span) {
246            self.push_str(self.snippet(rest_span));
247            self.block_indent = self.block_indent.block_unindent(self.config);
248        } else {
249            // Ignore the closing brace.
250            let missing_span = self.next_span(b.span.hi() - brace_compensation);
251            self.close_block(missing_span, self.unindent_comment_on_closing_brace(b));
252        }
253        self.last_pos = source!(self, b.span).hi();
254    }
255
256    fn close_block(&mut self, span: Span, unindent_comment: bool) {
257        let config = self.config;
258
259        let mut last_hi = span.lo();
260        let mut unindented = false;
261        let mut prev_ends_with_newline = false;
262        let mut extra_newline = false;
263
264        let skip_normal = |s: &str| {
265            let trimmed = s.trim();
266            trimmed.is_empty() || trimmed.chars().all(|c| c == ';')
267        };
268
269        let comment_snippet = self.snippet(span);
270
271        let align_to_right = if unindent_comment && contains_comment(comment_snippet) {
272            let first_lines = comment_snippet.splitn(2, '/').next().unwrap_or("");
273            last_line_width(first_lines) > last_line_width(comment_snippet)
274        } else {
275            false
276        };
277
278        for (kind, offset, sub_slice) in CommentCodeSlices::new(comment_snippet) {
279            let sub_slice = transform_missing_snippet(config, sub_slice);
280
281            debug!("close_block: {:?} {:?} {:?}", kind, offset, sub_slice);
282
283            match kind {
284                CodeCharKind::Comment => {
285                    if !unindented && unindent_comment && !align_to_right {
286                        unindented = true;
287                        self.block_indent = self.block_indent.block_unindent(config);
288                    }
289                    let span_in_between = mk_sp(last_hi, span.lo() + BytePos::from_usize(offset));
290                    let snippet_in_between = self.snippet(span_in_between);
291                    let mut comment_on_same_line = !snippet_in_between.contains('\n');
292
293                    let mut comment_shape =
294                        Shape::indented(self.block_indent, config).comment(config);
295                    if self.config.style_edition() >= StyleEdition::Edition2024
296                        && comment_on_same_line
297                    {
298                        self.push_str(" ");
299                        // put the first line of the comment on the same line as the
300                        // block's last line
301                        match sub_slice.find('\n') {
302                            None => {
303                                self.push_str(&sub_slice);
304                            }
305                            Some(offset) if offset + 1 == sub_slice.len() => {
306                                self.push_str(&sub_slice[..offset]);
307                            }
308                            Some(offset) => {
309                                let first_line = &sub_slice[..offset];
310                                self.push_str(first_line);
311                                self.push_str(&self.block_indent.to_string_with_newline(config));
312
313                                // put the other lines below it, shaping it as needed
314                                let other_lines = &sub_slice[offset + 1..];
315                                let comment_str =
316                                    rewrite_comment(other_lines, false, comment_shape, config);
317                                match comment_str {
318                                    Ok(ref s) => self.push_str(s),
319                                    Err(_) => self.push_str(other_lines),
320                                }
321                            }
322                        }
323                    } else {
324                        if comment_on_same_line {
325                            // 1 = a space before `//`
326                            let offset_len = 1 + last_line_width(&self.buffer)
327                                .saturating_sub(self.block_indent.width());
328                            match comment_shape
329                                .visual_indent(offset_len)
330                                .sub_width(offset_len)
331                            {
332                                Some(shp) => comment_shape = shp,
333                                None => comment_on_same_line = false,
334                            }
335                        };
336
337                        if comment_on_same_line {
338                            self.push_str(" ");
339                        } else {
340                            if count_newlines(snippet_in_between) >= 2 || extra_newline {
341                                self.push_str("\n");
342                            }
343                            self.push_str(&self.block_indent.to_string_with_newline(config));
344                        }
345
346                        let comment_str = rewrite_comment(&sub_slice, false, comment_shape, config);
347                        match comment_str {
348                            Ok(ref s) => self.push_str(s),
349                            Err(_) => self.push_str(&sub_slice),
350                        }
351                    }
352                }
353                CodeCharKind::Normal if skip_normal(&sub_slice) => {
354                    extra_newline = prev_ends_with_newline && sub_slice.contains('\n');
355                    continue;
356                }
357                CodeCharKind::Normal => {
358                    self.push_str(&self.block_indent.to_string_with_newline(config));
359                    self.push_str(sub_slice.trim());
360                }
361            }
362            prev_ends_with_newline = sub_slice.ends_with('\n');
363            extra_newline = false;
364            last_hi = span.lo() + BytePos::from_usize(offset + sub_slice.len());
365        }
366        if unindented {
367            self.block_indent = self.block_indent.block_indent(self.config);
368        }
369        self.block_indent = self.block_indent.block_unindent(self.config);
370        self.push_str(&self.block_indent.to_string_with_newline(config));
371        self.push_str("}");
372    }
373
374    fn unindent_comment_on_closing_brace(&self, b: &ast::Block) -> bool {
375        self.is_if_else_block && !b.stmts.is_empty()
376    }
377
378    // Note that this only gets called for function definitions. Required methods
379    // on traits do not get handled here.
380    pub(crate) fn visit_fn(
381        &mut self,
382        ident: Ident,
383        fk: visit::FnKind<'_>,
384        fd: &ast::FnDecl,
385        s: Span,
386        defaultness: ast::Defaultness,
387        inner_attrs: Option<&[ast::Attribute]>,
388    ) {
389        let indent = self.block_indent;
390        let block;
391        let rewrite = match fk {
392            visit::FnKind::Fn(
393                _,
394                _,
395                ast::Fn {
396                    body: Some(ref b), ..
397                },
398            ) => {
399                block = b;
400                self.rewrite_fn_before_block(
401                    indent,
402                    ident,
403                    &FnSig::from_fn_kind(&fk, fd, defaultness),
404                    mk_sp(s.lo(), b.span.lo()),
405                )
406            }
407            _ => unreachable!(),
408        };
409
410        if let Some((fn_str, fn_brace_style)) = rewrite {
411            self.format_missing_with_indent(source!(self, s).lo());
412
413            if let Some(rw) = self.single_line_fn(&fn_str, block, inner_attrs) {
414                self.push_str(&rw);
415                self.last_pos = s.hi();
416                return;
417            }
418
419            self.push_str(&fn_str);
420            match fn_brace_style {
421                FnBraceStyle::SameLine => self.push_str(" "),
422                FnBraceStyle::NextLine => {
423                    self.push_str(&self.block_indent.to_string_with_newline(self.config))
424                }
425                _ => unreachable!(),
426            }
427            self.last_pos = source!(self, block.span).lo();
428        } else {
429            self.format_missing(source!(self, block.span).lo());
430        }
431
432        self.visit_block(block, inner_attrs, true)
433    }
434
435    pub(crate) fn visit_item(&mut self, item: &ast::Item) {
436        skip_out_of_file_lines_range_visitor!(self, item.span);
437
438        // This is where we bail out if there is a skip attribute. This is only
439        // complex in the module case. It is complex because the module could be
440        // in a separate file and there might be attributes in both files, but
441        // the AST lumps them all together.
442        let filtered_attrs;
443        let mut attrs = &item.attrs;
444        let skip_context_saved = self.skip_context.clone();
445        self.skip_context.update_with_attrs(attrs);
446
447        let should_visit_node_again = match item.kind {
448            // For use/extern crate items, skip rewriting attributes but check for a skip attribute.
449            ast::ItemKind::Use(..) | ast::ItemKind::ExternCrate(..) => {
450                if contains_skip(attrs) {
451                    self.push_skipped_with_span(attrs.as_slice(), item.span(), item.span());
452                    false
453                } else {
454                    true
455                }
456            }
457            // Module is inline, in this case we treat it like any other item.
458            _ if !is_mod_decl(item) => {
459                if self.visit_attrs(&item.attrs, ast::AttrStyle::Outer) {
460                    self.push_skipped_with_span(item.attrs.as_slice(), item.span(), item.span());
461                    false
462                } else {
463                    true
464                }
465            }
466            // Module is not inline, but should be skipped.
467            ast::ItemKind::Mod(..) if contains_skip(&item.attrs) => false,
468            // Module is not inline and should not be skipped. We want
469            // to process only the attributes in the current file.
470            ast::ItemKind::Mod(..) => {
471                filtered_attrs = filter_inline_attrs(&item.attrs, item.span());
472                // Assert because if we should skip it should be caught by
473                // the above case.
474                assert!(!self.visit_attrs(&filtered_attrs, ast::AttrStyle::Outer));
475                attrs = &filtered_attrs;
476                true
477            }
478            _ => {
479                if self.visit_attrs(&item.attrs, ast::AttrStyle::Outer) {
480                    self.push_skipped_with_span(item.attrs.as_slice(), item.span(), item.span());
481                    false
482                } else {
483                    true
484                }
485            }
486        };
487
488        // TODO(calebcartwright): consider enabling box_patterns feature gate
489        if should_visit_node_again {
490            match item.kind {
491                ast::ItemKind::Use(ref tree) => self.format_import(item, tree),
492                ast::ItemKind::Impl(ref iimpl) => {
493                    let block_indent = self.block_indent;
494                    let rw = self.with_context(|ctx| format_impl(ctx, item, iimpl, block_indent));
495                    self.push_rewrite(item.span, rw);
496                }
497                ast::ItemKind::Trait(..) => {
498                    let block_indent = self.block_indent;
499                    let rw = self.with_context(|ctx| format_trait(ctx, item, block_indent));
500                    self.push_rewrite(item.span, rw);
501                }
502                ast::ItemKind::TraitAlias(ref ta) => {
503                    let shape = Shape::indented(self.block_indent, self.config);
504                    let rw = format_trait_alias(&self.get_context(), ta, &item.vis, shape);
505                    self.push_rewrite(item.span, rw);
506                }
507                ast::ItemKind::ExternCrate(..) => {
508                    let rw = rewrite_extern_crate(&self.get_context(), item, self.shape());
509                    let span = if attrs.is_empty() {
510                        item.span
511                    } else {
512                        mk_sp(attrs[0].span.lo(), item.span.hi())
513                    };
514                    self.push_rewrite(span, rw);
515                }
516                ast::ItemKind::Struct(..) | ast::ItemKind::Union(..) => {
517                    self.visit_struct(&StructParts::from_item(item));
518                }
519                ast::ItemKind::Enum(ident, ref generics, ref def) => {
520                    self.format_missing_with_indent(source!(self, item.span).lo());
521                    self.visit_enum(ident, &item.vis, def, generics, item.span);
522                    self.last_pos = source!(self, item.span).hi();
523                }
524                ast::ItemKind::Mod(safety, ident, ref mod_kind) => {
525                    self.format_missing_with_indent(source!(self, item.span).lo());
526                    self.format_mod(mod_kind, safety, &item.vis, item.span, ident, attrs);
527                }
528                ast::ItemKind::MacCall(ref mac) => {
529                    self.visit_mac(mac, MacroPosition::Item);
530                }
531                ast::ItemKind::ForeignMod(ref foreign_mod) => {
532                    self.format_missing_with_indent(source!(self, item.span).lo());
533                    self.format_foreign_mod(foreign_mod, item.span);
534                }
535                ast::ItemKind::Static(..) | ast::ItemKind::Const(..) => {
536                    self.visit_static(&StaticParts::from_item(item));
537                }
538                ast::ItemKind::ConstBlock(ast::ConstBlockItem {
539                    id: _,
540                    span,
541                    ref block,
542                }) => {
543                    let context = &self.get_context();
544                    let offset = self.block_indent;
545                    self.push_rewrite(
546                        item.span,
547                        block
548                            .rewrite(
549                                context,
550                                Shape::legacy(
551                                    context.budget(offset.block_indent),
552                                    offset.block_only(),
553                                ),
554                            )
555                            .map(|rhs| {
556                                recover_comment_removed(format!("const {rhs}"), span, context)
557                            }),
558                    );
559                }
560                ast::ItemKind::Fn(ref fn_kind) => {
561                    let ast::Fn {
562                        defaultness,
563                        ref sig,
564                        ident,
565                        ref generics,
566                        ref body,
567                        ..
568                    } = **fn_kind;
569                    if body.is_some() {
570                        let inner_attrs = inner_attributes(&item.attrs);
571                        let fn_ctxt = match sig.header.ext {
572                            ast::Extern::None => visit::FnCtxt::Free,
573                            _ => visit::FnCtxt::Foreign,
574                        };
575                        self.visit_fn(
576                            ident,
577                            visit::FnKind::Fn(fn_ctxt, &item.vis, fn_kind),
578                            &sig.decl,
579                            item.span,
580                            defaultness,
581                            Some(&inner_attrs),
582                        )
583                    } else {
584                        let indent = self.block_indent;
585                        let rewrite = self
586                            .rewrite_required_fn(indent, ident, sig, &item.vis, generics, item.span)
587                            .ok();
588                        self.push_rewrite(item.span, rewrite);
589                    }
590                }
591                ast::ItemKind::TyAlias(ref ty_alias) => {
592                    use ItemVisitorKind::Item;
593                    self.visit_ty_alias_kind(ty_alias, &item.vis, Item, item.span);
594                }
595                ast::ItemKind::GlobalAsm(..) => {
596                    let snippet = Some(self.snippet(item.span).to_owned());
597                    self.push_rewrite(item.span, snippet);
598                }
599                ast::ItemKind::MacroDef(ident, ref def) => {
600                    let rewrite = rewrite_macro_def(
601                        &self.get_context(),
602                        self.shape(),
603                        self.block_indent,
604                        def,
605                        ident,
606                        &item.vis,
607                        item.span,
608                    )
609                    .ok();
610                    self.push_rewrite(item.span, rewrite);
611                }
612                ast::ItemKind::Delegation(..) | ast::ItemKind::DelegationMac(..) => {
613                    // TODO: rewrite delegation items once syntax is established.
614                    // For now, leave the contents of the Span unformatted.
615                    self.push_rewrite(item.span, None)
616                }
617            };
618        }
619        self.skip_context = skip_context_saved;
620    }
621
622    fn visit_ty_alias_kind(
623        &mut self,
624        ty_kind: &ast::TyAlias,
625        vis: &ast::Visibility,
626        visitor_kind: ItemVisitorKind,
627        span: Span,
628    ) {
629        let rewrite = rewrite_type_alias(
630            ty_kind,
631            vis,
632            &self.get_context(),
633            self.block_indent,
634            visitor_kind,
635            span,
636        )
637        .ok();
638        self.push_rewrite(span, rewrite);
639    }
640
641    fn visit_assoc_item(&mut self, ai: &ast::AssocItem, visitor_kind: ItemVisitorKind) {
642        use ItemVisitorKind::*;
643        let assoc_ctxt = match visitor_kind {
644            AssocTraitItem => visit::AssocCtxt::Trait,
645            // There is no difference between trait and inherent assoc item formatting
646            AssocImplItem => visit::AssocCtxt::Impl { of_trait: false },
647            _ => unreachable!(),
648        };
649        // TODO(calebcartwright): Not sure the skip spans are correct
650        let skip_span = ai.span;
651        skip_out_of_file_lines_range_visitor!(self, ai.span);
652
653        if self.visit_attrs(&ai.attrs, ast::AttrStyle::Outer) {
654            self.push_skipped_with_span(ai.attrs.as_slice(), skip_span, skip_span);
655            return;
656        }
657
658        // TODO(calebcartwright): consider enabling box_patterns feature gate
659        match (&ai.kind, visitor_kind) {
660            (ast::AssocItemKind::Const(c), AssocTraitItem) => {
661                self.visit_static(&StaticParts::from_trait_item(ai, c.ident))
662            }
663            (ast::AssocItemKind::Const(c), AssocImplItem) => {
664                self.visit_static(&StaticParts::from_impl_item(ai, c.ident))
665            }
666            (ast::AssocItemKind::Fn(ref fn_kind), _) => {
667                let ast::Fn {
668                    defaultness,
669                    ref sig,
670                    ident,
671                    ref generics,
672                    ref body,
673                    ..
674                } = **fn_kind;
675                if body.is_some() {
676                    let inner_attrs = inner_attributes(&ai.attrs);
677                    let fn_ctxt = visit::FnCtxt::Assoc(assoc_ctxt);
678                    self.visit_fn(
679                        ident,
680                        visit::FnKind::Fn(fn_ctxt, &ai.vis, fn_kind),
681                        &sig.decl,
682                        ai.span,
683                        defaultness,
684                        Some(&inner_attrs),
685                    );
686                } else {
687                    let indent = self.block_indent;
688                    let rewrite = self
689                        .rewrite_required_fn(indent, fn_kind.ident, sig, &ai.vis, generics, ai.span)
690                        .ok();
691                    self.push_rewrite(ai.span, rewrite);
692                }
693            }
694            (ast::AssocItemKind::Type(ref ty_alias), _) => {
695                self.visit_ty_alias_kind(ty_alias, &ai.vis, visitor_kind, ai.span);
696            }
697            (ast::AssocItemKind::MacCall(ref mac), _) => {
698                self.visit_mac(mac, MacroPosition::Item);
699            }
700            _ => unreachable!(),
701        }
702    }
703
704    pub(crate) fn visit_trait_item(&mut self, ti: &ast::AssocItem) {
705        self.visit_assoc_item(ti, ItemVisitorKind::AssocTraitItem);
706    }
707
708    pub(crate) fn visit_impl_item(&mut self, ii: &ast::AssocItem) {
709        self.visit_assoc_item(ii, ItemVisitorKind::AssocImplItem);
710    }
711
712    fn visit_mac(&mut self, mac: &ast::MacCall, pos: MacroPosition) {
713        skip_out_of_file_lines_range_visitor!(self, mac.span());
714
715        // 1 = ;
716        let shape = self.shape().saturating_sub_width(1);
717        let rewrite = self.with_context(|ctx| rewrite_macro(mac, ctx, shape, pos).ok());
718        // As of v638 of the rustc-ap-* crates, the associated span no longer includes
719        // the trailing semicolon. This determines the correct span to ensure scenarios
720        // with whitespace between the delimiters and trailing semi (i.e. `foo!(abc)     ;`)
721        // are formatted correctly.
722        let (span, rewrite) = match macro_style(mac, &self.get_context()) {
723            Delimiter::Bracket | Delimiter::Parenthesis if MacroPosition::Item == pos => {
724                let search_span = mk_sp(mac.span().hi(), self.snippet_provider.end_pos());
725                let hi = self.snippet_provider.span_before(search_span, ";");
726                let target_span = mk_sp(mac.span().lo(), hi + BytePos(1));
727                let rewrite = rewrite.map(|rw| {
728                    if !rw.ends_with(';') {
729                        format!("{};", rw)
730                    } else {
731                        rw
732                    }
733                });
734                (target_span, rewrite)
735            }
736            _ => (mac.span(), rewrite),
737        };
738
739        self.push_rewrite(span, rewrite);
740    }
741
742    pub(crate) fn push_str(&mut self, s: &str) {
743        self.line_number += count_newlines(s);
744        self.buffer.push_str(s);
745    }
746
747    #[allow(clippy::needless_pass_by_value)]
748    fn push_rewrite_inner(&mut self, span: Span, rewrite: Option<String>) {
749        if let Some(ref s) = rewrite {
750            self.push_str(s);
751        } else {
752            let snippet = self.snippet(span);
753            self.push_str(snippet.trim());
754        }
755        self.last_pos = source!(self, span).hi();
756    }
757
758    pub(crate) fn push_rewrite(&mut self, span: Span, rewrite: Option<String>) {
759        self.format_missing_with_indent(source!(self, span).lo());
760        self.push_rewrite_inner(span, rewrite);
761    }
762
763    pub(crate) fn push_skipped_with_span(
764        &mut self,
765        attrs: &[ast::Attribute],
766        item_span: Span,
767        main_span: Span,
768    ) {
769        self.format_missing_with_indent(source!(self, item_span).lo());
770        // do not take into account the lines with attributes as part of the skipped range
771        let attrs_end = attrs
772            .iter()
773            .map(|attr| self.psess.line_of_byte_pos(attr.span.hi()))
774            .max()
775            .unwrap_or(1);
776        let first_line = self.psess.line_of_byte_pos(main_span.lo());
777        // Statement can start after some newlines and/or spaces
778        // or it can be on the same line as the last attribute.
779        // So here we need to take a minimum between the two.
780        let lo = std::cmp::min(attrs_end + 1, first_line);
781        self.push_rewrite_inner(item_span, None);
782        let hi = self.line_number + 1;
783        self.skipped_range.borrow_mut().push((lo, hi));
784    }
785
786    pub(crate) fn from_context(ctx: &'a RewriteContext<'_>) -> FmtVisitor<'a> {
787        let mut visitor = FmtVisitor::from_psess(
788            ctx.psess,
789            ctx.config,
790            ctx.snippet_provider,
791            ctx.report.clone(),
792        );
793        visitor.skip_context.update(ctx.skip_context.clone());
794        visitor.set_parent_context(ctx);
795        visitor
796    }
797
798    pub(crate) fn from_psess(
799        psess: &'a ParseSess,
800        config: &'a Config,
801        snippet_provider: &'a SnippetProvider,
802        report: FormatReport,
803    ) -> FmtVisitor<'a> {
804        let mut skip_context = SkipContext::default();
805        let mut macro_names = Vec::new();
806        for macro_selector in config.skip_macro_invocations().0 {
807            match macro_selector {
808                MacroSelector::Name(name) => macro_names.push(name.to_string()),
809                MacroSelector::All => skip_context.macros.skip_all(),
810            }
811        }
812        skip_context.macros.extend(macro_names);
813        FmtVisitor {
814            parent_context: None,
815            psess,
816            buffer: String::with_capacity(snippet_provider.big_snippet.len() * 2),
817            last_pos: BytePos(0),
818            block_indent: Indent::empty(),
819            config,
820            is_if_else_block: false,
821            snippet_provider,
822            line_number: 0,
823            skipped_range: Rc::new(RefCell::new(vec![])),
824            is_macro_def: false,
825            macro_rewrite_failure: false,
826            report,
827            skip_context,
828        }
829    }
830
831    pub(crate) fn opt_snippet(&'b self, span: Span) -> Option<&'a str> {
832        self.snippet_provider.span_to_snippet(span)
833    }
834
835    pub(crate) fn snippet(&'b self, span: Span) -> &'a str {
836        self.opt_snippet(span).unwrap()
837    }
838
839    // Returns true if we should skip the following item.
840    pub(crate) fn visit_attrs(&mut self, attrs: &[ast::Attribute], style: ast::AttrStyle) -> bool {
841        for attr in attrs {
842            if attr.has_name(depr_skip_annotation()) {
843                let file_name = self.psess.span_to_filename(attr.span);
844                self.report.append(
845                    file_name,
846                    vec![FormattingError::from_span(
847                        attr.span,
848                        self.psess,
849                        ErrorKind::DeprecatedAttr,
850                    )],
851                );
852            } else {
853                match &attr.kind {
854                    ast::AttrKind::Normal(ref normal)
855                        if self.is_unknown_rustfmt_attr(&normal.item.path.segments) =>
856                    {
857                        let file_name = self.psess.span_to_filename(attr.span);
858                        self.report.append(
859                            file_name,
860                            vec![FormattingError::from_span(
861                                attr.span,
862                                self.psess,
863                                ErrorKind::BadAttr,
864                            )],
865                        );
866                    }
867                    _ => (),
868                }
869            }
870        }
871        if contains_skip(attrs) {
872            return true;
873        }
874
875        let attrs: Vec<_> = attrs.iter().filter(|a| a.style == style).cloned().collect();
876        if attrs.is_empty() {
877            return false;
878        }
879
880        let rewrite = attrs.rewrite(&self.get_context(), self.shape());
881        let span = mk_sp(attrs[0].span.lo(), attrs[attrs.len() - 1].span.hi());
882        self.push_rewrite(span, rewrite);
883
884        false
885    }
886
887    fn is_unknown_rustfmt_attr(&self, segments: &[ast::PathSegment]) -> bool {
888        if segments[0].ident.to_string() != "rustfmt" {
889            return false;
890        }
891        !is_skip_attr(segments)
892    }
893
894    fn walk_mod_items(&mut self, items: &[Box<ast::Item>]) {
895        self.visit_items_with_reordering(&ptr_vec_to_ref_vec(items));
896    }
897
898    fn walk_stmts(&mut self, stmts: &[Stmt<'_>], include_current_empty_semi: bool) {
899        if stmts.is_empty() {
900            return;
901        }
902
903        // Extract leading `use ...;`.
904        let items: Vec<_> = stmts
905            .iter()
906            .take_while(|stmt| stmt.to_item().map_or(false, is_use_item))
907            .filter_map(|stmt| stmt.to_item())
908            .collect();
909
910        if items.is_empty() {
911            self.visit_stmt(&stmts[0], include_current_empty_semi);
912
913            // FIXME(calebcartwright 2021-01-03) - This exists strictly to maintain legacy
914            // formatting where rustfmt would preserve redundant semicolons on Items in a
915            // statement position.
916            //
917            // Starting in rustc-ap-* v692 (~2020-12-01) the rustc parser now parses this as
918            // two separate statements (Item and Empty kinds), whereas before it was parsed as
919            // a single statement with the statement's span including the redundant semicolon.
920            //
921            // rustfmt typically tosses unnecessary/redundant semicolons, and eventually we
922            // should toss these as well, but doing so at this time would
923            // break the Stability Guarantee
924            // N.B. This could be updated to utilize the version gates.
925            let include_next_empty = if stmts.len() > 1 {
926                matches!(
927                    (&stmts[0].as_ast_node().kind, &stmts[1].as_ast_node().kind),
928                    (ast::StmtKind::Item(_), ast::StmtKind::Empty)
929                )
930            } else {
931                false
932            };
933
934            self.walk_stmts(&stmts[1..], include_next_empty);
935        } else {
936            self.visit_items_with_reordering(&items);
937            self.walk_stmts(&stmts[items.len()..], false);
938        }
939    }
940
941    fn walk_block_stmts(&mut self, b: &ast::Block) {
942        self.walk_stmts(&Stmt::from_ast_nodes(b.stmts.iter()), false)
943    }
944
945    fn format_mod(
946        &mut self,
947        mod_kind: &ast::ModKind,
948        safety: ast::Safety,
949        vis: &ast::Visibility,
950        s: Span,
951        ident: symbol::Ident,
952        attrs: &[ast::Attribute],
953    ) {
954        let vis_str = utils::format_visibility(&self.get_context(), vis);
955        self.push_str(&*vis_str);
956        self.push_str(format_safety(safety));
957        self.push_str("mod ");
958        // Calling `to_owned()` to work around borrow checker.
959        let ident_str = rewrite_ident(&self.get_context(), ident).to_owned();
960        self.push_str(&ident_str);
961
962        if let ast::ModKind::Loaded(ref items, ast::Inline::Yes, ref spans) = mod_kind {
963            let ast::ModSpans {
964                inner_span,
965                inject_use_span: _,
966            } = *spans;
967            match self.config.brace_style() {
968                BraceStyle::AlwaysNextLine => {
969                    let indent_str = self.block_indent.to_string_with_newline(self.config);
970                    self.push_str(&indent_str);
971                    self.push_str("{");
972                }
973                _ => self.push_str(" {"),
974            }
975            // Hackery to account for the closing }.
976            let mod_lo = self.snippet_provider.span_after(source!(self, s), "{");
977            let body_snippet =
978                self.snippet(mk_sp(mod_lo, source!(self, inner_span).hi() - BytePos(1)));
979            let body_snippet = body_snippet.trim();
980            if body_snippet.is_empty() {
981                self.push_str("}");
982            } else {
983                self.last_pos = mod_lo;
984                self.block_indent = self.block_indent.block_indent(self.config);
985                self.visit_attrs(attrs, ast::AttrStyle::Inner);
986                self.walk_mod_items(items);
987                let missing_span = self.next_span(inner_span.hi() - BytePos(1));
988                self.close_block(missing_span, false);
989            }
990            self.last_pos = source!(self, inner_span).hi();
991        } else {
992            self.push_str(";");
993            self.last_pos = source!(self, s).hi();
994        }
995    }
996
997    pub(crate) fn format_separate_mod(&mut self, m: &Module<'_>, end_pos: BytePos) {
998        self.block_indent = Indent::empty();
999        let skipped = self.visit_attrs(m.attrs(), ast::AttrStyle::Inner);
1000        assert!(
1001            !skipped,
1002            "Skipping module must be handled before reaching this line."
1003        );
1004        self.walk_mod_items(&m.items);
1005        self.format_missing_with_indent(end_pos);
1006    }
1007
1008    pub(crate) fn skip_empty_lines(&mut self, end_pos: BytePos) {
1009        while let Some(pos) = self
1010            .snippet_provider
1011            .opt_span_after(self.next_span(end_pos), "\n")
1012        {
1013            if let Some(snippet) = self.opt_snippet(self.next_span(pos)) {
1014                if snippet.trim().is_empty() {
1015                    self.last_pos = pos;
1016                } else {
1017                    return;
1018                }
1019            }
1020        }
1021    }
1022
1023    pub(crate) fn with_context<F>(&mut self, f: F) -> Option<String>
1024    where
1025        F: Fn(&RewriteContext<'_>) -> Option<String>,
1026    {
1027        let context = self.get_context();
1028        let result = f(&context);
1029
1030        self.macro_rewrite_failure |= context.macro_rewrite_failure.get();
1031        result
1032    }
1033
1034    pub(crate) fn get_context(&self) -> RewriteContext<'_> {
1035        RewriteContext {
1036            psess: self.psess,
1037            config: self.config,
1038            inside_macro: Rc::new(Cell::new(false)),
1039            use_block: Cell::new(false),
1040            is_if_else_block: Cell::new(false),
1041            force_one_line_chain: Cell::new(false),
1042            snippet_provider: self.snippet_provider,
1043            macro_rewrite_failure: Cell::new(false),
1044            is_macro_def: self.is_macro_def,
1045            report: self.report.clone(),
1046            skip_context: self.skip_context.clone(),
1047            skipped_range: self.skipped_range.clone(),
1048        }
1049    }
1050}