From 9a7644e17174dff78f94f8b8d81f21379297d1cf Mon Sep 17 00:00:00 2001 From: dianne Date: Wed, 11 Sep 2024 12:36:40 -0700 Subject: [PATCH] rustc_expand: remember module #[path]s during expansion this fixes cycle detection for modules that need a second invocation collection pass after parsing --- compiler/rustc_expand/src/expand.rs | 12 ++++++++++-- compiler/rustc_expand/src/module.rs | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_expand/src/expand.rs b/compiler/rustc_expand/src/expand.rs index 0d56a005f15..84ae351ed72 100644 --- a/compiler/rustc_expand/src/expand.rs +++ b/compiler/rustc_expand/src/expand.rs @@ -41,7 +41,9 @@ }; use crate::fluent_generated; use crate::mbe::diagnostics::annotate_err_with_kind; -use crate::module::{mod_dir_path, parse_external_mod, DirOwnership, ParsedExternalMod}; +use crate::module::{ + mod_dir_path, mod_file_path_from_attr, parse_external_mod, DirOwnership, ParsedExternalMod, +}; use crate::placeholders::{placeholder, PlaceholderExpander}; macro_rules! ast_fragments { @@ -1202,8 +1204,14 @@ fn wrap_flat_map_node_walk_flat_map( ecx.current_expansion.dir_ownership, *inline, ); + // If the module was parsed from an external file, recover its path. + // This lets `parse_external_mod` catch cycles if it's self-referential. + let file_path = match inline { + Inline::Yes => None, + Inline::No => mod_file_path_from_attr(ecx.sess, &attrs, &dir_path), + }; node.attrs = attrs; - (None, dir_path, dir_ownership) + (file_path, dir_path, dir_ownership) } ModKind::Unloaded => { // We have an outline `mod foo;` so we need to parse the file. diff --git a/compiler/rustc_expand/src/module.rs b/compiler/rustc_expand/src/module.rs index 13348376851..27a9a330f3c 100644 --- a/compiler/rustc_expand/src/module.rs +++ b/compiler/rustc_expand/src/module.rs @@ -171,7 +171,7 @@ fn mod_file_path<'a>( /// Derive a submodule path from the first found `#[path = "path_string"]`. /// The provided `dir_path` is joined with the `path_string`. -fn mod_file_path_from_attr( +pub(crate) fn mod_file_path_from_attr( sess: &Session, attrs: &[Attribute], dir_path: &Path,