From a0ab5a3651f5cb30efa2f82a3ee15a7df479080a Mon Sep 17 00:00:00 2001 From: ksqsf Date: Thu, 1 Aug 2019 00:26:47 +0800 Subject: [PATCH 01/22] Add Result::cloned{,_err} and Result::copied{,_err} --- src/libcore/result.rs | 160 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) diff --git a/src/libcore/result.rs b/src/libcore/result.rs index 559877ddd5a1c..a8f0f422cb4d7 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -820,6 +820,166 @@ impl Result { } } +impl Result<&T, E> { + /// Maps a `Result<&T, E>` to a `Result` by copying the contents of the + /// `Ok` part. + /// + /// # Examples + /// + /// ``` + /// #![feature(result_copied)] + /// let val = 12; + /// let x = Ok(&val); + /// assert_eq!(x, Ok(&12)); + /// let copied = x.copied(); + /// assert_eq!(copied, Ok(12)); + /// ``` + #[unstable(feature = "result_copied", reason = "newly added", issue = "XXXXX")] + fn copied(self) -> Result { + self.map(|&t| t) + } +} + +impl Result<&mut T, E> { + /// Maps a `Result<&mut T, E>` to a `Result` by copying the contents of the + /// `Ok` part. + /// + /// # Examples + /// + /// ``` + /// #![feature(result_copied)] + /// let val = 12; + /// let x = Ok(&mut val); + /// assert_eq!(x, Ok(&mut 12)); + /// let copied = x.copied(); + /// assert_eq!(copied, Ok(12)); + /// ``` + #[unstable(feature = "result_copied", reason = "newly added", issue = "XXXXX")] + fn copied(self) -> Result { + self.map(|&mut t| t) + } +} + +impl Result { + /// Maps a `Result` to a `Result` by copying the contents of the + /// `Err` part. + /// + /// # Examples + /// + /// ``` + /// #![feature(result_copied)] + /// let val = 12; + /// let x = Err(&val); + /// assert_eq!(x, Err(&12)); + /// let copied = x.copied_err(); + /// assert_eq!(copied, Err(12)); + /// ``` + #[unstable(feature = "result_copied", reason = "newly added", issue = "XXXXX")] + fn copied_err(self) -> Result { + self.map_err(|&e| e) + } +} + +impl Result { + /// Maps a `Result` to a `Result` by copying the contents of the + /// `Err` part. + /// + /// # Examples + /// + /// ``` + /// #![feature(result_copied)] + /// let val = 12; + /// let x = Err(&mut val); + /// assert_eq!(x, Err(&mut 12)); + /// let copied = x.copied(); + /// assert_eq!(cloned, Err(12)); + /// ``` + #[unstable(feature = "result_copied", reason = "newly added", issue = "XXXXX")] + fn copied_err(self) -> Result { + self.map_err(|&mut e| e) + } +} + +impl Result<&T, E> { + /// Maps a `Result<&T, E>` to a `Result` by cloning the contents of the + /// `Ok` part. + /// + /// # Examples + /// + /// ``` + /// #![feature(result_cloned)] + /// let val = 12; + /// let x = Ok(&val); + /// assert_eq!(x, Ok(&12)); + /// let cloned = x.cloned(); + /// assert_eq!(cloned, Ok(12)); + /// ``` + #[unstable(feature = "result_cloned", reason = "newly added", issue = "XXXXX")] + fn cloned(self) -> Result { + self.map(|t| t.clone()) + } +} + +impl Result<&mut T, E> { + /// Maps a `Result<&mut T, E>` to a `Result` by cloning the contents of the + /// `Ok` part. + /// + /// # Examples + /// + /// ``` + /// #![feature(result_cloned)] + /// let val = 12; + /// let x = Ok(&mut val); + /// assert_eq!(x, Ok(&mut 12)); + /// let cloned = x.cloned(); + /// assert_eq!(cloned, Ok(12)); + /// ``` + #[unstable(feature = "result_cloned", reason = "newly added", issue = "XXXXX")] + fn cloned(self) -> Result { + self.map(|t| t.clone()) + } +} + +impl Result { + /// Maps a `Result` to a `Result` by cloning the contents of the + /// `Err` part. + /// + /// # Examples + /// + /// ``` + /// #![feature(result_cloned)] + /// let val = 12; + /// let x = Err(&mut val); + /// assert_eq!(x, Err(&mut 12)); + /// let cloned = x.cloned(); + /// assert_eq!(cloned, Err(12)); + /// ``` + #[unstable(feature = "result_cloned", reason = "newly added", issue = "XXXXX")] + fn cloned_err(self) -> Result { + self.map_err(|e| e.clone()) + } +} + +impl Result { + /// Maps a `Result` to a `Result` by cloning the contents of the + /// `Err` part. + /// + /// # Examples + /// + /// ``` + /// #![feature(result_cloned)] + /// let val = 12; + /// let x = Err(&mut val); + /// assert_eq!(x, Err(&mut 12)); + /// let cloned = x.cloned(); + /// assert_eq!(cloned, Err(12)); + /// ``` + #[unstable(feature = "result_cloned", reason = "newly added", issue = "XXXXX")] + fn cloned_err(self) -> Result { + self.map_err(|e| e.clone()) + } +} + impl Result { /// Unwraps a result, yielding the content of an [`Ok`]. /// From c784720f3a2d0b66142da3c9a9fd6039f5d3a036 Mon Sep 17 00:00:00 2001 From: ksqsf Date: Thu, 1 Aug 2019 00:55:36 +0800 Subject: [PATCH 02/22] Fix issue and impl --- src/libcore/result.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/libcore/result.rs b/src/libcore/result.rs index a8f0f422cb4d7..31584972b9f68 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -834,7 +834,7 @@ impl Result<&T, E> { /// let copied = x.copied(); /// assert_eq!(copied, Ok(12)); /// ``` - #[unstable(feature = "result_copied", reason = "newly added", issue = "XXXXX")] + #[unstable(feature = "result_copied", reason = "newly added", issue = "63168")] fn copied(self) -> Result { self.map(|&t| t) } @@ -854,7 +854,7 @@ impl Result<&mut T, E> { /// let copied = x.copied(); /// assert_eq!(copied, Ok(12)); /// ``` - #[unstable(feature = "result_copied", reason = "newly added", issue = "XXXXX")] + #[unstable(feature = "result_copied", reason = "newly added", issue = "63168")] fn copied(self) -> Result { self.map(|&mut t| t) } @@ -874,7 +874,7 @@ impl Result { /// let copied = x.copied_err(); /// assert_eq!(copied, Err(12)); /// ``` - #[unstable(feature = "result_copied", reason = "newly added", issue = "XXXXX")] + #[unstable(feature = "result_copied", reason = "newly added", issue = "63168")] fn copied_err(self) -> Result { self.map_err(|&e| e) } @@ -894,7 +894,7 @@ impl Result { /// let copied = x.copied(); /// assert_eq!(cloned, Err(12)); /// ``` - #[unstable(feature = "result_copied", reason = "newly added", issue = "XXXXX")] + #[unstable(feature = "result_copied", reason = "newly added", issue = "63168")] fn copied_err(self) -> Result { self.map_err(|&mut e| e) } @@ -914,7 +914,7 @@ impl Result<&T, E> { /// let cloned = x.cloned(); /// assert_eq!(cloned, Ok(12)); /// ``` - #[unstable(feature = "result_cloned", reason = "newly added", issue = "XXXXX")] + #[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")] fn cloned(self) -> Result { self.map(|t| t.clone()) } @@ -934,13 +934,13 @@ impl Result<&mut T, E> { /// let cloned = x.cloned(); /// assert_eq!(cloned, Ok(12)); /// ``` - #[unstable(feature = "result_cloned", reason = "newly added", issue = "XXXXX")] + #[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")] fn cloned(self) -> Result { self.map(|t| t.clone()) } } -impl Result { +impl Result { /// Maps a `Result` to a `Result` by cloning the contents of the /// `Err` part. /// @@ -954,7 +954,7 @@ impl Result { /// let cloned = x.cloned(); /// assert_eq!(cloned, Err(12)); /// ``` - #[unstable(feature = "result_cloned", reason = "newly added", issue = "XXXXX")] + #[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")] fn cloned_err(self) -> Result { self.map_err(|e| e.clone()) } @@ -974,7 +974,7 @@ impl Result { /// let cloned = x.cloned(); /// assert_eq!(cloned, Err(12)); /// ``` - #[unstable(feature = "result_cloned", reason = "newly added", issue = "XXXXX")] + #[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")] fn cloned_err(self) -> Result { self.map_err(|e| e.clone()) } From 5a36b0dba1d7a350bba04b1abf256f057b3d1079 Mon Sep 17 00:00:00 2001 From: ksqsf Date: Thu, 1 Aug 2019 01:09:07 +0800 Subject: [PATCH 03/22] Make these methods public --- src/libcore/result.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/libcore/result.rs b/src/libcore/result.rs index 31584972b9f68..77cb447b95460 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -835,7 +835,7 @@ impl Result<&T, E> { /// assert_eq!(copied, Ok(12)); /// ``` #[unstable(feature = "result_copied", reason = "newly added", issue = "63168")] - fn copied(self) -> Result { + pub fn copied(self) -> Result { self.map(|&t| t) } } @@ -855,7 +855,7 @@ impl Result<&mut T, E> { /// assert_eq!(copied, Ok(12)); /// ``` #[unstable(feature = "result_copied", reason = "newly added", issue = "63168")] - fn copied(self) -> Result { + pub fn copied(self) -> Result { self.map(|&mut t| t) } } @@ -875,7 +875,7 @@ impl Result { /// assert_eq!(copied, Err(12)); /// ``` #[unstable(feature = "result_copied", reason = "newly added", issue = "63168")] - fn copied_err(self) -> Result { + pub fn copied_err(self) -> Result { self.map_err(|&e| e) } } @@ -895,7 +895,7 @@ impl Result { /// assert_eq!(cloned, Err(12)); /// ``` #[unstable(feature = "result_copied", reason = "newly added", issue = "63168")] - fn copied_err(self) -> Result { + pub fn copied_err(self) -> Result { self.map_err(|&mut e| e) } } @@ -915,7 +915,7 @@ impl Result<&T, E> { /// assert_eq!(cloned, Ok(12)); /// ``` #[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")] - fn cloned(self) -> Result { + pub fn cloned(self) -> Result { self.map(|t| t.clone()) } } @@ -935,7 +935,7 @@ impl Result<&mut T, E> { /// assert_eq!(cloned, Ok(12)); /// ``` #[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")] - fn cloned(self) -> Result { + pub fn cloned(self) -> Result { self.map(|t| t.clone()) } } @@ -955,7 +955,7 @@ impl Result { /// assert_eq!(cloned, Err(12)); /// ``` #[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")] - fn cloned_err(self) -> Result { + pub fn cloned_err(self) -> Result { self.map_err(|e| e.clone()) } } @@ -975,7 +975,7 @@ impl Result { /// assert_eq!(cloned, Err(12)); /// ``` #[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")] - fn cloned_err(self) -> Result { + pub fn cloned_err(self) -> Result { self.map_err(|e| e.clone()) } } From 6c130817623426697d8ebdf5d505487bd11ee2f6 Mon Sep 17 00:00:00 2001 From: ksqsf Date: Thu, 1 Aug 2019 02:35:14 +0800 Subject: [PATCH 04/22] Rename {copied,cloned} to {copied,cloned}_ok, and add {copied,cloned} to copy/clone both Ok and Err --- src/libcore/result.rs | 144 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 140 insertions(+), 4 deletions(-) diff --git a/src/libcore/result.rs b/src/libcore/result.rs index 77cb447b95460..0aa8fcb69b9c6 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -835,7 +835,7 @@ impl Result<&T, E> { /// assert_eq!(copied, Ok(12)); /// ``` #[unstable(feature = "result_copied", reason = "newly added", issue = "63168")] - pub fn copied(self) -> Result { + pub fn copied_ok(self) -> Result { self.map(|&t| t) } } @@ -855,7 +855,7 @@ impl Result<&mut T, E> { /// assert_eq!(copied, Ok(12)); /// ``` #[unstable(feature = "result_copied", reason = "newly added", issue = "63168")] - pub fn copied(self) -> Result { + pub fn copied_ok(self) -> Result { self.map(|&mut t| t) } } @@ -900,6 +900,74 @@ impl Result { } } +impl Result<&T, &E> { + /// Maps a `Result<&T, &E>` to a `Result` by copying the + /// contents of the result. + /// + /// # Examples + /// + /// ``` + /// #![feature(result_copied)] + /// assert_eq!(Err(&1), Err(1)); + /// assert_eq!(Ok(&42), Ok(42)); + /// ``` + #[unstable(feature = "result_copied", reason = "newly added", issue = "63168")] + pub fn copied(self) -> Result { + self.copied_ok().copied_err() + } +} + +impl Result<&mut T, &E> { + /// Maps a `Result<&mut T, &E>` to a `Result` by copying the + /// contents of the result. + /// + /// # Examples + /// + /// ``` + /// #![feature(result_copied)] + /// assert_eq!(Err(&1), Err(1)); + /// assert_eq!(Ok(&mut 42), Ok(42)); + /// ``` + #[unstable(feature = "result_copied", reason = "newly added", issue = "63168")] + pub fn copied(self) -> Result { + self.copied_ok().copied_err() + } +} + +impl Result<&T, &mut E> { + /// Maps a `Result<&T, &mut E>` to a `Result` by copying the + /// contents of the result. + /// + /// # Examples + /// + /// ``` + /// #![feature(result_copied)] + /// assert_eq!(Err(&mut 1), Err(1)); + /// assert_eq!(Ok(&42), Ok(42)); + /// ``` + #[unstable(feature = "result_copied", reason = "newly added", issue = "63168")] + pub fn copied(self) -> Result { + self.copied_ok().copied_err() + } +} + +impl Result<&mut T, &mut E> { + /// Maps a `Result<&mut T, &mut E>` to a `Result` by copying + /// the contents of the result. + /// + /// # Examples + /// + /// ``` + /// #![feature(result_copied)] + /// assert_eq!(Err(&mut 1), Err(1)); + /// assert_eq!(Ok(&mut 42), Ok(42)); + /// ``` + #[unstable(feature = "result_copied", reason = "newly added", issue = "63168")] + pub fn copied(self) -> Result { + self.copied_ok().copied_err() + } +} + impl Result<&T, E> { /// Maps a `Result<&T, E>` to a `Result` by cloning the contents of the /// `Ok` part. @@ -915,7 +983,7 @@ impl Result<&T, E> { /// assert_eq!(cloned, Ok(12)); /// ``` #[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")] - pub fn cloned(self) -> Result { + pub fn cloned_ok(self) -> Result { self.map(|t| t.clone()) } } @@ -935,7 +1003,7 @@ impl Result<&mut T, E> { /// assert_eq!(cloned, Ok(12)); /// ``` #[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")] - pub fn cloned(self) -> Result { + pub fn cloned_ok(self) -> Result { self.map(|t| t.clone()) } } @@ -980,6 +1048,74 @@ impl Result { } } +impl Result<&T, &E> { + /// Maps a `Result<&T, &E>` to a `Result` by cloning the contents of the + /// result. + /// + /// # Examples + /// + /// ``` + /// #![feature(result_cloned)] + /// assert_eq!(Err(&1).cloned(), Err(1)); + /// assert_eq!(Ok(&42).cloned(), Ok(42)); + /// ``` + #[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")] + pub fn cloned(self) -> Result { + self.cloned_ok().cloned_err() + } +} + +impl Result<&mut T, &E> { + /// Maps a `Result<&mut T, &E>` to a `Result` by cloning the + /// contents of the result. + /// + /// # Examples + /// + /// ``` + /// #![feature(result_cloned)] + /// assert_eq!(Err(&1).cloned(), Err(1)); + /// assert_eq!(Ok(&mut 42).cloned(), Ok(42)); + /// ``` + #[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")] + pub fn cloned(self) -> Result { + self.cloned_ok().cloned_err() + } +} + +impl Result<&T, &mut E> { + /// Maps a `Result<&T, &mut E>` to a `Result` by cloning the + /// contents of the result. + /// + /// # Examples + /// + /// ``` + /// #![feature(result_cloned)] + /// assert_eq!(Err(&mut 1).cloned(), Err(1)); + /// assert_eq!(Ok(&42).cloned(), Ok(42)); + /// ``` + #[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")] + pub fn cloned(self) -> Result { + self.cloned_ok().cloned_err() + } +} + +impl Result<&mut T, &mut E> { + /// Maps a `Result<&mut T, &mut E>` to a `Result` by cloning + /// the contents of the result. + /// + /// # Examples + /// + /// ``` + /// #![feature(result_cloned)] + /// assert_eq!(Err(&mut 1).cloned(), Err(1)); + /// assert_eq!(Ok(&mut 42).cloned(), Ok(42)); + /// ``` + #[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")] + pub fn cloned(self) -> Result { + self.cloned_ok().cloned_err() + } +} + impl Result { /// Unwraps a result, yielding the content of an [`Ok`]. /// From 4b2f598986e135f0eb070a9be87a5a963e8ff3d4 Mon Sep 17 00:00:00 2001 From: ksqsf Date: Thu, 1 Aug 2019 13:38:23 +0800 Subject: [PATCH 05/22] Revert "cloned/copied" This reverts commit 6c130817623426697d8ebdf5d505487bd11ee2f6. --- src/libcore/result.rs | 144 ++---------------------------------------- 1 file changed, 4 insertions(+), 140 deletions(-) diff --git a/src/libcore/result.rs b/src/libcore/result.rs index 0aa8fcb69b9c6..77cb447b95460 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -835,7 +835,7 @@ impl Result<&T, E> { /// assert_eq!(copied, Ok(12)); /// ``` #[unstable(feature = "result_copied", reason = "newly added", issue = "63168")] - pub fn copied_ok(self) -> Result { + pub fn copied(self) -> Result { self.map(|&t| t) } } @@ -855,7 +855,7 @@ impl Result<&mut T, E> { /// assert_eq!(copied, Ok(12)); /// ``` #[unstable(feature = "result_copied", reason = "newly added", issue = "63168")] - pub fn copied_ok(self) -> Result { + pub fn copied(self) -> Result { self.map(|&mut t| t) } } @@ -900,74 +900,6 @@ impl Result { } } -impl Result<&T, &E> { - /// Maps a `Result<&T, &E>` to a `Result` by copying the - /// contents of the result. - /// - /// # Examples - /// - /// ``` - /// #![feature(result_copied)] - /// assert_eq!(Err(&1), Err(1)); - /// assert_eq!(Ok(&42), Ok(42)); - /// ``` - #[unstable(feature = "result_copied", reason = "newly added", issue = "63168")] - pub fn copied(self) -> Result { - self.copied_ok().copied_err() - } -} - -impl Result<&mut T, &E> { - /// Maps a `Result<&mut T, &E>` to a `Result` by copying the - /// contents of the result. - /// - /// # Examples - /// - /// ``` - /// #![feature(result_copied)] - /// assert_eq!(Err(&1), Err(1)); - /// assert_eq!(Ok(&mut 42), Ok(42)); - /// ``` - #[unstable(feature = "result_copied", reason = "newly added", issue = "63168")] - pub fn copied(self) -> Result { - self.copied_ok().copied_err() - } -} - -impl Result<&T, &mut E> { - /// Maps a `Result<&T, &mut E>` to a `Result` by copying the - /// contents of the result. - /// - /// # Examples - /// - /// ``` - /// #![feature(result_copied)] - /// assert_eq!(Err(&mut 1), Err(1)); - /// assert_eq!(Ok(&42), Ok(42)); - /// ``` - #[unstable(feature = "result_copied", reason = "newly added", issue = "63168")] - pub fn copied(self) -> Result { - self.copied_ok().copied_err() - } -} - -impl Result<&mut T, &mut E> { - /// Maps a `Result<&mut T, &mut E>` to a `Result` by copying - /// the contents of the result. - /// - /// # Examples - /// - /// ``` - /// #![feature(result_copied)] - /// assert_eq!(Err(&mut 1), Err(1)); - /// assert_eq!(Ok(&mut 42), Ok(42)); - /// ``` - #[unstable(feature = "result_copied", reason = "newly added", issue = "63168")] - pub fn copied(self) -> Result { - self.copied_ok().copied_err() - } -} - impl Result<&T, E> { /// Maps a `Result<&T, E>` to a `Result` by cloning the contents of the /// `Ok` part. @@ -983,7 +915,7 @@ impl Result<&T, E> { /// assert_eq!(cloned, Ok(12)); /// ``` #[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")] - pub fn cloned_ok(self) -> Result { + pub fn cloned(self) -> Result { self.map(|t| t.clone()) } } @@ -1003,7 +935,7 @@ impl Result<&mut T, E> { /// assert_eq!(cloned, Ok(12)); /// ``` #[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")] - pub fn cloned_ok(self) -> Result { + pub fn cloned(self) -> Result { self.map(|t| t.clone()) } } @@ -1048,74 +980,6 @@ impl Result { } } -impl Result<&T, &E> { - /// Maps a `Result<&T, &E>` to a `Result` by cloning the contents of the - /// result. - /// - /// # Examples - /// - /// ``` - /// #![feature(result_cloned)] - /// assert_eq!(Err(&1).cloned(), Err(1)); - /// assert_eq!(Ok(&42).cloned(), Ok(42)); - /// ``` - #[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")] - pub fn cloned(self) -> Result { - self.cloned_ok().cloned_err() - } -} - -impl Result<&mut T, &E> { - /// Maps a `Result<&mut T, &E>` to a `Result` by cloning the - /// contents of the result. - /// - /// # Examples - /// - /// ``` - /// #![feature(result_cloned)] - /// assert_eq!(Err(&1).cloned(), Err(1)); - /// assert_eq!(Ok(&mut 42).cloned(), Ok(42)); - /// ``` - #[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")] - pub fn cloned(self) -> Result { - self.cloned_ok().cloned_err() - } -} - -impl Result<&T, &mut E> { - /// Maps a `Result<&T, &mut E>` to a `Result` by cloning the - /// contents of the result. - /// - /// # Examples - /// - /// ``` - /// #![feature(result_cloned)] - /// assert_eq!(Err(&mut 1).cloned(), Err(1)); - /// assert_eq!(Ok(&42).cloned(), Ok(42)); - /// ``` - #[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")] - pub fn cloned(self) -> Result { - self.cloned_ok().cloned_err() - } -} - -impl Result<&mut T, &mut E> { - /// Maps a `Result<&mut T, &mut E>` to a `Result` by cloning - /// the contents of the result. - /// - /// # Examples - /// - /// ``` - /// #![feature(result_cloned)] - /// assert_eq!(Err(&mut 1).cloned(), Err(1)); - /// assert_eq!(Ok(&mut 42).cloned(), Ok(42)); - /// ``` - #[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")] - pub fn cloned(self) -> Result { - self.cloned_ok().cloned_err() - } -} - impl Result { /// Unwraps a result, yielding the content of an [`Ok`]. /// From 9733f0d16382c728e502c3efb4f38a19270f8202 Mon Sep 17 00:00:00 2001 From: ksqsf Date: Thu, 1 Aug 2019 17:12:13 +0800 Subject: [PATCH 06/22] Fix doc tests --- src/libcore/result.rs | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/libcore/result.rs b/src/libcore/result.rs index 77cb447b95460..7b9f6d9ff8a61 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -829,7 +829,7 @@ impl Result<&T, E> { /// ``` /// #![feature(result_copied)] /// let val = 12; - /// let x = Ok(&val); + /// let x: Result<&i32, i32> = Ok(&val); /// assert_eq!(x, Ok(&12)); /// let copied = x.copied(); /// assert_eq!(copied, Ok(12)); @@ -848,8 +848,8 @@ impl Result<&mut T, E> { /// /// ``` /// #![feature(result_copied)] - /// let val = 12; - /// let x = Ok(&mut val); + /// let mut val = 12; + /// let x: Result<&mut i32, i32> = Ok(&mut val); /// assert_eq!(x, Ok(&mut 12)); /// let copied = x.copied(); /// assert_eq!(copied, Ok(12)); @@ -869,7 +869,7 @@ impl Result { /// ``` /// #![feature(result_copied)] /// let val = 12; - /// let x = Err(&val); + /// let x: Result = Err(&val); /// assert_eq!(x, Err(&12)); /// let copied = x.copied_err(); /// assert_eq!(copied, Err(12)); @@ -888,11 +888,11 @@ impl Result { /// /// ``` /// #![feature(result_copied)] - /// let val = 12; - /// let x = Err(&mut val); + /// let mut val = 12; + /// let x: Result = Err(&mut val); /// assert_eq!(x, Err(&mut 12)); - /// let copied = x.copied(); - /// assert_eq!(cloned, Err(12)); + /// let copied = x.copied_err(); + /// assert_eq!(copied, Err(12)); /// ``` #[unstable(feature = "result_copied", reason = "newly added", issue = "63168")] pub fn copied_err(self) -> Result { @@ -909,7 +909,7 @@ impl Result<&T, E> { /// ``` /// #![feature(result_cloned)] /// let val = 12; - /// let x = Ok(&val); + /// let x: Result<&i32, i32> = Ok(&val); /// assert_eq!(x, Ok(&12)); /// let cloned = x.cloned(); /// assert_eq!(cloned, Ok(12)); @@ -928,8 +928,8 @@ impl Result<&mut T, E> { /// /// ``` /// #![feature(result_cloned)] - /// let val = 12; - /// let x = Ok(&mut val); + /// let mut val = 12; + /// let x: Result<&mut i32, i32> = Ok(&mut val); /// assert_eq!(x, Ok(&mut 12)); /// let cloned = x.cloned(); /// assert_eq!(cloned, Ok(12)); @@ -949,9 +949,9 @@ impl Result { /// ``` /// #![feature(result_cloned)] /// let val = 12; - /// let x = Err(&mut val); - /// assert_eq!(x, Err(&mut 12)); - /// let cloned = x.cloned(); + /// let x: Result = Err(&val); + /// assert_eq!(x, Err(&12)); + /// let cloned = x.cloned_err(); /// assert_eq!(cloned, Err(12)); /// ``` #[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")] @@ -968,10 +968,10 @@ impl Result { /// /// ``` /// #![feature(result_cloned)] - /// let val = 12; - /// let x = Err(&mut val); + /// let mut val = 12; + /// let x: Result = Err(&mut val); /// assert_eq!(x, Err(&mut 12)); - /// let cloned = x.cloned(); + /// let cloned = x.cloned_err(); /// assert_eq!(cloned, Err(12)); /// ``` #[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")] From 61e52866352339326600fe694e92373f2e07fd79 Mon Sep 17 00:00:00 2001 From: ksqsf Date: Fri, 2 Aug 2019 13:58:55 +0800 Subject: [PATCH 07/22] Remove Err variants of cloned and copied --- src/libcore/result.rs | 79 ------------------------------------------- 1 file changed, 79 deletions(-) diff --git a/src/libcore/result.rs b/src/libcore/result.rs index 7b9f6d9ff8a61..935e48574fdf5 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -860,46 +860,6 @@ impl Result<&mut T, E> { } } -impl Result { - /// Maps a `Result` to a `Result` by copying the contents of the - /// `Err` part. - /// - /// # Examples - /// - /// ``` - /// #![feature(result_copied)] - /// let val = 12; - /// let x: Result = Err(&val); - /// assert_eq!(x, Err(&12)); - /// let copied = x.copied_err(); - /// assert_eq!(copied, Err(12)); - /// ``` - #[unstable(feature = "result_copied", reason = "newly added", issue = "63168")] - pub fn copied_err(self) -> Result { - self.map_err(|&e| e) - } -} - -impl Result { - /// Maps a `Result` to a `Result` by copying the contents of the - /// `Err` part. - /// - /// # Examples - /// - /// ``` - /// #![feature(result_copied)] - /// let mut val = 12; - /// let x: Result = Err(&mut val); - /// assert_eq!(x, Err(&mut 12)); - /// let copied = x.copied_err(); - /// assert_eq!(copied, Err(12)); - /// ``` - #[unstable(feature = "result_copied", reason = "newly added", issue = "63168")] - pub fn copied_err(self) -> Result { - self.map_err(|&mut e| e) - } -} - impl Result<&T, E> { /// Maps a `Result<&T, E>` to a `Result` by cloning the contents of the /// `Ok` part. @@ -940,45 +900,6 @@ impl Result<&mut T, E> { } } -impl Result { - /// Maps a `Result` to a `Result` by cloning the contents of the - /// `Err` part. - /// - /// # Examples - /// - /// ``` - /// #![feature(result_cloned)] - /// let val = 12; - /// let x: Result = Err(&val); - /// assert_eq!(x, Err(&12)); - /// let cloned = x.cloned_err(); - /// assert_eq!(cloned, Err(12)); - /// ``` - #[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")] - pub fn cloned_err(self) -> Result { - self.map_err(|e| e.clone()) - } -} - -impl Result { - /// Maps a `Result` to a `Result` by cloning the contents of the - /// `Err` part. - /// - /// # Examples - /// - /// ``` - /// #![feature(result_cloned)] - /// let mut val = 12; - /// let x: Result = Err(&mut val); - /// assert_eq!(x, Err(&mut 12)); - /// let cloned = x.cloned_err(); - /// assert_eq!(cloned, Err(12)); - /// ``` - #[unstable(feature = "result_cloned", reason = "newly added", issue = "63168")] - pub fn cloned_err(self) -> Result { - self.map_err(|e| e.clone()) - } -} impl Result { /// Unwraps a result, yielding the content of an [`Ok`]. From b2b9b81c9a69ba1bd35e042122b4b132dd6e1e6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Mon, 26 Aug 2019 17:46:14 -0700 Subject: [PATCH 08/22] Account for doc comments coming from proc macros without spans --- .../passes/check_code_block_syntax.rs | 2 +- .../passes/collect_intra_doc_links.rs | 4 ++-- src/librustdoc/passes/mod.rs | 21 ++++++++++--------- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/librustdoc/passes/check_code_block_syntax.rs b/src/librustdoc/passes/check_code_block_syntax.rs index 357e17d2d1bc4..67aa014a788f2 100644 --- a/src/librustdoc/passes/check_code_block_syntax.rs +++ b/src/librustdoc/passes/check_code_block_syntax.rs @@ -69,7 +69,7 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> { // We couldn't calculate the span of the markdown block that had the error, so our // diagnostics are going to be a bit lacking. let mut diag = self.cx.sess().struct_span_warn( - super::span_of_attrs(&item.attrs), + super::span_of_attrs(&item.attrs).unwrap_or(item.source.span()), "doc comment contains an invalid Rust code block", ); diff --git a/src/librustdoc/passes/collect_intra_doc_links.rs b/src/librustdoc/passes/collect_intra_doc_links.rs index c73c46472d804..2951b2ccb2af9 100644 --- a/src/librustdoc/passes/collect_intra_doc_links.rs +++ b/src/librustdoc/passes/collect_intra_doc_links.rs @@ -465,7 +465,7 @@ fn resolution_failure( } }; let attrs = &item.attrs; - let sp = span_of_attrs(attrs); + let sp = span_of_attrs(attrs).unwrap_or(item.source.span()); let mut diag = cx.tcx.struct_span_lint_hir( lint::builtin::INTRA_DOC_LINK_RESOLUTION_FAILURE, @@ -517,7 +517,7 @@ fn ambiguity_error( } }; let attrs = &item.attrs; - let sp = span_of_attrs(attrs); + let sp = span_of_attrs(attrs).unwrap_or(item.source.span()); let mut msg = format!("`{}` is ", path_str); diff --git a/src/librustdoc/passes/mod.rs b/src/librustdoc/passes/mod.rs index 641a6df221446..49a34c7e46281 100644 --- a/src/librustdoc/passes/mod.rs +++ b/src/librustdoc/passes/mod.rs @@ -339,7 +339,7 @@ pub fn look_for_tests<'tcx>( find_testable_code(&dox, &mut tests, ErrorCodes::No); if check_missing_code == true && tests.found_tests == 0 { - let sp = span_of_attrs(&item.attrs).substitute_dummy(item.source.span()); + let sp = span_of_attrs(&item.attrs).unwrap_or(item.source.span()); let mut diag = cx.tcx.struct_span_lint_hir( lint::builtin::MISSING_DOC_CODE_EXAMPLES, hir_id, @@ -352,20 +352,23 @@ pub fn look_for_tests<'tcx>( let mut diag = cx.tcx.struct_span_lint_hir( lint::builtin::PRIVATE_DOC_TESTS, hir_id, - span_of_attrs(&item.attrs), + span_of_attrs(&item.attrs).unwrap_or(item.source.span()), "Documentation test in private item"); diag.emit(); } } /// Returns a span encompassing all the given attributes. -crate fn span_of_attrs(attrs: &clean::Attributes) -> Span { +crate fn span_of_attrs(attrs: &clean::Attributes) -> Option { if attrs.doc_strings.is_empty() { - return DUMMY_SP; + return None; } let start = attrs.doc_strings[0].span(); + if start == DUMMY_SP { + return None; + } let end = attrs.doc_strings.last().expect("No doc strings provided").span(); - start.to(end) + Some(start.to(end)) } /// Attempts to match a range of bytes from parsed markdown to a `Span` in the source code. @@ -391,7 +394,7 @@ crate fn source_span_for_markdown_range( let snippet = cx .sess() .source_map() - .span_to_snippet(span_of_attrs(attrs)) + .span_to_snippet(span_of_attrs(attrs)?) .ok()?; let starting_line = markdown[..md_range.start].matches('\n').count(); @@ -441,10 +444,8 @@ crate fn source_span_for_markdown_range( } } - let sp = span_of_attrs(attrs).from_inner(InnerSpan::new( + Some(span_of_attrs(attrs)?.from_inner(InnerSpan::new( md_range.start + start_bytes, md_range.end + start_bytes + end_bytes, - )); - - Some(sp) + ))) } From 7ed542da7815070b4fc7aa87d53e5b97aa2e8828 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Mon, 26 Aug 2019 21:12:59 -0700 Subject: [PATCH 09/22] add regression test --- .../auxiliary/through-proc-macro-aux.rs | 20 +++++++++++++++++++ src/test/rustdoc/through-proc-macro.rs | 12 +++++++++++ 2 files changed, 32 insertions(+) create mode 100644 src/test/rustdoc/auxiliary/through-proc-macro-aux.rs create mode 100644 src/test/rustdoc/through-proc-macro.rs diff --git a/src/test/rustdoc/auxiliary/through-proc-macro-aux.rs b/src/test/rustdoc/auxiliary/through-proc-macro-aux.rs new file mode 100644 index 0000000000000..5c4a01ee3a74a --- /dev/null +++ b/src/test/rustdoc/auxiliary/through-proc-macro-aux.rs @@ -0,0 +1,20 @@ +// force-host +// no-prefer-dynamic +#![crate_type = "proc-macro"] +#![crate_name="some_macros"] + +extern crate proc_macro; +use proc_macro::TokenStream; + +#[proc_macro_attribute] +pub fn first(_attr: TokenStream, item: TokenStream) -> TokenStream { + item // This doesn't erase the spans. +} + +#[proc_macro_attribute] +pub fn second(_attr: TokenStream, item: TokenStream) -> TokenStream { + // Make a new `TokenStream` to erase the spans: + let mut out: TokenStream = TokenStream::new(); + out.extend(item); + out +} diff --git a/src/test/rustdoc/through-proc-macro.rs b/src/test/rustdoc/through-proc-macro.rs new file mode 100644 index 0000000000000..348c9eea2dcbf --- /dev/null +++ b/src/test/rustdoc/through-proc-macro.rs @@ -0,0 +1,12 @@ +// aux-build:through-proc-macro-aux.rs +// build-aux-docs +#![warn(intra_doc_link_resolution_failure)] +extern crate some_macros; + +#[some_macros::second] +pub enum Boom { + /// [Oooops] + Bam, +} + +fn main() {} From 820aa5b2ef9b527a67b46ef70f001151f5d66441 Mon Sep 17 00:00:00 2001 From: Alessandro Ghedini Date: Wed, 28 Aug 2019 23:55:28 +0100 Subject: [PATCH 10/22] Stabilize pin_into_inner in 1.39.0 FCP: https://github.com/rust-lang/rust/issues/60245#issuecomment-522258129 Closes #60245 --- src/libcore/pin.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libcore/pin.rs b/src/libcore/pin.rs index 6efeaf9ee7d2e..1080fd32a8862 100644 --- a/src/libcore/pin.rs +++ b/src/libcore/pin.rs @@ -462,7 +462,7 @@ impl> Pin

{ /// can ignore the pinning invariants when unwrapping it. /// /// [`Unpin`]: ../../std/marker/trait.Unpin.html - #[unstable(feature = "pin_into_inner", issue = "60245")] + #[stable(feature = "pin_into_inner", since = "1.39.0")] #[inline(always)] pub fn into_inner(pin: Pin

) -> P { pin.pointer @@ -569,7 +569,7 @@ impl Pin

{ /// /// [`Unpin`]: ../../std/marker/trait.Unpin.html /// [`Pin::into_inner`]: #method.into_inner - #[unstable(feature = "pin_into_inner", issue = "60245")] + #[stable(feature = "pin_into_inner", since = "1.39.0")] #[inline(always)] pub unsafe fn into_inner_unchecked(pin: Pin

) -> P { pin.pointer From 5da1123c5efbd91813fb5ba7cec5298d960ae3af Mon Sep 17 00:00:00 2001 From: Tyler Mandry Date: Thu, 29 Aug 2019 17:49:46 -0700 Subject: [PATCH 11/22] Update zx_time_t to an i64 --- src/libstd/sys/unix/process/zircon.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libstd/sys/unix/process/zircon.rs b/src/libstd/sys/unix/process/zircon.rs index ec715d5490f6f..9224bef7c4a5f 100644 --- a/src/libstd/sys/unix/process/zircon.rs +++ b/src/libstd/sys/unix/process/zircon.rs @@ -3,7 +3,7 @@ use crate::convert::TryInto; use crate::io; use crate::os::raw::c_char; -use crate::u64; +use crate::i64; use libc::{c_int, c_void, size_t}; @@ -14,8 +14,8 @@ pub type zx_status_t = i32; pub const ZX_HANDLE_INVALID: zx_handle_t = 0; -pub type zx_time_t = u64; -pub const ZX_TIME_INFINITE : zx_time_t = u64::MAX; +pub type zx_time_t = i64; +pub const ZX_TIME_INFINITE : zx_time_t = i64::MAX; pub type zx_signals_t = u32; From 403701f97628b85bfa3e5ec0e5ca82b81d53ba1e Mon Sep 17 00:00:00 2001 From: Tyler Mandry Date: Fri, 30 Aug 2019 11:51:04 -0700 Subject: [PATCH 12/22] Don't try to use /dev/null on Fuchsia --- src/libstd/sys/unix/process/process_common.rs | 28 +++++++++++++++++-- .../sys/unix/process/process_fuchsia.rs | 15 ++++++---- src/libstd/sys/unix/process/zircon.rs | 2 +- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/libstd/sys/unix/process/process_common.rs b/src/libstd/sys/unix/process/process_common.rs index 6bb20bbe08794..511e67840803e 100644 --- a/src/libstd/sys/unix/process/process_common.rs +++ b/src/libstd/sys/unix/process/process_common.rs @@ -1,19 +1,27 @@ use crate::os::unix::prelude::*; -use crate::ffi::{OsString, OsStr, CString, CStr}; +use crate::ffi::{OsString, OsStr, CString}; use crate::fmt; use crate::io; use crate::ptr; use crate::sys::fd::FileDesc; -use crate::sys::fs::{File, OpenOptions}; +use crate::sys::fs::File; use crate::sys::pipe::{self, AnonPipe}; use crate::sys_common::process::{CommandEnv, DefaultEnvKey}; use crate::collections::BTreeMap; +#[cfg(not(target_os = "fuchsia"))] +use { + crate::ffi::CStr, + crate::sys::fs::OpenOptions, +}; + use libc::{c_int, gid_t, uid_t, c_char, EXIT_SUCCESS, EXIT_FAILURE}; cfg_if::cfg_if! { - if #[cfg(target_os = "redox")] { + if #[cfg(target_os = "fuchsia")] { + // fuchsia doesn't have /dev/null + } else if #[cfg(target_os = "redox")] { const DEV_NULL: &'static str = "null:\0"; } else { const DEV_NULL: &'static str = "/dev/null\0"; @@ -83,6 +91,11 @@ pub enum ChildStdio { Inherit, Explicit(c_int), Owned(FileDesc), + + // On Fuchsia, null stdio is the default, so we simply don't specify + // any actions at the time of spawning. + #[cfg(target_os = "fuchsia")] + Null, } pub enum Stdio { @@ -301,6 +314,7 @@ impl Stdio { Ok((ChildStdio::Owned(theirs.into_fd()), Some(ours))) } + #[cfg(not(target_os = "fuchsia"))] Stdio::Null => { let mut opts = OpenOptions::new(); opts.read(readable); @@ -311,6 +325,11 @@ impl Stdio { let fd = File::open_c(&path, &opts)?; Ok((ChildStdio::Owned(fd.into_fd()), None)) } + + #[cfg(target_os = "fuchsia")] + Stdio::Null => { + Ok((ChildStdio::Null, None)) + } } } } @@ -333,6 +352,9 @@ impl ChildStdio { ChildStdio::Inherit => None, ChildStdio::Explicit(fd) => Some(fd), ChildStdio::Owned(ref fd) => Some(fd.raw()), + + #[cfg(target_os = "fuchsia")] + ChildStdio::Null => None, } } } diff --git a/src/libstd/sys/unix/process/process_fuchsia.rs b/src/libstd/sys/unix/process/process_fuchsia.rs index 7c6be9b0a6047..295ec59eb32cf 100644 --- a/src/libstd/sys/unix/process/process_fuchsia.rs +++ b/src/libstd/sys/unix/process/process_fuchsia.rs @@ -52,7 +52,7 @@ impl Command { None => ptr::null(), }; - let transfer_or_clone = |opt_fd, target_fd| if let Some(local_fd) = opt_fd { + let make_action = |local_io: &ChildStdio, target_fd| if let Some(local_fd) = local_io.fd() { fdio_spawn_action_t { action: FDIO_SPAWN_ACTION_TRANSFER_FD, local_fd, @@ -60,6 +60,10 @@ impl Command { ..Default::default() } } else { + if let ChildStdio::Null = local_io { + // acts as no-op + return Default::default(); + } fdio_spawn_action_t { action: FDIO_SPAWN_ACTION_CLONE_FD, local_fd: target_fd, @@ -69,9 +73,9 @@ impl Command { }; // Clone stdin, stdout, and stderr - let action1 = transfer_or_clone(stdio.stdin.fd(), 0); - let action2 = transfer_or_clone(stdio.stdout.fd(), 1); - let action3 = transfer_or_clone(stdio.stderr.fd(), 2); + let action1 = make_action(&stdio.stdin, 0); + let action2 = make_action(&stdio.stdout, 1); + let action3 = make_action(&stdio.stderr, 2); let actions = [action1, action2, action3]; // We don't want FileDesc::drop to be called on any stdio. fdio_spawn_etc @@ -86,7 +90,8 @@ impl Command { zx_cvt(fdio_spawn_etc( 0, FDIO_SPAWN_CLONE_JOB | FDIO_SPAWN_CLONE_LDSVC | FDIO_SPAWN_CLONE_NAMESPACE, - self.get_argv()[0], self.get_argv().as_ptr(), envp, 3, actions.as_ptr(), + self.get_argv()[0], self.get_argv().as_ptr(), envp, + actions.len() as size_t, actions.as_ptr(), &mut process_handle, ptr::null_mut(), ))?; diff --git a/src/libstd/sys/unix/process/zircon.rs b/src/libstd/sys/unix/process/zircon.rs index 9224bef7c4a5f..29032f5e0d200 100644 --- a/src/libstd/sys/unix/process/zircon.rs +++ b/src/libstd/sys/unix/process/zircon.rs @@ -120,7 +120,7 @@ pub struct fdio_spawn_action_t { extern { pub fn fdio_spawn_etc(job: zx_handle_t, flags: u32, path: *const c_char, argv: *const *const c_char, envp: *const *const c_char, - action_count: u64, actions: *const fdio_spawn_action_t, + action_count: size_t, actions: *const fdio_spawn_action_t, process: *mut zx_handle_t, err_msg: *mut c_char) -> zx_status_t; } From 7bfa2be4efa2d4649e8db7548f1980156d58017e Mon Sep 17 00:00:00 2001 From: Tyler Mandry Date: Fri, 30 Aug 2019 18:21:57 -0700 Subject: [PATCH 13/22] fuchsia: Don't fail to spawn if no stdin exists --- .../sys/unix/process/process_fuchsia.rs | 57 ++++++++++++------- src/libstd/sys/unix/process/zircon.rs | 6 +- 2 files changed, 41 insertions(+), 22 deletions(-) diff --git a/src/libstd/sys/unix/process/process_fuchsia.rs b/src/libstd/sys/unix/process/process_fuchsia.rs index 295ec59eb32cf..2b3795292f42a 100644 --- a/src/libstd/sys/unix/process/process_fuchsia.rs +++ b/src/libstd/sys/unix/process/process_fuchsia.rs @@ -52,30 +52,45 @@ impl Command { None => ptr::null(), }; - let make_action = |local_io: &ChildStdio, target_fd| if let Some(local_fd) = local_io.fd() { - fdio_spawn_action_t { - action: FDIO_SPAWN_ACTION_TRANSFER_FD, - local_fd, - target_fd, - ..Default::default() - } - } else { - if let ChildStdio::Null = local_io { - // acts as no-op - return Default::default(); - } - fdio_spawn_action_t { - action: FDIO_SPAWN_ACTION_CLONE_FD, - local_fd: target_fd, - target_fd, - ..Default::default() + let make_action = |local_io: &ChildStdio, target_fd| -> io::Result { + if let Some(local_fd) = local_io.fd() { + Ok(fdio_spawn_action_t { + action: FDIO_SPAWN_ACTION_TRANSFER_FD, + local_fd, + target_fd, + ..Default::default() + }) + } else { + if let ChildStdio::Null = local_io { + // acts as no-op + return Ok(Default::default()); + } + + let mut handle = ZX_HANDLE_INVALID; + let status = fdio_fd_clone(target_fd, &mut handle); + if status == ERR_INVALID_ARGS || status == ERR_NOT_SUPPORTED { + // This descriptor is closed; skip it rather than generating an + // error. + return Ok(Default::default()); + } + zx_cvt(status)?; + + let mut cloned_fd = 0; + zx_cvt(fdio_fd_create(handle, &mut cloned_fd))?; + + Ok(fdio_spawn_action_t { + action: FDIO_SPAWN_ACTION_TRANSFER_FD, + local_fd: cloned_fd as i32, + target_fd, + ..Default::default() + }) } }; // Clone stdin, stdout, and stderr - let action1 = make_action(&stdio.stdin, 0); - let action2 = make_action(&stdio.stdout, 1); - let action3 = make_action(&stdio.stderr, 2); + let action1 = make_action(&stdio.stdin, 0)?; + let action2 = make_action(&stdio.stdout, 1)?; + let action3 = make_action(&stdio.stderr, 2)?; let actions = [action1, action2, action3]; // We don't want FileDesc::drop to be called on any stdio. fdio_spawn_etc @@ -88,7 +103,7 @@ impl Command { let mut process_handle: zx_handle_t = 0; zx_cvt(fdio_spawn_etc( - 0, + ZX_HANDLE_INVALID, FDIO_SPAWN_CLONE_JOB | FDIO_SPAWN_CLONE_LDSVC | FDIO_SPAWN_CLONE_NAMESPACE, self.get_argv()[0], self.get_argv().as_ptr(), envp, actions.len() as size_t, actions.as_ptr(), diff --git a/src/libstd/sys/unix/process/zircon.rs b/src/libstd/sys/unix/process/zircon.rs index 29032f5e0d200..1ba48de3c0785 100644 --- a/src/libstd/sys/unix/process/zircon.rs +++ b/src/libstd/sys/unix/process/zircon.rs @@ -2,8 +2,9 @@ use crate::convert::TryInto; use crate::io; -use crate::os::raw::c_char; use crate::i64; +use crate::mem::MaybeUninit; +use crate::os::raw::c_char; use libc::{c_int, c_void, size_t}; @@ -122,6 +123,9 @@ extern { argv: *const *const c_char, envp: *const *const c_char, action_count: size_t, actions: *const fdio_spawn_action_t, process: *mut zx_handle_t, err_msg: *mut c_char) -> zx_status_t; + + pub fn fdio_fd_clone(fd: c_int, out_handle: *mut zx_handle_t) -> zx_status_t; + pub fn fdio_fd_create(handle: zx_handle_t, fd: *mut c_int) -> zx_status_t; } // fdio_spawn_etc flags From 5f91ad0e3300c36033bf409ceefb00480fecbed3 Mon Sep 17 00:00:00 2001 From: Tyler Mandry Date: Fri, 30 Aug 2019 18:52:19 -0700 Subject: [PATCH 14/22] fuchsia: Fix default environment behavior when spawning --- src/libstd/sys/unix/process/process_fuchsia.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/libstd/sys/unix/process/process_fuchsia.rs b/src/libstd/sys/unix/process/process_fuchsia.rs index 2b3795292f42a..fff9fc6b3bbc8 100644 --- a/src/libstd/sys/unix/process/process_fuchsia.rs +++ b/src/libstd/sys/unix/process/process_fuchsia.rs @@ -48,8 +48,10 @@ impl Command { use crate::sys::process::zircon::*; let envp = match maybe_envp { - Some(envp) => envp.as_ptr(), + // None means to clone the current environment, which is done in the + // flags below. None => ptr::null(), + Some(envp) => envp.as_ptr(), }; let make_action = |local_io: &ChildStdio, target_fd| -> io::Result { @@ -104,7 +106,8 @@ impl Command { let mut process_handle: zx_handle_t = 0; zx_cvt(fdio_spawn_etc( ZX_HANDLE_INVALID, - FDIO_SPAWN_CLONE_JOB | FDIO_SPAWN_CLONE_LDSVC | FDIO_SPAWN_CLONE_NAMESPACE, + FDIO_SPAWN_CLONE_JOB | FDIO_SPAWN_CLONE_LDSVC | FDIO_SPAWN_CLONE_NAMESPACE + | FDIO_SPAWN_CLONE_ENVIRON, // this is ignored when envp is non-null self.get_argv()[0], self.get_argv().as_ptr(), envp, actions.len() as size_t, actions.as_ptr(), &mut process_handle, From 3c4d157c8419d73574587a22a8095ad32d860af5 Mon Sep 17 00:00:00 2001 From: Jethro Beekman Date: Fri, 30 Aug 2019 20:35:27 -0700 Subject: [PATCH 15/22] Fix unlock ordering in SGX synchronization primitives --- src/libstd/sys/sgx/condvar.rs | 3 +-- src/libstd/sys/sgx/mutex.rs | 4 ++-- src/libstd/sys/sgx/rwlock.rs | 34 +++++++++++++++++++-------------- src/libstd/sys/sgx/waitqueue.rs | 11 +++++++++-- 4 files changed, 32 insertions(+), 20 deletions(-) diff --git a/src/libstd/sys/sgx/condvar.rs b/src/libstd/sys/sgx/condvar.rs index 000bb19f2692a..cc1c04a83e752 100644 --- a/src/libstd/sys/sgx/condvar.rs +++ b/src/libstd/sys/sgx/condvar.rs @@ -27,8 +27,7 @@ impl Condvar { pub unsafe fn wait(&self, mutex: &Mutex) { let guard = self.inner.lock(); - mutex.unlock(); - WaitQueue::wait(guard); + WaitQueue::wait(guard, || mutex.unlock()); mutex.lock() } diff --git a/src/libstd/sys/sgx/mutex.rs b/src/libstd/sys/sgx/mutex.rs index f325fb1dd582f..662da8b3f6685 100644 --- a/src/libstd/sys/sgx/mutex.rs +++ b/src/libstd/sys/sgx/mutex.rs @@ -22,7 +22,7 @@ impl Mutex { let mut guard = self.inner.lock(); if *guard.lock_var() { // Another thread has the lock, wait - WaitQueue::wait(guard) + WaitQueue::wait(guard, ||{}) // Another thread has passed the lock to us } else { // We are just now obtaining the lock @@ -83,7 +83,7 @@ impl ReentrantMutex { match guard.lock_var().owner { Some(tcs) if tcs != thread::current() => { // Another thread has the lock, wait - WaitQueue::wait(guard); + WaitQueue::wait(guard, ||{}); // Another thread has passed the lock to us }, _ => { diff --git a/src/libstd/sys/sgx/rwlock.rs b/src/libstd/sys/sgx/rwlock.rs index 30c47e44eef8e..e2f94b1d928e1 100644 --- a/src/libstd/sys/sgx/rwlock.rs +++ b/src/libstd/sys/sgx/rwlock.rs @@ -31,7 +31,7 @@ impl RWLock { if *wguard.lock_var() || !wguard.queue_empty() { // Another thread has or is waiting for the write lock, wait drop(wguard); - WaitQueue::wait(rguard); + WaitQueue::wait(rguard, ||{}); // Another thread has passed the lock to us } else { // No waiting writers, acquire the read lock @@ -62,7 +62,7 @@ impl RWLock { if *wguard.lock_var() || rguard.lock_var().is_some() { // Another thread has the lock, wait drop(rguard); - WaitQueue::wait(wguard); + WaitQueue::wait(wguard, ||{}); // Another thread has passed the lock to us } else { // We are just now obtaining the lock @@ -97,6 +97,7 @@ impl RWLock { if let Ok(mut wguard) = WaitQueue::notify_one(wguard) { // A writer was waiting, pass the lock *wguard.lock_var_mut() = true; + wguard.drop_after(rguard); } else { // No writers were waiting, the lock is released rtassert!(rguard.queue_empty()); @@ -117,21 +118,26 @@ impl RWLock { rguard: SpinMutexGuard<'_, WaitVariable>>, wguard: SpinMutexGuard<'_, WaitVariable>, ) { - if let Err(mut wguard) = WaitQueue::notify_one(wguard) { - // No writers waiting, release the write lock - *wguard.lock_var_mut() = false; - if let Ok(mut rguard) = WaitQueue::notify_all(rguard) { - // One or more readers were waiting, pass the lock to them - if let NotifiedTcs::All { count } = rguard.notified_tcs() { - *rguard.lock_var_mut() = Some(count) + match WaitQueue::notify_one(wguard) { + Err(mut wguard) => { + // No writers waiting, release the write lock + *wguard.lock_var_mut() = false; + if let Ok(mut rguard) = WaitQueue::notify_all(rguard) { + // One or more readers were waiting, pass the lock to them + if let NotifiedTcs::All { count } = rguard.notified_tcs() { + *rguard.lock_var_mut() = Some(count) + } else { + unreachable!() // called notify_all + } + rguard.drop_after(wguard); } else { - unreachable!() // called notify_all + // No readers waiting, the lock is released } - } else { - // No readers waiting, the lock is released + }, + Ok(wguard) => { + // There was a thread waiting for write, just pass the lock + wguard.drop_after(rguard); } - } else { - // There was a thread waiting for write, just pass the lock } } diff --git a/src/libstd/sys/sgx/waitqueue.rs b/src/libstd/sys/sgx/waitqueue.rs index d542f9b410127..3cb40e509b6b2 100644 --- a/src/libstd/sys/sgx/waitqueue.rs +++ b/src/libstd/sys/sgx/waitqueue.rs @@ -98,6 +98,12 @@ impl<'a, T> WaitGuard<'a, T> { pub fn notified_tcs(&self) -> NotifiedTcs { self.notified_tcs } + + /// Drop this `WaitGuard`, after dropping another `guard`. + pub fn drop_after(self, guard: U) { + drop(guard); + drop(self); + } } impl<'a, T> Deref for WaitGuard<'a, T> { @@ -140,7 +146,7 @@ impl WaitQueue { /// until a wakeup event. /// /// This function does not return until this thread has been awoken. - pub fn wait(mut guard: SpinMutexGuard<'_, WaitVariable>) { + pub fn wait(mut guard: SpinMutexGuard<'_, WaitVariable>, before_wait: F) { // very unsafe: check requirements of UnsafeList::push unsafe { let mut entry = UnsafeListEntry::new(SpinMutex::new(WaitEntry { @@ -149,6 +155,7 @@ impl WaitQueue { })); let entry = guard.queue.inner.push(&mut entry); drop(guard); + before_wait(); while !entry.lock().wake { // don't panic, this would invalidate `entry` during unwinding let eventset = rtunwrap!(Ok, usercalls::wait(EV_UNPARK, WAIT_INDEFINITE)); @@ -545,7 +552,7 @@ mod tests { assert!(WaitQueue::notify_one(wq2.lock()).is_ok()); }); - WaitQueue::wait(locked); + WaitQueue::wait(locked, ||{}); t1.join().unwrap(); } From 754a875e15f778ed5bdd49b4208956b3003949c4 Mon Sep 17 00:00:00 2001 From: Matthew Jasper Date: Sat, 31 Aug 2019 16:31:50 +0100 Subject: [PATCH 16/22] Add some more tests for underscore imports --- .../auxiliary/duplicate.rs | 0 .../auxiliary/underscore-imports.rs | 0 .../basic.rs | 0 .../basic.stderr | 0 src/test/ui/underscore-imports/cycle.rs | 18 +++++++++++++++ .../duplicate.rs | 0 .../intercrate.rs | 0 src/test/ui/underscore-imports/shadow.rs | 23 +++++++++++++++++++ src/test/ui/underscore-imports/shadow.stderr | 13 +++++++++++ .../unused-2018.rs | 0 .../unused-2018.stderr | 0 11 files changed, 54 insertions(+) rename src/test/ui/{rfc-2166-underscore-imports => underscore-imports}/auxiliary/duplicate.rs (100%) rename src/test/ui/{rfc-2166-underscore-imports => underscore-imports}/auxiliary/underscore-imports.rs (100%) rename src/test/ui/{rfc-2166-underscore-imports => underscore-imports}/basic.rs (100%) rename src/test/ui/{rfc-2166-underscore-imports => underscore-imports}/basic.stderr (100%) create mode 100644 src/test/ui/underscore-imports/cycle.rs rename src/test/ui/{rfc-2166-underscore-imports => underscore-imports}/duplicate.rs (100%) rename src/test/ui/{rfc-2166-underscore-imports => underscore-imports}/intercrate.rs (100%) create mode 100644 src/test/ui/underscore-imports/shadow.rs create mode 100644 src/test/ui/underscore-imports/shadow.stderr rename src/test/ui/{rfc-2166-underscore-imports => underscore-imports}/unused-2018.rs (100%) rename src/test/ui/{rfc-2166-underscore-imports => underscore-imports}/unused-2018.stderr (100%) diff --git a/src/test/ui/rfc-2166-underscore-imports/auxiliary/duplicate.rs b/src/test/ui/underscore-imports/auxiliary/duplicate.rs similarity index 100% rename from src/test/ui/rfc-2166-underscore-imports/auxiliary/duplicate.rs rename to src/test/ui/underscore-imports/auxiliary/duplicate.rs diff --git a/src/test/ui/rfc-2166-underscore-imports/auxiliary/underscore-imports.rs b/src/test/ui/underscore-imports/auxiliary/underscore-imports.rs similarity index 100% rename from src/test/ui/rfc-2166-underscore-imports/auxiliary/underscore-imports.rs rename to src/test/ui/underscore-imports/auxiliary/underscore-imports.rs diff --git a/src/test/ui/rfc-2166-underscore-imports/basic.rs b/src/test/ui/underscore-imports/basic.rs similarity index 100% rename from src/test/ui/rfc-2166-underscore-imports/basic.rs rename to src/test/ui/underscore-imports/basic.rs diff --git a/src/test/ui/rfc-2166-underscore-imports/basic.stderr b/src/test/ui/underscore-imports/basic.stderr similarity index 100% rename from src/test/ui/rfc-2166-underscore-imports/basic.stderr rename to src/test/ui/underscore-imports/basic.stderr diff --git a/src/test/ui/underscore-imports/cycle.rs b/src/test/ui/underscore-imports/cycle.rs new file mode 100644 index 0000000000000..bacf9b2d5a96a --- /dev/null +++ b/src/test/ui/underscore-imports/cycle.rs @@ -0,0 +1,18 @@ +// Check that cyclic glob imports are allowed with underscore imports + +// check-pass + +mod x { + pub use crate::y::*; + pub use std::ops::Deref as _; +} + +mod y { + pub use crate::x::*; + pub use std::ops::Deref as _; +} + +pub fn main() { + use x::*; + (&0).deref(); +} diff --git a/src/test/ui/rfc-2166-underscore-imports/duplicate.rs b/src/test/ui/underscore-imports/duplicate.rs similarity index 100% rename from src/test/ui/rfc-2166-underscore-imports/duplicate.rs rename to src/test/ui/underscore-imports/duplicate.rs diff --git a/src/test/ui/rfc-2166-underscore-imports/intercrate.rs b/src/test/ui/underscore-imports/intercrate.rs similarity index 100% rename from src/test/ui/rfc-2166-underscore-imports/intercrate.rs rename to src/test/ui/underscore-imports/intercrate.rs diff --git a/src/test/ui/underscore-imports/shadow.rs b/src/test/ui/underscore-imports/shadow.rs new file mode 100644 index 0000000000000..325f2001b9ede --- /dev/null +++ b/src/test/ui/underscore-imports/shadow.rs @@ -0,0 +1,23 @@ +// Check that underscore imports don't cause glob imports to be unshadowed + +mod a { + pub use std::ops::Deref as Shadow; +} + +mod b { + pub use crate::a::*; + macro_rules! m { + ($i:ident) => { pub struct $i; } + } + m!(Shadow); +} + +mod c { + use crate::b::Shadow as _; // Only imports the struct + + fn f(x: &()) { + x.deref(); //~ ERROR no method named `deref` found + } +} + +fn main() {} diff --git a/src/test/ui/underscore-imports/shadow.stderr b/src/test/ui/underscore-imports/shadow.stderr new file mode 100644 index 0000000000000..92adca2c70490 --- /dev/null +++ b/src/test/ui/underscore-imports/shadow.stderr @@ -0,0 +1,13 @@ +error[E0599]: no method named `deref` found for type `&()` in the current scope + --> $DIR/shadow.rs:19:11 + | +LL | x.deref(); + | ^^^^^ + | + = help: items from traits can only be used if the trait is in scope + = note: the following trait is implemented but not in scope, perhaps add a `use` for it: + `use std::ops::Deref;` + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0599`. diff --git a/src/test/ui/rfc-2166-underscore-imports/unused-2018.rs b/src/test/ui/underscore-imports/unused-2018.rs similarity index 100% rename from src/test/ui/rfc-2166-underscore-imports/unused-2018.rs rename to src/test/ui/underscore-imports/unused-2018.rs diff --git a/src/test/ui/rfc-2166-underscore-imports/unused-2018.stderr b/src/test/ui/underscore-imports/unused-2018.stderr similarity index 100% rename from src/test/ui/rfc-2166-underscore-imports/unused-2018.stderr rename to src/test/ui/underscore-imports/unused-2018.stderr From b0bb3017c12d8e2cafb6a9852a10e12dc8e044e9 Mon Sep 17 00:00:00 2001 From: Michael Woerister Date: Mon, 2 Sep 2019 13:50:44 +0200 Subject: [PATCH 17/22] Update xLTO compatibility table in rustc book. --- src/doc/rustc/src/linker-plugin-lto.md | 1 + 1 file changed, 1 insertion(+) diff --git a/src/doc/rustc/src/linker-plugin-lto.md b/src/doc/rustc/src/linker-plugin-lto.md index 2ae726c4ba61d..6f1bbe60569fd 100644 --- a/src/doc/rustc/src/linker-plugin-lto.md +++ b/src/doc/rustc/src/linker-plugin-lto.md @@ -105,5 +105,6 @@ The following table shows known good combinations of toolchain versions. | Rust 1.34 | ✗ | ✓ | | Rust 1.35 | ✗ | ✓ | | Rust 1.36 | ✗ | ✓ | +| Rust 1.37 | ✗ | ✓ | Note that the compatibility policy for this feature might change in the future. From 8c74eb77902dc5c4fdf853e9159dd5fadd00601d Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 3 Sep 2019 17:21:58 +1000 Subject: [PATCH 18/22] Move path parsing earlier. It's a hot enough path that moving it slightly earlier gives a tiny but easy speedup. --- src/libsyntax/parse/parser/expr.rs | 56 ++++++++++++++++-------------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/src/libsyntax/parse/parser/expr.rs b/src/libsyntax/parse/parser/expr.rs index 5b9f0f1df6718..e502a08f4b253 100644 --- a/src/libsyntax/parse/parser/expr.rs +++ b/src/libsyntax/parse/parser/expr.rs @@ -889,6 +889,36 @@ impl<'a> Parser<'a> { hi = path.span; return Ok(self.mk_expr(lo.to(hi), ExprKind::Path(Some(qself), path), attrs)); } + if self.token.is_path_start() { + let path = self.parse_path(PathStyle::Expr)?; + + // `!`, as an operator, is prefix, so we know this isn't that + if self.eat(&token::Not) { + // MACRO INVOCATION expression + let (delim, tts) = self.expect_delimited_token_tree()?; + hi = self.prev_span; + ex = ExprKind::Mac(Mac { + path, + tts, + delim, + span: lo.to(hi), + prior_type_ascription: self.last_type_ascription, + }); + } else if self.check(&token::OpenDelim(token::Brace)) { + if let Some(expr) = self.maybe_parse_struct_expr(lo, &path, &attrs) { + return expr; + } else { + hi = path.span; + ex = ExprKind::Path(None, path); + } + } else { + hi = path.span; + ex = ExprKind::Path(None, path); + } + + let expr = self.mk_expr(lo.to(hi), ex, attrs); + return self.maybe_recover_from_bad_qpath(expr, true); + } if self.check_keyword(kw::Move) || self.check_keyword(kw::Static) { return self.parse_lambda_expr(attrs); } @@ -1007,32 +1037,6 @@ impl<'a> Parser<'a> { let (await_hi, e_kind) = self.parse_incorrect_await_syntax(lo, self.prev_span)?; hi = await_hi; ex = e_kind; - } else if self.token.is_path_start() { - let path = self.parse_path(PathStyle::Expr)?; - - // `!`, as an operator, is prefix, so we know this isn't that - if self.eat(&token::Not) { - // MACRO INVOCATION expression - let (delim, tts) = self.expect_delimited_token_tree()?; - hi = self.prev_span; - ex = ExprKind::Mac(Mac { - path, - tts, - delim, - span: lo.to(hi), - prior_type_ascription: self.last_type_ascription, - }); - } else if self.check(&token::OpenDelim(token::Brace)) { - if let Some(expr) = self.maybe_parse_struct_expr(lo, &path, &attrs) { - return expr; - } else { - hi = path.span; - ex = ExprKind::Path(None, path); - } - } else { - hi = path.span; - ex = ExprKind::Path(None, path); - } } else { if !self.unclosed_delims.is_empty() && self.check(&token::Semi) { // Don't complain about bare semicolons after unclosed braces From 23c76ff7b912f16f6fdbde167306b08e59353427 Mon Sep 17 00:00:00 2001 From: Daniel Henry-Mantilla Date: Tue, 3 Sep 2019 12:17:03 +0200 Subject: [PATCH 19/22] Added warning around code with reference to uninit bytes --- src/libstd/io/mod.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index 5060f368229bb..495c0c8d17256 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -371,6 +371,14 @@ where loop { if g.len == g.buf.len() { unsafe { + // FIXME(danielhenrymantilla): #42788 + // + // - This creates a (mut) reference to a slice of + // _uninitialized integers_. + // + // - This having defined behavior is **unstable**: + // it could become UB in the future, + // at which point it would have be changed. g.buf.reserve(reservation_size(r)); let capacity = g.buf.capacity(); g.buf.set_len(capacity); From b03d3dc478ba13f405cf9a877a4894de096a1cc1 Mon Sep 17 00:00:00 2001 From: Daniel Henry-Mantilla Date: Tue, 3 Sep 2019 15:36:21 +0200 Subject: [PATCH 20/22] Changed comment to better reflect std's exceptional situation --- src/libstd/io/mod.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index 495c0c8d17256..0386dbd490d03 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -374,11 +374,11 @@ where // FIXME(danielhenrymantilla): #42788 // // - This creates a (mut) reference to a slice of - // _uninitialized integers_. + // _uninitialized_ integers, which is **undefined behavior** // - // - This having defined behavior is **unstable**: - // it could become UB in the future, - // at which point it would have be changed. + // - Only the standard library gets to soundly "ignore" this, + // based on its privileged knowledge of unstable rustc + // internals; g.buf.reserve(reservation_size(r)); let capacity = g.buf.capacity(); g.buf.set_len(capacity); From fa893a322570ea60cc8815b3dddb5311f0cb3b63 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 31 Aug 2019 20:08:06 +0300 Subject: [PATCH 21/22] use TokenStream rather than &[TokenTree] for built-in macros That way, we don't loose the jointness info --- .../src/language-features/plugin.md | 6 +-- src/librustc_metadata/encoder.rs | 2 +- src/libsyntax/diagnostics/plugin.rs | 46 +++++++++---------- src/libsyntax/ext/base.rs | 27 +++++------ src/libsyntax/ext/expand.rs | 2 +- src/libsyntax/print/pprust.rs | 6 +-- src/libsyntax/tokenstream.rs | 2 +- src/libsyntax_ext/asm.rs | 24 ++++------ src/libsyntax_ext/assert.rs | 6 +-- src/libsyntax_ext/cfg.rs | 6 +-- src/libsyntax_ext/compile_error.rs | 4 +- src/libsyntax_ext/concat.rs | 4 +- src/libsyntax_ext/concat_idents.rs | 10 ++-- src/libsyntax_ext/env.rs | 6 +-- src/libsyntax_ext/format.rs | 10 ++-- src/libsyntax_ext/global_asm.rs | 6 +-- src/libsyntax_ext/log_syntax.rs | 4 +- src/libsyntax_ext/source_util.rs | 18 ++++---- src/libsyntax_ext/trace_macros.rs | 27 +++++++---- .../ui-fulldeps/auxiliary/roman-numerals.rs | 6 +-- 20 files changed, 109 insertions(+), 113 deletions(-) diff --git a/src/doc/unstable-book/src/language-features/plugin.md b/src/doc/unstable-book/src/language-features/plugin.md index 53e8393ec52e4..68877b48433d5 100644 --- a/src/doc/unstable-book/src/language-features/plugin.md +++ b/src/doc/unstable-book/src/language-features/plugin.md @@ -57,12 +57,12 @@ extern crate rustc; extern crate rustc_driver; use syntax::parse::token::{self, Token}; -use syntax::tokenstream::TokenTree; +use syntax::tokenstream::{TokenTree, TokenStream}; use syntax::ext::base::{ExtCtxt, MacResult, DummyResult, MacEager}; use syntax_pos::Span; use rustc_driver::plugin::Registry; -fn expand_rn(cx: &mut ExtCtxt, sp: Span, args: &[TokenTree]) +fn expand_rn(cx: &mut ExtCtxt, sp: Span, args: TokenStream) -> Box { static NUMERALS: &'static [(&'static str, usize)] = &[ @@ -78,7 +78,7 @@ fn expand_rn(cx: &mut ExtCtxt, sp: Span, args: &[TokenTree]) return DummyResult::any(sp); } - let text = match args[0] { + let text = match args.into_trees().next().unwrap() { TokenTree::Token(Token { kind: token::Ident(s, _), .. }) => s.to_string(), _ => { cx.span_err(sp, "argument should be a single identifier"); diff --git a/src/librustc_metadata/encoder.rs b/src/librustc_metadata/encoder.rs index db212408d8ebd..f430f01542efe 100644 --- a/src/librustc_metadata/encoder.rs +++ b/src/librustc_metadata/encoder.rs @@ -1354,7 +1354,7 @@ impl EncodeContext<'tcx> { let def_id = self.tcx.hir().local_def_id(macro_def.hir_id); Entry { kind: EntryKind::MacroDef(self.lazy(MacroDef { - body: pprust::tokens_to_string(macro_def.body.clone()), + body: pprust::tts_to_string(macro_def.body.clone()), legacy: macro_def.legacy, })), visibility: self.lazy(ty::Visibility::Public), diff --git a/src/libsyntax/diagnostics/plugin.rs b/src/libsyntax/diagnostics/plugin.rs index e9a55af52e878..5de39c8d14d17 100644 --- a/src/libsyntax/diagnostics/plugin.rs +++ b/src/libsyntax/diagnostics/plugin.rs @@ -6,7 +6,7 @@ use crate::ext::base::{ExtCtxt, MacEager, MacResult}; use crate::parse::token::{self, Token}; use crate::ptr::P; use crate::symbol::kw; -use crate::tokenstream::{TokenTree}; +use crate::tokenstream::{TokenTree, TokenStream}; use smallvec::smallvec; use syntax_pos::Span; @@ -27,12 +27,11 @@ pub type ErrorMap = BTreeMap; pub fn expand_diagnostic_used<'cx>(ecx: &'cx mut ExtCtxt<'_>, span: Span, - token_tree: &[TokenTree]) + tts: TokenStream) -> Box { - let code = match token_tree { - [ - TokenTree::Token(Token { kind: token::Ident(code, _), .. }) - ] => code, + assert_eq!(tts.len(), 1); + let code = match tts.into_trees().next() { + Some(TokenTree::Token(Token { kind: token::Ident(code, _), .. })) => code, _ => unreachable!() }; @@ -62,20 +61,21 @@ pub fn expand_diagnostic_used<'cx>(ecx: &'cx mut ExtCtxt<'_>, pub fn expand_register_diagnostic<'cx>(ecx: &'cx mut ExtCtxt<'_>, span: Span, - token_tree: &[TokenTree]) + tts: TokenStream) -> Box { - let (code, description) = match token_tree { - [ - TokenTree::Token(Token { kind: token::Ident(code, _), .. }) - ] => { - (*code, None) - }, - [ - TokenTree::Token(Token { kind: token::Ident(code, _), .. }), - TokenTree::Token(Token { kind: token::Comma, .. }), - TokenTree::Token(Token { kind: token::Literal(token::Lit { symbol, .. }), ..}) - ] => { - (*code, Some(*symbol)) + assert!(tts.len() == 1 || tts.len() == 3); + let mut cursor = tts.into_trees(); + let code = match cursor.next() { + Some(TokenTree::Token(Token { kind: token::Ident(code, _), .. })) => code, + _ => unreachable!() + }; + let description = match (cursor.next(), cursor.next()) { + (None, None) => None, + ( + Some(TokenTree::Token(Token { kind: token::Comma, .. })), + Some(TokenTree::Token(Token { kind: token::Literal(token::Lit { symbol, .. }), ..})) + ) => { + Some(symbol) }, _ => unreachable!() }; @@ -121,12 +121,12 @@ pub fn expand_register_diagnostic<'cx>(ecx: &'cx mut ExtCtxt<'_>, pub fn expand_build_diagnostic_array<'cx>(ecx: &'cx mut ExtCtxt<'_>, span: Span, - token_tree: &[TokenTree]) + tts: TokenStream) -> Box { - assert_eq!(token_tree.len(), 3); - let ident = match &token_tree[2] { + assert_eq!(tts.len(), 3); + let ident = match tts.into_trees().nth(2) { // DIAGNOSTICS ident. - &TokenTree::Token(Token { kind: token::Ident(name, _), span }) + Some(TokenTree::Token(Token { kind: token::Ident(name, _), span })) => Ident::new(name, span), _ => unreachable!() }; diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index 5d68983d7cb66..109ba041016c6 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -10,7 +10,7 @@ use crate::parse::token; use crate::ptr::P; use crate::symbol::{kw, sym, Ident, Symbol}; use crate::{ThinVec, MACRO_ARGUMENTS}; -use crate::tokenstream::{self, TokenStream, TokenTree}; +use crate::tokenstream::{self, TokenStream}; use crate::visit::Visitor; use errors::{DiagnosticBuilder, DiagnosticId}; @@ -235,18 +235,18 @@ pub trait TTMacroExpander { } pub type MacroExpanderFn = - for<'cx> fn(&'cx mut ExtCtxt<'_>, Span, &[tokenstream::TokenTree]) + for<'cx> fn(&'cx mut ExtCtxt<'_>, Span, TokenStream) -> Box; impl TTMacroExpander for F - where F: for<'cx> Fn(&'cx mut ExtCtxt<'_>, Span, &[tokenstream::TokenTree]) + where F: for<'cx> Fn(&'cx mut ExtCtxt<'_>, Span, TokenStream) -> Box { fn expand<'cx>( &self, ecx: &'cx mut ExtCtxt<'_>, span: Span, - input: TokenStream, + mut input: TokenStream, ) -> Box { struct AvoidInterpolatedIdents; @@ -268,10 +268,8 @@ impl TTMacroExpander for F mut_visit::noop_visit_mac(mac, self) } } - - let input: Vec<_> = - input.trees().map(|mut tt| { AvoidInterpolatedIdents.visit_tt(&mut tt); tt }).collect(); - (*self)(ecx, span, &input) + AvoidInterpolatedIdents.visit_tts(&mut input); + (*self)(ecx, span, input) } } @@ -677,7 +675,7 @@ impl SyntaxExtension { } pub fn dummy_bang(edition: Edition) -> SyntaxExtension { - fn expander<'cx>(_: &'cx mut ExtCtxt<'_>, span: Span, _: &[TokenTree]) + fn expander<'cx>(_: &'cx mut ExtCtxt<'_>, span: Span, _: TokenStream) -> Box { DummyResult::any(span) } @@ -811,9 +809,8 @@ impl<'a> ExtCtxt<'a> { pub fn monotonic_expander<'b>(&'b mut self) -> expand::MacroExpander<'b, 'a> { expand::MacroExpander::new(self, true) } - - pub fn new_parser_from_tts(&self, tts: &[tokenstream::TokenTree]) -> parser::Parser<'a> { - parse::stream_to_parser(self.parse_sess, tts.iter().cloned().collect(), MACRO_ARGUMENTS) + pub fn new_parser_from_tts(&self, stream: TokenStream) -> parser::Parser<'a> { + parse::stream_to_parser(self.parse_sess, stream, MACRO_ARGUMENTS) } pub fn source_map(&self) -> &'a SourceMap { self.parse_sess.source_map() } pub fn parse_sess(&self) -> &'a parse::ParseSess { self.parse_sess } @@ -1019,7 +1016,7 @@ pub fn expr_to_string(cx: &mut ExtCtxt<'_>, expr: P, err_msg: &str) /// done as rarely as possible). pub fn check_zero_tts(cx: &ExtCtxt<'_>, sp: Span, - tts: &[tokenstream::TokenTree], + tts: TokenStream, name: &str) { if !tts.is_empty() { cx.span_err(sp, &format!("{} takes no arguments", name)); @@ -1030,7 +1027,7 @@ pub fn check_zero_tts(cx: &ExtCtxt<'_>, /// expect exactly one string literal, or emit an error and return `None`. pub fn get_single_str_from_tts(cx: &mut ExtCtxt<'_>, sp: Span, - tts: &[tokenstream::TokenTree], + tts: TokenStream, name: &str) -> Option { let mut p = cx.new_parser_from_tts(tts); @@ -1053,7 +1050,7 @@ pub fn get_single_str_from_tts(cx: &mut ExtCtxt<'_>, /// parsing error, emit a non-fatal error and return `None`. pub fn get_exprs_from_tts(cx: &mut ExtCtxt<'_>, sp: Span, - tts: &[tokenstream::TokenTree]) -> Option>> { + tts: TokenStream) -> Option>> { let mut p = cx.new_parser_from_tts(tts); let mut es = Vec::new(); while p.token != token::Eof { diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 7b4a516744642..4fd0c367288bf 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -701,7 +701,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> { path: &Path, span: Span, ) -> AstFragment { - let mut parser = self.cx.new_parser_from_tts(&toks.into_trees().collect::>()); + let mut parser = self.cx.new_parser_from_tts(toks); match parser.parse_ast_fragment(kind, false) { Ok(fragment) => { parser.ensure_complete_parse(path, kind.name(), span); diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index bead941b20d5a..37305055e62a3 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -356,11 +356,7 @@ pub fn tt_to_string(tt: tokenstream::TokenTree) -> String { to_string(|s| s.print_tt(tt, false)) } -pub fn tts_to_string(tts: &[tokenstream::TokenTree]) -> String { - tokens_to_string(tts.iter().cloned().collect()) -} - -pub fn tokens_to_string(tokens: TokenStream) -> String { +pub fn tts_to_string(tokens: TokenStream) -> String { to_string(|s| s.print_tts(tokens, false)) } diff --git a/src/libsyntax/tokenstream.rs b/src/libsyntax/tokenstream.rs index 0d9f3769ce904..480553586197e 100644 --- a/src/libsyntax/tokenstream.rs +++ b/src/libsyntax/tokenstream.rs @@ -506,7 +506,7 @@ impl Cursor { impl fmt::Display for TokenStream { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_str(&pprust::tokens_to_string(self.clone())) + f.write_str(&pprust::tts_to_string(self.clone())) } } diff --git a/src/libsyntax_ext/asm.rs b/src/libsyntax_ext/asm.rs index 28f907441d872..328b307361d9e 100644 --- a/src/libsyntax_ext/asm.rs +++ b/src/libsyntax_ext/asm.rs @@ -8,13 +8,12 @@ use errors::DiagnosticBuilder; use syntax::ast; use syntax::ext::base::{self, *}; -use syntax::parse; use syntax::parse::token::{self, Token}; use syntax::ptr::P; use syntax::symbol::{kw, sym, Symbol}; use syntax::ast::AsmDialect; use syntax_pos::Span; -use syntax::tokenstream; +use syntax::tokenstream::{self, TokenStream}; use syntax::{span_err, struct_span_err}; enum State { @@ -43,7 +42,7 @@ const OPTIONS: &[Symbol] = &[sym::volatile, sym::alignstack, sym::intel]; pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt<'_>, sp: Span, - tts: &[tokenstream::TokenTree]) + tts: TokenStream) -> Box { let mut inline_asm = match parse_inline_asm(cx, sp, tts) { Ok(Some(inline_asm)) => inline_asm, @@ -71,20 +70,20 @@ pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt<'_>, fn parse_inline_asm<'a>( cx: &mut ExtCtxt<'a>, sp: Span, - tts: &[tokenstream::TokenTree], + tts: TokenStream, ) -> Result, DiagnosticBuilder<'a>> { // Split the tts before the first colon, to avoid `asm!("x": y)` being // parsed as `asm!(z)` with `z = "x": y` which is type ascription. - let first_colon = tts.iter() + let first_colon = tts.trees() .position(|tt| { - match *tt { + match tt { tokenstream::TokenTree::Token(Token { kind: token::Colon, .. }) | tokenstream::TokenTree::Token(Token { kind: token::ModSep, .. }) => true, _ => false, } }) .unwrap_or(tts.len()); - let mut p = cx.new_parser_from_tts(&tts[first_colon..]); + let mut p = cx.new_parser_from_tts(tts.trees().skip(first_colon).collect()); let mut asm = kw::Invalid; let mut asm_str_style = None; let mut outputs = Vec::new(); @@ -110,7 +109,8 @@ fn parse_inline_asm<'a>( )); } // Nested parser, stop before the first colon (see above). - let mut p2 = cx.new_parser_from_tts(&tts[..first_colon]); + let mut p2 = + cx.new_parser_from_tts(tts.trees().take(first_colon).collect()); if p2.token == token::Eof { let mut err = @@ -129,12 +129,8 @@ fn parse_inline_asm<'a>( // This is most likely malformed. if p2.token != token::Eof { let mut extra_tts = p2.parse_all_token_trees()?; - extra_tts.extend(tts[first_colon..].iter().cloned()); - p = parse::stream_to_parser( - cx.parse_sess, - extra_tts.into_iter().collect(), - Some("inline assembly"), - ); + extra_tts.extend(tts.trees().skip(first_colon)); + p = cx.new_parser_from_tts(extra_tts.into_iter().collect()); } asm = s; diff --git a/src/libsyntax_ext/assert.rs b/src/libsyntax_ext/assert.rs index 84583d0e5eca7..001996e1db718 100644 --- a/src/libsyntax_ext/assert.rs +++ b/src/libsyntax_ext/assert.rs @@ -13,7 +13,7 @@ use syntax_pos::{Span, DUMMY_SP}; pub fn expand_assert<'cx>( cx: &'cx mut ExtCtxt<'_>, sp: Span, - tts: &[TokenTree], + tts: TokenStream, ) -> Box { let Assert { cond_expr, custom_message } = match parse_assert(cx, sp, tts) { Ok(assert) => assert, @@ -59,9 +59,9 @@ struct Assert { fn parse_assert<'a>( cx: &mut ExtCtxt<'a>, sp: Span, - tts: &[TokenTree] + stream: TokenStream ) -> Result> { - let mut parser = cx.new_parser_from_tts(tts); + let mut parser = cx.new_parser_from_tts(stream); if parser.token == token::Eof { let mut err = cx.struct_span_err(sp, "macro requires a boolean expression as an argument"); diff --git a/src/libsyntax_ext/cfg.rs b/src/libsyntax_ext/cfg.rs index 21cee8ae1cb9c..0342e442df2e9 100644 --- a/src/libsyntax_ext/cfg.rs +++ b/src/libsyntax_ext/cfg.rs @@ -7,14 +7,14 @@ use errors::DiagnosticBuilder; use syntax::ast; use syntax::ext::base::{self, *}; use syntax::attr; -use syntax::tokenstream; +use syntax::tokenstream::TokenStream; use syntax::parse::token; use syntax_pos::Span; pub fn expand_cfg( cx: &mut ExtCtxt<'_>, sp: Span, - tts: &[tokenstream::TokenTree], + tts: TokenStream, ) -> Box { let sp = cx.with_legacy_ctxt(sp); @@ -33,7 +33,7 @@ pub fn expand_cfg( fn parse_cfg<'a>( cx: &mut ExtCtxt<'a>, sp: Span, - tts: &[tokenstream::TokenTree], + tts: TokenStream, ) -> Result> { let mut p = cx.new_parser_from_tts(tts); diff --git a/src/libsyntax_ext/compile_error.rs b/src/libsyntax_ext/compile_error.rs index 59d3f2c9c7813..24f3a66d4ae19 100644 --- a/src/libsyntax_ext/compile_error.rs +++ b/src/libsyntax_ext/compile_error.rs @@ -2,11 +2,11 @@ use syntax::ext::base::{self, *}; use syntax_pos::Span; -use syntax::tokenstream; +use syntax::tokenstream::TokenStream; pub fn expand_compile_error<'cx>(cx: &'cx mut ExtCtxt<'_>, sp: Span, - tts: &[tokenstream::TokenTree]) + tts: TokenStream) -> Box { let var = match get_single_str_from_tts(cx, sp, tts, "compile_error!") { None => return DummyResult::any(sp), diff --git a/src/libsyntax_ext/concat.rs b/src/libsyntax_ext/concat.rs index ffa5154ca0c36..514e95390bca8 100644 --- a/src/libsyntax_ext/concat.rs +++ b/src/libsyntax_ext/concat.rs @@ -1,14 +1,14 @@ use syntax::ast; use syntax::ext::base::{self, DummyResult}; use syntax::symbol::Symbol; -use syntax::tokenstream; +use syntax::tokenstream::TokenStream; use std::string::String; pub fn expand_syntax_ext( cx: &mut base::ExtCtxt<'_>, sp: syntax_pos::Span, - tts: &[tokenstream::TokenTree], + tts: TokenStream, ) -> Box { let es = match base::get_exprs_from_tts(cx, sp, tts) { Some(e) => e, diff --git a/src/libsyntax_ext/concat_idents.rs b/src/libsyntax_ext/concat_idents.rs index 96677072d1b80..73de25b222586 100644 --- a/src/libsyntax_ext/concat_idents.rs +++ b/src/libsyntax_ext/concat_idents.rs @@ -6,11 +6,11 @@ use syntax::parse::token::{self, Token}; use syntax::ptr::P; use syntax_pos::Span; use syntax_pos::symbol::Symbol; -use syntax::tokenstream::TokenTree; +use syntax::tokenstream::{TokenTree, TokenStream}; pub fn expand_syntax_ext<'cx>(cx: &'cx mut ExtCtxt<'_>, sp: Span, - tts: &[TokenTree]) + tts: TokenStream) -> Box { if tts.is_empty() { cx.span_err(sp, "concat_idents! takes 1 or more arguments."); @@ -18,9 +18,9 @@ pub fn expand_syntax_ext<'cx>(cx: &'cx mut ExtCtxt<'_>, } let mut res_str = String::new(); - for (i, e) in tts.iter().enumerate() { + for (i, e) in tts.into_trees().enumerate() { if i & 1 == 1 { - match *e { + match e { TokenTree::Token(Token { kind: token::Comma, .. }) => {} _ => { cx.span_err(sp, "concat_idents! expecting comma."); @@ -28,7 +28,7 @@ pub fn expand_syntax_ext<'cx>(cx: &'cx mut ExtCtxt<'_>, } } } else { - match *e { + match e { TokenTree::Token(Token { kind: token::Ident(name, _), .. }) => res_str.push_str(&name.as_str()), _ => { diff --git a/src/libsyntax_ext/env.rs b/src/libsyntax_ext/env.rs index 6343d218de829..179b7fe00a97a 100644 --- a/src/libsyntax_ext/env.rs +++ b/src/libsyntax_ext/env.rs @@ -7,13 +7,13 @@ use syntax::ast::{self, Ident, GenericArg}; use syntax::ext::base::{self, *}; use syntax::symbol::{kw, sym, Symbol}; use syntax_pos::Span; -use syntax::tokenstream; +use syntax::tokenstream::TokenStream; use std::env; pub fn expand_option_env<'cx>(cx: &'cx mut ExtCtxt<'_>, sp: Span, - tts: &[tokenstream::TokenTree]) + tts: TokenStream) -> Box { let var = match get_single_str_from_tts(cx, sp, tts, "option_env!") { None => return DummyResult::any(sp), @@ -45,7 +45,7 @@ pub fn expand_option_env<'cx>(cx: &'cx mut ExtCtxt<'_>, pub fn expand_env<'cx>(cx: &'cx mut ExtCtxt<'_>, sp: Span, - tts: &[tokenstream::TokenTree]) + tts: TokenStream) -> Box { let mut exprs = match get_exprs_from_tts(cx, sp, tts) { Some(ref exprs) if exprs.is_empty() => { diff --git a/src/libsyntax_ext/format.rs b/src/libsyntax_ext/format.rs index 47394c02b4186..ad275f421af49 100644 --- a/src/libsyntax_ext/format.rs +++ b/src/libsyntax_ext/format.rs @@ -11,7 +11,7 @@ use syntax::ext::base::{self, *}; use syntax::parse::token; use syntax::ptr::P; use syntax::symbol::{Symbol, sym}; -use syntax::tokenstream; +use syntax::tokenstream::TokenStream; use syntax_pos::{MultiSpan, Span}; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; @@ -126,7 +126,7 @@ struct Context<'a, 'b> { fn parse_args<'a>( ecx: &mut ExtCtxt<'a>, sp: Span, - tts: &[tokenstream::TokenTree] + tts: TokenStream, ) -> Result<(P, Vec>, FxHashMap), DiagnosticBuilder<'a>> { let mut args = Vec::>::new(); let mut names = FxHashMap::::default(); @@ -794,7 +794,7 @@ impl<'a, 'b> Context<'a, 'b> { fn expand_format_args_impl<'cx>( ecx: &'cx mut ExtCtxt<'_>, mut sp: Span, - tts: &[tokenstream::TokenTree], + tts: TokenStream, nl: bool, ) -> Box { sp = ecx.with_def_site_ctxt(sp); @@ -812,7 +812,7 @@ fn expand_format_args_impl<'cx>( pub fn expand_format_args<'cx>( ecx: &'cx mut ExtCtxt<'_>, sp: Span, - tts: &[tokenstream::TokenTree], + tts: TokenStream, ) -> Box { expand_format_args_impl(ecx, sp, tts, false) } @@ -820,7 +820,7 @@ pub fn expand_format_args<'cx>( pub fn expand_format_args_nl<'cx>( ecx: &'cx mut ExtCtxt<'_>, sp: Span, - tts: &[tokenstream::TokenTree], + tts: TokenStream, ) -> Box { expand_format_args_impl(ecx, sp, tts, true) } diff --git a/src/libsyntax_ext/global_asm.rs b/src/libsyntax_ext/global_asm.rs index a8b61593db749..6140f0df58af9 100644 --- a/src/libsyntax_ext/global_asm.rs +++ b/src/libsyntax_ext/global_asm.rs @@ -16,12 +16,12 @@ use syntax::ext::base::{self, *}; use syntax::parse::token; use syntax::ptr::P; use syntax_pos::Span; -use syntax::tokenstream; +use syntax::tokenstream::TokenStream; use smallvec::smallvec; pub fn expand_global_asm<'cx>(cx: &'cx mut ExtCtxt<'_>, sp: Span, - tts: &[tokenstream::TokenTree]) -> Box { + tts: TokenStream) -> Box { match parse_global_asm(cx, sp, tts) { Ok(Some(global_asm)) => { MacEager::items(smallvec![P(ast::Item { @@ -45,7 +45,7 @@ pub fn expand_global_asm<'cx>(cx: &'cx mut ExtCtxt<'_>, fn parse_global_asm<'a>( cx: &mut ExtCtxt<'a>, sp: Span, - tts: &[tokenstream::TokenTree] + tts: TokenStream ) -> Result, DiagnosticBuilder<'a>> { let mut p = cx.new_parser_from_tts(tts); diff --git a/src/libsyntax_ext/log_syntax.rs b/src/libsyntax_ext/log_syntax.rs index cbdfd08b4977f..d09d72a97be15 100644 --- a/src/libsyntax_ext/log_syntax.rs +++ b/src/libsyntax_ext/log_syntax.rs @@ -1,11 +1,11 @@ use syntax::ext::base; use syntax::print; -use syntax::tokenstream; +use syntax::tokenstream::TokenStream; use syntax_pos; pub fn expand_syntax_ext<'cx>(_cx: &'cx mut base::ExtCtxt<'_>, sp: syntax_pos::Span, - tts: &[tokenstream::TokenTree]) + tts: TokenStream) -> Box { println!("{}", print::pprust::tts_to_string(tts)); diff --git a/src/libsyntax_ext/source_util.rs b/src/libsyntax_ext/source_util.rs index e008ed710e4d0..9dc9d66b86f1d 100644 --- a/src/libsyntax_ext/source_util.rs +++ b/src/libsyntax_ext/source_util.rs @@ -4,7 +4,7 @@ use syntax::parse::{self, token, DirectoryOwnership}; use syntax::print::pprust; use syntax::ptr::P; use syntax::symbol::Symbol; -use syntax::tokenstream; +use syntax::tokenstream::TokenStream; use smallvec::SmallVec; use syntax_pos::{self, Pos, Span}; @@ -16,7 +16,7 @@ use rustc_data_structures::sync::Lrc; // a given file into the current one. /// line!(): expands to the current line number -pub fn expand_line(cx: &mut ExtCtxt<'_>, sp: Span, tts: &[tokenstream::TokenTree]) +pub fn expand_line(cx: &mut ExtCtxt<'_>, sp: Span, tts: TokenStream) -> Box { base::check_zero_tts(cx, sp, tts, "line!"); @@ -27,7 +27,7 @@ pub fn expand_line(cx: &mut ExtCtxt<'_>, sp: Span, tts: &[tokenstream::TokenTree } /* column!(): expands to the current column number */ -pub fn expand_column(cx: &mut ExtCtxt<'_>, sp: Span, tts: &[tokenstream::TokenTree]) +pub fn expand_column(cx: &mut ExtCtxt<'_>, sp: Span, tts: TokenStream) -> Box { base::check_zero_tts(cx, sp, tts, "column!"); @@ -40,7 +40,7 @@ pub fn expand_column(cx: &mut ExtCtxt<'_>, sp: Span, tts: &[tokenstream::TokenTr /// file!(): expands to the current filename */ /// The source_file (`loc.file`) contains a bunch more information we could spit /// out if we wanted. -pub fn expand_file(cx: &mut ExtCtxt<'_>, sp: Span, tts: &[tokenstream::TokenTree]) +pub fn expand_file(cx: &mut ExtCtxt<'_>, sp: Span, tts: TokenStream) -> Box { base::check_zero_tts(cx, sp, tts, "file!"); @@ -49,13 +49,13 @@ pub fn expand_file(cx: &mut ExtCtxt<'_>, sp: Span, tts: &[tokenstream::TokenTree base::MacEager::expr(cx.expr_str(topmost, Symbol::intern(&loc.file.name.to_string()))) } -pub fn expand_stringify(cx: &mut ExtCtxt<'_>, sp: Span, tts: &[tokenstream::TokenTree]) +pub fn expand_stringify(cx: &mut ExtCtxt<'_>, sp: Span, tts: TokenStream) -> Box { let s = pprust::tts_to_string(tts); base::MacEager::expr(cx.expr_str(sp, Symbol::intern(&s))) } -pub fn expand_mod(cx: &mut ExtCtxt<'_>, sp: Span, tts: &[tokenstream::TokenTree]) +pub fn expand_mod(cx: &mut ExtCtxt<'_>, sp: Span, tts: TokenStream) -> Box { base::check_zero_tts(cx, sp, tts, "module_path!"); let mod_path = &cx.current_expansion.module.mod_path; @@ -67,7 +67,7 @@ pub fn expand_mod(cx: &mut ExtCtxt<'_>, sp: Span, tts: &[tokenstream::TokenTree] /// include! : parse the given file as an expr /// This is generally a bad idea because it's going to behave /// unhygienically. -pub fn expand_include<'cx>(cx: &'cx mut ExtCtxt<'_>, sp: Span, tts: &[tokenstream::TokenTree]) +pub fn expand_include<'cx>(cx: &'cx mut ExtCtxt<'_>, sp: Span, tts: TokenStream) -> Box { let file = match get_single_str_from_tts(cx, sp, tts, "include!") { Some(f) => f, @@ -105,7 +105,7 @@ pub fn expand_include<'cx>(cx: &'cx mut ExtCtxt<'_>, sp: Span, tts: &[tokenstrea } // include_str! : read the given file, insert it as a literal string expr -pub fn expand_include_str(cx: &mut ExtCtxt<'_>, sp: Span, tts: &[tokenstream::TokenTree]) +pub fn expand_include_str(cx: &mut ExtCtxt<'_>, sp: Span, tts: TokenStream) -> Box { let file = match get_single_str_from_tts(cx, sp, tts, "include_str!") { Some(f) => f, @@ -130,7 +130,7 @@ pub fn expand_include_str(cx: &mut ExtCtxt<'_>, sp: Span, tts: &[tokenstream::To } } -pub fn expand_include_bytes(cx: &mut ExtCtxt<'_>, sp: Span, tts: &[tokenstream::TokenTree]) +pub fn expand_include_bytes(cx: &mut ExtCtxt<'_>, sp: Span, tts: TokenStream) -> Box { let file = match get_single_str_from_tts(cx, sp, tts, "include_bytes!") { Some(f) => f, diff --git a/src/libsyntax_ext/trace_macros.rs b/src/libsyntax_ext/trace_macros.rs index 0dce8a36f4c7b..d83c24046d9e5 100644 --- a/src/libsyntax_ext/trace_macros.rs +++ b/src/libsyntax_ext/trace_macros.rs @@ -1,20 +1,27 @@ use syntax::ext::base::{self, ExtCtxt}; use syntax::symbol::kw; use syntax_pos::Span; -use syntax::tokenstream::TokenTree; +use syntax::tokenstream::{TokenTree, TokenStream}; pub fn expand_trace_macros(cx: &mut ExtCtxt<'_>, sp: Span, - tt: &[TokenTree]) + tt: TokenStream) -> Box { - match tt { - [TokenTree::Token(token)] if token.is_keyword(kw::True) => { - cx.set_trace_macros(true); - } - [TokenTree::Token(token)] if token.is_keyword(kw::False) => { - cx.set_trace_macros(false); - } - _ => cx.span_err(sp, "trace_macros! accepts only `true` or `false`"), + let mut cursor = tt.into_trees(); + let mut err = false; + let value = match &cursor.next() { + Some(TokenTree::Token(token)) if token.is_keyword(kw::True) => true, + Some(TokenTree::Token(token)) if token.is_keyword(kw::False) => false, + _ => { + err = true; + false + }, + }; + err |= cursor.next().is_some(); + if err { + cx.span_err(sp, "trace_macros! accepts only `true` or `false`") + } else { + cx.set_trace_macros(value); } base::DummyResult::any_valid(sp) diff --git a/src/test/ui-fulldeps/auxiliary/roman-numerals.rs b/src/test/ui-fulldeps/auxiliary/roman-numerals.rs index 027025b72b31b..2b57e9289b525 100644 --- a/src/test/ui-fulldeps/auxiliary/roman-numerals.rs +++ b/src/test/ui-fulldeps/auxiliary/roman-numerals.rs @@ -15,12 +15,12 @@ extern crate rustc; extern crate rustc_driver; use syntax::parse::token::{self, Token}; -use syntax::tokenstream::TokenTree; +use syntax::tokenstream::{TokenTree, TokenStream}; use syntax::ext::base::{ExtCtxt, MacResult, DummyResult, MacEager}; use syntax_pos::Span; use rustc_driver::plugin::Registry; -fn expand_rn(cx: &mut ExtCtxt, sp: Span, args: &[TokenTree]) +fn expand_rn(cx: &mut ExtCtxt, sp: Span, args: TokenStream) -> Box { static NUMERALS: &'static [(&'static str, usize)] = &[ @@ -36,7 +36,7 @@ fn expand_rn(cx: &mut ExtCtxt, sp: Span, args: &[TokenTree]) return DummyResult::any(sp); } - let text = match args[0] { + let text = match args.into_trees().next().unwrap() { TokenTree::Token(Token { kind: token::Ident(s, _), .. }) => s.to_string(), _ => { cx.span_err(sp, "argument should be a single identifier"); From 613649584a9571168c292f82156aee1c173337a8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 3 Sep 2019 17:43:25 +0300 Subject: [PATCH 22/22] use consistent naming for buildin expansion functions --- src/libsyntax_ext/concat.rs | 2 +- src/libsyntax_ext/concat_idents.rs | 8 ++++---- src/libsyntax_ext/lib.rs | 6 +++--- src/libsyntax_ext/log_syntax.rs | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/libsyntax_ext/concat.rs b/src/libsyntax_ext/concat.rs index 514e95390bca8..fc56dff65e4e2 100644 --- a/src/libsyntax_ext/concat.rs +++ b/src/libsyntax_ext/concat.rs @@ -5,7 +5,7 @@ use syntax::tokenstream::TokenStream; use std::string::String; -pub fn expand_syntax_ext( +pub fn expand_concat( cx: &mut base::ExtCtxt<'_>, sp: syntax_pos::Span, tts: TokenStream, diff --git a/src/libsyntax_ext/concat_idents.rs b/src/libsyntax_ext/concat_idents.rs index 73de25b222586..6391b62b58dc9 100644 --- a/src/libsyntax_ext/concat_idents.rs +++ b/src/libsyntax_ext/concat_idents.rs @@ -8,10 +8,10 @@ use syntax_pos::Span; use syntax_pos::symbol::Symbol; use syntax::tokenstream::{TokenTree, TokenStream}; -pub fn expand_syntax_ext<'cx>(cx: &'cx mut ExtCtxt<'_>, - sp: Span, - tts: TokenStream) - -> Box { +pub fn expand_concat_idents<'cx>(cx: &'cx mut ExtCtxt<'_>, + sp: Span, + tts: TokenStream) + -> Box { if tts.is_empty() { cx.span_err(sp, "concat_idents! takes 1 or more arguments."); return DummyResult::any(sp); diff --git a/src/libsyntax_ext/lib.rs b/src/libsyntax_ext/lib.rs index 1a6176916623b..26ef80b2b06df 100644 --- a/src/libsyntax_ext/lib.rs +++ b/src/libsyntax_ext/lib.rs @@ -67,8 +67,8 @@ pub fn register_builtin_macros(resolver: &mut dyn syntax::ext::base::Resolver, e cfg: cfg::expand_cfg, column: source_util::expand_column, compile_error: compile_error::expand_compile_error, - concat_idents: concat_idents::expand_syntax_ext, - concat: concat::expand_syntax_ext, + concat_idents: concat_idents::expand_concat_idents, + concat: concat::expand_concat, env: env::expand_env, file: source_util::expand_file, format_args_nl: format::expand_format_args_nl, @@ -78,7 +78,7 @@ pub fn register_builtin_macros(resolver: &mut dyn syntax::ext::base::Resolver, e include_str: source_util::expand_include_str, include: source_util::expand_include, line: source_util::expand_line, - log_syntax: log_syntax::expand_syntax_ext, + log_syntax: log_syntax::expand_log_syntax, module_path: source_util::expand_mod, option_env: env::expand_option_env, stringify: source_util::expand_stringify, diff --git a/src/libsyntax_ext/log_syntax.rs b/src/libsyntax_ext/log_syntax.rs index d09d72a97be15..92130bfaf68e3 100644 --- a/src/libsyntax_ext/log_syntax.rs +++ b/src/libsyntax_ext/log_syntax.rs @@ -3,7 +3,7 @@ use syntax::print; use syntax::tokenstream::TokenStream; use syntax_pos; -pub fn expand_syntax_ext<'cx>(_cx: &'cx mut base::ExtCtxt<'_>, +pub fn expand_log_syntax<'cx>(_cx: &'cx mut base::ExtCtxt<'_>, sp: syntax_pos::Span, tts: TokenStream) -> Box {