From eb637d26ba4652ea65ef58288af0697c32ebc503 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Fri, 26 Oct 2018 22:06:59 +1100 Subject: [PATCH 1/7] Avoid unnecessary allocations in `float_lit` and `integer_lit`. This commit avoids an allocation when parsing any float and integer literals that don't involved underscores. This reduces the number of allocations done for the `tuple-stress` benchmark by 10%, reducing its instruction count by just under 1%. --- src/libsyntax/parse/mod.rs | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index a4b8ab86f37f6..77a2ae6acf00b 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -494,8 +494,17 @@ fn float_lit(s: &str, suffix: Option, diag: Option<(Span, &Handler)>) -> Option { debug!("float_lit: {:?}, {:?}", s, suffix); // FIXME #2252: bounds checking float literals is deferred until trans - let s = s.chars().filter(|&c| c != '_').collect::(); - filtered_float_lit(Symbol::intern(&s), suffix, diag) + + // Strip underscores without allocating a new String unless necessary. + let s2; + let s = if s.chars().any(|c| c == '_') { + s2 = s.chars().filter(|&c| c != '_').collect::(); + &s2 + } else { + s + }; + + filtered_float_lit(Symbol::intern(s), suffix, diag) } /// Parse a string representing a byte literal into its final form. Similar to `char_lit` @@ -591,8 +600,14 @@ fn integer_lit(s: &str, suffix: Option, diag: Option<(Span, &Handler)>) -> Option { // s can only be ascii, byte indexing is fine - let s2 = s.chars().filter(|&c| c != '_').collect::(); - let mut s = &s2[..]; + // Strip underscores without allocating a new String unless necessary. + let s2; + let mut s = if s.chars().any(|c| c == '_') { + s2 = s.chars().filter(|&c| c != '_').collect::(); + &s2 + } else { + s + }; debug!("integer_lit: {}, {:?}", s, suffix); From b7546150b2f372bc31f908c41c0078936c3d8b66 Mon Sep 17 00:00:00 2001 From: "Zack M. Davis" Date: Sat, 27 Oct 2018 14:55:07 -0700 Subject: [PATCH 2/7] back out bogus `Ok`-wrapping suggestion on `?` arm type mismatch This suggestion was introduced in #51938 / 6cc78bf8d7 (while introducing different language for type errors coming from `?` rather than a `match`), but it has a lot of false-positives (as repeatedly reported in Issues #52537, #52598, #54578, #55336), and incorrect suggestions carry more badness than marginal good suggestions do goodness. Just get rid of it (unless and until someone figures out how to do it correctly). Resolves #52537, resolves #54578. --- src/librustc/infer/error_reporting/mod.rs | 12 +-------- ...51632-try-desugar-incompatible-types.fixed | 25 ------------------- ...ue-51632-try-desugar-incompatible-types.rs | 3 --- ...1632-try-desugar-incompatible-types.stderr | 7 ++---- 4 files changed, 3 insertions(+), 44 deletions(-) delete mode 100644 src/test/ui/issues/issue-51632-try-desugar-incompatible-types.fixed diff --git a/src/librustc/infer/error_reporting/mod.rs b/src/librustc/infer/error_reporting/mod.rs index 8b4669c89fe83..d19c495af3b96 100644 --- a/src/librustc/infer/error_reporting/mod.rs +++ b/src/librustc/infer/error_reporting/mod.rs @@ -479,17 +479,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> { err.span_label(arm_span, msg); } } - hir::MatchSource::TryDesugar => { - // Issue #51632 - if let Ok(try_snippet) = self.tcx.sess.source_map().span_to_snippet(arm_span) { - err.span_suggestion_with_applicability( - arm_span, - "try wrapping with a success variant", - format!("Ok({})", try_snippet), - Applicability::MachineApplicable, - ); - } - } + hir::MatchSource::TryDesugar => {} _ => { let msg = "match arm with an incompatible type"; if self.tcx.sess.source_map().is_multiline(arm_span) { diff --git a/src/test/ui/issues/issue-51632-try-desugar-incompatible-types.fixed b/src/test/ui/issues/issue-51632-try-desugar-incompatible-types.fixed deleted file mode 100644 index 016cff914bd2d..0000000000000 --- a/src/test/ui/issues/issue-51632-try-desugar-incompatible-types.fixed +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright 2018 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. - -// run-rustfix - -#![allow(dead_code)] - -fn missing_discourses() -> Result { - Ok(1) -} - -fn forbidden_narratives() -> Result { - Ok(missing_discourses()?) - //~^ ERROR try expression alternatives have incompatible types - //~| HELP try wrapping with a success variant -} - -fn main() {} diff --git a/src/test/ui/issues/issue-51632-try-desugar-incompatible-types.rs b/src/test/ui/issues/issue-51632-try-desugar-incompatible-types.rs index 315773a85f004..32ea715b64fa7 100644 --- a/src/test/ui/issues/issue-51632-try-desugar-incompatible-types.rs +++ b/src/test/ui/issues/issue-51632-try-desugar-incompatible-types.rs @@ -8,8 +8,6 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -// run-rustfix - #![allow(dead_code)] fn missing_discourses() -> Result { @@ -19,7 +17,6 @@ fn missing_discourses() -> Result { fn forbidden_narratives() -> Result { missing_discourses()? //~^ ERROR try expression alternatives have incompatible types - //~| HELP try wrapping with a success variant } fn main() {} diff --git a/src/test/ui/issues/issue-51632-try-desugar-incompatible-types.stderr b/src/test/ui/issues/issue-51632-try-desugar-incompatible-types.stderr index a50af5624c0cf..590cbff67a4bc 100644 --- a/src/test/ui/issues/issue-51632-try-desugar-incompatible-types.stderr +++ b/src/test/ui/issues/issue-51632-try-desugar-incompatible-types.stderr @@ -1,11 +1,8 @@ error[E0308]: try expression alternatives have incompatible types - --> $DIR/issue-51632-try-desugar-incompatible-types.rs:20:5 + --> $DIR/issue-51632-try-desugar-incompatible-types.rs:18:5 | LL | missing_discourses()? - | ^^^^^^^^^^^^^^^^^^^^^ - | | - | expected enum `std::result::Result`, found isize - | help: try wrapping with a success variant: `Ok(missing_discourses()?)` + | ^^^^^^^^^^^^^^^^^^^^^ expected enum `std::result::Result`, found isize | = note: expected type `std::result::Result` found type `isize` From dc77d4977555430f3dafb8a725ec69751d7a1b9d Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Sun, 28 Oct 2018 00:59:04 +0200 Subject: [PATCH 3/7] Make a bunch of trivial methods of NonNull be `#[inline]` --- src/libcore/ptr.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index b699cb028842b..0fe82b93ff7a1 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -2867,6 +2867,7 @@ impl NonNull { /// sentinel value. Types that lazily allocate must track initialization by /// some other means. #[stable(feature = "nonnull", since = "1.25.0")] + #[inline] pub fn dangling() -> Self { unsafe { let ptr = mem::align_of::() as *mut T; @@ -2882,12 +2883,14 @@ impl NonNull { /// /// `ptr` must be non-null. #[stable(feature = "nonnull", since = "1.25.0")] + #[inline] pub const unsafe fn new_unchecked(ptr: *mut T) -> Self { NonNull { pointer: NonZero(ptr as _) } } /// Creates a new `NonNull` if `ptr` is non-null. #[stable(feature = "nonnull", since = "1.25.0")] + #[inline] pub fn new(ptr: *mut T) -> Option { if !ptr.is_null() { Some(NonNull { pointer: NonZero(ptr as _) }) @@ -2898,6 +2901,7 @@ impl NonNull { /// Acquires the underlying `*mut` pointer. #[stable(feature = "nonnull", since = "1.25.0")] + #[inline] pub fn as_ptr(self) -> *mut T { self.pointer.0 as *mut T } @@ -2908,6 +2912,7 @@ impl NonNull { /// it were actually an instance of T that is getting borrowed. If a longer /// (unbound) lifetime is needed, use `&*my_ptr.as_ptr()`. #[stable(feature = "nonnull", since = "1.25.0")] + #[inline] pub unsafe fn as_ref(&self) -> &T { &*self.as_ptr() } @@ -2918,12 +2923,14 @@ impl NonNull { /// it were actually an instance of T that is getting borrowed. If a longer /// (unbound) lifetime is needed, use `&mut *my_ptr.as_ptr()`. #[stable(feature = "nonnull", since = "1.25.0")] + #[inline] pub unsafe fn as_mut(&mut self) -> &mut T { &mut *self.as_ptr() } /// Cast to a pointer of another type #[stable(feature = "nonnull_cast", since = "1.27.0")] + #[inline] pub fn cast(self) -> NonNull { unsafe { NonNull::new_unchecked(self.as_ptr() as *mut U) @@ -2963,6 +2970,7 @@ impl Eq for NonNull {} #[stable(feature = "nonnull", since = "1.25.0")] impl PartialEq for NonNull { + #[inline] fn eq(&self, other: &Self) -> bool { self.as_ptr() == other.as_ptr() } @@ -2970,6 +2978,7 @@ impl PartialEq for NonNull { #[stable(feature = "nonnull", since = "1.25.0")] impl Ord for NonNull { + #[inline] fn cmp(&self, other: &Self) -> Ordering { self.as_ptr().cmp(&other.as_ptr()) } @@ -2977,6 +2986,7 @@ impl Ord for NonNull { #[stable(feature = "nonnull", since = "1.25.0")] impl PartialOrd for NonNull { + #[inline] fn partial_cmp(&self, other: &Self) -> Option { self.as_ptr().partial_cmp(&other.as_ptr()) } @@ -2984,6 +2994,7 @@ impl PartialOrd for NonNull { #[stable(feature = "nonnull", since = "1.25.0")] impl hash::Hash for NonNull { + #[inline] fn hash(&self, state: &mut H) { self.as_ptr().hash(state) } @@ -2991,6 +3002,7 @@ impl hash::Hash for NonNull { #[unstable(feature = "ptr_internals", issue = "0")] impl From> for NonNull { + #[inline] fn from(unique: Unique) -> Self { NonNull { pointer: unique.pointer } } @@ -2998,6 +3010,7 @@ impl From> for NonNull { #[stable(feature = "nonnull", since = "1.25.0")] impl<'a, T: ?Sized> From<&'a mut T> for NonNull { + #[inline] fn from(reference: &'a mut T) -> Self { NonNull { pointer: NonZero(reference as _) } } @@ -3005,6 +3018,7 @@ impl<'a, T: ?Sized> From<&'a mut T> for NonNull { #[stable(feature = "nonnull", since = "1.25.0")] impl<'a, T: ?Sized> From<&'a T> for NonNull { + #[inline] fn from(reference: &'a T) -> Self { NonNull { pointer: NonZero(reference as _) } } From 695ddbfbd56accb265d6b85d9e70cfb23094632c Mon Sep 17 00:00:00 2001 From: Konrad Borowski Date: Sun, 28 Oct 2018 12:03:29 +0100 Subject: [PATCH 4/7] Avoid directly catching BaseException in bootstrap configure script It includes stuff like pressing CTRL+C, which likely isn't intended. --- src/bootstrap/configure.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bootstrap/configure.py b/src/bootstrap/configure.py index ddb894eb1f659..d5f8d9d27d956 100755 --- a/src/bootstrap/configure.py +++ b/src/bootstrap/configure.py @@ -397,7 +397,7 @@ def is_number(value): try: float(value) return True - except: + except ValueError: return False # Here we walk through the constructed configuration we have from the parsed From 613f9f201331d45aa0fd65081c9e64996ec3ed4b Mon Sep 17 00:00:00 2001 From: Konrad Borowski Date: Sun, 28 Oct 2018 12:06:28 +0100 Subject: [PATCH 5/7] Remove unused sys import from generate-deriving-span-tests --- src/etc/generate-deriving-span-tests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/etc/generate-deriving-span-tests.py b/src/etc/generate-deriving-span-tests.py index 2e9169ce5b942..d8a52893ea077 100755 --- a/src/etc/generate-deriving-span-tests.py +++ b/src/etc/generate-deriving-span-tests.py @@ -18,7 +18,7 @@ sample usage: src/etc/generate-deriving-span-tests.py """ -import sys, os, datetime, stat, re +import os, datetime, stat, re TEST_DIR = os.path.abspath( os.path.join(os.path.dirname(__file__), '../test/compile-fail')) From b7c2b471cf7368b4c785f17b74368e2648bdd212 Mon Sep 17 00:00:00 2001 From: Konrad Borowski Date: Sun, 28 Oct 2018 12:12:29 +0100 Subject: [PATCH 6/7] Remove unreachable code in hasClass function in Rustdoc --- src/librustdoc/html/static/main.js | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index 9d1a5c3837830..3e275f9c8df1b 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -93,11 +93,6 @@ var end = start + className.length; return !(end < elemClass.length && elemClass[end] !== ' '); } - if (start > 0 && elemClass[start - 1] !== ' ') { - return false; - } - var end = start + className.length; - return !(end < elemClass.length && elemClass[end] !== ' '); } return false; } From ea026b865a5209fb4d57ca77d062f33af9820457 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Sun, 28 Oct 2018 12:12:00 -0400 Subject: [PATCH 7/7] Fix invalid path in generate-deriving-span-tests.py. This script broke after #53196 after the tests were moved. --- src/etc/generate-deriving-span-tests.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/etc/generate-deriving-span-tests.py b/src/etc/generate-deriving-span-tests.py index 2e9169ce5b942..ba62903b022a7 100755 --- a/src/etc/generate-deriving-span-tests.py +++ b/src/etc/generate-deriving-span-tests.py @@ -21,7 +21,7 @@ import sys, os, datetime, stat, re TEST_DIR = os.path.abspath( - os.path.join(os.path.dirname(__file__), '../test/compile-fail')) + os.path.join(os.path.dirname(__file__), '../test/ui/derives/')) YEAR = datetime.datetime.now().year