about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorPatrick Walton <pcwalton@mimiga.net>2014-09-10 22:26:41 -0700
committerPatrick Walton <pcwalton@mimiga.net>2014-09-13 02:07:39 -0700
commit467bea04fa1d5fd894d64b2b2901d94260301631 (patch)
treea57cedfccc75f43f37995d0de6f9cb3f1bf27900 /src
parenta9cf19889ae7030c7df25cf41105906d12616f1e (diff)
downloadrust-467bea04fa1d5fd894d64b2b2901d94260301631.tar.gz
rust-467bea04fa1d5fd894d64b2b2901d94260301631.zip
librustc: Forbid inherent implementations that aren't adjacent to the
type they provide an implementation for.

This breaks code like:

    mod foo {
        struct Foo { ... }
    }

    impl foo::Foo {
        ...
    }

Change this code to:

    mod foo {
        struct Foo { ... }

        impl Foo {
            ...
        }
    }

Additionally, if you used the I/O path extension methods `stat`,
`lstat`, `exists`, `is_file`, or `is_dir`, note that these methods have
been moved to the the `std::io::fs::PathExtensions` trait. This breaks
code like:

    fn is_it_there() -> bool {
        Path::new("/foo/bar/baz").exists()
    }

Change this code to:

    use std::io::fs::PathExtensions;

    fn is_it_there() -> bool {
        Path::new("/foo/bar/baz").exists()
    }

Closes #17059.

RFC #155.

[breaking-change]
Diffstat (limited to 'src')
-rw-r--r--src/compiletest/runtest.rs1
-rw-r--r--src/libglob/lib.rs1
-rw-r--r--src/libnative/io/process.rs1
-rw-r--r--src/librustc/back/link.rs1
-rw-r--r--src/librustc/metadata/filesearch.rs5
-rw-r--r--src/librustc/metadata/loader.rs1
-rw-r--r--src/librustc/middle/resolve.rs12
-rw-r--r--src/librustc_back/archive.rs1
-rw-r--r--src/librustdoc/html/render.rs1
-rw-r--r--src/libstd/io/fs.rs39
-rw-r--r--src/libstd/io/mod.rs1
-rw-r--r--src/libstd/path/mod.rs2
-rw-r--r--src/libsyntax/parse/parser.rs1
-rw-r--r--src/libterm/terminfo/searcher.rs1
-rw-r--r--src/libtest/lib.rs1
-rw-r--r--src/test/auxiliary/inner_static.rs24
-rw-r--r--src/test/compile-fail/impl-not-adjacent-to-type.rs24
-rw-r--r--src/test/run-pass/rename-directory.rs5
-rw-r--r--src/test/run-pass/stat.rs2
-rw-r--r--src/test/run-pass/tempfile.rs1
20 files changed, 89 insertions, 36 deletions
diff --git a/src/compiletest/runtest.rs b/src/compiletest/runtest.rs
index 32671722ba7..dfc0b7c1628 100644
--- a/src/compiletest/runtest.rs
+++ b/src/compiletest/runtest.rs
@@ -20,6 +20,7 @@ use util::logv;
 use util;
 
 use std::io::File;
+use std::io::fs::PathExtensions;
 use std::io::fs;
 use std::io::net::tcp;
 use std::io::process::ProcessExit;
diff --git a/src/libglob/lib.rs b/src/libglob/lib.rs
index 39193547bf1..0cc9bb1a8aa 100644
--- a/src/libglob/lib.rs
+++ b/src/libglob/lib.rs
@@ -37,6 +37,7 @@
 
 use std::cell::Cell;
 use std::{cmp, os, path};
+use std::io::fs::PathExtensions;
 use std::io::fs;
 use std::path::is_sep;
 use std::string::String;
diff --git a/src/libnative/io/process.rs b/src/libnative/io/process.rs
index cad2ed0b97e..e5165929ad3 100644
--- a/src/libnative/io/process.rs
+++ b/src/libnative/io/process.rs
@@ -21,6 +21,7 @@ use std::rt::rtio;
 use super::file;
 use super::util;
 
+#[cfg(windows)] use std::io::fs::PathExtensions;
 #[cfg(windows)] use std::string::String;
 #[cfg(unix)] use super::c;
 #[cfg(unix)] use super::retry;
