Skip to content

Commit 9f1dd8e

Browse files
randomPoisonthedataking
authored andcommitted
refactor: honor #[path] attribute when loading modules
1 parent 3a30607 commit 9f1dd8e

1 file changed

Lines changed: 19 additions & 6 deletions

File tree

c2rust-refactor/src/ast_manip/load_modules.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_ast::*;
44
use rustc_parse::new_parser_from_file;
55
use rustc_session::parse::ParseSess;
66
use rustc_span::source_map::SourceMap;
7-
use rustc_span::FileName;
7+
use rustc_span::{sym, FileName};
88
use smallvec::SmallVec;
99
use std::path::PathBuf;
1010

@@ -39,11 +39,24 @@ impl<'a> MutVisitor for LoadModules<'a> {
3939
}
4040

4141
ModKind::Unloaded => {
42-
// Look for dir_path/foo.rs, then try dir_path/foo/mod.rs
43-
let mut mod_file_path = self.dir_path.join(ident.as_str()).with_extension("rs");
44-
if !self.source_map.file_exists(&mod_file_path) {
45-
mod_file_path = self.dir_path.join(ident.as_str()).join("mod.rs");
46-
}
42+
// An explicit `#[path = "..."]` overrides the usual file lookup;
43+
// the transpiler emits this for modules whose name was deconflicted
44+
// from a sibling (e.g. `hash.c` and `hash/` becoming `hash_` and `hash`).
45+
let path_attr = attrs
46+
.iter()
47+
.find(|attr| attr.has_name(sym::path))
48+
.and_then(|attr| attr.value_str());
49+
50+
let mod_file_path = if let Some(path) = path_attr {
51+
self.dir_path.join(path.as_str())
52+
} else {
53+
// Look for dir_path/foo.rs, then try dir_path/foo/mod.rs
54+
let mut p = self.dir_path.join(ident.as_str()).with_extension("rs");
55+
if !self.source_map.file_exists(&p) {
56+
p = self.dir_path.join(ident.as_str()).join("mod.rs");
57+
}
58+
p
59+
};
4760
if !self.source_map.file_exists(&mod_file_path) {
4861
panic!("unable to load module file {mod_file_path:?}");
4962
}

0 commit comments

Comments
 (0)