Skip to content

Commit 6ce3767

Browse files
committed
Auto merge of #130246 - dianne:issue-97589-fix, r=petrochenkov
rustc_expand: remember module `#[path]`s during expansion During invocation collection, if a module item parsed from a `#[path]` attribute needed a second pass after parsing, its path wouldn't get added to the file path stack, so cycle detection broke. This checks the `#[path]` in such cases, so that it gets added appropriately. I think it should work identically to the case for external modules that don't need a second pass, but I'm not 100% sure. Fixes #97589
2 parents 55043f0 + a187d0a commit 6ce3767

File tree

5 files changed

+31
-3
lines changed

5 files changed

+31
-3
lines changed

compiler/rustc_expand/src/expand.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ use crate::errors::{
4141
};
4242
use crate::fluent_generated;
4343
use crate::mbe::diagnostics::annotate_err_with_kind;
44-
use crate::module::{mod_dir_path, parse_external_mod, DirOwnership, ParsedExternalMod};
44+
use crate::module::{
45+
mod_dir_path, mod_file_path_from_attr, parse_external_mod, DirOwnership, ParsedExternalMod,
46+
};
4547
use crate::placeholders::{placeholder, PlaceholderExpander};
4648

4749
macro_rules! ast_fragments {
@@ -1202,8 +1204,14 @@ impl InvocationCollectorNode for P<ast::Item> {
12021204
ecx.current_expansion.dir_ownership,
12031205
*inline,
12041206
);
1207+
// If the module was parsed from an external file, recover its path.
1208+
// This lets `parse_external_mod` catch cycles if it's self-referential.
1209+
let file_path = match inline {
1210+
Inline::Yes => None,
1211+
Inline::No => mod_file_path_from_attr(ecx.sess, &attrs, &dir_path),
1212+
};
12051213
node.attrs = attrs;
1206-
(None, dir_path, dir_ownership)
1214+
(file_path, dir_path, dir_ownership)
12071215
}
12081216
ModKind::Unloaded => {
12091217
// We have an outline `mod foo;` so we need to parse the file.

compiler/rustc_expand/src/module.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ fn mod_file_path<'a>(
171171

172172
/// Derive a submodule path from the first found `#[path = "path_string"]`.
173173
/// The provided `dir_path` is joined with the `path_string`.
174-
fn mod_file_path_from_attr(
174+
pub(crate) fn mod_file_path_from_attr(
175175
sess: &Session,
176176
attrs: &[Attribute],
177177
dir_path: &Path,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//@ error-pattern: circular modules
2+
// Regression test for #97589: a doc-comment on a circular module bypassed cycle detection
3+
4+
#![crate_type = "lib"]
5+
6+
pub mod recursive;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: circular modules: $DIR/recursive.rs -> $DIR/recursive.rs
2+
--> $DIR/recursive.rs:6:1
3+
|
4+
LL | mod recursive;
5+
| ^^^^^^^^^^^^^^
6+
7+
error: aborting due to 1 previous error
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//@ ignore-test: this is an auxiliary file for circular-module-with-doc-comment-issue-97589.rs
2+
3+
//! this comment caused the circular dependency checker to break
4+
5+
#[path = "recursive.rs"]
6+
mod recursive;

0 commit comments

Comments
 (0)