From e4943ac81163db364bd8c8ad1ea9c65b4adabcd3 Mon Sep 17 00:00:00 2001 From: follower Date: Sat, 26 Sep 2020 03:26:45 +1200 Subject: [PATCH 01/17] Link to documentation-specific guidelines. Changed because it's not obvious how to get from the previously used URL to the documentation-specific content. This is partly because the original URL was previously changed to point to different content: * https://github.com/rust-lang/rust/pull/74037/files#diff-242481015141f373dcb178e93cffa850L88 * https://github.com/rust-lang/rust/commit/3f6928f1f6eff367e6ddbfb63ebc5e568ffe0eb1#diff-6a3371457528722a734f3c51d9238c13L12 --- library/std/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/lib.rs b/library/std/src/lib.rs index ac0075ad129c5..86c0e0c30b828 100644 --- a/library/std/src/lib.rs +++ b/library/std/src/lib.rs @@ -85,7 +85,7 @@ //! # Contributing changes to the documentation //! //! Check out the rust contribution guidelines [here]( -//! https://rustc-dev-guide.rust-lang.org/getting-started.html). +//! https://rustc-dev-guide.rust-lang.org/contributing.html#writing-documentation). //! The source for this documentation can be found on //! [GitHub](https://github.com/rust-lang/rust). //! To contribute changes, make sure you read the guidelines first, then submit From 176b96516fed01dbf858b1b9d342dd2e3aa48a37 Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Thu, 8 Oct 2020 23:39:31 +0800 Subject: [PATCH 02/17] Link Vec leak doc to Box --- library/alloc/src/vec.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/alloc/src/vec.rs b/library/alloc/src/vec.rs index e8e52299d0b70..3f4a97df08d38 100644 --- a/library/alloc/src/vec.rs +++ b/library/alloc/src/vec.rs @@ -1476,7 +1476,7 @@ impl Vec { /// `'a`. If the type has only static references, or none at all, then this /// may be chosen to be `'static`. /// - /// This function is similar to the `leak` function on `Box`. + /// This function is similar to the [`leak`][Box::leak] function on [`Box`]. /// /// This function is mainly useful for data that lives for the remainder of /// the program's life. Dropping the returned reference will cause a memory From bd49ded308f7243d1ba3170ea1bd0d5855d0544b Mon Sep 17 00:00:00 2001 From: Julian Wollersberger Date: Fri, 9 Oct 2020 11:12:54 +0200 Subject: [PATCH 03/17] Noticed a potential bug in `eat_while()`: it doesn't account for number of UTF8 bytes. Fixed it by inlining it in the two places where the count is used and simplified the logic there. --- compiler/rustc_lexer/src/lib.rs | 34 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/compiler/rustc_lexer/src/lib.rs b/compiler/rustc_lexer/src/lib.rs index d784a86f14cee..c5b59a041abf6 100644 --- a/compiler/rustc_lexer/src/lib.rs +++ b/compiler/rustc_lexer/src/lib.rs @@ -48,6 +48,7 @@ impl Token { } /// Enum representing common lexeme types. +// perf note: Changing all `usize` to `u32` doesn't change performance. See #77629 #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] pub enum TokenKind { // Multi-char tokens: @@ -160,6 +161,7 @@ pub enum LiteralKind { /// - `r##~"abcde"##`: `InvalidStarter` /// - `r###"abcde"##`: `NoTerminator { expected: 3, found: 2, possible_terminator_offset: Some(11)` /// - Too many `#`s (>65535): `TooManyDelimiters` +// perf note: It doesn't matter that this makes `Token` 36 bytes bigger. See #77629 #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)] pub enum RawStrError { /// Non `#` characters exist between `r` and `"` eg. `r#~"..` @@ -689,7 +691,12 @@ impl Cursor<'_> { let mut max_hashes = 0; // Count opening '#' symbols. - let n_start_hashes = self.eat_while(|c| c == '#'); + let mut eaten = 0; + while self.first() == '#' { + eaten += 1; + self.bump(); + } + let n_start_hashes = eaten; // Check that string is started. match self.bump() { @@ -724,16 +731,11 @@ impl Cursor<'_> { // Note that this will not consume extra trailing `#` characters: // `r###"abcde"####` is lexed as a `RawStr { n_hashes: 3 }` // followed by a `#` token. - let mut hashes_left = n_start_hashes; - let is_closing_hash = |c| { - if c == '#' && hashes_left != 0 { - hashes_left -= 1; - true - } else { - false - } - }; - let n_end_hashes = self.eat_while(is_closing_hash); + let mut n_end_hashes = 0; + while self.first() == '#' && n_end_hashes < n_start_hashes { + n_end_hashes += 1; + self.bump(); + } if n_end_hashes == n_start_hashes { return (n_start_hashes, None); @@ -807,17 +809,9 @@ impl Cursor<'_> { } /// Eats symbols while predicate returns true or until the end of file is reached. - /// Returns amount of eaten symbols. - fn eat_while(&mut self, mut predicate: F) -> usize - where - F: FnMut(char) -> bool, - { - let mut eaten: usize = 0; + fn eat_while(&mut self, mut predicate: impl FnMut(char) -> bool) { while predicate(self.first()) && !self.is_eof() { - eaten += 1; self.bump(); } - - eaten } } From 6cd9b88a2559c1c8276275ab7a278827f949bfdd Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 9 Oct 2020 11:17:08 +0200 Subject: [PATCH 04/17] fix __rust_alloc_error_handler comment --- library/alloc/src/alloc.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/library/alloc/src/alloc.rs b/library/alloc/src/alloc.rs index ce70de6ebdd63..f40bc2c96cffd 100644 --- a/library/alloc/src/alloc.rs +++ b/library/alloc/src/alloc.rs @@ -26,8 +26,6 @@ extern "Rust" { fn __rust_realloc(ptr: *mut u8, old_size: usize, align: usize, new_size: usize) -> *mut u8; #[rustc_allocator_nounwind] fn __rust_alloc_zeroed(size: usize, align: usize) -> *mut u8; - #[rustc_allocator_nounwind] - fn __rust_alloc_error_handler(size: usize, align: usize) -> !; } /// The global memory allocator. @@ -323,6 +321,16 @@ pub(crate) unsafe fn box_free(ptr: Unique) { } } +// # Allocation error handler + +extern "Rust" { + // This is the magic symbol to call the global alloc error handler. rustc generates + // it to call `__rg_oom` if there is a `#[alloc_error_handler]`, or to call the + // default implementations below (`__rdl_oom`) otherwise. + #[rustc_allocator_nounwind] + fn __rust_alloc_error_handler(size: usize, align: usize) -> !; +} + /// Abort on memory allocation error or failure. /// /// Callers of memory allocation APIs wishing to abort computation From 1911d2186683e4b9818ea16225b4f909c6e2b070 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 9 Oct 2020 11:31:54 +0200 Subject: [PATCH 05/17] also extend global allocator comment --- library/alloc/src/alloc.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/library/alloc/src/alloc.rs b/library/alloc/src/alloc.rs index f40bc2c96cffd..82c9e4482d5d5 100644 --- a/library/alloc/src/alloc.rs +++ b/library/alloc/src/alloc.rs @@ -14,8 +14,9 @@ mod tests; extern "Rust" { // These are the magic symbols to call the global allocator. rustc generates - // them from the `#[global_allocator]` attribute if there is one, or uses the - // default implementations in libstd (`__rdl_alloc` etc in `src/libstd/alloc.rs`) + // them to call `__rg_alloc` etc if there is a `#[global_allocator]` attribute + // (the code expanding that attribute macro generates those functions), or to call + // the default implementations in libstd (`__rdl_alloc` etc in `src/libstd/alloc.rs`) // otherwise. #[rustc_allocator] #[rustc_allocator_nounwind] From b6bedc80c9922833d92e81c6bf91eb5b24e11c86 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 9 Oct 2020 11:39:28 +0200 Subject: [PATCH 06/17] rename __default_lib_allocator -> __default_alloc_error_handler --- library/alloc/src/alloc.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/alloc/src/alloc.rs b/library/alloc/src/alloc.rs index 82c9e4482d5d5..4646d4a833525 100644 --- a/library/alloc/src/alloc.rs +++ b/library/alloc/src/alloc.rs @@ -376,7 +376,7 @@ pub fn handle_alloc_error(layout: Layout) -> ! { #[doc(hidden)] #[allow(unused_attributes)] #[unstable(feature = "alloc_internals", issue = "none")] -pub mod __default_lib_allocator { +pub mod __alloc_error_handler { use crate::alloc::Layout; // called via generated `__rust_alloc_error_handler` From 0c97c24a6c681d0c84d4b0730fec1a6b80415c48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Miku=C5=82a?= Date: Thu, 8 Oct 2020 21:54:33 +0200 Subject: [PATCH 07/17] Remove some dead code in windows-gnu std --- library/std/src/sys/windows/c.rs | 11 ------- library/std/src/sys/windows/mod.rs | 53 ------------------------------ 2 files changed, 64 deletions(-) diff --git a/library/std/src/sys/windows/c.rs b/library/std/src/sys/windows/c.rs index 559c4dc9c7cd8..657421e3fa4cc 100644 --- a/library/std/src/sys/windows/c.rs +++ b/library/std/src/sys/windows/c.rs @@ -47,7 +47,6 @@ pub type LPWCH = *mut WCHAR; pub type LPWIN32_FIND_DATAW = *mut WIN32_FIND_DATAW; pub type LPWSADATA = *mut WSADATA; pub type LPWSAPROTOCOL_INFO = *mut WSAPROTOCOL_INFO; -pub type LPSTR = *mut CHAR; pub type LPWSTR = *mut WCHAR; pub type LPFILETIME = *mut FILETIME; pub type LPWSABUF = *mut WSABUF; @@ -876,16 +875,6 @@ extern "system" { pub fn DeleteFileW(lpPathName: LPCWSTR) -> BOOL; pub fn GetCurrentDirectoryW(nBufferLength: DWORD, lpBuffer: LPWSTR) -> DWORD; pub fn SetCurrentDirectoryW(lpPathName: LPCWSTR) -> BOOL; - pub fn WideCharToMultiByte( - CodePage: UINT, - dwFlags: DWORD, - lpWideCharStr: LPCWSTR, - cchWideChar: c_int, - lpMultiByteStr: LPSTR, - cbMultiByte: c_int, - lpDefaultChar: LPCSTR, - lpUsedDefaultChar: LPBOOL, - ) -> c_int; pub fn closesocket(socket: SOCKET) -> c_int; pub fn recv(socket: SOCKET, buf: *mut c_void, len: c_int, flags: c_int) -> c_int; diff --git a/library/std/src/sys/windows/mod.rs b/library/std/src/sys/windows/mod.rs index 8178e6806b9b3..8c19cc78b09cd 100644 --- a/library/std/src/sys/windows/mod.rs +++ b/library/std/src/sys/windows/mod.rs @@ -4,7 +4,6 @@ use crate::ffi::{OsStr, OsString}; use crate::io::ErrorKind; use crate::os::windows::ffi::{OsStrExt, OsStringExt}; use crate::path::PathBuf; -use crate::ptr; use crate::time::Duration; pub use self::rand::hashmap_random_keys; @@ -206,58 +205,6 @@ fn os2path(s: &[u16]) -> PathBuf { PathBuf::from(OsString::from_wide(s)) } -#[allow(dead_code)] // Only used in backtrace::gnu::get_executable_filename() -fn wide_char_to_multi_byte( - code_page: u32, - flags: u32, - s: &[u16], - no_default_char: bool, -) -> crate::io::Result> { - unsafe { - let mut size = c::WideCharToMultiByte( - code_page, - flags, - s.as_ptr(), - s.len() as i32, - ptr::null_mut(), - 0, - ptr::null(), - ptr::null_mut(), - ); - if size == 0 { - return Err(crate::io::Error::last_os_error()); - } - - let mut buf = Vec::with_capacity(size as usize); - buf.set_len(size as usize); - - let mut used_default_char = c::FALSE; - size = c::WideCharToMultiByte( - code_page, - flags, - s.as_ptr(), - s.len() as i32, - buf.as_mut_ptr(), - buf.len() as i32, - ptr::null(), - if no_default_char { &mut used_default_char } else { ptr::null_mut() }, - ); - if size == 0 { - return Err(crate::io::Error::last_os_error()); - } - if no_default_char && used_default_char == c::TRUE { - return Err(crate::io::Error::new( - crate::io::ErrorKind::InvalidData, - "string cannot be converted to requested code page", - )); - } - - buf.set_len(size as usize); - - Ok(buf) - } -} - pub fn truncate_utf16_at_nul(v: &[u16]) -> &[u16] { match unrolled_find_u16s(0, v) { // don't include the 0 From 8818fda7f0ba5ed9e83f89d760647c3203c00e8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Miku=C5=82a?= Date: Thu, 8 Oct 2020 21:54:59 +0200 Subject: [PATCH 08/17] Remove useless `all` in cfg --- library/unwind/src/libunwind.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/unwind/src/libunwind.rs b/library/unwind/src/libunwind.rs index 806df572cf944..ff1d82fc99040 100644 --- a/library/unwind/src/libunwind.rs +++ b/library/unwind/src/libunwind.rs @@ -89,7 +89,7 @@ extern "C" { } cfg_if::cfg_if! { -if #[cfg(all(any(target_os = "ios", target_os = "netbsd", not(target_arch = "arm"))))] { +if #[cfg(any(target_os = "ios", target_os = "netbsd", not(target_arch = "arm")))] { // Not ARM EHABI #[repr(C)] #[derive(Copy, Clone, PartialEq)] From 7993ddd89d8d2e0754bf9b12756573f56b76e254 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Fri, 9 Oct 2020 15:28:32 +0200 Subject: [PATCH 09/17] Add find_map_relevant_impl --- .../src/rmeta/decoder/cstore_impl.rs | 2 +- compiler/rustc_middle/src/ty/trait_def.rs | 35 +++++++++++++++++++ compiler/rustc_middle/src/ty/util.rs | 8 ++--- .../transform/check_const_item_mutation.rs | 3 +- .../src/traits/error_reporting/mod.rs | 12 ++----- compiler/rustc_typeck/src/check/mod.rs | 2 +- .../passes/collect_intra_doc_links.rs | 16 ++++----- 7 files changed, 51 insertions(+), 27 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index 60705f68681a1..05b8dad3097e4 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -94,7 +94,7 @@ provide! { <'tcx> tcx, def_id, other, cdata, adt_def => { cdata.get_adt_def(def_id.index, tcx) } adt_destructor => { let _ = cdata; - tcx.calculate_dtor(def_id, &mut |_,_| Ok(())) + tcx.calculate_dtor(def_id, |_,_| Ok(())) } variances_of => { tcx.arena.alloc_from_iter(cdata.get_item_variances(def_id.index)) } associated_item_def_ids => { diff --git a/compiler/rustc_middle/src/ty/trait_def.rs b/compiler/rustc_middle/src/ty/trait_def.rs index 9d5b558234b3a..5599216c316da 100644 --- a/compiler/rustc_middle/src/ty/trait_def.rs +++ b/compiler/rustc_middle/src/ty/trait_def.rs @@ -167,6 +167,41 @@ impl<'tcx> TyCtxt<'tcx> { } } + /// Applies function to every impl that could possibly match the self type `self_ty` and returns + /// the first non-none value. + pub fn find_map_relevant_impl Option>( + self, + def_id: DefId, + self_ty: Ty<'tcx>, + f: F, + ) -> Option { + let impls = self.trait_impls_of(def_id); + + for &impl_def_id in impls.blanket_impls.iter() { + if let result @ Some(_) = f(impl_def_id) { + return result; + } + } + + if let Some(simp) = fast_reject::simplify_type(self, self_ty, true) { + if let Some(impls) = impls.non_blanket_impls.get(&simp) { + for &impl_def_id in impls { + if let result @ Some(_) = f(impl_def_id) { + return result; + } + } + } + } else { + for &impl_def_id in impls.non_blanket_impls.values().flatten() { + if let result @ Some(_) = f(impl_def_id) { + return result; + } + } + } + + None + } + /// Returns an iterator containing all impls pub fn all_impls(self, def_id: DefId) -> impl Iterator + 'tcx { let TraitImpls { blanket_impls, non_blanket_impls } = self.trait_impls_of(def_id); diff --git a/compiler/rustc_middle/src/ty/util.rs b/compiler/rustc_middle/src/ty/util.rs index 4127b6535bca6..5ac12dfa99366 100644 --- a/compiler/rustc_middle/src/ty/util.rs +++ b/compiler/rustc_middle/src/ty/util.rs @@ -341,19 +341,19 @@ impl<'tcx> TyCtxt<'tcx> { pub fn calculate_dtor( self, adt_did: DefId, - validate: &mut dyn FnMut(Self, DefId) -> Result<(), ErrorReported>, + validate: impl Fn(Self, DefId) -> Result<(), ErrorReported>, ) -> Option { let drop_trait = self.lang_items().drop_trait()?; self.ensure().coherent_trait(drop_trait); - let mut dtor_did = None; let ty = self.type_of(adt_did); - self.for_each_relevant_impl(drop_trait, ty, |impl_did| { + let dtor_did = self.find_map_relevant_impl(drop_trait, ty, |impl_did| { if let Some(item) = self.associated_items(impl_did).in_definition_order().next() { if validate(self, impl_did).is_ok() { - dtor_did = Some(item.def_id); + return Some(item.def_id); } } + None }); Some(ty::Destructor { did: dtor_did? }) diff --git a/compiler/rustc_mir/src/transform/check_const_item_mutation.rs b/compiler/rustc_mir/src/transform/check_const_item_mutation.rs index 4d4e9b989171a..fb89b36060a28 100644 --- a/compiler/rustc_mir/src/transform/check_const_item_mutation.rs +++ b/compiler/rustc_mir/src/transform/check_const_item_mutation.rs @@ -34,7 +34,6 @@ impl<'a, 'tcx> ConstMutationChecker<'a, 'tcx> { fn is_const_item_without_destructor(&self, local: Local) -> Option { let def_id = self.is_const_item(local)?; - let mut any_dtor = |_tcx, _def_id| Ok(()); // We avoid linting mutation of a const item if the const's type has a // Drop impl. The Drop logic observes the mutation which was performed. @@ -54,7 +53,7 @@ impl<'a, 'tcx> ConstMutationChecker<'a, 'tcx> { // // #[const_mutation_allowed] // pub const LOG: Log = Log { msg: "" }; - match self.tcx.calculate_dtor(def_id, &mut any_dtor) { + match self.tcx.calculate_dtor(def_id, |_, _| Ok(())) { Some(_) => None, None => Some(def_id), } diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs index cb3de57cfed07..05e3ed3435113 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs @@ -1384,17 +1384,11 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> { trait_ref: &ty::PolyTraitRef<'tcx>, ) { let get_trait_impl = |trait_def_id| { - let mut trait_impl = None; - self.tcx.for_each_relevant_impl( + self.tcx.find_map_relevant_impl( trait_def_id, trait_ref.skip_binder().self_ty(), - |impl_def_id| { - if trait_impl.is_none() { - trait_impl = Some(impl_def_id); - } - }, - ); - trait_impl + |impl_def_id| Some(impl_def_id), + ) }; let required_trait_path = self.tcx.def_path_str(trait_ref.def_id()); let all_traits = self.tcx.all_traits(LOCAL_CRATE); diff --git a/compiler/rustc_typeck/src/check/mod.rs b/compiler/rustc_typeck/src/check/mod.rs index 97172d391ba65..1cb6ae21a47bb 100644 --- a/compiler/rustc_typeck/src/check/mod.rs +++ b/compiler/rustc_typeck/src/check/mod.rs @@ -264,7 +264,7 @@ pub fn provide(providers: &mut Providers) { } fn adt_destructor(tcx: TyCtxt<'_>, def_id: DefId) -> Option { - tcx.calculate_dtor(def_id, &mut dropck::check_drop_impl) + tcx.calculate_dtor(def_id, dropck::check_drop_impl) } /// If this `DefId` is a "primary tables entry", returns diff --git a/src/librustdoc/passes/collect_intra_doc_links.rs b/src/librustdoc/passes/collect_intra_doc_links.rs index b9be3e2f92b46..ab39910766164 100644 --- a/src/librustdoc/passes/collect_intra_doc_links.rs +++ b/src/librustdoc/passes/collect_intra_doc_links.rs @@ -650,14 +650,9 @@ fn traits_implemented_by(cx: &DocContext<'_>, type_: DefId, module: DefId) -> Fx let ty = cx.tcx.type_of(type_); let iter = in_scope_traits.iter().flat_map(|&trait_| { trace!("considering explicit impl for trait {:?}", trait_); - let mut saw_impl = false; - // Look at each trait implementation to see if it's an impl for `did` - cx.tcx.for_each_relevant_impl(trait_, ty, |impl_| { - // FIXME: this is inefficient, find a way to short-circuit for_each_* so this doesn't take as long - if saw_impl { - return; - } + // Look at each trait implementation to see if it's an impl for `did` + cx.tcx.find_map_relevant_impl(trait_, ty, |impl_| { let trait_ref = cx.tcx.impl_trait_ref(impl_).expect("this is not an inherent impl"); // Check if these are the same type. let impl_type = trait_ref.self_ty(); @@ -668,7 +663,7 @@ fn traits_implemented_by(cx: &DocContext<'_>, type_: DefId, module: DefId) -> Fx type_ ); // Fast path: if this is a primitive simple `==` will work - saw_impl = impl_type == ty + let saw_impl = impl_type == ty || match impl_type.kind() { // Check if these are the same def_id ty::Adt(def, _) => { @@ -678,8 +673,9 @@ fn traits_implemented_by(cx: &DocContext<'_>, type_: DefId, module: DefId) -> Fx ty::Foreign(def_id) => *def_id == type_, _ => false, }; - }); - if saw_impl { Some(trait_) } else { None } + + if saw_impl { Some(trait_) } else { None } + }) }); iter.collect() } From 18318a9d848950680c12c17183f175c7c511c2ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Fri, 9 Oct 2020 16:56:09 +0200 Subject: [PATCH 10/17] Reimplement for_each_relevant_impl on top of find_map... --- compiler/rustc_middle/src/ty/trait_def.rs | 47 ++++++++--------------- 1 file changed, 17 insertions(+), 30 deletions(-) diff --git a/compiler/rustc_middle/src/ty/trait_def.rs b/compiler/rustc_middle/src/ty/trait_def.rs index 5599216c316da..86476dffc0312 100644 --- a/compiler/rustc_middle/src/ty/trait_def.rs +++ b/compiler/rustc_middle/src/ty/trait_def.rs @@ -123,10 +123,26 @@ impl<'tcx> TyCtxt<'tcx> { self_ty: Ty<'tcx>, mut f: F, ) { + let _: Option<()> = self.find_map_relevant_impl(def_id, self_ty, |did| { + f(did); + None + }); + } + + /// Applies function to every impl that could possibly match the self type `self_ty` and returns + /// the first non-none value. + pub fn find_map_relevant_impl Option>( + self, + def_id: DefId, + self_ty: Ty<'tcx>, + mut f: F, + ) -> Option { let impls = self.trait_impls_of(def_id); for &impl_def_id in impls.blanket_impls.iter() { - f(impl_def_id); + if let result @ Some(_) = f(impl_def_id) { + return result; + } } // simplify_type(.., false) basically replaces type parameters and @@ -154,35 +170,6 @@ impl<'tcx> TyCtxt<'tcx> { // blanket and non-blanket impls, and compare them separately. // // I think we'll cross that bridge when we get to it. - if let Some(simp) = fast_reject::simplify_type(self, self_ty, true) { - if let Some(impls) = impls.non_blanket_impls.get(&simp) { - for &impl_def_id in impls { - f(impl_def_id); - } - } - } else { - for &impl_def_id in impls.non_blanket_impls.values().flatten() { - f(impl_def_id); - } - } - } - - /// Applies function to every impl that could possibly match the self type `self_ty` and returns - /// the first non-none value. - pub fn find_map_relevant_impl Option>( - self, - def_id: DefId, - self_ty: Ty<'tcx>, - f: F, - ) -> Option { - let impls = self.trait_impls_of(def_id); - - for &impl_def_id in impls.blanket_impls.iter() { - if let result @ Some(_) = f(impl_def_id) { - return result; - } - } - if let Some(simp) = fast_reject::simplify_type(self, self_ty, true) { if let Some(impls) = impls.non_blanket_impls.get(&simp) { for &impl_def_id in impls { From 217d6f9741819aedfe22e6d3ec9cca6e4a49f77d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Fri, 9 Oct 2020 17:18:57 +0200 Subject: [PATCH 11/17] Revert calculate_dtor signature change --- compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs | 2 +- compiler/rustc_middle/src/ty/util.rs | 2 +- compiler/rustc_mir/src/transform/check_const_item_mutation.rs | 2 +- compiler/rustc_typeck/src/check/mod.rs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index 05b8dad3097e4..60705f68681a1 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -94,7 +94,7 @@ provide! { <'tcx> tcx, def_id, other, cdata, adt_def => { cdata.get_adt_def(def_id.index, tcx) } adt_destructor => { let _ = cdata; - tcx.calculate_dtor(def_id, |_,_| Ok(())) + tcx.calculate_dtor(def_id, &mut |_,_| Ok(())) } variances_of => { tcx.arena.alloc_from_iter(cdata.get_item_variances(def_id.index)) } associated_item_def_ids => { diff --git a/compiler/rustc_middle/src/ty/util.rs b/compiler/rustc_middle/src/ty/util.rs index 5ac12dfa99366..d8ea2f67393b2 100644 --- a/compiler/rustc_middle/src/ty/util.rs +++ b/compiler/rustc_middle/src/ty/util.rs @@ -341,7 +341,7 @@ impl<'tcx> TyCtxt<'tcx> { pub fn calculate_dtor( self, adt_did: DefId, - validate: impl Fn(Self, DefId) -> Result<(), ErrorReported>, + validate: &mut dyn FnMut(Self, DefId) -> Result<(), ErrorReported>, ) -> Option { let drop_trait = self.lang_items().drop_trait()?; self.ensure().coherent_trait(drop_trait); diff --git a/compiler/rustc_mir/src/transform/check_const_item_mutation.rs b/compiler/rustc_mir/src/transform/check_const_item_mutation.rs index fb89b36060a28..26993a6b941fb 100644 --- a/compiler/rustc_mir/src/transform/check_const_item_mutation.rs +++ b/compiler/rustc_mir/src/transform/check_const_item_mutation.rs @@ -53,7 +53,7 @@ impl<'a, 'tcx> ConstMutationChecker<'a, 'tcx> { // // #[const_mutation_allowed] // pub const LOG: Log = Log { msg: "" }; - match self.tcx.calculate_dtor(def_id, |_, _| Ok(())) { + match self.tcx.calculate_dtor(def_id, &mut |_, _| Ok(())) { Some(_) => None, None => Some(def_id), } diff --git a/compiler/rustc_typeck/src/check/mod.rs b/compiler/rustc_typeck/src/check/mod.rs index 1cb6ae21a47bb..97172d391ba65 100644 --- a/compiler/rustc_typeck/src/check/mod.rs +++ b/compiler/rustc_typeck/src/check/mod.rs @@ -264,7 +264,7 @@ pub fn provide(providers: &mut Providers) { } fn adt_destructor(tcx: TyCtxt<'_>, def_id: DefId) -> Option { - tcx.calculate_dtor(def_id, dropck::check_drop_impl) + tcx.calculate_dtor(def_id, &mut dropck::check_drop_impl) } /// If this `DefId` is a "primary tables entry", returns From f200c1e7afdd04b42c01c0108735e5b14ca07d93 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Fri, 9 Oct 2020 20:12:26 -0700 Subject: [PATCH 12/17] doc: disambiguate stat in MetadataExt::as_raw_stat A few architectures in `os::linux::raw` import `libc::stat`, rather than defining that type directly. However, that also imports the _function_ called `stat`, which makes this doc link ambiguous: error: `crate::os::linux::raw::stat` is both a struct and a function --> library/std/src/os/linux/fs.rs:21:19 | 21 | /// [`stat`]: crate::os::linux::raw::stat | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ ambiguous link | = note: `-D broken-intra-doc-links` implied by `-D warnings` help: to link to the struct, prefix with the item type | 21 | /// [`stat`]: struct@crate::os::linux::raw::stat | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: to link to the function, add parentheses | 21 | /// [`stat`]: crate::os::linux::raw::stat() | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ We want the `struct`, so it's now prefixed accordingly. --- library/std/src/os/linux/fs.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/os/linux/fs.rs b/library/std/src/os/linux/fs.rs index ff23c3d67e3b4..9b7af97616c9d 100644 --- a/library/std/src/os/linux/fs.rs +++ b/library/std/src/os/linux/fs.rs @@ -20,7 +20,7 @@ pub trait MetadataExt { /// Unix platforms. The `os::unix::fs::MetadataExt` trait contains the /// cross-Unix abstractions contained within the raw stat. /// - /// [`stat`]: crate::os::linux::raw::stat + /// [`stat`]: struct@crate::os::linux::raw::stat /// /// # Examples /// From 55e92f913a6d3368dcc6e7734ec84563da0226ca Mon Sep 17 00:00:00 2001 From: Naoki Hayama Date: Sat, 10 Oct 2020 18:02:53 +0900 Subject: [PATCH 13/17] Fix typo in error code description s/abitrary/arbitrary/ --- compiler/rustc_error_codes/src/error_codes/E0424.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_error_codes/src/error_codes/E0424.md b/compiler/rustc_error_codes/src/error_codes/E0424.md index a9f6f579b4275..a58c16b59e91e 100644 --- a/compiler/rustc_error_codes/src/error_codes/E0424.md +++ b/compiler/rustc_error_codes/src/error_codes/E0424.md @@ -21,7 +21,7 @@ impl Foo { The `self` keyword can only be used inside methods, which are associated functions (functions defined inside of a `trait` or `impl` block) that have a `self` receiver as its first parameter, like `self`, `&self`, `&mut self` or -`self: &mut Pin` (this last one is an example of an ["abitrary `self` +`self: &mut Pin` (this last one is an example of an ["arbitrary `self` type"](https://github.com/rust-lang/rust/issues/44874)). Check if the associated function's parameter list should have contained a `self` From d2ca0c4c9b639651175446c05532e255a640cf3f Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Sat, 10 Oct 2020 10:08:36 -0400 Subject: [PATCH 14/17] Update `changelog-seen` in config.toml.example This got out of sync when the version was bumped last time. Long-term we may want to find an easier way to maintain this that doesn't require bumping the version in three different places. Off the top of my head I can't think of anything, though. --- config.toml.example | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.toml.example b/config.toml.example index 6dc9eccbdfceb..9b3a2d5aa72be 100644 --- a/config.toml.example +++ b/config.toml.example @@ -13,7 +13,7 @@ # If it does not match the version that is currently running, # `x.py` will prompt you to update it and read the changelog. # See `src/bootstrap/CHANGELOG.md` for more information. -changelog-seen = 1 +changelog-seen = 2 # ============================================================================= # Global Settings From 66369a6c70d470872f0a64633e71d91e5385a6c6 Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Sat, 10 Oct 2020 22:12:28 +0800 Subject: [PATCH 15/17] Alloc vec doc mention cannot undo leak --- library/alloc/src/vec.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/library/alloc/src/vec.rs b/library/alloc/src/vec.rs index 3f4a97df08d38..fb43f21446128 100644 --- a/library/alloc/src/vec.rs +++ b/library/alloc/src/vec.rs @@ -1476,7 +1476,8 @@ impl Vec { /// `'a`. If the type has only static references, or none at all, then this /// may be chosen to be `'static`. /// - /// This function is similar to the [`leak`][Box::leak] function on [`Box`]. + /// This function is similar to the [`leak`][Box::leak] function on [`Box`] + /// except there are no way to undo the leak yet. /// /// This function is mainly useful for data that lives for the remainder of /// the program's life. Dropping the returned reference will cause a memory From 8688fa825016f3bcf320b1e9b499616e13bb64d2 Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Sat, 10 Oct 2020 22:17:48 +0800 Subject: [PATCH 16/17] Improve vec leak wording Co-authored-by: Joshua Nelson --- library/alloc/src/vec.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/alloc/src/vec.rs b/library/alloc/src/vec.rs index fb43f21446128..5e68f76693fcf 100644 --- a/library/alloc/src/vec.rs +++ b/library/alloc/src/vec.rs @@ -1477,7 +1477,7 @@ impl Vec { /// may be chosen to be `'static`. /// /// This function is similar to the [`leak`][Box::leak] function on [`Box`] - /// except there are no way to undo the leak yet. + /// except that there is no way to recover the leaked memory. /// /// This function is mainly useful for data that lives for the remainder of /// the program's life. Dropping the returned reference will cause a memory From 2224e265780943035b25936529d4435732ef411f Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Sat, 10 Oct 2020 05:36:33 +0900 Subject: [PATCH 17/17] Clarify the debug-related values should take boolean They should take boolean values and the current placeholders are confusing, at least for me. --- config.toml.example | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/config.toml.example b/config.toml.example index 6dc9eccbdfceb..dc58fc433f595 100644 --- a/config.toml.example +++ b/config.toml.example @@ -370,13 +370,13 @@ changelog-seen = 1 # binary, otherwise they are omitted. # # Defaults to rust.debug value -#debug-assertions = debug +#debug-assertions = rust.debug (boolean) # Whether or not debug assertions are enabled for the standard library. # Overrides the `debug-assertions` option, if defined. # # Defaults to rust.debug-assertions value -#debug-assertions-std = debug-assertions +#debug-assertions-std = rust.debug-assertions (boolean) # Whether or not to leave debug! and trace! calls in the rust binary. # Overrides the `debug-assertions` option, if defined. @@ -386,7 +386,7 @@ changelog-seen = 1 # If you see a message from `tracing` saying # `max_level_info` is enabled and means logging won't be shown, # set this value to `true`. -#debug-logging = debug-assertions +#debug-logging = rust.debug-assertions (boolean) # Debuginfo level for most of Rust code, corresponds to the `-C debuginfo=N` option of `rustc`. # `0` - no debug info