]> git.lizzy.rs Git - rust.git/commitdiff
resolve: Prohibit relative paths in visibilities on 2018 edition
authorVadim Petrochenkov <vadim.petrochenkov@gmail.com>
Tue, 30 Oct 2018 21:22:19 +0000 (00:22 +0300)
committerVadim Petrochenkov <vadim.petrochenkov@gmail.com>
Sun, 25 Nov 2018 13:40:03 +0000 (16:40 +0300)
src/librustc_resolve/lib.rs
src/test/ui/privacy/restricted/relative-2018.rs [new file with mode: 0644]
src/test/ui/privacy/restricted/relative-2018.stderr [new file with mode: 0644]

index 12dabd2a31da18b94d541d7c9b27a41482049d2a..a392ab717c06c9908ae62890235859318e17974b 100644 (file)
@@ -4710,7 +4710,18 @@ fn resolve_visibility(&mut self, vis: &ast::Visibility) -> ty::Visibility {
                 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<_>>();
diff --git a/src/test/ui/privacy/restricted/relative-2018.rs b/src/test/ui/privacy/restricted/relative-2018.rs
new file mode 100644 (file)
index 0000000..69b7c1e
--- /dev/null
@@ -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() {}
diff --git a/src/test/ui/privacy/restricted/relative-2018.stderr b/src/test/ui/privacy/restricted/relative-2018.stderr
new file mode 100644 (file)
index 0000000..61effc4
--- /dev/null
@@ -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
+