Skip to content

Commit bc18eb4

Browse files
committed
expand: Remove obsolete DirectoryOwnership::UnownedViaMod
This ownership kind is only constructed in the case of path attributes like `#[path = ".."]` without a file name segment, which always represent some kind of directories and will produce and error on attempt to parse them as a module file.
1 parent 45b3c28 commit bc18eb4

File tree

3 files changed

+24
-52
lines changed

3 files changed

+24
-52
lines changed

compiler/rustc_expand/src/module.rs

Lines changed: 9 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ pub enum DirectoryOwnership {
2222
relative: Option<Ident>,
2323
},
2424
UnownedViaBlock,
25-
UnownedViaMod,
2625
}
2726

2827
/// Information about the path to a module.
@@ -134,23 +133,20 @@ fn submod_path<'a>(
134133
dir_path: &Path,
135134
) -> PResult<'a, ModulePathSuccess> {
136135
if let Some(path) = submod_path_from_attr(sess, attrs, dir_path) {
137-
let ownership = match path.file_name().and_then(|s| s.to_str()) {
138-
// All `#[path]` files are treated as though they are a `mod.rs` file.
139-
// This means that `mod foo;` declarations inside `#[path]`-included
140-
// files are siblings,
141-
//
142-
// Note that this will produce weirdness when a file named `foo.rs` is
143-
// `#[path]` included and contains a `mod foo;` declaration.
144-
// If you encounter this, it's your own darn fault :P
145-
Some(_) => DirectoryOwnership::Owned { relative: None },
146-
_ => DirectoryOwnership::UnownedViaMod,
147-
};
136+
// All `#[path]` files are treated as though they are a `mod.rs` file.
137+
// This means that `mod foo;` declarations inside `#[path]`-included
138+
// files are siblings,
139+
//
140+
// Note that this will produce weirdness when a file named `foo.rs` is
141+
// `#[path]` included and contains a `mod foo;` declaration.
142+
// If you encounter this, it's your own darn fault :P
143+
let ownership = DirectoryOwnership::Owned { relative: None };
148144
return Ok(ModulePathSuccess { ownership, path });
149145
}
150146

151147
let relative = match ownership {
152148
DirectoryOwnership::Owned { relative } => relative,
153-
DirectoryOwnership::UnownedViaBlock | DirectoryOwnership::UnownedViaMod => None,
149+
DirectoryOwnership::UnownedViaBlock => None,
154150
};
155151
let ModulePath { path_exists, name, result } =
156152
default_submod_path(&sess.parse_sess, id, span, relative, dir_path);
@@ -160,10 +156,6 @@ fn submod_path<'a>(
160156
let _ = result.map_err(|mut err| err.cancel());
161157
error_decl_mod_in_block(&sess.parse_sess, span, path_exists, &name)
162158
}
163-
DirectoryOwnership::UnownedViaMod => {
164-
let _ = result.map_err(|mut err| err.cancel());
165-
error_cannot_declare_mod_here(&sess.parse_sess, span, path_exists, &name)
166-
}
167159
}
168160
}
169161

@@ -182,41 +174,6 @@ fn error_decl_mod_in_block<'a, T>(
182174
Err(err)
183175
}
184176

185-
fn error_cannot_declare_mod_here<'a, T>(
186-
sess: &'a ParseSess,
187-
span: Span,
188-
path_exists: bool,
189-
name: &str,
190-
) -> PResult<'a, T> {
191-
let mut err =
192-
sess.span_diagnostic.struct_span_err(span, "cannot declare a new module at this location");
193-
if !span.is_dummy() {
194-
if let FileName::Real(src_name) = sess.source_map().span_to_filename(span) {
195-
let src_path = src_name.into_local_path();
196-
if let Some(stem) = src_path.file_stem() {
197-
let mut dest_path = src_path.clone();
198-
dest_path.set_file_name(stem);
199-
dest_path.push("mod.rs");
200-
err.span_note(
201-
span,
202-
&format!(
203-
"maybe move this module `{}` to its own directory via `{}`",
204-
src_path.display(),
205-
dest_path.display()
206-
),
207-
);
208-
}
209-
}
210-
}
211-
if path_exists {
212-
err.span_note(
213-
span,
214-
&format!("... or maybe `use` the module `{}` instead of possibly redeclaring it", name),
215-
);
216-
}
217-
Err(err)
218-
}
219-
220177
/// Derive a submodule path from the first found `#[path = "path_string"]`.
221178
/// The provided `dir_path` is joined with the `path_string`.
222179
pub(super) fn submod_path_from_attr(
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// normalize-stderr-test: "\.:.*\(" -> ".: $$ACCESS_DENIED_MSG ("
2+
// normalize-stderr-test: "os error \d+" -> "os error $$ACCESS_DENIED_CODE"
3+
4+
#[path = "."]
5+
mod m; //~ ERROR couldn't read
6+
7+
fn main() {}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: couldn't read $DIR/.: $ACCESS_DENIED_MSG (os error $ACCESS_DENIED_CODE)
2+
--> $DIR/path-no-file-name.rs:5:1
3+
|
4+
LL | mod m;
5+
| ^^^^^^
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)