]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_expand/src/module.rs
fd25b8f0440ca7b4f76dcb7d8ef9892edf4edee6
[rust.git] / compiler / rustc_expand / src / module.rs
1 use crate::base::ModuleData;
2 use rustc_ast::ptr::P;
3 use rustc_ast::{token, Attribute, Inline, Item};
4 use rustc_errors::{struct_span_err, DiagnosticBuilder};
5 use rustc_parse::new_parser_from_file;
6 use rustc_session::parse::ParseSess;
7 use rustc_session::Session;
8 use rustc_span::symbol::{sym, Ident};
9 use rustc_span::Span;
10
11 use std::path::{self, Path, PathBuf};
12
13 #[derive(Copy, Clone)]
14 pub enum DirOwnership {
15     Owned {
16         // None if `mod.rs`, `Some("foo")` if we're in `foo.rs`.
17         relative: Option<Ident>,
18     },
19     UnownedViaBlock,
20 }
21
22 // Public for rustfmt usage.
23 pub struct ModulePathSuccess {
24     pub file_path: PathBuf,
25     pub dir_ownership: DirOwnership,
26 }
27
28 crate struct ParsedExternalMod {
29     pub items: Vec<P<Item>>,
30     pub inner_span: Span,
31     pub file_path: PathBuf,
32     pub dir_path: PathBuf,
33     pub dir_ownership: DirOwnership,
34 }
35
36 pub enum ModError<'a> {
37     CircularInclusion(Vec<PathBuf>),
38     ModInBlock(Option<Ident>),
39     FileNotFound(Ident, PathBuf),
40     MultipleCandidates(Ident, String, String),
41     ParserError(DiagnosticBuilder<'a>),
42 }
43
44 crate fn parse_external_mod(
45     sess: &Session,
46     ident: Ident,
47     span: Span, // The span to blame on errors.
48     module: &ModuleData,
49     mut dir_ownership: DirOwnership,
50     attrs: &mut Vec<Attribute>,
51 ) -> ParsedExternalMod {
52     // We bail on the first error, but that error does not cause a fatal error... (1)
53     let result: Result<_, ModError<'_>> = try {
54         // Extract the file path and the new ownership.
55         let mp = mod_file_path(sess, ident, &attrs, &module.dir_path, dir_ownership)?;
56         dir_ownership = mp.dir_ownership;
57
58         // Ensure file paths are acyclic.
59         if let Some(pos) = module.file_path_stack.iter().position(|p| p == &mp.file_path) {
60             Err(ModError::CircularInclusion(module.file_path_stack[pos..].to_vec()))?;
61         }
62
63         // Actually parse the external file as a module.
64         let mut parser = new_parser_from_file(&sess.parse_sess, &mp.file_path, Some(span));
65         let (mut inner_attrs, items, inner_span) =
66             parser.parse_mod(&token::Eof).map_err(|err| ModError::ParserError(err))?;
67         attrs.append(&mut inner_attrs);
68         (items, inner_span, mp.file_path)
69     };
70     // (1) ...instead, we return a dummy module.
71     let (items, inner_span, file_path) =
72         result.map_err(|err| err.report(sess, span)).unwrap_or_default();
73
74     // Extract the directory path for submodules of the module.
75     let dir_path = file_path.parent().unwrap_or(&file_path).to_owned();
76
77     ParsedExternalMod { items, inner_span, file_path, dir_path, dir_ownership }
78 }
79
80 crate fn mod_dir_path(
81     sess: &Session,
82     ident: Ident,
83     attrs: &[Attribute],
84     module: &ModuleData,
85     mut dir_ownership: DirOwnership,
86     inline: Inline,
87 ) -> (PathBuf, DirOwnership) {
88     match inline {
89         Inline::Yes => {
90             if let Some(file_path) = mod_file_path_from_attr(sess, attrs, &module.dir_path) {
91                 // For inline modules file path from `#[path]` is actually the directory path
92                 // for historical reasons, so we don't pop the last segment here.
93                 return (file_path, DirOwnership::Owned { relative: None });
94             }
95
96             // We have to push on the current module name in the case of relative
97             // paths in order to ensure that any additional module paths from inline
98             // `mod x { ... }` come after the relative extension.
99             //
100             // For example, a `mod z { ... }` inside `x/y.rs` should set the current
101             // directory path to `/x/y/z`, not `/x/z` with a relative offset of `y`.
102             let mut dir_path = module.dir_path.clone();
103             if let DirOwnership::Owned { relative } = &mut dir_ownership {
104                 if let Some(ident) = relative.take() {
105                     // Remove the relative offset.
106                     dir_path.push(&*ident.as_str());
107                 }
108             }
109             dir_path.push(&*ident.as_str());
110
111             (dir_path, dir_ownership)
112         }
113         Inline::No => {
114             // FIXME: This is a subset of `parse_external_mod` without actual parsing,
115             // check whether the logic for unloaded, loaded and inline modules can be unified.
116             let file_path = mod_file_path(sess, ident, &attrs, &module.dir_path, dir_ownership)
117                 .map(|mp| {
118                     dir_ownership = mp.dir_ownership;
119                     mp.file_path
120                 })
121                 .unwrap_or_default();
122
123             // Extract the directory path for submodules of the module.
124             let dir_path = file_path.parent().unwrap_or(&file_path).to_owned();
125
126             (dir_path, dir_ownership)
127         }
128     }
129 }
130
131 fn mod_file_path<'a>(
132     sess: &'a Session,
133     ident: Ident,
134     attrs: &[Attribute],
135     dir_path: &Path,
136     dir_ownership: DirOwnership,
137 ) -> Result<ModulePathSuccess, ModError<'a>> {
138     if let Some(file_path) = mod_file_path_from_attr(sess, attrs, dir_path) {
139         // All `#[path]` files are treated as though they are a `mod.rs` file.
140         // This means that `mod foo;` declarations inside `#[path]`-included
141         // files are siblings,
142         //
143         // Note that this will produce weirdness when a file named `foo.rs` is
144         // `#[path]` included and contains a `mod foo;` declaration.
145         // If you encounter this, it's your own darn fault :P
146         let dir_ownership = DirOwnership::Owned { relative: None };
147         return Ok(ModulePathSuccess { file_path, dir_ownership });
148     }
149
150     let relative = match dir_ownership {
151         DirOwnership::Owned { relative } => relative,
152         DirOwnership::UnownedViaBlock => None,
153     };
154     let result = default_submod_path(&sess.parse_sess, ident, relative, dir_path);
155     match dir_ownership {
156         DirOwnership::Owned { .. } => result,
157         DirOwnership::UnownedViaBlock => Err(ModError::ModInBlock(match result {
158             Ok(_) | Err(ModError::MultipleCandidates(..)) => Some(ident),
159             _ => None,
160         })),
161     }
162 }
163
164 /// Derive a submodule path from the first found `#[path = "path_string"]`.
165 /// The provided `dir_path` is joined with the `path_string`.
166 fn mod_file_path_from_attr(
167     sess: &Session,
168     attrs: &[Attribute],
169     dir_path: &Path,
170 ) -> Option<PathBuf> {
171     // Extract path string from first `#[path = "path_string"]` attribute.
172     let path_string = sess.first_attr_value_str_by_name(attrs, sym::path)?.as_str();
173
174     // On windows, the base path might have the form
175     // `\\?\foo\bar` in which case it does not tolerate
176     // mixed `/` and `\` separators, so canonicalize
177     // `/` to `\`.
178     #[cfg(windows)]
179     let path_string = path_string.replace("/", "\\");
180
181     Some(dir_path.join(&*path_string))
182 }
183
184 /// Returns a path to a module.
185 // Public for rustfmt usage.
186 pub fn default_submod_path<'a>(
187     sess: &'a ParseSess,
188     ident: Ident,
189     relative: Option<Ident>,
190     dir_path: &Path,
191 ) -> Result<ModulePathSuccess, ModError<'a>> {
192     // If we're in a foo.rs file instead of a mod.rs file,
193     // we need to look for submodules in
194     // `./foo/<ident>.rs` and `./foo/<ident>/mod.rs` rather than
195     // `./<ident>.rs` and `./<ident>/mod.rs`.
196     let relative_prefix_string;
197     let relative_prefix = if let Some(ident) = relative {
198         relative_prefix_string = format!("{}{}", ident.name, path::MAIN_SEPARATOR);
199         &relative_prefix_string
200     } else {
201         ""
202     };
203
204     let mod_name = ident.name.to_string();
205     let default_path_str = format!("{}{}.rs", relative_prefix, mod_name);
206     let secondary_path_str =
207         format!("{}{}{}mod.rs", relative_prefix, mod_name, path::MAIN_SEPARATOR);
208     let default_path = dir_path.join(&default_path_str);
209     let secondary_path = dir_path.join(&secondary_path_str);
210     let default_exists = sess.source_map().file_exists(&default_path);
211     let secondary_exists = sess.source_map().file_exists(&secondary_path);
212
213     match (default_exists, secondary_exists) {
214         (true, false) => Ok(ModulePathSuccess {
215             file_path: default_path,
216             dir_ownership: DirOwnership::Owned { relative: Some(ident) },
217         }),
218         (false, true) => Ok(ModulePathSuccess {
219             file_path: secondary_path,
220             dir_ownership: DirOwnership::Owned { relative: None },
221         }),
222         (false, false) => Err(ModError::FileNotFound(ident, default_path)),
223         (true, true) => {
224             Err(ModError::MultipleCandidates(ident, default_path_str, secondary_path_str))
225         }
226     }
227 }
228
229 impl ModError<'_> {
230     fn report(self, sess: &Session, span: Span) {
231         let diag = &sess.parse_sess.span_diagnostic;
232         match self {
233             ModError::CircularInclusion(file_paths) => {
234                 let mut msg = String::from("circular modules: ");
235                 for file_path in &file_paths {
236                     msg.push_str(&file_path.display().to_string());
237                     msg.push_str(" -> ");
238                 }
239                 msg.push_str(&file_paths[0].display().to_string());
240                 diag.struct_span_err(span, &msg)
241             }
242             ModError::ModInBlock(ident) => {
243                 let msg = "cannot declare a non-inline module inside a block unless it has a path attribute";
244                 let mut err = diag.struct_span_err(span, msg);
245                 if let Some(ident) = ident {
246                     let note =
247                         format!("maybe `use` the module `{}` instead of redeclaring it", ident);
248                     err.span_note(span, &note);
249                 }
250                 err
251             }
252             ModError::FileNotFound(ident, default_path) => {
253                 let mut err = struct_span_err!(
254                     diag,
255                     span,
256                     E0583,
257                     "file not found for module `{}`",
258                     ident,
259                 );
260                 err.help(&format!(
261                     "to create the module `{}`, create file \"{}\"",
262                     ident,
263                     default_path.display(),
264                 ));
265                 err
266             }
267             ModError::MultipleCandidates(ident, default_path_short, secondary_path_short) => {
268                 let mut err = struct_span_err!(
269                     diag,
270                     span,
271                     E0761,
272                     "file for module `{}` found at both \"{}\" and \"{}\"",
273                     ident,
274                     default_path_short,
275                     secondary_path_short,
276                 );
277                 err.help("delete or rename one of them to remove the ambiguity");
278                 err
279             }
280             ModError::ParserError(err) => err,
281         }.emit()
282     }
283 }