Skip to content

Commit 13037a3

Browse files
committed
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
2 parents 0f99aba + 467bea0 commit 13037a3

File tree

20 files changed

+89
-36
lines changed

20 files changed

+89
-36
lines changed

src/compiletest/runtest.rs

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use util::logv;
2020
use util;
2121

2222
use std::io::File;
23+
use std::io::fs::PathExtensions;
2324
use std::io::fs;
2425
use std::io::net::tcp;
2526
use std::io::process::ProcessExit;

src/libglob/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
use std::cell::Cell;
3939
use std::{cmp, os, path};
40+
use std::io::fs::PathExtensions;
4041
use std::io::fs;
4142
use std::path::is_sep;
4243
use std::string::String;

src/libnative/io/process.rs

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use std::rt::rtio;
2121
use super::file;
2222
use super::util;
2323

24+
#[cfg(windows)] use std::io::fs::PathExtensions;
2425
#[cfg(windows)] use std::string::String;
2526
#[cfg(unix)] use super::c;
2627
#[cfg(unix)] use super::retry;

src/librustc/back/link.rs

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use util::ppaux;
2828
use util::sha2::{Digest, Sha256};
2929

3030
use std::char;
31+
use std::io::fs::PathExtensions;
3132
use std::io::{fs, TempDir, Command};
3233
use std::io;
3334
use std::mem;

src/librustc/metadata/filesearch.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
#![allow(non_camel_case_types)]
1212

1313
use std::cell::RefCell;
14-
use std::os;
15-
use std::io::fs;
1614
use std::collections::HashSet;
15+
use std::io::fs::PathExtensions;
16+
use std::io::fs;
17+
use std::os;
1718

1819
use util::fs as myfs;
1920

src/librustc/metadata/loader.rs

+1
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ use util::fs;
229229

230230
use std::c_str::ToCStr;
231231
use std::cmp;
232+
use std::io::fs::PathExtensions;
232233
use std::io;
233234
use std::mem;
234235
use std::ptr;

src/librustc/middle/resolve.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -1313,10 +1313,6 @@ impl<'a> Resolver<'a> {
13131313
// If this implements an anonymous trait, then add all the
13141314
// methods within to a new module, if the type was defined
13151315
// within this module.
1316-
//
1317-
// FIXME (#3785): This is quite unsatisfactory. Perhaps we
1318-
// should modify anonymous traits to only be implementable in
1319-
// the same module that declared the type.
13201316

13211317
// Create the module and add all methods.
13221318
match ty.node {
@@ -1400,7 +1396,13 @@ impl<'a> Resolver<'a> {
14001396
}
14011397
}
14021398
}
1403-
_ => {}
1399+
_ => {
1400+
self.resolve_error(ty.span,
1401+
"inherent implementations may \
1402+
only be implemented in the same \
1403+
module as the type they are \
1404+
implemented for")
1405+
}
14041406
}
14051407

14061408
parent

src/librustc_back/archive.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
//! A helper class for dealing with static archives
1212
13+
use std::io::fs::PathExtensions;
1314
use std::io::process::{Command, ProcessOutput};
1415
use std::io::{fs, TempDir};
1516
use std::io;

src/librustdoc/html/render.rs

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
3636
use std::collections::{HashMap, HashSet};
3737
use std::fmt;
38+
use std::io::fs::PathExtensions;
3839
use std::io::{fs, File, BufferedWriter, MemWriter, BufferedReader};
3940
use std::io;
4041
use std::str;

src/libstd/io/fs.rs

+25-14
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ particular bits of it, etc.
3131
3232
```rust
3333
# #![allow(unused_must_use)]
34+
use std::io::fs::PathExtensions;
3435
use std::io::{File, fs};
3536
3637
let path = Path::new("foo.txt");
@@ -622,8 +623,9 @@ pub fn rmdir(path: &Path) -> IoResult<()> {
622623
/// # Example
623624
///
624625
/// ```rust
625-
/// use std::io;
626+
/// use std::io::fs::PathExtensions;
626627
/// use std::io::fs;
628+
/// use std::io;
627629
///
628630
/// // one possible implementation of fs::walk_dir only visiting files
629631
/// fn visit_dirs(dir: &Path, cb: |&Path|) -> io::IoResult<()> {
@@ -868,45 +870,54 @@ impl Seek for File {
868870
}
869871
}
870872

