Skip to content

Commit e9035f7

Browse files
committed
refactor: prepare to associate multiple spans with a module.
1 parent 4566094 commit e9035f7

File tree

9 files changed

+30
-14
lines changed

9 files changed

+30
-14
lines changed

compiler/rustc_ast/src/ast.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -2317,11 +2317,24 @@ pub enum ModKind {
23172317
/// or with definition outlined to a separate file `mod foo;` and already loaded from it.
23182318
/// The inner span is from the first token past `{` to the last token until `}`,
23192319
/// or from the first to the last token in the loaded file.
2320-
Loaded(Vec<P<Item>>, Inline, Span),
2320+
Loaded(Vec<P<Item>>, Inline, ModSpans),
23212321
/// Module with definition outlined to a separate file `mod foo;` but not yet loaded from it.
23222322
Unloaded,
23232323
}
23242324

2325+
#[derive(Clone, Encodable, Decodable, Debug)]
2326+
pub struct ModSpans {
2327+
/// `inner_span` covers the body of the module; for a file module, its the whole file.
2328+
/// For an inline module, its the span inside the `{ ... }`, not including the curly braces.
2329+
pub inner_span: Span,
2330+
}
2331+
2332+
impl Default for ModSpans {
2333+
fn default() -> ModSpans {
2334+
ModSpans { inner_span: Default::default() }
2335+
}
2336+
}
2337+
23252338
/// Foreign module declaration.
23262339
///
23272340
/// E.g., `extern { .. }` or `extern "C" { .. }`.

