Skip to content

Commit 46b67aa

Browse files
committed
expand: Some more consistent naming in module loading
1 parent 3d0b622 commit 46b67aa

File tree

4 files changed

+59
-57
lines changed

4 files changed

+59
-57
lines changed

compiler/rustc_builtin_macros/src/source_util.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_ast::token;
44
use rustc_ast::tokenstream::TokenStream;
55
use rustc_ast_pretty::pprust;
66
use rustc_expand::base::{self, *};
7-
use rustc_expand::module::DirectoryOwnership;
7+
use rustc_expand::module::DirOwnership;
88
use rustc_parse::parser::{ForceCollect, Parser};
99
use rustc_parse::{self, new_parser_from_file};
1010
use rustc_session::lint::builtin::INCOMPLETE_INCLUDE;
@@ -116,7 +116,7 @@ pub fn expand_include<'cx>(
116116
// `MacroExpander::fully_expand_fragment` later restores, so "stack discipline" is maintained.
117117
let dir_path = file.parent().unwrap_or(&file).to_owned();
118118
cx.current_expansion.module = Rc::new(cx.current_expansion.module.with_dir_path(dir_path));
119-
cx.current_expansion.directory_ownership = DirectoryOwnership::Owned { relative: None };
119+
cx.current_expansion.dir_ownership = DirOwnership::Owned { relative: None };
120120

121121
struct ExpandResult<'a> {
122122
p: Parser<'a>,

compiler/rustc_expand/src/base.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::expand::{self, AstFragment, Invocation};
2-
use crate::module::DirectoryOwnership;
2+
use crate::module::DirOwnership;
33

44
use rustc_ast::ptr::P;
55
use rustc_ast::token::{self, Nonterminal};
@@ -921,7 +921,7 @@ pub struct ExpansionData {
921921
pub id: ExpnId,
922922
pub depth: usize,
923923
pub module: Rc<ModuleData>,
924-
pub directory_ownership: DirectoryOwnership,
924+
pub dir_ownership: DirOwnership,
925925
pub prior_type_ascription: Option<(Span, bool)>,
926926
}
927927

@@ -963,7 +963,7 @@ impl<'a> ExtCtxt<'a> {
963963
id: ExpnId::root(),
964964
depth: 0,
965965
module: Default::default(),
966-
directory_ownership: DirectoryOwnership::Owned { relative: None },
966+
dir_ownership: DirOwnership::Owned { relative: None },
967967
prior_type_ascription: None,
968968
},
969969
force_mode: false,

compiler/rustc_expand/src/expand.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::config::StripUnconfigured;
33
use crate::configure;
44
use crate::hygiene::SyntaxContext;
55
use crate::mbe::macro_rules::annotate_err_with_kind;
6-
use crate::module::{parse_external_mod, push_directory, DirectoryOwnership, ParsedExternalMod};
6+
use crate::module::{mod_dir_path, parse_external_mod, DirOwnership, ParsedExternalMod};
77
use crate::placeholders::{placeholder, PlaceholderExpander};
88

99
use rustc_ast as ast;
@@ -1246,10 +1246,12 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
12461246
}
12471247

12481248
fn visit_block(&mut self, block: &mut P<Block>) {
1249-
let old_directory_ownership = self.cx.current_expansion.directory_ownership;
1250-
self.cx.current_expansion.directory_ownership = DirectoryOwnership::UnownedViaBlock;
1249+
let orig_dir_ownership = mem::replace(
1250+
&mut self.cx.current_expansion.dir_ownership,
1251+
DirOwnership::UnownedViaBlock,
1252+
);
12511253
noop_visit_block(block, self);
1252-
self.cx.current_expansion.directory_ownership = old_directory_ownership;
1254+
self.cx.current_expansion.dir_ownership = orig_dir_ownership;
12531255
}
12541256

