refactor: prepare to associate multiple spans with a module.
This commit is contained in:
parent
4566094913
commit
e9035f7bef
9 changed files with 30 additions and 14 deletions
|
@ -2317,11 +2317,24 @@ pub enum ModKind {
|
|||
/// or with definition outlined to a separate file `mod foo;` and already loaded from it.
|
||||
/// The inner span is from the first token past `{` to the last token until `}`,
|
||||
/// or from the first to the last token in the loaded file.
|
||||
Loaded(Vec<P<Item>>, Inline, Span),
|
||||
Loaded(Vec<P<Item>>, Inline, ModSpans),
|
||||
/// Module with definition outlined to a separate file `mod foo;` but not yet loaded from it.
|
||||
Unloaded,
|
||||
}
|
||||
|
||||
#[derive(Clone, Encodable, Decodable, Debug)]
|
||||
pub struct ModSpans {
|
||||
/// `inner_span` covers the body of the module; for a file module, its the whole file.
|
||||
/// For an inline module, its the span inside the `{ ... }`, not including the curly braces.
|
||||
pub inner_span: Span,
|
||||
}
|
||||
|
||||
impl Default for ModSpans {
|
||||
fn default() -> ModSpans {
|
||||
ModSpans { inner_span: Default::default() }
|
||||
}
|
||||
}
|
||||
|
||||
/// Foreign module declaration.
|
||||
///
|
||||
/// E.g., `extern { .. }` or `extern "C" { .. }`.
|
||||
|
|
|
@ -1009,7 +1009,7 @@ pub fn noop_visit_item_kind<T: MutVisitor>(kind: &mut ItemKind, vis: &mut T) {
|
|||
ItemKind::Mod(unsafety, mod_kind) => {
|
||||
visit_unsafety(unsafety, vis);
|
||||
match mod_kind {
|
||||
ModKind::Loaded(items, _inline, inner_span) => {
|
||||
ModKind::Loaded(items, _inline, ModSpans { inner_span }) => {
|
||||
vis.visit_span(inner_span);
|
||||
items.flat_map_in_place(|item| vis.flat_map_item(item));
|
||||
}
|
||||
|
|
|
@ -263,7 +263,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
|
|||
})
|
||||
}
|
||||
ItemKind::Mod(_, ref mod_kind) => match mod_kind {
|
||||
ModKind::Loaded(items, _, inner_span) => {
|
||||
ModKind::Loaded(items, _, ModSpans { inner_span }) => {
|
||||
hir::ItemKind::Mod(self.lower_mod(items, *inner_span))
|
||||
}
|
||||
ModKind::Unloaded => panic!("`mod` items should have been loaded by now"),
|
||||
|
|
|
@ -129,7 +129,9 @@ impl<'a> MutVisitor for TestHarnessGenerator<'a> {
|
|||
|
||||
// We don't want to recurse into anything other than mods, since
|
||||
// mods or tests inside of functions will break things
|
||||
if let ast::ItemKind::Mod(_, ModKind::Loaded(.., span)) = item.kind {
|
||||
if let ast::ItemKind::Mod(_, ModKind::Loaded(.., ast::ModSpans { inner_span: span })) =
|
||||
item.kind
|
||||
{
|
||||
let prev_tests = mem::take(&mut self.tests);
|
||||
noop_visit_item_kind(&mut item.kind, self);
|
||||
self.add_test_cases(item.id, span, prev_tests);
|
||||
|
|
|
@ -12,8 +12,8 @@ use rustc_ast::token;
|
|||
use rustc_ast::tokenstream::TokenStream;
|
||||
use rustc_ast::visit::{self, AssocCtxt, Visitor};
|
||||
use rustc_ast::{AssocItemKind, AstLike, AstLikeWrapper, AttrStyle, ExprKind, ForeignItemKind};
|
||||
use rustc_ast::{Inline, ItemKind, MacArgs, MacStmtStyle, MetaItemKind, ModKind, NestedMetaItem};
|
||||
use rustc_ast::{NodeId, PatKind, StmtKind, TyKind};
|
||||
use rustc_ast::{Inline, ItemKind, MacArgs, MacStmtStyle, MetaItemKind, ModKind, ModSpans};
|
||||
use rustc_ast::{NestedMetaItem, NodeId, PatKind, StmtKind, TyKind};
|
||||
use rustc_ast_pretty::pprust;
|
||||
use rustc_data_structures::map_in_place::MapInPlace;
|
||||
use rustc_data_structures::sync::Lrc;
|
||||
|
@ -1112,7 +1112,7 @@ impl InvocationCollectorNode for P<ast::Item> {
|
|||
);
|
||||
}
|
||||
|
||||
*mod_kind = ModKind::Loaded(items, Inline::No, inner_span);
|
||||
*mod_kind = ModKind::Loaded(items, Inline::No, ModSpans { inner_span });
|
||||
node.attrs = attrs;
|
||||
if node.attrs.len() > old_attrs_len {
|
||||
// If we loaded an out-of-line module and added some inner attributes,
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use crate::base::ModuleData;
|
||||
use rustc_ast::ptr::P;
|
||||
use rustc_ast::{token, Attribute, Inline, Item};
|
||||
use rustc_ast::{token, Attribute, Inline, Item, ModSpans};
|
||||
use rustc_errors::{struct_span_err, DiagnosticBuilder, ErrorGuaranteed};
|
||||
use rustc_parse::new_parser_from_file;
|
||||
use rustc_parse::validate_attr;
|
||||
|
@ -69,7 +69,7 @@ crate fn parse_external_mod(
|
|||
(items, inner_span, mp.file_path)
|
||||
};
|
||||
// (1) ...instead, we return a dummy module.
|
||||
let (items, inner_span, file_path) =
|
||||
let (items, ModSpans { inner_span }, file_path) =
|
||||
result.map_err(|err| err.report(sess, span)).unwrap_or_default();
|
||||
|
||||
// Extract the directory path for submodules of the module.
|
||||
|
|
|
@ -26,7 +26,8 @@ use tracing::debug;
|
|||
impl<'a> Parser<'a> {
|
||||
/// Parses a source module as a crate. This is the main entry point for the parser.
|
||||
pub fn parse_crate_mod(&mut self) -> PResult<'a, ast::Crate> {
|
||||
let (attrs, items, span) = self.parse_mod(&token::Eof)?;
|
||||
let (attrs, items, spans) = self.parse_mod(&token::Eof)?;
|
||||
let span = spans.inner_span;
|
||||
Ok(ast::Crate { attrs, items, span, id: DUMMY_NODE_ID, is_placeholder: false })
|
||||
}
|
||||
|
||||
|
@ -51,7 +52,7 @@ impl<'a> Parser<'a> {
|
|||
pub fn parse_mod(
|
||||
&mut self,
|
||||
term: &TokenKind,
|
||||
) -> PResult<'a, (Vec<Attribute>, Vec<P<Item>>, Span)> {
|
||||
) -> PResult<'a, (Vec<Attribute>, Vec<P<Item>>, ModSpans)> {
|
||||
let lo = self.token.span;
|
||||
let attrs = self.parse_inner_attributes()?;
|
||||
|
||||
|
@ -71,7 +72,7 @@ impl<'a> Parser<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
Ok((attrs, items, lo.to(self.prev_token.span)))
|
||||
Ok((attrs, items, ModSpans { inner_span: lo.to(self.prev_token.span) }))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -113,7 +113,7 @@ impl<'a> Parser<'a> {
|
|||
let result = catch_unwind(AssertUnwindSafe(|| {
|
||||
let mut parser = new_parser_from_file(sess.inner(), path, Some(span));
|
||||
match parser.parse_mod(&TokenKind::Eof) {
|
||||
Ok(result) => Some(result),
|
||||
Ok((a, i, ast::ModSpans { inner_span })) => Some((a, i, inner_span)),
|
||||
Err(mut e) => {
|
||||
e.emit();
|
||||
if sess.can_reset_errors() {
|
||||
|
|
|
@ -915,7 +915,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
|
|||
let ident_str = rewrite_ident(&self.get_context(), ident).to_owned();
|
||||
self.push_str(&ident_str);
|
||||
|
||||
if let ast::ModKind::Loaded(ref items, ast::Inline::Yes, inner_span) = mod_kind {
|
||||
if let ast::ModKind::Loaded(ref items, ast::Inline::Yes, ast::ModSpans{ inner_span }) = mod_kind {
|
||||
match self.config.brace_style() {
|
||||
BraceStyle::AlwaysNextLine => {
|
||||
let indent_str = self.block_indent.to_string_with_newline(self.config);
|
||||
|
|
Loading…
Add table
Reference in a new issue