diff --git a/src/librustc/back/link.rs b/src/librustc/back/link.rs
index e418284ffce..9a38105b70b 100644
--- a/src/librustc/back/link.rs
+++ b/src/librustc/back/link.rs
@@ -28,6 +28,7 @@ use util::ppaux;
 use util::sha2::{Digest, Sha256};
 
 use std::char;
+use std::io::fs::PathExtensions;
 use std::io::{fs, TempDir, Command};
 use std::io;
 use std::mem;
diff --git a/src/librustc/metadata/filesearch.rs b/src/librustc/metadata/filesearch.rs
index bdabb3efb98..148aaf95686 100644
--- a/src/librustc/metadata/filesearch.rs
+++ b/src/librustc/metadata/filesearch.rs
@@ -11,9 +11,10 @@
 #![allow(non_camel_case_types)]
 
 use std::cell::RefCell;
-use std::os;
-use std::io::fs;
 use std::collections::HashSet;
+use std::io::fs::PathExtensions;
+use std::io::fs;
+use std::os;
 
 use util::fs as myfs;
 
diff --git a/src/librustc/metadata/loader.rs b/src/librustc/metadata/loader.rs
index 5face544c89..fc20a86af70 100644
--- a/src/librustc/metadata/loader.rs
+++ b/src/librustc/metadata/loader.rs
@@ -229,6 +229,7 @@ use util::fs;
 
 use std::c_str::ToCStr;
 use std::cmp;
+use std::io::fs::PathExtensions;
 use std::io;
 use std::mem;
 use std::ptr;
diff --git a/src/librustc/middle/resolve.rs b/src/librustc/middle/resolve.rs
index ed795ff0aac..12dc04010e4 100644
--- a/src/librustc/middle/resolve.rs
+++ b/src/librustc/middle/resolve.rs
@@ -1313,10 +1313,6 @@ impl<'a> Resolver<'a> {
                 // If this implements an anonymous trait, then add all the
                 // methods within to a new module, if the type was defined
                 // within this module.
-                //
-                // FIXME (#3785): This is quite unsatisfactory. Perhaps we
-                // should modify anonymous traits to only be implementable in
-                // the same module that declared the type.
 
                 // Create the module and add all methods.
                 match ty.node {
@@ -1400,7 +1396,13 @@ impl<'a> Resolver<'a> {
                             }
                         }
                     }
-                    _ => {}
+                    _ => {
+                        self.resolve_error(ty.span,
+                                           "inherent implementations may \
+                                            only be implemented in the same \
+                                            module as the type they are \
+                                            implemented for")
+                    }
                 }
 
                 parent
diff --git a/src/librustc_back/archive.rs b/src/librustc_back/archive.rs
index bd6770b3256..060dda5934f 100644
--- a/src/librustc_back/archive.rs
+++ b/src/librustc_back/archive.rs
@@ -10,6 +10,7 @@
 
 //! A helper class for dealing with static archives
 
+use std::io::fs::PathExtensions;
 use std::io::process::{Command, ProcessOutput};
 use std::io::{fs, TempDir};
 use std::io;
diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs
index 77e0a641e18..06346e0b6b9 100644
--- a/src/librustdoc/html/render.rs
+++ b/src/librustdoc/html/render.rs
@@ -35,6 +35,7 @@
 
 use std::collections::{HashMap, HashSet};
 use std::fmt;
+use std::io::fs::PathExtensions;
 use std::io::{fs, File, BufferedWriter, MemWriter, BufferedReader};
 use std::io;
 use std::str;
diff --git a/src/libstd/io/fs.rs b/src/libstd/io/fs.rs
index 3d6bb96063e..b7b7d1fb93d 100644
--- a/src/libstd/io/fs.rs
+++ b/src/libstd/io/fs.rs
@@ -31,6 +31,7 @@ particular bits of it, etc.
 
 ```rust
 # #![allow(unused_must_use)]
+use std::io::fs::PathExtensions;
 use std::io::{File, fs};
 
 let path = Path::new("foo.txt");
@@ -622,8 +623,9 @@ pub fn rmdir(path: &Path) -> IoResult<()> {
 /// # Example
 ///
 /// ```rust