compiler/rustc_ast/src/mut_visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1009,7 +1009,7 @@ pub fn noop_visit_item_kind<T: MutVisitor>(kind: &mut ItemKind, vis: &mut T) {
10091009
ItemKind::Mod(unsafety, mod_kind) => {
10101010
visit_unsafety(unsafety, vis);
10111011
match mod_kind {
1012-
ModKind::Loaded(items, _inline, inner_span) => {
1012+
ModKind::Loaded(items, _inline, ModSpans { inner_span }) => {
10131013
vis.visit_span(inner_span);
10141014
items.flat_map_in_place(|item| vis.flat_map_item(item));
10151015
}

compiler/rustc_ast_lowering/src/item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
263263
})
264264
}
265265
ItemKind::Mod(_, ref mod_kind) => match mod_kind {
266-
ModKind::Loaded(items, _, inner_span) => {
266+
ModKind::Loaded(items, _, ModSpans { inner_span }) => {
267267
hir::ItemKind::Mod(self.lower_mod(items, *inner_span))
268268
}
269269
ModKind::Unloaded => panic!("`mod` items should have been loaded by now"),

compiler/rustc_builtin_macros/src/test_harness.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ impl<'a> MutVisitor for TestHarnessGenerator<'a> {
129129

130130
// We don't want to recurse into anything other than mods, since
131131
// mods or tests inside of functions will break things
132-
if let ast::ItemKind::Mod(_, ModKind::Loaded(.., span)) = item.kind {
132+
if let ast::ItemKind::Mod(_, ModKind::Loaded(.., ast::ModSpans { inner_span: span })) =
133+
item.kind
134+
{
133135
let prev_tests = mem::take(&mut self.tests);
134136
noop_visit_item_kind(&mut item.kind, self);
135137
self.add_test_cases(item.id, span, prev_tests);

compiler/rustc_expand/src/expand.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use rustc_ast::token;
1212
use rustc_ast::tokenstream::TokenStream;
1313
use rustc_ast::visit::{self, AssocCtxt, Visitor};
1414
use rustc_ast::{AssocItemKind, AstLike, AstLikeWrapper, AttrStyle, ExprKind, ForeignItemKind};
15-
use rustc_ast::{Inline, ItemKind, MacArgs, MacStmtStyle, MetaItemKind, ModKind, NestedMetaItem};
16-
use rustc_ast::{NodeId, PatKind, StmtKind, TyKind};
15+
use rustc_ast::{Inline, ItemKind, MacArgs, MacStmtStyle, MetaItemKind, ModKind, ModSpans};
16+
use rustc_ast::{NestedMetaItem, NodeId, PatKind, StmtKind, TyKind};
1717
use rustc_ast_pretty::pprust;
1818
use rustc_data_structures::map_in_place::MapInPlace;
1919
use rustc_data_structures::sync::Lrc;
@@ -1112,7 +1112,7 @@ impl InvocationCollectorNode for P<ast::Item> {
11121112
);
11131113
}
11141114

1115-
*mod_kind = ModKind::Loaded(items, Inline::No, inner_span);
1115+
*mod_kind = ModKind::Loaded(items, Inline::No, ModSpans { inner_span });
11161116
node.attrs = attrs;
11171117
if node.attrs.len() > old_attrs_len {
11181118
// If we loaded an out-of-line module and added some inner attributes,

compiler/rustc_expand/src/module.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::base::ModuleData;
22
use rustc_ast::ptr::P;
3-
use rustc_ast::{token, Attribute, Inline, Item};
3+
use rustc_ast::{token, Attribute, Inline, Item, ModSpans};
44
use rustc_errors::{struct_span_err, DiagnosticBuilder, ErrorGuaranteed};
55
use rustc_parse::new_parser_from_file;
66
use rustc_parse::validate_attr;
@@ -69,7 +69,7 @@ crate fn parse_external_mod(
6969
(items, inner_span, mp.file_path)
7070
};
7171
// (1) ...instead, we return a dummy module.
72-
let (items, inner_span, file_path) =
72+
let (items, ModSpans { inner_span }, file_path) =
7373
result.map_err(|err| err.report(sess, span)).unwrap_or_default();
7474

7575
// Extract the directory path for submodules of the module.

compiler/rustc_parse/src/parser/item.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ use tracing::debug;
2626
impl<'a> Parser<'a> {
2727
/// Parses a source module as a crate. This is the main entry point for the parser.
2828
pub fn parse_crate_mod(&mut self) -> PResult<'a, ast::Crate> {
29-
let (attrs, items, span) = self.parse_mod(&token::Eof)?;
29+
let (attrs, items, spans) = self.parse_mod(&token::Eof)?;
30+
let span = spans.inner_span;
3031
Ok(ast::Crate { attrs, items, span, id: DUMMY_NODE_ID, is_placeholder: false })
3132
}
3233

@@ -51,7 +52,7 @@ impl<'a> Parser<'a> {
5152
pub fn parse_mod(
5253
&mut self,
5354
term: &TokenKind,
54-
) -> PResult<'a, (Vec<Attribute>, Vec<P<Item>>, Span)> {
55+
) -> PResult<'a, (Vec<Attribute>, Vec<P<Item>>, ModSpans)> {
5556
let lo = self.token.span;
5657
let attrs = self.parse_inner_attributes()?;
5758

@@ -71,7 +72,7 @@ impl<'a> Parser<'a> {
7172
}
7273
}
7374

74-
Ok((attrs, items, lo.to(self.prev_token.span)))
75+
Ok((attrs, items, ModSpans { inner_span: lo.to(self.prev_token.span) }))
7576
}
7677
}
7778

src/tools/rustfmt/src/parse/parser.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl<'a> Parser<'a> {
113113
let result = catch_unwind(AssertUnwindSafe(|| {
114114
let mut parser = new_parser_from_file(sess.inner(), path, Some(span));
115115
match parser.parse_mod(&TokenKind::Eof) {
116-
Ok(result) => Some(result),
116+
Ok((a, i, ast::ModSpans { inner_span })) => Some((a, i, inner_span)),
117117
Err(mut e) => {
118118
e.emit();
119119
if sess.can_reset_errors() {

src/tools/rustfmt/src/visitor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
915915
let ident_str = rewrite_ident(&self.get_context(), ident).to_owned();
916916
self.push_str(&ident_str);
917917

918-
if let ast::ModKind::Loaded(ref items, ast::Inline::Yes, inner_span) = mod_kind {
918+
if let ast::ModKind::Loaded(ref items, ast::Inline::Yes, ast::ModSpans{ inner_span }) = mod_kind {
919919
match self.config.brace_style() {
920920
BraceStyle::AlwaysNextLine => {
921921
let indent_str = self.block_indent.to_string_with_newline(self.config);

0 commit comments

Comments
 (0)