1
Fork 0

auto merge of #17163 : pcwalton/rust/impls-next-to-struct, r=alexcrichton

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 {
            ...
        }
    }

Closes #17059.

RFC #155.

[breaking-change]

r? @brson
This commit is contained in:
bors 2014-09-14 08:11:04 +00:00
commit 13037a3727
20 changed files with 89 additions and 36 deletions

View file

@ -20,6 +20,7 @@ use util::logv;
use util; use util;
use std::io::File; use std::io::File;
use std::io::fs::PathExtensions;
use std::io::fs; use std::io::fs;
use std::io::net::tcp; use std::io::net::tcp;
use std::io::process::ProcessExit; use std::io::process::ProcessExit;

View file

@ -37,6 +37,7 @@
use std::cell::Cell; use std::cell::Cell;
use std::{cmp, os, path}; use std::{cmp, os, path};
use std::io::fs::PathExtensions;
use std::io::fs; use std::io::fs;
use std::path::is_sep; use std::path::is_sep;
use std::string::String; use std::string::String;

View file

@ -21,6 +21,7 @@ use std::rt::rtio;
use super::file; use super::file;
use super::util; use super::util;
#[cfg(windows)] use std::io::fs::PathExtensions;
#[cfg(windows)] use std::string::String; #[cfg(windows)] use std::string::String;
#[cfg(unix)] use super::c; #[cfg(unix)] use super::c;
#[cfg(unix)] use super::retry; #[cfg(unix)] use super::retry;

View file

@ -28,6 +28,7 @@ use util::ppaux;
use util::sha2::{Digest, Sha256}; use util::sha2::{Digest, Sha256};
use std::char; use std::char;
use std::io::fs::PathExtensions;
use std::io::{fs, TempDir, Command}; use std::io::{fs, TempDir, Command};
use std::io; use std::io;
use std::mem; use std::mem;

View file

@ -11,9 +11,10 @@
#![allow(non_camel_case_types)] #![allow(non_camel_case_types)]
use std::cell::RefCell; use std::cell::RefCell;
use std::os;
use std::io::fs;
use std::collections::HashSet; use std::collections::HashSet;
use std::io::fs::PathExtensions;
use std::io::fs;
use std::os;
use util::fs as myfs; use util::fs as myfs;

View file

@ -229,6 +229,7 @@ use util::fs;
use std::c_str::ToCStr; use std::c_str::ToCStr;
use std::cmp; use std::cmp;
use std::io::fs::PathExtensions;
use std::io; use std::io;
use std::mem; use std::mem;
use std::ptr; use std::ptr;

View file

