Skip to content

Commit d4a78da

Browse files
committed
resolve: Prohibit relative paths in visibilities on 2018 edition
1 parent abe19a7 commit d4a78da

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

src/librustc_resolve/lib.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -4710,7 +4710,18 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
47104710
ty::Visibility::Restricted(self.current_module.normal_ancestor_id)
47114711
}
47124712
ast::VisibilityKind::Restricted { ref path, id, .. } => {
4713-
// Visibilities are resolved as global by default, add starting root segment.
4713+
// For visibilities we are not ready to provide correct implementation of "uniform
4714+
// paths" right now, so on 2018 edition we only allow module-relative paths for now.
4715+
let first_ident = path.segments[0].ident;
4716+
if self.session.rust_2018() && !first_ident.is_path_segment_keyword() {
4717+
let msg = "relative paths are not supported in visibilities on 2018 edition";
4718+
self.session.struct_span_err(first_ident.span, msg)
4719+
.span_suggestion(path.span, "try", format!("crate::{}", path))
4720+
.emit();
4721+
return ty::Visibility::Public;
4722+
}
4723+
// On 2015 visibilities are resolved as crate-relative by default,
4724+
// add starting root segment if necessary.
47144725
let segments = path.make_root().iter().chain(path.segments.iter())
47154726
.map(|seg| Segment { ident: seg.ident, id: Some(seg.id) })
47164727
.collect::<Vec<_>>();
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() {}
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)