-/// use std::io;
+/// use std::io::fs::PathExtensions;
 /// use std::io::fs;
+/// use std::io;
 ///
 /// // one possible implementation of fs::walk_dir only visiting files
 /// fn visit_dirs(dir: &Path, cb: |&Path|) -> io::IoResult<()> {
@@ -868,13 +870,14 @@ impl Seek for File {
     }
 }
 
-impl path::Path {
+/// Utility methods for paths.
+pub trait PathExtensions {
     /// Get information on the file, directory, etc at this path.
     ///
     /// Consult the `fs::stat` documentation for more info.
     ///
     /// This call preserves identical runtime/error semantics with `file::stat`.
-    pub fn stat(&self) -> IoResult<FileStat> { stat(self) }
+    fn stat(&self) -> IoResult<FileStat>;
 
     /// Get information on the file, directory, etc at this path, not following
     /// symlinks.
@@ -882,31 +885,39 @@ impl path::Path {
     /// Consult the `fs::lstat` documentation for more info.
     ///
     /// This call preserves identical runtime/error semantics with `file::lstat`.
-    pub fn lstat(&self) -> IoResult<FileStat> { lstat(self) }
+    fn lstat(&self) -> IoResult<FileStat>;
 
     /// Boolean value indicator whether the underlying file exists on the local
     /// filesystem. Returns false in exactly the cases where `fs::stat` fails.
-    pub fn exists(&self) -> bool {
-        self.stat().is_ok()
-    }
+    fn exists(&self) -> bool;
 
     /// Whether the underlying implementation (be it a file path, or something
     /// else) points at a "regular file" on the FS. Will return false for paths
     /// to non-existent locations or directories or other non-regular files
     /// (named pipes, etc). Follows links when making this determination.
-    pub fn is_file(&self) -> bool {
-        match self.stat() {
-            Ok(s) => s.kind == io::TypeFile,
-            Err(..) => false
-        }
-    }
+    fn is_file(&self) -> bool;
 
     /// Whether the underlying implementation (be it a file path, or something
     /// else) is pointing at a directory in the underlying FS. Will return
     /// false for paths to non-existent locations or if the item is not a
     /// directory (eg files, named pipes, etc). Follows links when making this
     /// determination.
-    pub fn is_dir(&self) -> bool {
+    fn is_dir(&self) -> bool;
+}
+
+impl PathExtensions for path::Path {
+    fn stat(&self) -> IoResult<FileStat> { stat(self) }
+    fn lstat(&self) -> IoResult<FileStat> { lstat(self) }
+    fn exists(&self) -> bool {
+        self.stat().is_ok()
+    }
+    fn is_file(&self) -> bool {
+        match self.stat() {
+            Ok(s) => s.kind == io::TypeFile,
+            Err(..) => false
+        }
+    }
+    fn is_dir(&self) -> bool {
         match self.stat() {
             Ok(s) => s.kind == io::TypeDirectory,
             Err(..) => false
diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs
index 0d0c9e933b0..7a2bcab8706 100644
--- a/src/libstd/io/mod.rs
+++ b/src/libstd/io/mod.rs
@@ -1721,6 +1721,7 @@ pub enum FileType {
 /// # Example
 ///
 /// ```
+/// # use std::io::fs::PathExtensions;
 /// # fn main() {}
 /// # fn foo() {
 /// let info = match Path::new("foo.txt").stat() {
diff --git a/src/libstd/path/mod.rs b/src/libstd/path/mod.rs
index 5a5068f4d01..d84848545bd 100644
--- a/src/libstd/path/mod.rs
+++ b/src/libstd/path/mod.rs
@@ -53,6 +53,8 @@ actually operates on the path; it is only intended for display.
 ## Example
 
 ```rust
+use std::io::fs::PathExtensions;
+
 let mut path = Path::new("/tmp/path");
 println!("path: {}", path.display());
 path.set_filename("foo");
diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs
index 328bdf88335..b4c8ecb3812 100644
--- a/src/libsyntax/parse/parser.rs
+++ b/src/libsyntax/parse/parser.rs
@@ -82,6 +82,7 @@ use parse::{new_sub_parser_from_file, ParseSess};
 use owned_slice::OwnedSlice;
 
 use std::collections::HashSet;
+use std::io::fs::PathExtensions;
 use std::mem::replace;
 use std::rc::Rc;
 use std::gc::{Gc, GC};
diff --git a/src/libterm/terminfo/searcher.rs b/src/libterm/terminfo/searcher.rs
index ebec59924e8..a89afab505b 100644
--- a/src/libterm/terminfo/searcher.rs
+++ b/src/libterm/terminfo/searcher.rs
@@ -13,6 +13,7 @@
 //! Does not support hashed database, only filesystem!
 
 use std::io::File;
+use std::io::fs::PathExtensions;
 use std::os::getenv;
 use std::os;
 
diff --git a/src/libtest/lib.rs b/src/libtest/lib.rs
index df69443ed28..ea5c8424ea3 100644
--- a/src/libtest/lib.rs
+++ b/src/libtest/lib.rs
@@ -56,6 +56,7 @@ use std::f64;
 use std::fmt;
 use std::fmt::Show;
 use std::from_str::FromStr;
+use std::io::fs::PathExtensions;
 use std::io::stdio::StdWriter;
 use std::io::{File, ChanReader, ChanWriter};
 use std::io;
diff --git a/src/test/auxiliary/inner_static.rs b/src/test/auxiliary/inner_static.rs
index 841da21c5e8..94acea06618 100644
--- a/src/test/auxiliary/inner_static.rs
+++ b/src/test/auxiliary/inner_static.rs
@@ -13,6 +13,18 @@ pub struct B<T>;
 
 pub mod test {
     pub struct A<T>;
+
+    impl<T> A<T> {
+        pub fn foo(&self) -> int {
+            static a: int = 5;
+            return a
+        }
+
+        pub fn bar(&self) -> int {
+            static a: int = 6;
+            return a;
+        }
+    }
 }
 
 impl<T> A<T> {
@@ -39,18 +51,6 @@ impl<T> B<T> {
     }
 }
 
-impl<T> test::A<T> {
-    pub fn foo(&self) -> int {
-        static a: int = 5;
-        return a
-    }
-
-    pub fn bar(&self) -> int {
-        static a: int = 6;
-        return a;
-    }
-}
-
 pub fn foo() -> int {
     let a = A::<()>;
     let b = B::<()>;
diff --git a/src/test/compile-fail/impl-not-adjacent-to-type.rs b/src/test/compile-fail/impl-not-adjacent-to-type.rs
new file mode 100644
index 00000000000..7a5428d63e8
--- /dev/null
+++ b/src/test/compile-fail/impl-not-adjacent-to-type.rs
@@ -0,0 +1,24 @@
+// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+mod foo {
+    pub struct Foo {
+        x: int,
+        y: int,
+    }
+}
+
+impl foo::Foo {
+//~^ ERROR implementations may only be implemented in the same module
+    fn bar() {}
+}
+
+fn main() {}
+
diff --git a/src/test/run-pass/rename-directory.rs b/src/test/run-pass/rename-directory.rs
index c1b542e0315..e1918335a14 100644
--- a/src/test/run-pass/rename-directory.rs
+++ b/src/test/run-pass/rename-directory.rs
@@ -14,9 +14,10 @@
 extern crate libc;
 
 use std::io::TempDir;
-use std::os;
-use std::io;
+use std::io::fs::PathExtensions;
 use std::io::fs;
+use std::io;
+use std::os;
 
 fn rename_directory() {
     unsafe {
diff --git a/src/test/run-pass/stat.rs b/src/test/run-pass/stat.rs
index d9eae02670f..67728e6c8dd 100644
--- a/src/test/run-pass/stat.rs
+++ b/src/test/run-pass/stat.rs
@@ -8,7 +8,7 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-
+use std::io::fs::PathExtensions;
 use std::io::{File, TempDir};
 
 pub fn main() {
diff --git a/src/test/run-pass/tempfile.rs b/src/test/run-pass/tempfile.rs
index 3a41cac1fa3..322bdc8f3bb 100644
--- a/src/test/run-pass/tempfile.rs
+++ b/src/test/run-pass/tempfile.rs
@@ -20,6 +20,7 @@
 
 extern crate debug;
 
+use std::io::fs::PathExtensions;
 use std::io::{fs, TempDir};
 use std::io;
 use std::os;