871-
impl path::Path {
873+
/// Utility methods for paths.
874+
pub trait PathExtensions {
872875
/// Get information on the file, directory, etc at this path.
873876
///
874877
/// Consult the `fs::stat` documentation for more info.
875878
///
876879
/// This call preserves identical runtime/error semantics with `file::stat`.
877-
pub fn stat(&self) -> IoResult<FileStat> { stat(self) }
880+
fn stat(&self) -> IoResult<FileStat>;
878881

879882
/// Get information on the file, directory, etc at this path, not following
880883
/// symlinks.
881884
///
882885
/// Consult the `fs::lstat` documentation for more info.
883886
///
884887
/// This call preserves identical runtime/error semantics with `file::lstat`.
885-
pub fn lstat(&self) -> IoResult<FileStat> { lstat(self) }
888+
fn lstat(&self) -> IoResult<FileStat>;
886889

887890
/// Boolean value indicator whether the underlying file exists on the local
888891
/// filesystem. Returns false in exactly the cases where `fs::stat` fails.
889-
pub fn exists(&self) -> bool {
890-
self.stat().is_ok()
891-
}
892+
fn exists(&self) -> bool;
892893

893894
/// Whether the underlying implementation (be it a file path, or something
894895
/// else) points at a "regular file" on the FS. Will return false for paths
895896
/// to non-existent locations or directories or other non-regular files
896897
/// (named pipes, etc). Follows links when making this determination.
897-
pub fn is_file(&self) -> bool {
898-
match self.stat() {
899-
Ok(s) => s.kind == io::TypeFile,
900-
Err(..) => false
901-
}
902-
}
898+
fn is_file(&self) -> bool;
903899

904900
/// Whether the underlying implementation (be it a file path, or something
905901
/// else) is pointing at a directory in the underlying FS. Will return
906902
/// false for paths to non-existent locations or if the item is not a
907903
/// directory (eg files, named pipes, etc). Follows links when making this
908904
/// determination.
909-
pub fn is_dir(&self) -> bool {
905+
fn is_dir(&self) -> bool;
906+
}
907+
908+
impl PathExtensions for path::Path {
909+
fn stat(&self) -> IoResult<FileStat> { stat(self) }
910+
fn lstat(&self) -> IoResult<FileStat> { lstat(self) }
911+
fn exists(&self) -> bool {
912+
self.stat().is_ok()
913+
}
914+
fn is_file(&self) -> bool {
915+
match self.stat() {
916+
Ok(s) => s.kind == io::TypeFile,
917+
Err(..) => false
918+
}
919+
}
920+
fn is_dir(&self) -> bool {
910921
match self.stat() {
911922
Ok(s) => s.kind == io::TypeDirectory,
912923
Err(..) => false

src/libstd/io/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1721,6 +1721,7 @@ pub enum FileType {
17211721
/// # Example
17221722
///
17231723
/// ```
1724+
/// # use std::io::fs::PathExtensions;
17241725
/// # fn main() {}
17251726
/// # fn foo() {
17261727
/// let info = match Path::new("foo.txt").stat() {

src/libstd/path/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ actually operates on the path; it is only intended for display.
5353
## Example
5454
5555
```rust
56+
use std::io::fs::PathExtensions;
57+
5658
let mut path = Path::new("/tmp/path");
5759
println!("path: {}", path.display());
5860
path.set_filename("foo");

src/libsyntax/parse/parser.rs

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ use ptr::P;
8282
use owned_slice::OwnedSlice;
8383

8484
use std::collections::HashSet;
85+
use std::io::fs::PathExtensions;
8586
use std::mem::replace;
8687
use std::mem;
8788
use std::rc::Rc;

src/libterm/terminfo/searcher.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
//! Does not support hashed database, only filesystem!
1414
1515
use std::io::File;
16+
use std::io::fs::PathExtensions;
1617
use std::os::getenv;
1718
use std::os;
1819

src/libtest/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ use std::f64;
5656
use std::fmt;
5757
use std::fmt::Show;
5858
use std::from_str::FromStr;
59+
use std::io::fs::PathExtensions;
5960
use std::io::stdio::StdWriter;
6061
use std::io::{File, ChanReader, ChanWriter};
6162
use std::io;

src/test/auxiliary/inner_static.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ pub struct B<T>;
1313

1414
pub mod test {
1515
pub struct A<T>;
16+
17+
impl<T> A<T> {
18+
pub fn foo(&self) -> int {
19+
static a: int = 5;
20+
return a
21+
}
22+
23+
pub fn bar(&self) -> int {
24+
static a: int = 6;
25+
return a;
26+
}
27+
}
1628
}
1729

1830
impl<T> A<T> {
@@ -39,18 +51,6 @@ impl<T> B<T> {
3951
}
4052
}
4153

42-
impl<T> test::A<T> {
43-
pub fn foo(&self) -> int {
44-
static a: int = 5;
45-
return a
46-
}
47-
48-
pub fn bar(&self) -> int {
49-
static a: int = 6;
50-
return a;
51-
}
52-
}
53-
5454
pub fn foo() -> int {
5555
let a = A::<()>;
5656
let b = B::<()>;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
mod foo {
12+
pub struct Foo {
13+
x: int,
14+
y: int,
15+
}
16+
}
17+
18+
impl foo::Foo {
19+
//~^ ERROR implementations may only be implemented in the same module
20+
fn bar() {}
21+
}
22+
23+
fn main() {}
24+

src/test/run-pass/rename-directory.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
extern crate libc;
1515

1616
use std::io::TempDir;
17-
use std::os;
18-
use std::io;
17+
use std::io::fs::PathExtensions;
1918
use std::io::fs;
19+
use std::io;
20+
use std::os;
2021

2122
fn rename_directory() {
2223
unsafe {

src/test/run-pass/stat.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
11+
use std::io::fs::PathExtensions;
1212
use std::io::{File, TempDir};
1313

1414
pub fn main() {

src/test/run-pass/tempfile.rs

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
extern crate debug;
2222

23+
use std::io::fs::PathExtensions;
2324
use std::io::{fs, TempDir};
2425
use std::io;
2526
use std::os;

0 commit comments

Comments
 (0)