From 467bea04fa1d5fd894d64b2b2901d94260301631 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Wed, 10 Sep 2014 22:26:41 -0700 Subject: [PATCH] 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] --- src/compiletest/runtest.rs | 1 + src/libglob/lib.rs | 1 + src/libnative/io/process.rs | 1 + src/librustc/back/link.rs | 1 + src/librustc/metadata/filesearch.rs | 5 ++- src/librustc/metadata/loader.rs | 1 + src/librustc/middle/resolve.rs | 12 +++--- src/librustc_back/archive.rs | 1 + src/librustdoc/html/render.rs | 1 + src/libstd/io/fs.rs | 39 ++++++++++++------- src/libstd/io/mod.rs | 1 + src/libstd/path/mod.rs | 2 + src/libsyntax/parse/parser.rs | 1 + src/libterm/terminfo/searcher.rs | 1 + src/libtest/lib.rs | 1 + src/test/auxiliary/inner_static.rs | 24 ++++++------ .../compile-fail/impl-not-adjacent-to-type.rs | 24 ++++++++++++ src/test/run-pass/rename-directory.rs | 5 ++- src/test/run-pass/stat.rs | 2 +- src/test/run-pass/tempfile.rs | 1 + 20 files changed, 89 insertions(+), 36 deletions(-) create mode 100644 src/test/compile-fail/impl-not-adjacent-to-type.rs diff --git a/src/compiletest/runtest.rs b/src/compiletest/runtest.rs index 32671722ba728..dfc0b7c16280e 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 39193547bf130..0cc9bb1a8aa6c 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 cad2ed0b97ec5..e5165929ad388 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 e418284ffce7a..9a38105b70bea 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 bdabb3efb98ba..148aaf9568675 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 5face544c89bf..fc20a86af7064 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 ed795ff0aacc4..12dc04010e4bb 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 bd6770b32561c..060dda5934f8e 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 77e0a641e181d..06346e0b6b918 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 3d6bb96063ee1..b7b7d1fb93d1a 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 { stat(self) } + fn stat(&self) -> IoResult; /// 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 { lstat(self) } + fn lstat(&self) -> IoResult; /// 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 { stat(self) } + fn lstat(&self) -> IoResult { 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 0d0c9e933b0ab..7a2bcab8706da 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 5a5068f4d01d8..d84848545bda8 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 328bdf883356c..b4c8ecb38120a 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 ebec59924e8f3..a89afab505b8d 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 df69443ed28d2..ea5c8424ea302 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 841da21c5e845..94acea0661838 100644 --- a/src/test/auxiliary/inner_static.rs +++ b/src/test/auxiliary/inner_static.rs @@ -13,6 +13,18 @@ pub struct B; pub mod test { pub struct A; + + impl A { + pub fn foo(&self) -> int { + static a: int = 5; + return a + } + + pub fn bar(&self) -> int { + static a: int = 6; + return a; + } + } } impl A { @@ -39,18 +51,6 @@ impl B { } } -impl test::A { - 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 0000000000000..7a5428d63e83f --- /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 or the MIT license +// , 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 c1b542e03156c..e1918335a1478 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 d9eae02670f38..67728e6c8dd44 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 3a41cac1fa332..322bdc8f3bb77 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;