resolve: Prohibit relative paths in visibilities on 2018 edition

This commit is contained in:
Vadim Petrochenkov 2018-10-31 00:22:19 +03:00
parent abe19a7305
commit d4a78da543
3 changed files with 41 additions and 1 deletions

View file

@ -4710,7 +4710,18 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
ty::Visibility::Restricted(self.current_module.normal_ancestor_id)
}
ast::VisibilityKind::Restricted { ref path, id, .. } => {
// Visibilities are resolved as global by default, add starting root segment.
// For visibilities we are not ready to provide correct implementation of "uniform
// paths" right now, so on 2018 edition we only allow module-relative paths for now.
let first_ident = path.segments[0].ident;
if self.session.rust_2018() && !first_ident.is_path_segment_keyword() {
let msg = "relative paths are not supported in visibilities on 2018 edition";
self.session.struct_span_err(first_ident.span, msg)
.span_suggestion(path.span, "try", format!("crate::{}", path))
.emit();
return ty::Visibility::Public;
}
// On 2015 visibilities are resolved as crate-relative by default,
// add starting root segment if necessary.
let segments = path.make_root().iter().chain(path.segments.iter())
.map(|seg| Segment { ident: seg.ident, id: Some(seg.id) })
.collect::<Vec<_>>();

View file

@ -0,0 +1,13 @@
// edition:2018
mod m {
pub(in crate) struct S1; // OK
pub(in super) struct S2; // OK
pub(in self) struct S3; // OK
pub(in ::core) struct S4;
//~^ ERROR visibilities can only be restricted to ancestor modules
pub(in a::b) struct S5;
//~^ ERROR relative paths are not supported in visibilities on 2018 edition
}
fn main() {}

View file

@ -0,0 +1,16 @@
error: visibilities can only be restricted to ancestor modules
--> $DIR/relative-2018.rs:7:12
|
LL | pub(in ::core) struct S4;
| ^^^^^^
error: relative paths are not supported in visibilities on 2018 edition
--> $DIR/relative-2018.rs:9:12
|
LL | pub(in a::b) struct S5;
| ^---
| |
| help: try: `crate::a::b`
error: aborting due to 2 previous errors