12551257
fn flat_map_item(&mut self, item: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
@@ -1280,12 +1282,12 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
12801282
let (file_path, dir_path, dir_ownership) = match mod_kind {
12811283
ModKind::Loaded(_, Inline::Yes, _) => {
12821284
// Inline `mod foo { ... }`, but we still need to push directories.
1283-
let (dir_path, dir_ownership) = push_directory(
1285+
let (dir_path, dir_ownership) = mod_dir_path(
12841286
&self.cx.sess,
12851287
ident,
12861288
&attrs,
12871289
&self.cx.current_expansion.module,
1288-
self.cx.current_expansion.directory_ownership,
1290+
self.cx.current_expansion.dir_ownership,
12891291
);
12901292
item.attrs = attrs;
12911293
(None, dir_path, dir_ownership)
@@ -1306,7 +1308,7 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
13061308
ident,
13071309
span,
13081310
&self.cx.current_expansion.module,
1309-
self.cx.current_expansion.directory_ownership,
1311+
self.cx.current_expansion.dir_ownership,
13101312
&mut attrs,
13111313
);
13121314

@@ -1334,12 +1336,12 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
13341336
let orig_module =
13351337
mem::replace(&mut self.cx.current_expansion.module, Rc::new(module));
13361338
let orig_dir_ownership =
1337-
mem::replace(&mut self.cx.current_expansion.directory_ownership, dir_ownership);
1339+
mem::replace(&mut self.cx.current_expansion.dir_ownership, dir_ownership);
13381340

13391341
let result = noop_flat_map_item(item, self);
13401342

13411343
// Restore the module info.
1342-
self.cx.current_expansion.directory_ownership = orig_dir_ownership;
1344+
self.cx.current_expansion.dir_ownership = orig_dir_ownership;
13431345
self.cx.current_expansion.module = orig_module;
13441346

13451347
result

compiler/rustc_expand/src/module.rs

+43-43
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_span::Span;
1111
use std::path::{self, Path, PathBuf};
1212

1313
#[derive(Copy, Clone)]
14-
pub enum DirectoryOwnership {
14+
pub enum DirOwnership {
1515
Owned {
1616
// None if `mod.rs`, `Some("foo")` if we're in `foo.rs`.
1717
relative: Option<Ident>,
@@ -29,40 +29,40 @@ pub struct ModulePath<'a> {
2929

3030
// Public for rustfmt usage.
3131
pub struct ModulePathSuccess {
32-
pub path: PathBuf,
33-
pub ownership: DirectoryOwnership,
32+
pub file_path: PathBuf,
33+
pub dir_ownership: DirOwnership,
3434
}
3535

3636
crate struct ParsedExternalMod {
3737
pub items: Vec<P<Item>>,
3838
pub inner_span: Span,
3939
pub file_path: PathBuf,
4040
pub dir_path: PathBuf,
41-
pub dir_ownership: DirectoryOwnership,
41+
pub dir_ownership: DirOwnership,
4242
}
4343

4444
crate fn parse_external_mod(
4545
sess: &Session,
46-
id: Ident,
46+
ident: Ident,
4747
span: Span, // The span to blame on errors.
4848
module: &ModuleData,
49-
mut dir_ownership: DirectoryOwnership,
49+
mut dir_ownership: DirOwnership,
5050
attrs: &mut Vec<Attribute>,
5151
) -> ParsedExternalMod {
5252
// We bail on the first error, but that error does not cause a fatal error... (1)
5353
let result: PResult<'_, _> = try {
5454
// Extract the file path and the new ownership.
55-
let mp = submod_path(sess, id, span, &attrs, dir_ownership, &module.dir_path)?;
56-
dir_ownership = mp.ownership;
55+
let mp = mod_file_path(sess, ident, span, &attrs, &module.dir_path, dir_ownership)?;
56+
dir_ownership = mp.dir_ownership;
5757

5858
// Ensure file paths are acyclic.
59-
error_on_circular_module(&sess.parse_sess, span, &mp.path, &module.file_path_stack)?;
59+
error_on_circular_module(&sess.parse_sess, span, &mp.file_path, &module.file_path_stack)?;
6060

6161
// Actually parse the external file as a module.
62-
let mut parser = new_parser_from_file(&sess.parse_sess, &mp.path, Some(span));
62+
let mut parser = new_parser_from_file(&sess.parse_sess, &mp.file_path, Some(span));
6363
let (mut inner_attrs, items, inner_span) = parser.parse_mod(&token::Eof)?;
6464
attrs.append(&mut inner_attrs);
65-
(items, inner_span, mp.path)
65+
(items, inner_span, mp.file_path)
6666
};
6767
// (1) ...instead, we return a dummy module.
6868
let (items, inner_span, file_path) = result.map_err(|mut err| err.emit()).unwrap_or_default();
@@ -76,80 +76,80 @@ crate fn parse_external_mod(
7676
fn error_on_circular_module<'a>(
7777
sess: &'a ParseSess,
7878
span: Span,
79-
path: &Path,
79+
file_path: &Path,
8080
file_path_stack: &[PathBuf],
8181
) -> PResult<'a, ()> {
82-
if let Some(i) = file_path_stack.iter().position(|p| *p == path) {
82+
if let Some(i) = file_path_stack.iter().position(|p| *p == file_path) {
8383
let mut err = String::from("circular modules: ");
8484
for p in &file_path_stack[i..] {
8585
err.push_str(&p.to_string_lossy());
8686
err.push_str(" -> ");
8787
}
88-
err.push_str(&path.to_string_lossy());
88+
err.push_str(&file_path.to_string_lossy());
8989
return Err(sess.span_diagnostic.struct_span_err(span, &err[..]));
9090
}
9191
Ok(())
9292
}
9393

94-
crate fn push_directory(
94+
crate fn mod_dir_path(
9595
sess: &Session,
96-
id: Ident,
96+
ident: Ident,
9797
attrs: &[Attribute],
9898
module: &ModuleData,
99-
mut dir_ownership: DirectoryOwnership,
100-
) -> (PathBuf, DirectoryOwnership) {
99+
mut dir_ownership: DirOwnership,
100+
) -> (PathBuf, DirOwnership) {
101101
let mut dir_path = module.dir_path.clone();
102102
if let Some(file_path) = sess.first_attr_value_str_by_name(attrs, sym::path) {
103103
dir_path.push(&*file_path.as_str());
104-
dir_ownership = DirectoryOwnership::Owned { relative: None };
104+
dir_ownership = DirOwnership::Owned { relative: None };
105105
} else {
106106
// We have to push on the current module name in the case of relative
107107
// paths in order to ensure that any additional module paths from inline
108108
// `mod x { ... }` come after the relative extension.
109109
//
110110
// For example, a `mod z { ... }` inside `x/y.rs` should set the current
111111
// directory path to `/x/y/z`, not `/x/z` with a relative offset of `y`.
112-
if let DirectoryOwnership::Owned { relative } = &mut dir_ownership {
112+
if let DirOwnership::Owned { relative } = &mut dir_ownership {
113113
if let Some(ident) = relative.take() {
114114
// Remove the relative offset.
115115
dir_path.push(&*ident.as_str());
116116
}
117117
}
118-
dir_path.push(&*id.as_str());
118+
dir_path.push(&*ident.as_str());
119119
}
120120

121121
(dir_path, dir_ownership)
122122
}
123123

124-
fn submod_path<'a>(
124+
fn mod_file_path<'a>(
125125
sess: &'a Session,
126-
id: Ident,
126+
ident: Ident,
127127
span: Span,
128128
attrs: &[Attribute],
129-
ownership: DirectoryOwnership,
130129
dir_path: &Path,
130+
dir_ownership: DirOwnership,
131131
) -> PResult<'a, ModulePathSuccess> {
132-
if let Some(path) = submod_path_from_attr(sess, attrs, dir_path) {
132+
if let Some(file_path) = mod_file_path_from_attr(sess, attrs, dir_path) {
133133
// All `#[path]` files are treated as though they are a `mod.rs` file.
134134
// This means that `mod foo;` declarations inside `#[path]`-included
135135
// files are siblings,
136136
//
137137
// Note that this will produce weirdness when a file named `foo.rs` is
138138
// `#[path]` included and contains a `mod foo;` declaration.
139139
// If you encounter this, it's your own darn fault :P
140-
let ownership = DirectoryOwnership::Owned { relative: None };
141-
return Ok(ModulePathSuccess { ownership, path });
140+
let dir_ownership = DirOwnership::Owned { relative: None };
141+
return Ok(ModulePathSuccess { file_path, dir_ownership });
142142
}
143143

144-
let relative = match ownership {
145-
DirectoryOwnership::Owned { relative } => relative,
146-
DirectoryOwnership::UnownedViaBlock => None,
144+
let relative = match dir_ownership {
145+
DirOwnership::Owned { relative } => relative,
146+
DirOwnership::UnownedViaBlock => None,
147147
};
148148
let ModulePath { path_exists, name, result } =
149-
default_submod_path(&sess.parse_sess, id, span, relative, dir_path);
150-
match ownership {
151-
DirectoryOwnership::Owned { .. } => Ok(result?),
152-
DirectoryOwnership::UnownedViaBlock => {
149+
default_submod_path(&sess.parse_sess, ident, span, relative, dir_path);
150+
match dir_ownership {
151+
DirOwnership::Owned { .. } => Ok(result?),
152+
DirOwnership::UnownedViaBlock => {
153153
let _ = result.map_err(|mut err| err.cancel());
154154
error_decl_mod_in_block(&sess.parse_sess, span, path_exists, &name)
155155
}
@@ -173,7 +173,7 @@ fn error_decl_mod_in_block<'a, T>(
173173

174174
/// Derive a submodule path from the first found `#[path = "path_string"]`.
175175
/// The provided `dir_path` is joined with the `path_string`.
176-
pub(super) fn submod_path_from_attr(
176+
fn mod_file_path_from_attr(
177177
sess: &Session,
178178
attrs: &[Attribute],
179179
dir_path: &Path,
@@ -196,15 +196,15 @@ pub(super) fn submod_path_from_attr(
196196
// Public for rustfmt usage.
197197
pub fn default_submod_path<'a>(
198198
sess: &'a ParseSess,
199-
id: Ident,
199+
ident: Ident,
200200
span: Span,
201201
relative: Option<Ident>,
202202
dir_path: &Path,
203203
) -> ModulePath<'a> {
204204
// If we're in a foo.rs file instead of a mod.rs file,
205205
// we need to look for submodules in
206-
// `./foo/<id>.rs` and `./foo/<id>/mod.rs` rather than
207-
// `./<id>.rs` and `./<id>/mod.rs`.
206+
// `./foo/<ident>.rs` and `./foo/<ident>/mod.rs` rather than
207+
// `./<ident>.rs` and `./<ident>/mod.rs`.
208208
let relative_prefix_string;
209209
let relative_prefix = if let Some(ident) = relative {
210210
relative_prefix_string = format!("{}{}", ident.name, path::MAIN_SEPARATOR);
@@ -213,7 +213,7 @@ pub fn default_submod_path<'a>(
213213
""
214214
};
215215

216-
let mod_name = id.name.to_string();
216+
let mod_name = ident.name.to_string();
217217
let default_path_str = format!("{}{}.rs", relative_prefix, mod_name);
218218
let secondary_path_str =
219219
format!("{}{}{}mod.rs", relative_prefix, mod_name, path::MAIN_SEPARATOR);
@@ -224,12 +224,12 @@ pub fn default_submod_path<'a>(
224224

225225
let result = match (default_exists, secondary_exists) {
226226
(true, false) => Ok(ModulePathSuccess {
227-
path: default_path,
228-
ownership: DirectoryOwnership::Owned { relative: Some(id) },
227+
file_path: default_path,
228+
dir_ownership: DirOwnership::Owned { relative: Some(ident) },
229229
}),
230230
(false, true) => Ok(ModulePathSuccess {
231-
path: secondary_path,
232-
ownership: DirectoryOwnership::Owned { relative: None },
231+
file_path: secondary_path,
232+
dir_ownership: DirOwnership::Owned { relative: None },
233233
}),
234234
(false, false) => {
235235
let mut err = struct_span_err!(

0 commit comments

Comments
 (0)