diff options
| author | Dylan DPC <dylan.dpc@gmail.com> | 2021-02-19 02:49:08 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-02-19 02:49:08 +0100 |
| commit | 30f39fee9ddaacbe0dc0a62b5a7fef6338a90a00 (patch) | |
| tree | 42475839b09338a40ebf58f52595dd862b0d1608 /compiler/rustc_parse/src/parser | |
| parent | f8b61d852c2de4632278322d6d00a7f066277e14 (diff) | |
| parent | 4a8816512477513f57986685738cd065e72c4908 (diff) | |
| download | rust-30f39fee9ddaacbe0dc0a62b5a7fef6338a90a00.tar.gz rust-30f39fee9ddaacbe0dc0a62b5a7fef6338a90a00.zip | |
Rollup merge of #82238 - petrochenkov:nocratemod, r=Aaron1011
ast: Keep expansion status for out-of-line module items I.e. whether a module `mod foo;` is already loaded from a file or not. This is a pre-requisite to correctly treating inner attributes on such modules (https://github.com/rust-lang/rust/issues/81661). With this change AST structures for `mod` items diverge even more for AST structure for the crate root, which previously used `ast::Mod`. Therefore this PR removes `ast::Mod` from `ast::Crate` in the first commit, these two things are sufficiently different from each other, at least at syntactic level. Customization points for visiting a "`mod` item or crate root" were also removed from AST visitors (`fn visit_mod`). `ast::Mod` itself was refactored away in the second commit in favor of `ItemKind::Mod(Unsafe, ModKind)`.
Diffstat (limited to 'compiler/rustc_parse/src/parser')
| -rw-r--r-- | compiler/rustc_parse/src/parser/item.rs | 35 |
1 files changed, 11 insertions, 24 deletions
diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index cdea82f50ed..0f907859a19 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -25,11 +25,9 @@ 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 lo = self.token.span; - let (module, attrs) = self.parse_mod(&token::Eof, Unsafe::No)?; - let span = lo.to(self.token.span); + let (attrs, items, span) = self.parse_mod(&token::Eof)?; let proc_macros = Vec::new(); // Filled in by `proc_macro_harness::inject()`. - Ok(ast::Crate { attrs, module, span, proc_macros }) + Ok(ast::Crate { attrs, items, span, proc_macros }) } /// Parses a `mod <foo> { ... }` or `mod <foo>;` item. @@ -37,35 +35,26 @@ impl<'a> Parser<'a> { let unsafety = self.parse_unsafety(); self.expect_keyword(kw::Mod)?; let id = self.parse_ident()?; - let (module, mut inner_attrs) = if self.eat(&token::Semi) { - (Mod { inner: Span::default(), unsafety, items: Vec::new(), inline: false }, Vec::new()) + let mod_kind = if self.eat(&token::Semi) { + ModKind::Unloaded } else { self.expect(&token::OpenDelim(token::Brace))?; - self.parse_mod(&token::CloseDelim(token::Brace), unsafety)? + let (mut inner_attrs, items, inner_span) = + self.parse_mod(&token::CloseDelim(token::Brace))?; + attrs.append(&mut inner_attrs); + ModKind::Loaded(items, Inline::Yes, inner_span) }; - attrs.append(&mut inner_attrs); - Ok((id, ItemKind::Mod(module))) + Ok((id, ItemKind::Mod(unsafety, mod_kind))) } /// Parses the contents of a module (inner attributes followed by module items). pub fn parse_mod( &mut self, term: &TokenKind, - unsafety: Unsafe, - ) -> PResult<'a, (Mod, Vec<Attribute>)> { + ) -> PResult<'a, (Vec<Attribute>, Vec<P<Item>>, Span)> { let lo = self.token.span; let attrs = self.parse_inner_attributes()?; - let module = self.parse_mod_items(term, lo, unsafety)?; - Ok((module, attrs)) - } - /// Given a termination token, parses all of the items in a module. - fn parse_mod_items( - &mut self, - term: &TokenKind, - inner_lo: Span, - unsafety: Unsafe, - ) -> PResult<'a, Mod> { let mut items = vec![]; while let Some(item) = self.parse_item(ForceCollect::No)? { items.push(item); @@ -82,9 +71,7 @@ impl<'a> Parser<'a> { } } - let hi = if self.token.span.is_dummy() { inner_lo } else { self.prev_token.span }; - - Ok(Mod { inner: inner_lo.to(hi), unsafety, items, inline: true }) + Ok((attrs, items, lo.to(self.prev_token.span))) } } |
