From e161d5cf736f1340f299268163a677c5871af313 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Tue, 19 May 2015 21:57:39 -0700 Subject: [PATCH 1/8] Stabilize debug builders for 1.2.0 --- src/libcollections/lib.rs | 1 - src/libcore/fmt/builders.rs | 31 +++++++++++-------- src/libcore/fmt/mod.rs | 15 +++------ src/libcoretest/lib.rs | 1 - src/libstd/lib.rs | 1 - .../run-pass/deriving-associated-types.rs | 2 +- 6 files changed, 24 insertions(+), 27 deletions(-) diff --git a/src/libcollections/lib.rs b/src/libcollections/lib.rs index cfdb9053da183..f7e784d3892e7 100644 --- a/src/libcollections/lib.rs +++ b/src/libcollections/lib.rs @@ -39,7 +39,6 @@ #![feature(str_char)] #![feature(str_words)] #![feature(slice_patterns)] -#![feature(debug_builders)] #![feature(utf8_error)] #![cfg_attr(test, feature(rand, rustc_private, test, hash, collections, collections_drain, collections_range))] diff --git a/src/libcore/fmt/builders.rs b/src/libcore/fmt/builders.rs index c1786dc4c28b5..32d6aa19c6456 100644 --- a/src/libcore/fmt/builders.rs +++ b/src/libcore/fmt/builders.rs @@ -54,6 +54,7 @@ impl<'a, 'b: 'a> fmt::Write for PadAdapter<'a, 'b> { /// /// Constructed by the `Formatter::debug_struct` method. #[must_use] +#[stable(feature = "debug_builders", since = "1.2.0")] pub struct DebugStruct<'a, 'b: 'a> { fmt: &'a mut fmt::Formatter<'b>, result: fmt::Result, @@ -72,7 +73,7 @@ pub fn debug_struct_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>, name: &str) impl<'a, 'b: 'a> DebugStruct<'a, 'b> { /// Adds a new field to the generated struct output. - #[unstable(feature = "debug_builders", reason = "method was just created")] + #[stable(feature = "debug_builders", since = "1.2.0")] pub fn field(&mut self, name: &str, value: &fmt::Debug) -> &mut DebugStruct<'a, 'b> { self.result = self.result.and_then(|_| { let prefix = if self.has_fields { @@ -94,7 +95,7 @@ impl<'a, 'b: 'a> DebugStruct<'a, 'b> { } /// Finishes output and returns any error encountered. - #[unstable(feature = "debug_builders", reason = "method was just created")] + #[stable(feature = "debug_builders", since = "1.2.0")] pub fn finish(&mut self) -> fmt::Result { if self.has_fields { self.result = self.result.and_then(|_| { @@ -117,6 +118,7 @@ impl<'a, 'b: 'a> DebugStruct<'a, 'b> { /// /// Constructed by the `Formatter::debug_tuple` method. #[must_use] +#[stable(feature = "debug_builders", since = "1.2.0")] pub struct DebugTuple<'a, 'b: 'a> { fmt: &'a mut fmt::Formatter<'b>, result: fmt::Result, @@ -134,7 +136,7 @@ pub fn debug_tuple_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>, name: &str) -> D impl<'a, 'b: 'a> DebugTuple<'a, 'b> { /// Adds a new field to the generated tuple struct output. - #[unstable(feature = "debug_builders", reason = "method was just created")] + #[stable(feature = "debug_builders", since = "1.2.0")] pub fn field(&mut self, value: &fmt::Debug) -> &mut DebugTuple<'a, 'b> { self.result = self.result.and_then(|_| { let (prefix, space) = if self.has_fields { @@ -156,7 +158,7 @@ impl<'a, 'b: 'a> DebugTuple<'a, 'b> { } /// Finishes output and returns any error encountered. - #[unstable(feature = "debug_builders", reason = "method was just created")] + #[stable(feature = "debug_builders", since = "1.2.0")] pub fn finish(&mut self) -> fmt::Result { if self.has_fields { self.result = self.result.and_then(|_| { @@ -211,6 +213,7 @@ impl<'a, 'b: 'a> DebugInner<'a, 'b> { /// /// Constructed by the `Formatter::debug_set` method. #[must_use] +#[stable(feature = "debug_builders", since = "1.2.0")] pub struct DebugSet<'a, 'b: 'a> { inner: DebugInner<'a, 'b>, } @@ -228,14 +231,14 @@ pub fn debug_set_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugSet<'a, 'b impl<'a, 'b: 'a> DebugSet<'a, 'b> { /// Adds a new entry to the set output. - #[unstable(feature = "debug_builders", reason = "method was just created")] + #[stable(feature = "debug_builders", since = "1.2.0")] pub fn entry(&mut self, entry: &fmt::Debug) -> &mut DebugSet<'a, 'b> { self.inner.entry(entry); self } /// Adds the contents of an iterator of entries to the set output. - #[unstable(feature = "debug_builders", reason = "method was just created")] + #[stable(feature = "debug_builders", since = "1.2.0")] pub fn entries(&mut self, entries: I) -> &mut DebugSet<'a, 'b> where D: fmt::Debug, I: IntoIterator { for entry in entries { @@ -245,7 +248,7 @@ impl<'a, 'b: 'a> DebugSet<'a, 'b> { } /// Finishes output and returns any error encountered. - #[unstable(feature = "debug_builders", reason = "method was just created")] + #[stable(feature = "debug_builders", since = "1.2.0")] pub fn finish(&mut self) -> fmt::Result { self.inner.finish(); self.inner.result.and_then(|_| self.inner.fmt.write_str("}")) @@ -256,6 +259,7 @@ impl<'a, 'b: 'a> DebugSet<'a, 'b> { /// /// Constructed by the `Formatter::debug_list` method. #[must_use] +#[stable(feature = "debug_builders", since = "1.2.0")] pub struct DebugList<'a, 'b: 'a> { inner: DebugInner<'a, 'b>, } @@ -273,14 +277,14 @@ pub fn debug_list_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugList<'a, impl<'a, 'b: 'a> DebugList<'a, 'b> { /// Adds a new entry to the list output. - #[unstable(feature = "debug_builders", reason = "method was just created")] + #[stable(feature = "debug_builders", since = "1.2.0")] pub fn entry(&mut self, entry: &fmt::Debug) -> &mut DebugList<'a, 'b> { self.inner.entry(entry); self } /// Adds the contents of an iterator of entries to the list output. - #[unstable(feature = "debug_builders", reason = "method was just created")] + #[stable(feature = "debug_builders", since = "1.2.0")] pub fn entries(&mut self, entries: I) -> &mut DebugList<'a, 'b> where D: fmt::Debug, I: IntoIterator { for entry in entries { @@ -290,7 +294,7 @@ impl<'a, 'b: 'a> DebugList<'a, 'b> { } /// Finishes output and returns any error encountered. - #[unstable(feature = "debug_builders", reason = "method was just created")] + #[stable(feature = "debug_builders", since = "1.2.0")] pub fn finish(&mut self) -> fmt::Result { self.inner.finish(); self.inner.result.and_then(|_| self.inner.fmt.write_str("]")) @@ -301,6 +305,7 @@ impl<'a, 'b: 'a> DebugList<'a, 'b> { /// /// Constructed by the `Formatter::debug_map` method. #[must_use] +#[stable(feature = "debug_builders", since = "1.2.0")] pub struct DebugMap<'a, 'b: 'a> { fmt: &'a mut fmt::Formatter<'b>, result: fmt::Result, @@ -318,7 +323,7 @@ pub fn debug_map_new<'a, 'b>(fmt: &'a mut fmt::Formatter<'b>) -> DebugMap<'a, 'b impl<'a, 'b: 'a> DebugMap<'a, 'b> { /// Adds a new entry to the map output. - #[unstable(feature = "debug_builders", reason = "method was just created")] + #[stable(feature = "debug_builders", since = "1.2.0")] pub fn entry(&mut self, key: &fmt::Debug, value: &fmt::Debug) -> &mut DebugMap<'a, 'b> { self.result = self.result.and_then(|_| { if self.is_pretty() { @@ -336,7 +341,7 @@ impl<'a, 'b: 'a> DebugMap<'a, 'b> { } /// Adds the contents of an iterator of entries to the map output. - #[unstable(feature = "debug_builders", reason = "method was just created")] + #[stable(feature = "debug_builders", since = "1.2.0")] pub fn entries(&mut self, entries: I) -> &mut DebugMap<'a, 'b> where K: fmt::Debug, V: fmt::Debug, I: IntoIterator { for (k, v) in entries { @@ -346,7 +351,7 @@ impl<'a, 'b: 'a> DebugMap<'a, 'b> { } /// Finishes output and returns any error encountered. - #[unstable(feature = "debug_builders", reason = "method was just created")] + #[stable(feature = "debug_builders", since = "1.2.0")] pub fn finish(&mut self) -> fmt::Result { let prefix = if self.is_pretty() && self.has_fields { "\n" } else { "" }; self.result.and_then(|_| write!(self.fmt, "{}}}", prefix)) diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs index 22575f340d78a..da873f76d1bdd 100644 --- a/src/libcore/fmt/mod.rs +++ b/src/libcore/fmt/mod.rs @@ -719,7 +719,6 @@ impl<'a> Formatter<'a> { /// # Examples /// /// ```rust - /// # #![feature(debug_builders, core)] /// use std::fmt; /// /// struct Foo { @@ -739,7 +738,7 @@ impl<'a> Formatter<'a> { /// // prints "Foo { bar: 10, baz: "Hello World" }" /// println!("{:?}", Foo { bar: 10, baz: "Hello World".to_string() }); /// ``` - #[unstable(feature = "debug_builders", reason = "method was just created")] + #[stable(feature = "debug_builders", since = "1.2.0")] #[inline] pub fn debug_struct<'b>(&'b mut self, name: &str) -> DebugStruct<'b, 'a> { builders::debug_struct_new(self, name) @@ -751,7 +750,6 @@ impl<'a> Formatter<'a> { /// # Examples /// /// ```rust - /// # #![feature(debug_builders, core)] /// use std::fmt; /// /// struct Foo(i32, String); @@ -768,7 +766,7 @@ impl<'a> Formatter<'a> { /// // prints "Foo(10, "Hello World")" /// println!("{:?}", Foo(10, "Hello World".to_string())); /// ``` - #[unstable(feature = "debug_builders", reason = "method was just created")] + #[stable(feature = "debug_builders", since = "1.2.0")] #[inline] pub fn debug_tuple<'b>(&'b mut self, name: &str) -> DebugTuple<'b, 'a> { builders::debug_tuple_new(self, name) @@ -780,7 +778,6 @@ impl<'a> Formatter<'a> { /// # Examples /// /// ```rust - /// # #![feature(debug_builders, core)] /// use std::fmt; /// /// struct Foo(Vec); @@ -794,7 +791,7 @@ impl<'a> Formatter<'a> { /// // prints "[10, 11]" /// println!("{:?}", Foo(vec![10, 11])); /// ``` - #[unstable(feature = "debug_builders", reason = "method was just created")] + #[stable(feature = "debug_builders", since = "1.2.0")] #[inline] pub fn debug_list<'b>(&'b mut self) -> DebugList<'b, 'a> { builders::debug_list_new(self) @@ -806,7 +803,6 @@ impl<'a> Formatter<'a> { /// # Examples /// /// ```rust - /// # #![feature(debug_builders, core)] /// use std::fmt; /// /// struct Foo(Vec); @@ -820,7 +816,7 @@ impl<'a> Formatter<'a> { /// // prints "{10, 11}" /// println!("{:?}", Foo(vec![10, 11])); /// ``` - #[unstable(feature = "debug_builders", reason = "method was just created")] + #[stable(feature = "debug_builders", since = "1.2.0")] #[inline] pub fn debug_set<'b>(&'b mut self) -> DebugSet<'b, 'a> { builders::debug_set_new(self) @@ -832,7 +828,6 @@ impl<'a> Formatter<'a> { /// # Examples /// /// ```rust - /// # #![feature(debug_builders, core)] /// use std::fmt; /// /// struct Foo(Vec<(String, i32)>); @@ -846,7 +841,7 @@ impl<'a> Formatter<'a> { /// // prints "{"A": 10, "B": 11}" /// println!("{:?}", Foo(vec![("A".to_string(), 10), ("B".to_string(), 11)])); /// ``` - #[unstable(feature = "debug_builders", reason = "method was just created")] + #[stable(feature = "debug_builders", since = "1.2.0")] #[inline] pub fn debug_map<'b>(&'b mut self) -> DebugMap<'b, 'a> { builders::debug_map_new(self) diff --git a/src/libcoretest/lib.rs b/src/libcoretest/lib.rs index 90c1e8b132e42..78c7215f55009 100644 --- a/src/libcoretest/lib.rs +++ b/src/libcoretest/lib.rs @@ -20,7 +20,6 @@ #![feature(std_misc)] #![feature(libc)] #![feature(hash)] -#![feature(debug_builders)] #![feature(unique)] #![feature(step_by)] #![feature(slice_patterns)] diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index 1c6a02f1dcf6c..1ad627cedd66e 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -109,7 +109,6 @@ #![feature(box_syntax)] #![feature(collections)] #![feature(core)] -#![feature(debug_builders)] #![feature(into_cow)] #![feature(lang_items)] #![feature(libc)] diff --git a/src/test/run-pass/deriving-associated-types.rs b/src/test/run-pass/deriving-associated-types.rs index 59eb5506c45c7..632ef5e0c8ad6 100644 --- a/src/test/run-pass/deriving-associated-types.rs +++ b/src/test/run-pass/deriving-associated-types.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#![feature(core, debug_builders)] +#![feature(core)] pub trait DeclaredTrait { type Type; From a3c4ce444470af4e372d08e98f7df2daf5e2363a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Steinbrink?= Date: Wed, 20 May 2015 20:26:53 +0200 Subject: [PATCH 2/8] Fix ICE trying to pass DST to C functions Fixes #25581 --- src/librustc_trans/trans/callee.rs | 2 +- src/test/run-make/issue-25581/Makefile | 7 ++++++ src/test/run-make/issue-25581/test.c | 16 +++++++++++++ src/test/run-make/issue-25581/test.rs | 32 ++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 src/test/run-make/issue-25581/Makefile create mode 100644 src/test/run-make/issue-25581/test.c create mode 100644 src/test/run-make/issue-25581/test.rs diff --git a/src/librustc_trans/trans/callee.rs b/src/librustc_trans/trans/callee.rs index e87c058faf9c1..027f2dbc717b5 100644 --- a/src/librustc_trans/trans/callee.rs +++ b/src/librustc_trans/trans/callee.rs @@ -846,7 +846,7 @@ pub fn trans_call_inner<'a, 'blk, 'tcx, F>(bcx: Block<'blk, 'tcx>, let mut llargs = Vec::new(); let arg_tys = match args { - ArgExprs(a) => a.iter().map(|x| common::expr_ty(bcx, &**x)).collect(), + ArgExprs(a) => a.iter().map(|x| common::expr_ty_adjusted(bcx, &**x)).collect(), _ => panic!("expected arg exprs.") }; bcx = trans_args(bcx, diff --git a/src/test/run-make/issue-25581/Makefile b/src/test/run-make/issue-25581/Makefile new file mode 100644 index 0000000000000..ea6971853fe99 --- /dev/null +++ b/src/test/run-make/issue-25581/Makefile @@ -0,0 +1,7 @@ +-include ../tools.mk + +all: + $(CC) -std=c99 test.c -c -o $(TMPDIR)/test.o + $(AR) rcs $(TMPDIR)/libtest.a $(TMPDIR)/test.o + $(RUSTC) test.rs -L $(TMPDIR) + $(call RUN,test) || exit 1 diff --git a/src/test/run-make/issue-25581/test.c b/src/test/run-make/issue-25581/test.c new file mode 100644 index 0000000000000..5736b1730216d --- /dev/null +++ b/src/test/run-make/issue-25581/test.c @@ -0,0 +1,16 @@ +// ignore-license +#include +#include + +struct ByteSlice { + uint8_t *data; + size_t len; +}; + +size_t slice_len(struct ByteSlice bs) { + return bs.len; +} + +uint8_t slice_elem(struct ByteSlice bs, size_t idx) { + return bs.data[idx]; +} diff --git a/src/test/run-make/issue-25581/test.rs b/src/test/run-make/issue-25581/test.rs new file mode 100644 index 0000000000000..e2e86df59cd38 --- /dev/null +++ b/src/test/run-make/issue-25581/test.rs @@ -0,0 +1,32 @@ +// 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. + +#![feature(libc)] + +extern crate libc; + +#[link(name = "test")] +extern { + fn slice_len(s: &[u8]) -> libc::size_t; + fn slice_elem(s: &[u8], idx: libc::size_t) -> u8; +} + +fn main() { + let data = [1,2,3,4,5]; + + unsafe { + assert_eq!(data.len(), slice_len(&data) as usize); + assert_eq!(data[0], slice_elem(&data, 0)); + assert_eq!(data[1], slice_elem(&data, 1)); + assert_eq!(data[2], slice_elem(&data, 2)); + assert_eq!(data[3], slice_elem(&data, 3)); + assert_eq!(data[4], slice_elem(&data, 4)); + } +} From 65ead717a7719f0d0b761794500e5e8d3ebbcb65 Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Thu, 21 May 2015 11:12:51 -0400 Subject: [PATCH 3/8] she -> they in Dining Philosophers Also, when checking for common gendered words elsewhere, I found one 'he', moved to 'they' as well. https://github.com/rust-lang/rust/pull/25640#issuecomment-104304643 --- src/doc/trpl/dining-philosophers.md | 86 +++++++++++++++-------------- src/liballoc/rc.rs | 2 +- 2 files changed, 45 insertions(+), 43 deletions(-) diff --git a/src/doc/trpl/dining-philosophers.md b/src/doc/trpl/dining-philosophers.md index 4c230c3b0e686..7e37473ac8fea 100644 --- a/src/doc/trpl/dining-philosophers.md +++ b/src/doc/trpl/dining-philosophers.md @@ -2,26 +2,28 @@ For our second project, let’s look at a classic concurrency problem. It’s called ‘the dining philosophers’. It was originally conceived by Dijkstra in -1965, but we’ll use the version from [this paper][paper] by Tony Hoare in 1985. +1965, but we’ll use a lightly adapted version from [this paper][paper] by Tony +Hoare in 1985. [paper]: http://www.usingcsp.com/cspbook.pdf > In ancient times, a wealthy philanthropist endowed a College to accommodate -> five eminent philosophers. Each philosopher had a room in which she could -> engage in her professional activity of thinking; there was also a common +> five eminent philosophers. Each philosopher had a room in which they could +> engage in their professional activity of thinking; there was also a common > dining room, furnished with a circular table, surrounded by five chairs, each > labelled by the name of the philosopher who was to sit in it. They sat > anticlockwise around the table. To the left of each philosopher there was > laid a golden fork, and in the centre stood a large bowl of spaghetti, which -> was constantly replenished. A philosopher was expected to spend most of her -> time thinking; but when she felt hungry, she went to the dining room, sat down -> in her own chair, picked up her own fork on her left, and plunged it into the -> spaghetti. But such is the tangled nature of spaghetti that a second fork is -> required to carry it to the mouth. The philosopher therefore had also to pick -> up the fork on her right. When she was finished she would put down both her -> forks, get up from her chair, and continue thinking. Of course, a fork can be -> used by only one philosopher at a time. If the other philosopher wants it, she -> just has to wait until the fork is available again. +> was constantly replenished. A philosopher was expected to spend most of +> their time thinking; but when they felt hungry, they went to the dining +> room, sat down in their own chair, picked up their own fork on their left, +> and plunged it into the spaghetti. But such is the tangled nature of +> spaghetti that a second fork is required to carry it to the mouth. The +> philosopher therefore had also to pick up the fork on their right. When +> they were finished they would put down both their forks, get up from their +> chair, and continue thinking. Of course, a fork can be used by only one +> philosopher at a time. If the other philosopher wants it, they just have +> to wait until the fork is available again. This classic problem shows off a few different elements of concurrency. The reason is that it's actually slightly tricky to implement: a simple @@ -60,10 +62,10 @@ impl Philosopher { } fn main() { - let p1 = Philosopher::new("Baruch Spinoza"); + let p1 = Philosopher::new("Judith Butler"); let p2 = Philosopher::new("Gilles Deleuze"); let p3 = Philosopher::new("Karl Marx"); - let p4 = Philosopher::new("Friedrich Nietzsche"); + let p4 = Philosopher::new("Emma Goldman"); let p5 = Philosopher::new("Michel Foucault"); } ``` @@ -159,10 +161,10 @@ look at `main()` again: # } # fn main() { - let p1 = Philosopher::new("Baruch Spinoza"); + let p1 = Philosopher::new("Judith Butler"); let p2 = Philosopher::new("Gilles Deleuze"); let p3 = Philosopher::new("Karl Marx"); - let p4 = Philosopher::new("Friedrich Nietzsche"); + let p4 = Philosopher::new("Emma Goldman"); let p5 = Philosopher::new("Michel Foucault"); } ``` @@ -176,10 +178,10 @@ that `new()` function, it would look like this: # name: String, # } fn main() { - let p1 = Philosopher { name: "Baruch Spinoza".to_string() }; + let p1 = Philosopher { name: "Judith Butler".to_string() }; let p2 = Philosopher { name: "Gilles Deleuze".to_string() }; let p3 = Philosopher { name: "Karl Marx".to_string() }; - let p4 = Philosopher { name: "Friedrich Nietzche".to_string() }; + let p4 = Philosopher { name: "Emma Goldman".to_string() }; let p5 = Philosopher { name: "Michel Foucault".to_string() }; } ``` @@ -211,10 +213,10 @@ impl Philosopher { fn main() { let philosophers = vec![ - Philosopher::new("Baruch Spinoza"), + Philosopher::new("Judith Butler"), Philosopher::new("Gilles Deleuze"), Philosopher::new("Karl Marx"), - Philosopher::new("Friedrich Nietzsche"), + Philosopher::new("Emma Goldman"), Philosopher::new("Michel Foucault"), ]; @@ -247,10 +249,10 @@ mention they’re done eating. Running this program should give you the followin output: ```text -Baruch Spinoza is done eating. +Judith Butler is done eating. Gilles Deleuze is done eating. Karl Marx is done eating. -Friedrich Nietzsche is done eating. +Emma Goldman is done eating. Michel Foucault is done eating. ``` @@ -285,10 +287,10 @@ impl Philosopher { fn main() { let philosophers = vec![ - Philosopher::new("Baruch Spinoza"), + Philosopher::new("Judith Butler"), Philosopher::new("Gilles Deleuze"), Philosopher::new("Karl Marx"), - Philosopher::new("Friedrich Nietzsche"), + Philosopher::new("Emma Goldman"), Philosopher::new("Michel Foucault"), ]; @@ -323,14 +325,14 @@ simulate the time it takes a philosopher to eat. If you run this program, you should see each philosopher eat in turn: ```text -Baruch Spinoza is eating. -Baruch Spinoza is done eating. +Judith Butler is eating. +Judith Butler is done eating. Gilles Deleuze is eating. Gilles Deleuze is done eating. Karl Marx is eating. Karl Marx is done eating. -Friedrich Nietzsche is eating. -Friedrich Nietzsche is done eating. +Emma Goldman is eating. +Emma Goldman is done eating. Michel Foucault is eating. Michel Foucault is done eating. ``` @@ -366,10 +368,10 @@ impl Philosopher { fn main() { let philosophers = vec![ - Philosopher::new("Baruch Spinoza"), + Philosopher::new("Judith Butler"), Philosopher::new("Gilles Deleuze"), Philosopher::new("Karl Marx"), - Philosopher::new("Friedrich Nietzsche"), + Philosopher::new("Emma Goldman"), Philosopher::new("Michel Foucault"), ]; @@ -458,11 +460,11 @@ We have multi-threading! ```text Gilles Deleuze is eating. Gilles Deleuze is done eating. -Friedrich Nietzsche is eating. -Friedrich Nietzsche is done eating. +Emma Goldman is eating. +Emma Goldman is done eating. Michel Foucault is eating. -Baruch Spinoza is eating. -Baruch Spinoza is done eating. +Judith Butler is eating. +Judith Butler is done eating. Karl Marx is eating. Karl Marx is done eating. Michel Foucault is done eating. @@ -532,10 +534,10 @@ fn main() { ]}); let philosophers = vec![ - Philosopher::new("Baruch Spinoza", 0, 1), + Philosopher::new("Judith Butler", 0, 1), Philosopher::new("Gilles Deleuze", 1, 2), Philosopher::new("Karl Marx", 2, 3), - Philosopher::new("Friedrich Nietzsche", 3, 4), + Philosopher::new("Emma Goldman", 3, 4), Philosopher::new("Michel Foucault", 0, 4), ]; @@ -643,10 +645,10 @@ count will go up, and when each thread ends, it will go back down. ```rust,ignore let philosophers = vec![ - Philosopher::new("Baruch Spinoza", 0, 1), + Philosopher::new("Judith Butler", 0, 1), Philosopher::new("Gilles Deleuze", 1, 2), Philosopher::new("Karl Marx", 2, 3), - Philosopher::new("Friedrich Nietzsche", 3, 4), + Philosopher::new("Emma Goldman", 3, 4), Philosopher::new("Michel Foucault", 0, 4), ]; ``` @@ -679,12 +681,12 @@ and so you’ll get some output like this: ```text Gilles Deleuze is eating. -Friedrich Nietzsche is eating. -Friedrich Nietzsche is done eating. +Emma Goldman is eating. +Emma Goldman is done eating. Gilles Deleuze is done eating. -Baruch Spinoza is eating. +Judith Butler is eating. Karl Marx is eating. -Baruch Spinoza is done eating. +Judith Butler is done eating. Michel Foucault is eating. Karl Marx is done eating. Michel Foucault is done eating. diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index 1d783ed8d3614..1f660449593fb 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -144,7 +144,7 @@ //! // At the end of the method, gadget_owner, gadget1 and gadget2 get //! // destroyed. There are now no strong (`Rc`) references to the gadgets. //! // Once they get destroyed, the Gadgets get destroyed. This zeroes the -//! // reference count on Gadget Man, so he gets destroyed as well. +//! // reference count on Gadget Man, they get destroyed as well. //! } //! ``` From 3241b487fdcacf26ef7056e1a51d5220f66eafc2 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Thu, 21 May 2015 14:01:44 -0400 Subject: [PATCH 4/8] Use `Self` to simplify --- src/libcore/num/mod.rs | 218 ++++++++++++++++++++--------------------- 1 file changed, 109 insertions(+), 109 deletions(-) diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index e560fae31a17e..4b03e51eacb02 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -73,11 +73,11 @@ macro_rules! zero_one_impl { ($($t:ty)*) => ($( impl Zero for $t { #[inline] - fn zero() -> $t { 0 } + fn zero() -> Self { 0 } } impl One for $t { #[inline] - fn one() -> $t { 1 } + fn one() -> Self { 1 } } )*) } @@ -87,20 +87,20 @@ macro_rules! zero_one_impl_float { ($($t:ty)*) => ($( impl Zero for $t { #[inline] - fn zero() -> $t { 0.0 } + fn zero() -> Self { 0.0 } } impl One for $t { #[inline] - fn one() -> $t { 1.0 } + fn one() -> Self { 1.0 } } )*) } zero_one_impl_float! { f32 f64 } macro_rules! checked_op { - ($T:ty, $U:ty, $op:path, $x:expr, $y:expr) => {{ + ($U:ty, $op:path, $x:expr, $y:expr) => {{ let (result, overflowed) = unsafe { $op($x as $U, $y as $U) }; - if overflowed { None } else { Some(result as $T) } + if overflowed { None } else { Some(result as Self) } }} } @@ -110,22 +110,22 @@ unsafe fn bswap8(x: u8) -> u8 { x } // `Int` + `SignedInt` implemented for signed integers macro_rules! int_impl { - ($T:ident = $ActualT:ty, $UnsignedT:ty, $BITS:expr, + ($ActualT:ty, $UnsignedT:ty, $BITS:expr, $add_with_overflow:path, $sub_with_overflow:path, $mul_with_overflow:path) => { /// Returns the smallest value that can be represented by this integer type. #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn min_value() -> $T { - (-1 as $T) << ($BITS - 1) + pub fn min_value() -> Self { + (-1 as Self) << ($BITS - 1) } /// Returns the largest value that can be represented by this integer type. #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn max_value() -> $T { - let min = $T::min_value(); !min + pub fn max_value() -> Self { + let min = Self::min_value(); !min } /// Converts a string slice in a given base to an integer. @@ -139,7 +139,7 @@ macro_rules! int_impl { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[allow(deprecated)] - pub fn from_str_radix(src: &str, radix: u32) -> Result<$T, ParseIntError> { + pub fn from_str_radix(src: &str, radix: u32) -> Result { from_str_radix(src, radix) } @@ -216,8 +216,8 @@ macro_rules! int_impl { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn rotate_left(self, n: u32) -> $T { - (self as $UnsignedT).rotate_left(n) as $T + pub fn rotate_left(self, n: u32) -> Self { + (self as $UnsignedT).rotate_left(n) as Self } /// Shifts the bits to the right by a specified amount, `n`, @@ -234,8 +234,8 @@ macro_rules! int_impl { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn rotate_right(self, n: u32) -> $T { - (self as $UnsignedT).rotate_right(n) as $T + pub fn rotate_right(self, n: u32) -> Self { + (self as $UnsignedT).rotate_right(n) as Self } /// Reverses the byte order of the integer. @@ -250,8 +250,8 @@ macro_rules! int_impl { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn swap_bytes(self) -> $T { - (self as $UnsignedT).swap_bytes() as $T + pub fn swap_bytes(self) -> Self { + (self as $UnsignedT).swap_bytes() as Self } /// Converts an integer from big endian to the target's endianness. @@ -272,7 +272,7 @@ macro_rules! int_impl { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn from_be(x: $T) -> $T { + pub fn from_be(x: Self) -> Self { if cfg!(target_endian = "big") { x } else { x.swap_bytes() } } @@ -294,7 +294,7 @@ macro_rules! int_impl { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn from_le(x: $T) -> $T { + pub fn from_le(x: Self) -> Self { if cfg!(target_endian = "little") { x } else { x.swap_bytes() } } @@ -316,7 +316,7 @@ macro_rules! int_impl { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn to_be(self) -> $T { // or not to be? + pub fn to_be(self) -> Self { // or not to be? if cfg!(target_endian = "big") { self } else { self.swap_bytes() } } @@ -338,7 +338,7 @@ macro_rules! int_impl { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn to_le(self) -> $T { + pub fn to_le(self) -> Self { if cfg!(target_endian = "little") { self } else { self.swap_bytes() } } @@ -353,8 +353,8 @@ macro_rules! int_impl { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn checked_add(self, other: $T) -> Option<$T> { - checked_op!($T, $ActualT, $add_with_overflow, self, other) + pub fn checked_add(self, other: Self) -> Option { + checked_op!($ActualT, $add_with_overflow, self, other) } /// Checked integer subtraction. Computes `self - other`, returning @@ -368,8 +368,8 @@ macro_rules! int_impl { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn checked_sub(self, other: $T) -> Option<$T> { - checked_op!($T, $ActualT, $sub_with_overflow, self, other) + pub fn checked_sub(self, other: Self) -> Option { + checked_op!($ActualT, $sub_with_overflow, self, other) } /// Checked integer multiplication. Computes `self * other`, returning @@ -383,8 +383,8 @@ macro_rules! int_impl { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn checked_mul(self, other: $T) -> Option<$T> { - checked_op!($T, $ActualT, $mul_with_overflow, self, other) + pub fn checked_mul(self, other: Self) -> Option { + checked_op!($ActualT, $mul_with_overflow, self, other) } /// Checked integer division. Computes `self / other`, returning `None` @@ -399,10 +399,10 @@ macro_rules! int_impl { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn checked_div(self, v: $T) -> Option<$T> { + pub fn checked_div(self, v: Self) -> Option { match v { 0 => None, - -1 if self == <$T>::min_value() + -1 if self == Self::min_value() => None, v => Some(self / v), } @@ -412,11 +412,11 @@ macro_rules! int_impl { /// the numeric bounds instead of overflowing. #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn saturating_add(self, other: $T) -> $T { + pub fn saturating_add(self, other: Self) -> Self { match self.checked_add(other) { Some(x) => x, - None if other >= <$T as Zero>::zero() => <$T>::max_value(), - None => <$T>::min_value(), + None if other >= Self::zero() => Self::max_value(), + None => Self::min_value(), } } @@ -424,11 +424,11 @@ macro_rules! int_impl { /// at the numeric bounds instead of overflowing. #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn saturating_sub(self, other: $T) -> $T { + pub fn saturating_sub(self, other: Self) -> Self { match self.checked_sub(other) { Some(x) => x, - None if other >= <$T as Zero>::zero() => <$T>::min_value(), - None => <$T>::max_value(), + None if other >= Self::zero() => Self::min_value(), + None => Self::max_value(), } } @@ -436,7 +436,7 @@ macro_rules! int_impl { /// wrapping around at the boundary of the type. #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn wrapping_add(self, rhs: $T) -> $T { + pub fn wrapping_add(self, rhs: Self) -> Self { unsafe { intrinsics::overflowing_add(self, rhs) } @@ -446,7 +446,7 @@ macro_rules! int_impl { /// wrapping around at the boundary of the type. #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn wrapping_sub(self, rhs: $T) -> $T { + pub fn wrapping_sub(self, rhs: Self) -> Self { unsafe { intrinsics::overflowing_sub(self, rhs) } @@ -456,7 +456,7 @@ macro_rules! int_impl { /// other`, wrapping around at the boundary of the type. #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn wrapping_mul(self, rhs: $T) -> $T { + pub fn wrapping_mul(self, rhs: Self) -> Self { unsafe { intrinsics::overflowing_mul(self, rhs) } @@ -473,7 +473,7 @@ macro_rules! int_impl { /// itself.. #[unstable(feature = "core", since = "1.0.0")] #[inline(always)] - pub fn wrapping_div(self, rhs: $T) -> $T { + pub fn wrapping_div(self, rhs: Self) -> Self { self.overflowing_div(rhs).0 } @@ -486,7 +486,7 @@ macro_rules! int_impl { /// minimal value). In such a case, this function returns `0`. #[unstable(feature = "core", since = "1.0.0")] #[inline(always)] - pub fn wrapping_rem(self, rhs: $T) -> $T { + pub fn wrapping_rem(self, rhs: Self) -> Self { self.overflowing_rem(rhs).0 } @@ -500,7 +500,7 @@ macro_rules! int_impl { /// a case, this function returns `MIN` itself. #[unstable(feature = "core", since = "1.0.0")] #[inline(always)] - pub fn wrapping_neg(self) -> $T { + pub fn wrapping_neg(self) -> Self { self.overflowing_neg().0 } @@ -509,7 +509,7 @@ macro_rules! int_impl { /// would cause the shift to exceed the bitwidth of the type. #[unstable(feature = "core", since = "1.0.0")] #[inline(always)] - pub fn wrapping_shl(self, rhs: u32) -> $T { + pub fn wrapping_shl(self, rhs: u32) -> Self { self.overflowing_shl(rhs).0 } @@ -518,7 +518,7 @@ macro_rules! int_impl { /// would cause the shift to exceed the bitwidth of the type. #[unstable(feature = "core", since = "1.0.0")] #[inline(always)] - pub fn wrapping_shr(self, rhs: u32) -> $T { + pub fn wrapping_shr(self, rhs: u32) -> Self { self.overflowing_shr(rhs).0 } @@ -533,9 +533,9 @@ macro_rules! int_impl { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn pow(self, mut exp: u32) -> $T { + pub fn pow(self, mut exp: u32) -> Self { let mut base = self; - let mut acc = <$T as One>::one(); + let mut acc = Self::one(); let mut prev_base = self; let mut base_oflo = false; @@ -569,7 +569,7 @@ macro_rules! int_impl { /// optimized code will return `i32::min_value()` without a panic. #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn abs(self) -> $T { + pub fn abs(self) -> Self { if self.is_negative() { // Note that the #[inline] above means that the overflow // semantics of this negation depend on the crate we're being @@ -587,7 +587,7 @@ macro_rules! int_impl { /// - `-1` if the number is negative #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn signum(self) -> $T { + pub fn signum(self) -> Self { match self { n if n > 0 => 1, 0 => 0, @@ -611,7 +611,7 @@ macro_rules! int_impl { #[lang = "i8"] impl i8 { - int_impl! { i8 = i8, u8, 8, + int_impl! { i8, u8, 8, intrinsics::i8_add_with_overflow, intrinsics::i8_sub_with_overflow, intrinsics::i8_mul_with_overflow } @@ -619,7 +619,7 @@ impl i8 { #[lang = "i16"] impl i16 { - int_impl! { i16 = i16, u16, 16, + int_impl! { i16, u16, 16, intrinsics::i16_add_with_overflow, intrinsics::i16_sub_with_overflow, intrinsics::i16_mul_with_overflow } @@ -627,7 +627,7 @@ impl i16 { #[lang = "i32"] impl i32 { - int_impl! { i32 = i32, u32, 32, + int_impl! { i32, u32, 32, intrinsics::i32_add_with_overflow, intrinsics::i32_sub_with_overflow, intrinsics::i32_mul_with_overflow } @@ -635,7 +635,7 @@ impl i32 { #[lang = "i64"] impl i64 { - int_impl! { i64 = i64, u64, 64, + int_impl! { i64, u64, 64, intrinsics::i64_add_with_overflow, intrinsics::i64_sub_with_overflow, intrinsics::i64_mul_with_overflow } @@ -644,7 +644,7 @@ impl i64 { #[cfg(target_pointer_width = "32")] #[lang = "isize"] impl isize { - int_impl! { isize = i32, u32, 32, + int_impl! { i32, u32, 32, intrinsics::i32_add_with_overflow, intrinsics::i32_sub_with_overflow, intrinsics::i32_mul_with_overflow } @@ -653,7 +653,7 @@ impl isize { #[cfg(target_pointer_width = "64")] #[lang = "isize"] impl isize { - int_impl! { isize = i64, u64, 64, + int_impl! { i64, u64, 64, intrinsics::i64_add_with_overflow, intrinsics::i64_sub_with_overflow, intrinsics::i64_mul_with_overflow } @@ -661,7 +661,7 @@ impl isize { // `Int` + `UnsignedInt` implemented for signed integers macro_rules! uint_impl { - ($T:ty = $ActualT:ty, $BITS:expr, + ($ActualT:ty, $BITS:expr, $ctpop:path, $ctlz:path, $cttz:path, @@ -671,11 +671,11 @@ macro_rules! uint_impl { $mul_with_overflow:path) => { /// Returns the smallest value that can be represented by this integer type. #[stable(feature = "rust1", since = "1.0.0")] - pub fn min_value() -> $T { 0 } + pub fn min_value() -> Self { 0 } /// Returns the largest value that can be represented by this integer type. #[stable(feature = "rust1", since = "1.0.0")] - pub fn max_value() -> $T { !0 } + pub fn max_value() -> Self { !0 } /// Converts a string slice in a given base to an integer. /// @@ -692,7 +692,7 @@ macro_rules! uint_impl { /// Otherwise, `Ok(n)` where `n` is the integer represented by `src`. #[stable(feature = "rust1", since = "1.0.0")] #[allow(deprecated)] - pub fn from_str_radix(src: &str, radix: u32) -> Result<$T, ParseIntError> { + pub fn from_str_radix(src: &str, radix: u32) -> Result { from_str_radix(src, radix) } @@ -784,7 +784,7 @@ macro_rules! uint_impl { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn rotate_left(self, n: u32) -> $T { + pub fn rotate_left(self, n: u32) -> Self { // Protect against undefined behaviour for over-long bit shifts let n = n % $BITS; (self << n) | (self >> (($BITS - n) % $BITS)) @@ -804,7 +804,7 @@ macro_rules! uint_impl { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn rotate_right(self, n: u32) -> $T { + pub fn rotate_right(self, n: u32) -> Self { // Protect against undefined behaviour for over-long bit shifts let n = n % $BITS; (self >> n) | (self << (($BITS - n) % $BITS)) @@ -822,8 +822,8 @@ macro_rules! uint_impl { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn swap_bytes(self) -> $T { - unsafe { $bswap(self as $ActualT) as $T } + pub fn swap_bytes(self) -> Self { + unsafe { $bswap(self as $ActualT) as Self } } /// Converts an integer from big endian to the target's endianness. @@ -844,7 +844,7 @@ macro_rules! uint_impl { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn from_be(x: $T) -> $T { + pub fn from_be(x: Self) -> Self { if cfg!(target_endian = "big") { x } else { x.swap_bytes() } } @@ -866,7 +866,7 @@ macro_rules! uint_impl { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn from_le(x: $T) -> $T { + pub fn from_le(x: Self) -> Self { if cfg!(target_endian = "little") { x } else { x.swap_bytes() } } @@ -888,7 +888,7 @@ macro_rules! uint_impl { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn to_be(self) -> $T { // or not to be? + pub fn to_be(self) -> Self { // or not to be? if cfg!(target_endian = "big") { self } else { self.swap_bytes() } } @@ -910,7 +910,7 @@ macro_rules! uint_impl { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn to_le(self) -> $T { + pub fn to_le(self) -> Self { if cfg!(target_endian = "little") { self } else { self.swap_bytes() } } @@ -925,8 +925,8 @@ macro_rules! uint_impl { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn checked_add(self, other: $T) -> Option<$T> { - checked_op!($T, $ActualT, $add_with_overflow, self, other) + pub fn checked_add(self, other: Self) -> Option { + checked_op!($ActualT, $add_with_overflow, self, other) } /// Checked integer subtraction. Computes `self - other`, returning @@ -940,8 +940,8 @@ macro_rules! uint_impl { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn checked_sub(self, other: $T) -> Option<$T> { - checked_op!($T, $ActualT, $sub_with_overflow, self, other) + pub fn checked_sub(self, other: Self) -> Option { + checked_op!($ActualT, $sub_with_overflow, self, other) } /// Checked integer multiplication. Computes `self * other`, returning @@ -955,8 +955,8 @@ macro_rules! uint_impl { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn checked_mul(self, other: $T) -> Option<$T> { - checked_op!($T, $ActualT, $mul_with_overflow, self, other) + pub fn checked_mul(self, other: Self) -> Option { + checked_op!($ActualT, $mul_with_overflow, self, other) } /// Checked integer division. Computes `self / other`, returning `None` @@ -971,7 +971,7 @@ macro_rules! uint_impl { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn checked_div(self, v: $T) -> Option<$T> { + pub fn checked_div(self, v: Self) -> Option { match v { 0 => None, v => Some(self / v), @@ -982,11 +982,11 @@ macro_rules! uint_impl { /// the numeric bounds instead of overflowing. #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn saturating_add(self, other: $T) -> $T { + pub fn saturating_add(self, other: Self) -> Self { match self.checked_add(other) { Some(x) => x, - None if other >= <$T as Zero>::zero() => <$T>::max_value(), - None => <$T>::min_value(), + None if other >= Self::zero() => Self::max_value(), + None => Self::min_value(), } } @@ -994,11 +994,11 @@ macro_rules! uint_impl { /// at the numeric bounds instead of overflowing. #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn saturating_sub(self, other: $T) -> $T { + pub fn saturating_sub(self, other: Self) -> Self { match self.checked_sub(other) { Some(x) => x, - None if other >= <$T as Zero>::zero() => <$T>::min_value(), - None => <$T>::max_value(), + None if other >= Self::zero() => Self::min_value(), + None => Self::max_value(), } } @@ -1006,7 +1006,7 @@ macro_rules! uint_impl { /// wrapping around at the boundary of the type. #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn wrapping_add(self, rhs: $T) -> $T { + pub fn wrapping_add(self, rhs: Self) -> Self { unsafe { intrinsics::overflowing_add(self, rhs) } @@ -1016,7 +1016,7 @@ macro_rules! uint_impl { /// wrapping around at the boundary of the type. #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn wrapping_sub(self, rhs: $T) -> $T { + pub fn wrapping_sub(self, rhs: Self) -> Self { unsafe { intrinsics::overflowing_sub(self, rhs) } @@ -1026,7 +1026,7 @@ macro_rules! uint_impl { /// other`, wrapping around at the boundary of the type. #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn wrapping_mul(self, rhs: $T) -> $T { + pub fn wrapping_mul(self, rhs: Self) -> Self { unsafe { intrinsics::overflowing_mul(self, rhs) } @@ -1043,7 +1043,7 @@ macro_rules! uint_impl { /// itself.. #[unstable(feature = "core", since = "1.0.0")] #[inline(always)] - pub fn wrapping_div(self, rhs: $T) -> $T { + pub fn wrapping_div(self, rhs: Self) -> Self { self.overflowing_div(rhs).0 } @@ -1056,7 +1056,7 @@ macro_rules! uint_impl { /// minimal value). In such a case, this function returns `0`. #[unstable(feature = "core", since = "1.0.0")] #[inline(always)] - pub fn wrapping_rem(self, rhs: $T) -> $T { + pub fn wrapping_rem(self, rhs: Self) -> Self { self.overflowing_rem(rhs).0 } @@ -1070,7 +1070,7 @@ macro_rules! uint_impl { /// a case, this function returns `MIN` itself. #[unstable(feature = "core", since = "1.0.0")] #[inline(always)] - pub fn wrapping_neg(self) -> $T { + pub fn wrapping_neg(self) -> Self { self.overflowing_neg().0 } @@ -1079,7 +1079,7 @@ macro_rules! uint_impl { /// would cause the shift to exceed the bitwidth of the type. #[unstable(feature = "core", since = "1.0.0")] #[inline(always)] - pub fn wrapping_shl(self, rhs: u32) -> $T { + pub fn wrapping_shl(self, rhs: u32) -> Self { self.overflowing_shl(rhs).0 } @@ -1088,7 +1088,7 @@ macro_rules! uint_impl { /// would cause the shift to exceed the bitwidth of the type. #[unstable(feature = "core", since = "1.0.0")] #[inline(always)] - pub fn wrapping_shr(self, rhs: u32) -> $T { + pub fn wrapping_shr(self, rhs: u32) -> Self { self.overflowing_shr(rhs).0 } @@ -1101,9 +1101,9 @@ macro_rules! uint_impl { /// ``` #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn pow(self, mut exp: u32) -> $T { + pub fn pow(self, mut exp: u32) -> Self { let mut base = self; - let mut acc = <$T as One>::one(); + let mut acc = Self::one(); let mut prev_base = self; let mut base_oflo = false; @@ -1131,17 +1131,17 @@ macro_rules! uint_impl { #[stable(feature = "rust1", since = "1.0.0")] #[inline] pub fn is_power_of_two(self) -> bool { - (self.wrapping_sub(<$T as One>::one())) & self == <$T as Zero>::zero() && - !(self == <$T as Zero>::zero()) + (self.wrapping_sub(Self::one())) & self == Self::zero() && + !(self == Self::zero()) } /// Returns the smallest power of two greater than or equal to `self`. /// Unspecified behavior on overflow. #[stable(feature = "rust1", since = "1.0.0")] #[inline] - pub fn next_power_of_two(self) -> $T { - let bits = size_of::<$T>() * 8; - let one: $T = <$T as One>::one(); + pub fn next_power_of_two(self) -> Self { + let bits = size_of::() * 8; + let one: Self = Self::one(); one << ((bits - self.wrapping_sub(one).leading_zeros() as usize) % bits) } @@ -1149,7 +1149,7 @@ macro_rules! uint_impl { /// the next power of two is greater than the type's maximum value, /// `None` is returned, otherwise the power of two is wrapped in `Some`. #[stable(feature = "rust1", since = "1.0.0")] - pub fn checked_next_power_of_two(self) -> Option<$T> { + pub fn checked_next_power_of_two(self) -> Option { let npot = self.next_power_of_two(); if npot >= self { Some(npot) @@ -1162,7 +1162,7 @@ macro_rules! uint_impl { #[lang = "u8"] impl u8 { - uint_impl! { u8 = u8, 8, + uint_impl! { u8, 8, intrinsics::ctpop8, intrinsics::ctlz8, intrinsics::cttz8, @@ -1174,7 +1174,7 @@ impl u8 { #[lang = "u16"] impl u16 { - uint_impl! { u16 = u16, 16, + uint_impl! { u16, 16, intrinsics::ctpop16, intrinsics::ctlz16, intrinsics::cttz16, @@ -1186,7 +1186,7 @@ impl u16 { #[lang = "u32"] impl u32 { - uint_impl! { u32 = u32, 32, + uint_impl! { u32, 32, intrinsics::ctpop32, intrinsics::ctlz32, intrinsics::cttz32, @@ -1199,7 +1199,7 @@ impl u32 { #[lang = "u64"] impl u64 { - uint_impl! { u64 = u64, 64, + uint_impl! { u64, 64, intrinsics::ctpop64, intrinsics::ctlz64, intrinsics::cttz64, @@ -1212,7 +1212,7 @@ impl u64 { #[cfg(target_pointer_width = "32")] #[lang = "usize"] impl usize { - uint_impl! { usize = u32, 32, + uint_impl! { u32, 32, intrinsics::ctpop32, intrinsics::ctlz32, intrinsics::cttz32, @@ -1225,7 +1225,7 @@ impl usize { #[cfg(target_pointer_width = "64")] #[lang = "usize"] impl usize { - uint_impl! { usize = u64, 64, + uint_impl! { u64, 64, intrinsics::ctpop64, intrinsics::ctlz64, intrinsics::cttz64, @@ -1395,8 +1395,8 @@ macro_rules! from_str_float_impl { /// number represented by `src`. #[inline] #[allow(deprecated)] - fn from_str(src: &str) -> Result<$T, ParseFloatError> { - $T::from_str_radix(src, 10) + fn from_str(src: &str) -> Result { + Self::from_str_radix(src, 10) } } } @@ -1410,7 +1410,7 @@ macro_rules! from_str_radix_int_impl { #[allow(deprecated)] impl FromStr for $T { type Err = ParseIntError; - fn from_str(src: &str) -> Result<$T, ParseIntError> { + fn from_str(src: &str) -> Result { from_str_radix(src, 10) } } @@ -1429,16 +1429,16 @@ trait FromStrRadixHelper: PartialOrd + Copy { macro_rules! doit { ($($t:ident)*) => ($(impl FromStrRadixHelper for $t { - fn min_value() -> Self { <$t>::min_value() } - fn from_u32(u: u32) -> Self { u as $t } + fn min_value() -> Self { Self::min_value() } + fn from_u32(u: u32) -> Self { u as Self } fn checked_mul(&self, other: u32) -> Option { - <$t>::checked_mul(*self, other as $t) + Self::checked_mul(*self, other as Self) } fn checked_sub(&self, other: u32) -> Option { - <$t>::checked_sub(*self, other as $t) + Self::checked_sub(*self, other as Self) } fn checked_add(&self, other: u32) -> Option { - <$t>::checked_add(*self, other as $t) + Self::checked_add(*self, other as Self) } })*) } From cf11c261f0a1293a71ab0d579f4047525dbf8cef Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Thu, 21 May 2015 14:04:13 -0400 Subject: [PATCH 5/8] Standardize on `$t:ty` --- src/libcore/num/mod.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 4b03e51eacb02..bf26022692d09 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -1362,9 +1362,9 @@ pub trait Float { } macro_rules! from_str_float_impl { - ($T:ident) => { + ($t:ty) => { #[stable(feature = "rust1", since = "1.0.0")] - impl FromStr for $T { + impl FromStr for $t { type Err = ParseFloatError; /// Converts a string in base 10 to a float. @@ -1405,10 +1405,10 @@ from_str_float_impl!(f32); from_str_float_impl!(f64); macro_rules! from_str_radix_int_impl { - ($($T:ident)*) => {$( + ($($t:ty)*) => {$( #[stable(feature = "rust1", since = "1.0.0")] #[allow(deprecated)] - impl FromStr for $T { + impl FromStr for $t { type Err = ParseIntError; fn from_str(src: &str) -> Result { from_str_radix(src, 10) @@ -1428,7 +1428,7 @@ trait FromStrRadixHelper: PartialOrd + Copy { } macro_rules! doit { - ($($t:ident)*) => ($(impl FromStrRadixHelper for $t { + ($($t:ty)*) => ($(impl FromStrRadixHelper for $t { fn min_value() -> Self { Self::min_value() } fn from_u32(u: u32) -> Self { u as Self } fn checked_mul(&self, other: u32) -> Option { From 5263d628b14d5511f7121f85b1d5ead3bd7b9813 Mon Sep 17 00:00:00 2001 From: Tshepang Lekhonkhobe Date: Thu, 21 May 2015 03:29:46 +0200 Subject: [PATCH 6/8] doc: miscellaneous improvements to std::path::Path examples --- src/libstd/path.rs | 78 ++++++++++++++++++++++++---------------------- 1 file changed, 40 insertions(+), 38 deletions(-) diff --git a/src/libstd/path.rs b/src/libstd/path.rs index 6732af556e0f1..8d846a7fe4df5 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -1247,9 +1247,10 @@ impl Path { /// ``` /// use std::path::Path; /// - /// let s = String::from("bar.txt"); - /// let p = Path::new(&s); - /// Path::new(&p); + /// let string = String::from("foo.txt"); + /// let from_string = Path::new(&string); + /// let from_path = Path::new(&from_string); + /// assert_eq!(from_string, from_path); /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn new + ?Sized>(s: &S) -> &Path { @@ -1264,6 +1265,7 @@ impl Path { /// use std::path::Path; /// /// let os_str = Path::new("foo.txt").as_os_str(); + /// assert_eq!(os_str, std::ffi::OsStr::new("foo.txt")); /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn as_os_str(&self) -> &OsStr { @@ -1280,6 +1282,7 @@ impl Path { /// use std::path::Path; /// /// let path_str = Path::new("foo.txt").to_str(); + //// assert_eq!(path_str, Some("foo.txt")); /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn to_str(&self) -> Option<&str> { @@ -1296,6 +1299,7 @@ impl Path { /// use std::path::Path; /// /// let path_str = Path::new("foo.txt").to_string_lossy(); + /// assert_eq!(path_str, "foo.txt"); /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn to_string_lossy(&self) -> Cow { @@ -1309,7 +1313,8 @@ impl Path { /// ``` /// use std::path::Path; /// - /// let path_str = Path::new("foo.txt").to_path_buf(); + /// let path_buf = Path::new("foo.txt").to_path_buf(); + /// assert_eq!(path_buf, std::path::PathBuf::from("foo.txt")); /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn to_path_buf(&self) -> PathBuf { @@ -1330,7 +1335,7 @@ impl Path { /// ``` /// use std::path::Path; /// - /// assert_eq!(false, Path::new("foo.txt").is_absolute()); + /// assert!(!Path::new("foo.txt").is_absolute()); /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn is_absolute(&self) -> bool { @@ -1393,14 +1398,12 @@ impl Path { /// use std::path::Path; /// /// let path = Path::new("/foo/bar"); - /// let foo = path.parent().unwrap(); + /// let parent = path.parent().unwrap(); + /// assert_eq!(parent, Path::new("/foo")); /// - /// assert!(foo == Path::new("/foo")); - /// - /// let root = foo.parent().unwrap(); - /// - /// assert!(root == Path::new("/")); - /// assert!(root.parent() == None); + /// let grand_parent = parent.parent().unwrap(); + /// assert_eq!(grand_parent, Path::new("/")); + /// assert_eq!(grand_parent.parent(), None); /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn parent(&self) -> Option<&Path> { @@ -1416,18 +1419,19 @@ impl Path { /// The final component of the path, if it is a normal file. /// - /// If the path terminates in `.`, `..`, or consists solely or a root of + /// If the path terminates in `.`, `..`, or consists solely of a root of /// prefix, `file_name` will return `None`. /// /// # Examples /// /// ``` /// use std::path::Path; + /// use std::ffi::OsStr; /// - /// let path = Path::new("hello_world.rs"); - /// let filename = "hello_world.rs"; + /// let path = Path::new("foo.txt"); + /// let os_str = OsStr::new("foo.txt"); /// - /// assert_eq!(filename, path.file_name().unwrap()); + /// assert_eq!(Some(os_str), path.file_name()); /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn file_name(&self) -> Option<&OsStr> { @@ -1538,11 +1542,9 @@ impl Path { /// # Examples /// /// ``` - /// use std::path::Path; - /// - /// let path = Path::new("/tmp"); + /// use std::path::{Path, PathBuf}; /// - /// let new_path = path.join("foo"); + /// assert_eq!(Path::new("/etc").join("passwd"), PathBuf::from("/etc/passwd")); /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn join>(&self, path: P) -> PathBuf { @@ -1558,11 +1560,10 @@ impl Path { /// # Examples /// /// ``` - /// use std::path::Path; - /// - /// let path = Path::new("/tmp/foo.rs"); + /// use std::path::{Path, PathBuf}; /// - /// let new_path = path.with_file_name("bar.rs"); + /// let path = Path::new("/tmp/foo.txt"); + /// assert_eq!(path.with_file_name("bar.txt"), PathBuf::from("/tmp/bar.txt")); /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn with_file_name>(&self, file_name: S) -> PathBuf { @@ -1580,10 +1581,8 @@ impl Path { /// ``` /// use std::path::{Path, PathBuf}; /// - /// let path = Path::new("/tmp/foo.rs"); - /// - /// let new_path = path.with_extension("txt"); - /// assert_eq!(new_path, PathBuf::from("/tmp/foo.txt")); + /// let path = Path::new("foo.rs"); + /// assert_eq!(path.with_extension("txt"), PathBuf::from("foo.txt")); /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn with_extension>(&self, extension: S) -> PathBuf { @@ -1597,13 +1596,15 @@ impl Path { /// # Examples /// /// ``` - /// use std::path::Path; + /// use std::path::{Path, Component}; + /// use std::ffi::OsStr; /// - /// let path = Path::new("/tmp/foo.rs"); + /// let mut components = Path::new("/tmp/foo.txt").components(); /// - /// for component in path.components() { - /// println!("{:?}", component); - /// } + /// assert_eq!(components.next(), Some(Component::RootDir)); + /// assert_eq!(components.next(), Some(Component::Normal(OsStr::new("tmp")))); + /// assert_eq!(components.next(), Some(Component::Normal(OsStr::new("foo.txt")))); + /// assert_eq!(components.next(), None) /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn components(&self) -> Components { @@ -1623,12 +1624,13 @@ impl Path { /// /// ``` /// use std::path::Path; + /// use std::ffi::OsStr; /// - /// let path = Path::new("/tmp/foo.rs"); - /// - /// for component in path.iter() { - /// println!("{:?}", component); - /// } + /// let mut it = Path::new("/tmp/foo.txt").iter(); + /// assert_eq!(it.next(), Some(OsStr::new("/"))); + /// assert_eq!(it.next(), Some(OsStr::new("tmp"))); + /// assert_eq!(it.next(), Some(OsStr::new("foo.txt"))); + /// assert_eq!(it.next(), None) /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn iter(&self) -> Iter { From 5dc03a8246e76e9e3900d98f2f5d2574abaeaa6b Mon Sep 17 00:00:00 2001 From: Eduard Burtescu Date: Fri, 22 May 2015 16:15:21 +0300 Subject: [PATCH 7/8] Lazy-load filemaps from external crates. --- src/librustc/metadata/creader.rs | 10 +++++----- src/librustc/metadata/cstore.rs | 21 +++++++++++++++++---- src/librustc/middle/astencode.rs | 26 +++++++++++++------------- 3 files changed, 35 insertions(+), 22 deletions(-) diff --git a/src/librustc/metadata/creader.rs b/src/librustc/metadata/creader.rs index 12112fd45ebe9..8562d8c01cc67 100644 --- a/src/librustc/metadata/creader.rs +++ b/src/librustc/metadata/creader.rs @@ -21,6 +21,7 @@ use metadata::decoder; use metadata::loader; use metadata::loader::CratePaths; +use std::cell::RefCell; use std::path::PathBuf; use std::rc::Rc; use std::fs; @@ -376,14 +377,13 @@ impl<'a> CrateReader<'a> { let loader::Library { dylib, rlib, metadata } = lib; let cnum_map = self.resolve_crate_deps(root, metadata.as_slice(), span); - let codemap_import_info = import_codemap(self.sess.codemap(), &metadata); let cmeta = Rc::new( cstore::crate_metadata { name: name.to_string(), data: metadata, cnum_map: cnum_map, cnum: cnum, - codemap_import_info: codemap_import_info, + codemap_import_info: RefCell::new(vec![]), span: span, }); @@ -616,9 +616,9 @@ impl<'a> CrateReader<'a> { /// file they represent, just information about length, line breaks, and /// multibyte characters. This information is enough to generate valid debuginfo /// for items inlined from other crates. -fn import_codemap(local_codemap: &codemap::CodeMap, - metadata: &MetadataBlob) - -> Vec { +pub fn import_codemap(local_codemap: &codemap::CodeMap, + metadata: &MetadataBlob) + -> Vec { let external_codemap = decoder::get_imported_filemaps(metadata.as_slice()); let imported_filemaps = external_codemap.into_iter().map(|filemap_to_import| { diff --git a/src/librustc/metadata/cstore.rs b/src/librustc/metadata/cstore.rs index 1f18b13fc46fe..885e7ffb3050e 100644 --- a/src/librustc/metadata/cstore.rs +++ b/src/librustc/metadata/cstore.rs @@ -18,12 +18,11 @@ pub use self::LinkagePreference::*; pub use self::NativeLibraryKind::*; use back::svh::Svh; -use metadata::decoder; -use metadata::loader; +use metadata::{creader, decoder, loader}; use session::search_paths::PathKind; use util::nodemap::{FnvHashMap, NodeMap}; -use std::cell::RefCell; +use std::cell::{RefCell, Ref}; use std::rc::Rc; use std::path::PathBuf; use flate::Bytes; @@ -58,7 +57,7 @@ pub struct crate_metadata { pub data: MetadataBlob, pub cnum_map: cnum_map, pub cnum: ast::CrateNum, - pub codemap_import_info: Vec, + pub codemap_import_info: RefCell>, pub span: codemap::Span, } @@ -240,6 +239,20 @@ impl crate_metadata { pub fn data<'a>(&'a self) -> &'a [u8] { self.data.as_slice() } pub fn name(&self) -> String { decoder::get_crate_name(self.data()) } pub fn hash(&self) -> Svh { decoder::get_crate_hash(self.data()) } + pub fn imported_filemaps<'a>(&'a self, codemap: &codemap::CodeMap) + -> Ref<'a, Vec> { + let filemaps = self.codemap_import_info.borrow(); + if filemaps.is_empty() { + drop(filemaps); + let filemaps = creader::import_codemap(codemap, &self.data); + + // This shouldn't borrow twice, but there is no way to downgrade RefMut to Ref. + *self.codemap_import_info.borrow_mut() = filemaps; + self.codemap_import_info.borrow() + } else { + filemaps + } + } } impl MetadataBlob { diff --git a/src/librustc/middle/astencode.rs b/src/librustc/middle/astencode.rs index 226010aaa817c..b7e57819b93b4 100644 --- a/src/librustc/middle/astencode.rs +++ b/src/librustc/middle/astencode.rs @@ -233,8 +233,6 @@ impl<'a, 'b, 'tcx> DecodeContext<'a, 'b, 'tcx> { /// codemap as a side-effect of creating the crate_metadata's /// `codemap_import_info`. pub fn tr_span(&self, span: Span) -> Span { - let imported_filemaps = &self.cdata.codemap_import_info[..]; - let span = if span.lo > span.hi { // Currently macro expansion sometimes produces invalid Span values // where lo > hi. In order not to crash the compiler when trying to @@ -248,16 +246,18 @@ impl<'a, 'b, 'tcx> DecodeContext<'a, 'b, 'tcx> { span }; - let filemap_index = { + let imported_filemaps = self.cdata.imported_filemaps(self.tcx.sess.codemap()); + let filemap = { // Optimize for the case that most spans within a translated item // originate from the same filemap. let last_filemap_index = self.last_filemap_index.get(); + let last_filemap = &imported_filemaps[last_filemap_index]; - if span.lo >= imported_filemaps[last_filemap_index].original_start_pos && - span.lo <= imported_filemaps[last_filemap_index].original_end_pos && - span.hi >= imported_filemaps[last_filemap_index].original_start_pos && - span.hi <= imported_filemaps[last_filemap_index].original_end_pos { - last_filemap_index + if span.lo >= last_filemap.original_start_pos && + span.lo <= last_filemap.original_end_pos && + span.hi >= last_filemap.original_start_pos && + span.hi <= last_filemap.original_end_pos { + last_filemap } else { let mut a = 0; let mut b = imported_filemaps.len(); @@ -272,14 +272,14 @@ impl<'a, 'b, 'tcx> DecodeContext<'a, 'b, 'tcx> { } self.last_filemap_index.set(a); - a + &imported_filemaps[a] } }; - let lo = (span.lo - imported_filemaps[filemap_index].original_start_pos) + - imported_filemaps[filemap_index].translated_filemap.start_pos; - let hi = (span.hi - imported_filemaps[filemap_index].original_start_pos) + - imported_filemaps[filemap_index].translated_filemap.start_pos; + let lo = (span.lo - filemap.original_start_pos) + + filemap.translated_filemap.start_pos; + let hi = (span.hi - filemap.original_start_pos) + + filemap.translated_filemap.start_pos; codemap::mk_sp(lo, hi) } From e790db7518fd5111ea806ac38140c2b960907556 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20=C4=BDach?= Date: Fri, 22 May 2015 16:42:57 +0100 Subject: [PATCH 8/8] better describe the stdlib --- src/libstd/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index 1c6a02f1dcf6c..dd16ad36b38f8 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -26,7 +26,7 @@ //! //! ## What is in the standard library //! -//! The standard library is minimal, a set of battle-tested +//! The standard library is a set of minimal, battle-tested //! core types and shared abstractions for the [broader Rust //! ecosystem](https://crates.io) to build on. //!