Skip to content

Commit 3724f16

Browse files
committed
resolve: Prohibit relative paths in visibilities on 2018 edition
1 parent d4de9b1 commit 3724f16

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

src/librustc_resolve/lib.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4496,7 +4496,18 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
44964496
ty::Visibility::Restricted(self.current_module.normal_ancestor_id)
44974497
}
44984498
ast::VisibilityKind::Restricted { ref path, id, .. } => {
4499-
// Visibilities are resolved as global by default, add starting root segment.
4499+
// For visibilities we are not ready to provide correct implementation of "uniform
4500+
// paths" right now, so on 2018 edition we only allow module-relative paths for now.
4501+
let first_ident = path.segments[0].ident;
4502+
if self.session.rust_2018() && !first_ident.is_path_segment_keyword() {
4503+
let msg = "relative paths are not supported in visibilities on 2018 edition";
4504+
self.session.struct_span_err(first_ident.span, msg)
4505+
.span_suggestion(path.span, "try", format!("crate::{}", path))
4506+
.emit();
4507+
return ty::Visibility::Public;
4508+
}
4509+
// On 2015 visibilities are resolved as crate-relative by default,
4510+
// add starting root segment if necessary.
45004511
let segments = path.make_root().iter().chain(path.segments.iter())
45014512
.map(|seg| seg.ident)
45024513
.collect::<Vec<_>>();
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// edition:2018
2+
3+
mod m {
4+
pub(in crate) struct S1; // OK
5+
pub(in super) struct S2; // OK
6+
pub(in self) struct S3; // OK
7+
pub(in ::core) struct S4;
8+
//~^ ERROR visibilities can only be restricted to ancestor modules
9+
pub(in a::b) struct S5;
10+
//~^ ERROR relative paths are not supported in visibilities on 2018 edition
11+
}
12+
13+
fn main() {}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: visibilities can only be restricted to ancestor modules
2+
--> $DIR/relative-2018.rs:7:12
3+
|
4+
LL | pub(in ::core) struct S4;
5+
| ^^^^^^
6+
7+
error: relative paths are not supported in visibilities on 2018 edition
8+
--> $DIR/relative-2018.rs:9:12
9+
|
10+
LL | pub(in a::b) struct S5;
11+
| ^---
12+
| |
13+
| help: try: `crate::a::b`
14+
15+
error: aborting due to 2 previous errors
16+

0 commit comments

Comments
 (0)