@@ -4,7 +4,7 @@ use rustc_ast::*;
44use rustc_parse:: new_parser_from_file;
55use rustc_session:: parse:: ParseSess ;
66use rustc_span:: source_map:: SourceMap ;
7- use rustc_span:: FileName ;
7+ use rustc_span:: { sym , FileName } ;
88use smallvec:: SmallVec ;
99use 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