@ -1313,10 +1313,6 @@ impl<'a> Resolver<'a> {
// If this implements an anonymous trait, then add all the // If this implements an anonymous trait, then add all the
// methods within to a new module, if the type was defined // methods within to a new module, if the type was defined
// within this module. // 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. // Create the module and add all methods.
match ty.node { 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 parent

View file

@ -10,6 +10,7 @@
//! A helper class for dealing with static archives //! A helper class for dealing with static archives
use std::io::fs::PathExtensions;
use std::io::process::{Command, ProcessOutput}; use std::io::process::{Command, ProcessOutput};
use std::io::{fs, TempDir}; use std::io::{fs, TempDir};
use std::io; use std::io;

View file

@ -35,6 +35,7 @@
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::fmt; use std::fmt;
use std::io::fs::PathExtensions;
use std::io::{fs, File, BufferedWriter, MemWriter, BufferedReader}; use std::io::{fs, File, BufferedWriter, MemWriter, BufferedReader};
use std::io; use std::io;
use std::str; use std::str;

View file

@ -31,6 +31,7 @@ particular bits of it, etc.
```rust ```rust
# #![allow(unused_must_use)] # #![allow(unused_must_use)]
use std::io::fs::PathExtensions;
use std::io::{File, fs}; use std::io::{File, fs};
let path = Path::new("foo.txt"); let path = Path::new("foo.txt");
@ -622,8 +623,9 @@ pub fn rmdir(path: &Path) -> IoResult<()> {
/// # Example /// # Example
/// ///
/// ```rust /// ```rust
/// use std::io; /// use std::io::fs::PathExtensions;
/// use std::io::fs; /// use std::io::fs;
/// use std::io;
/// ///
/// // one possible implementation of fs::walk_dir only visiting files /// // one possible implementation of fs::walk_dir only visiting files
/// fn visit_dirs(dir: &Path, cb: |&Path|) -> io::IoResult<()> { /// 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. /// Get information on the file, directory, etc at this path.
/// ///
/// Consult the `fs::stat` documentation for more info. /// Consult the `fs::stat` documentation for more info.
/// ///
/// This call preserves identical runtime/error semantics with `file::stat`. /// 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 /// Get information on the file, directory, etc at this path, not following
/// symlinks. /// symlinks.
@ -882,31 +885,39 @@ impl path::Path {
/// Consult the `fs::lstat` documentation for more info. /// Consult the `fs::lstat` documentation for more info.
/// ///
/// This call preserves identical runtime/error semantics with `file::lstat`. /// 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 /// Boolean value indicator whether the underlying file exists on the local
/// filesystem. Returns false in exactly the cases where `fs::stat` fails. /// filesystem. Returns false in exactly the cases where `fs::stat` fails.
pub fn exists(&self) -> bool { fn exists(&self) -> bool;
self.stat().is_ok()
}
/// Whether the underlying implementation (be it a file path, or something /// 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 /// 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 /// to non-existent locations or directories or other non-regular files
/// (named pipes, etc). Follows links when making this determination. /// (named pipes, etc). Follows links when making this determination.
pub fn is_file(&self) -> bool { fn is_file(&self) -> bool;
match self.stat() {
Ok(s) => s.kind == io::TypeFile,
Err(..) => false
}
}
/// Whether the underlying implementation (be it a file path, or something /// Whether the underlying implementation (be it a file path, or something
/// else) is pointing at a directory in the underlying FS. Will return /// 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 /// 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 /// directory (eg files, named pipes, etc). Follows links when making this
/// determination. /// 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() { match self.stat() {
Ok(s) => s.kind == io::TypeDirectory, Ok(s) => s.kind == io::TypeDirectory,
Err(..) => false Err(..) => false

View file

@ -1721,6 +1721,7 @@ pub enum FileType {
/// # Example /// # Example
/// ///
/// ``` /// ```
/// # use std::io::fs::PathExtensions;
/// # fn main() {} /// # fn main() {}
/// # fn foo() { /// # fn foo() {
/// let info = match Path::new("foo.txt").stat() { /// let info = match Path::new("foo.txt").stat() {

View file

@ -53,6 +53,8 @@ actually operates on the path; it is only intended for display.
## Example ## Example
```rust ```rust
use std::io::fs::PathExtensions;
let mut path = Path::new("/tmp/path"); let mut path = Path::new("/tmp/path");
println!("path: {}", path.display()); println!("path: {}", path.display());
path.set_filename("foo"); path.set_filename("foo");

View file

@ -82,6 +82,7 @@ use ptr::P;
use owned_slice::OwnedSlice; use owned_slice::OwnedSlice;
use std::collections::HashSet; use std::collections::HashSet;
use std::io::fs::PathExtensions;
use std::mem::replace; use std::mem::replace;
use std::mem; use std::mem;
use std::rc::Rc; use std::rc::Rc;

View file

@ -13,6 +13,7 @@
//! Does not support hashed database, only filesystem! //! Does not support hashed database, only filesystem!
use std::io::File; use std::io::File;
use std::io::fs::PathExtensions;
use std::os::getenv; use std::os::getenv;
use std::os; use std::os;

View file

@ -56,6 +56,7 @@ use std::f64;
use std::fmt; use std::fmt;
use std::fmt::Show; use std::fmt::Show;
use std::from_str::FromStr; use std::from_str::FromStr;
use std::io::fs::PathExtensions;
use std::io::stdio::StdWriter; use std::io::stdio::StdWriter;
use std::io::{File, ChanReader, ChanWriter}; use std::io::{File, ChanReader, ChanWriter};
use std::io; use std::io;

View file

@ -13,6 +13,18 @@ pub struct B<T>;
pub mod test { pub mod test {
pub struct A<T>; 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> { 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 { pub fn foo() -> int {
let a = A::<()>; let a = A::<()>;
let b = B::<()>; let b = B::<()>;

View file

@ -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() {}

View file

@ -14,9 +14,10 @@
extern crate libc; extern crate libc;
use std::io::TempDir; use std::io::TempDir;
use std::os; use std::io::fs::PathExtensions;
use std::io;
use std::io::fs; use std::io::fs;
use std::io;
use std::os;
fn rename_directory() { fn rename_directory() {
unsafe { unsafe {

View file

@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
use std::io::fs::PathExtensions;
use std::io::{File, TempDir}; use std::io::{File, TempDir};
pub fn main() { pub fn main() {

View file

@ -20,6 +20,7 @@
extern crate debug; extern crate debug;
use std::io::fs::PathExtensions;
use std::io::{fs, TempDir}; use std::io::{fs, TempDir};
use std::io; use std::io;
use std::os; use std::os;