From 8ca8b7724f753e71f26cb442e11bb1f1c40ebf0a Mon Sep 17 00:00:00 2001 From: "J. Ryan Stinnett" Date: Sat, 5 Dec 2020 19:04:58 +0000 Subject: [PATCH 01/14] Add test for trimming with tabs (#78438) --- src/test/ui/terminal-width/tabs-trimming.rs | 13 +++++++++++++ src/test/ui/terminal-width/tabs-trimming.stderr | 12 ++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 src/test/ui/terminal-width/tabs-trimming.rs create mode 100644 src/test/ui/terminal-width/tabs-trimming.stderr diff --git a/src/test/ui/terminal-width/tabs-trimming.rs b/src/test/ui/terminal-width/tabs-trimming.rs new file mode 100644 index 0000000000000..ade21753b457c --- /dev/null +++ b/src/test/ui/terminal-width/tabs-trimming.rs @@ -0,0 +1,13 @@ +// Test for #78438: ensure underline alignment with many tabs on the left, long line on the right + +// ignore-tidy-linelength +// ignore-tidy-tab + + fn main() { + let money = 42i32; + match money { + v @ 1 | 2 | 3 => panic!("You gave me too little money {}", v), // Long text here: TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT + //~^ ERROR variable `v` is not bound in all patterns + v => println!("Enough money {}", v), + } + } diff --git a/src/test/ui/terminal-width/tabs-trimming.stderr b/src/test/ui/terminal-width/tabs-trimming.stderr new file mode 100644 index 0000000000000..6c8d9afc73b2a --- /dev/null +++ b/src/test/ui/terminal-width/tabs-trimming.stderr @@ -0,0 +1,12 @@ +error[E0408]: variable `v` is not bound in all patterns + --> $DIR/tabs-trimming.rs:9:16 + | +LL | ... v @ 1 | 2 | 3 => panic!("You gave me too little money {}", v), // Long text here: TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT... + | - ^ ^ pattern doesn't bind `v` + | | | + | | pattern doesn't bind `v` + | variable not in all patterns + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0408`. From 3537bd80ff1ef12c9fa994133969d8cfcaa356d3 Mon Sep 17 00:00:00 2001 From: "J. Ryan Stinnett" Date: Sun, 6 Dec 2020 00:39:07 +0000 Subject: [PATCH 02/14] Replace tabs earlier in diagnostics This replaces tabs earlier in the diagnostics emitting process, which allows various margin calculations to ignore the existence of tabs. It does add a string copy for the source lines that are emitted. --- compiler/rustc_errors/src/emitter.rs | 25 ++++++++++++++++---- compiler/rustc_errors/src/styled_buffer.rs | 27 +++------------------- 2 files changed, 23 insertions(+), 29 deletions(-) diff --git a/compiler/rustc_errors/src/emitter.rs b/compiler/rustc_errors/src/emitter.rs index 32104e6f00d44..00882bb287a4f 100644 --- a/compiler/rustc_errors/src/emitter.rs +++ b/compiler/rustc_errors/src/emitter.rs @@ -644,6 +644,8 @@ impl EmitterWriter { code_offset: usize, margin: Margin, ) { + // Tabs are assumed to have been replaced by spaces in calling code. + assert!(!source_string.contains('\t')); let line_len = source_string.len(); // Create the source line we will highlight. let left = margin.left(line_len); @@ -707,7 +709,7 @@ impl EmitterWriter { } let source_string = match file.get_line(line.line_index - 1) { - Some(s) => s, + Some(s) => replace_tabs(&*s), None => return Vec::new(), }; @@ -1376,8 +1378,17 @@ impl EmitterWriter { let file = annotated_file.file.clone(); let line = &annotated_file.lines[line_idx]; if let Some(source_string) = file.get_line(line.line_index - 1) { - let leading_whitespace = - source_string.chars().take_while(|c| c.is_whitespace()).count(); + let leading_whitespace = source_string + .chars() + .take_while(|c| c.is_whitespace()) + .map(|c| { + match c { + // Tabs are displayed as 4 spaces + '\t' => 4, + _ => 1, + } + }) + .sum(); if source_string.chars().any(|c| !c.is_whitespace()) { whitespace_margin = min(whitespace_margin, leading_whitespace); } @@ -1502,7 +1513,7 @@ impl EmitterWriter { self.draw_line( &mut buffer, - &unannotated_line, + &replace_tabs(&unannotated_line), annotated_file.lines[line_idx + 1].line_index - 1, last_buffer_line_num, width_offset, @@ -1598,7 +1609,7 @@ impl EmitterWriter { ); // print the suggestion draw_col_separator(&mut buffer, row_num, max_line_num_len + 1); - buffer.append(row_num, line, Style::NoStyle); + buffer.append(row_num, &replace_tabs(line), Style::NoStyle); row_num += 1; } @@ -1930,6 +1941,10 @@ impl FileWithAnnotatedLines { } } +fn replace_tabs(str: &str) -> String { + str.replace('\t', " ") +} + fn draw_col_separator(buffer: &mut StyledBuffer, line: usize, col: usize) { buffer.puts(line, col, "| ", Style::LineNumber); } diff --git a/compiler/rustc_errors/src/styled_buffer.rs b/compiler/rustc_errors/src/styled_buffer.rs index f2d255d7d9524..a4dd0f391bd3a 100644 --- a/compiler/rustc_errors/src/styled_buffer.rs +++ b/compiler/rustc_errors/src/styled_buffer.rs @@ -13,34 +13,13 @@ impl StyledBuffer { StyledBuffer { text: vec![], styles: vec![] } } - fn replace_tabs(&mut self) { - for (line_pos, line) in self.text.iter_mut().enumerate() { - let mut tab_pos = vec![]; - for (pos, c) in line.iter().enumerate() { - if *c == '\t' { - tab_pos.push(pos); - } - } - // start with the tabs at the end of the line to replace them with 4 space chars - for pos in tab_pos.iter().rev() { - assert_eq!(line.remove(*pos), '\t'); - // fix the position of the style to match up after replacing the tabs - let s = self.styles[line_pos].remove(*pos); - for _ in 0..4 { - line.insert(*pos, ' '); - self.styles[line_pos].insert(*pos, s); - } - } - } - } + pub fn render(&self) -> Vec> { + // Tabs are assumed to have been replaced by spaces in calling code. + assert!(self.text.iter().all(|r| !r.contains(&'\t'))); - pub fn render(&mut self) -> Vec> { let mut output: Vec> = vec![]; let mut styled_vec: Vec = vec![]; - // before we render, replace tabs with spaces - self.replace_tabs(); - for (row, row_style) in self.text.iter().zip(&self.styles) { let mut current_style = Style::NoStyle; let mut current_text = String::new(); From 826bc3648af1596413de0e7dcfe5f32665619608 Mon Sep 17 00:00:00 2001 From: CoffeeBlend Date: Fri, 1 Jan 2021 22:03:14 +0100 Subject: [PATCH 03/14] Implement MaybeUninit::array_assume_init --- library/core/src/mem/maybe_uninit.rs | 49 ++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/library/core/src/mem/maybe_uninit.rs b/library/core/src/mem/maybe_uninit.rs index b2a4d897eeded..b157eec24b410 100644 --- a/library/core/src/mem/maybe_uninit.rs +++ b/library/core/src/mem/maybe_uninit.rs @@ -804,6 +804,55 @@ impl MaybeUninit { } } + /// Extracts the values from an array of `MaybeUninit` containers. + /// + /// # Safety + /// + /// It is up to the caller to guarantee that all elements of the array are + /// in an initialized state. + /// + /// # Examples + /// + /// ``` + /// #![feature(maybe_uninit_uninit_array)] + /// #![feature(maybe_uninit_array_assume_init)] + /// use std::mem::MaybeUninit; + /// + /// let mut array: [MaybeUninit; 3] = MaybeUninit::uninit_array(); + /// array[0] = MaybeUninit::new(0); + /// array[1] = MaybeUninit::new(1); + /// array[2] = MaybeUninit::new(2); + /// + /// // SAFETY: Now safe as we initialised all elements + /// let array = unsafe { + /// MaybeUninit::array_assume_init(array) + /// }; + /// + /// assert_eq!(array, [0, 1, 2]); + /// ``` + #[unstable(feature = "maybe_uninit_array_assume_init", issue = "none")] + #[inline(always)] + pub unsafe fn array_assume_init(array: [Self; N]) -> [T; N] { + // Convert using a union because mem::transmute does not support const_generics + union ArrayInit { + maybe_uninit: ManuallyDrop<[MaybeUninit; N]>, + init: ManuallyDrop<[T; N]>, + } + + // SAFETY: + // * The caller guarantees that all elements of the array are initialized, + // * `MaybeUninit` and T are guaranteed to have the same layout, + // Therefore the conversion is safe + unsafe { + intrinsics::assert_inhabited::(); + + let array = ArrayInit { + maybe_uninit: ManuallyDrop::new(array), + }; + ManuallyDrop::into_inner(array.init) + } + } + /// Assuming all the elements are initialized, get a slice to them. /// /// # Safety From 0ff1e6c697f711aa25b8fa2c0cc43bf2ad84ff52 Mon Sep 17 00:00:00 2001 From: CoffeeBlend Date: Fri, 1 Jan 2021 22:12:49 +0100 Subject: [PATCH 04/14] Add test for MaybeUninit::array_assume_init --- library/core/tests/lib.rs | 2 ++ library/core/tests/mem.rs | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/library/core/tests/lib.rs b/library/core/tests/lib.rs index e01aaa4cbf179..bc737cd1927cf 100644 --- a/library/core/tests/lib.rs +++ b/library/core/tests/lib.rs @@ -36,6 +36,8 @@ #![feature(raw)] #![feature(sort_internals)] #![feature(slice_partition_at_index)] +#![feature(maybe_uninit_uninit_array)] +#![feature(maybe_uninit_array_assume_init)] #![feature(maybe_uninit_extra)] #![feature(maybe_uninit_write_slice)] #![feature(min_specialization)] diff --git a/library/core/tests/mem.rs b/library/core/tests/mem.rs index 79ca2bba40388..547f6a2c78cc9 100644 --- a/library/core/tests/mem.rs +++ b/library/core/tests/mem.rs @@ -140,6 +140,22 @@ fn assume_init_good() { assert!(TRUE); } +#[test] +fn uninit_array_assume_init() { + let mut array: [MaybeUninit; 5] = MaybeUninit::uninit_array(); + array[0].write(3); + array[1].write(1); + array[2].write(4); + array[3].write(1); + array[4].write(5); + + let array = unsafe { + MaybeUninit::array_assume_init(array) + }; + + assert_eq!(array, [3, 1, 4, 1, 5]); +} + #[test] fn uninit_write_slice() { let mut dst = [MaybeUninit::new(255); 64]; From 72a3dee16f6c854fc61d475581b4730189984fa5 Mon Sep 17 00:00:00 2001 From: CoffeeBlend Date: Fri, 1 Jan 2021 22:56:54 +0100 Subject: [PATCH 05/14] Format code --- library/core/src/mem/maybe_uninit.rs | 18 ++++++++---------- library/core/tests/mem.rs | 6 ++---- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/library/core/src/mem/maybe_uninit.rs b/library/core/src/mem/maybe_uninit.rs index b157eec24b410..a39f93466377a 100644 --- a/library/core/src/mem/maybe_uninit.rs +++ b/library/core/src/mem/maybe_uninit.rs @@ -804,12 +804,12 @@ impl MaybeUninit { } } - /// Extracts the values from an array of `MaybeUninit` containers. - /// + /// Extracts the values from an array of `MaybeUninit` containers. + /// /// # Safety - /// + /// /// It is up to the caller to guarantee that all elements of the array are - /// in an initialized state. + /// in an initialized state. /// /// # Examples /// @@ -817,17 +817,17 @@ impl MaybeUninit { /// #![feature(maybe_uninit_uninit_array)] /// #![feature(maybe_uninit_array_assume_init)] /// use std::mem::MaybeUninit; - /// + /// /// let mut array: [MaybeUninit; 3] = MaybeUninit::uninit_array(); /// array[0] = MaybeUninit::new(0); /// array[1] = MaybeUninit::new(1); /// array[2] = MaybeUninit::new(2); - /// + /// /// // SAFETY: Now safe as we initialised all elements /// let array = unsafe { /// MaybeUninit::array_assume_init(array) /// }; - /// + /// /// assert_eq!(array, [0, 1, 2]); /// ``` #[unstable(feature = "maybe_uninit_array_assume_init", issue = "none")] @@ -846,9 +846,7 @@ impl MaybeUninit { unsafe { intrinsics::assert_inhabited::(); - let array = ArrayInit { - maybe_uninit: ManuallyDrop::new(array), - }; + let array = ArrayInit { maybe_uninit: ManuallyDrop::new(array) }; ManuallyDrop::into_inner(array.init) } } diff --git a/library/core/tests/mem.rs b/library/core/tests/mem.rs index 547f6a2c78cc9..2279a16429f98 100644 --- a/library/core/tests/mem.rs +++ b/library/core/tests/mem.rs @@ -149,10 +149,8 @@ fn uninit_array_assume_init() { array[3].write(1); array[4].write(5); - let array = unsafe { - MaybeUninit::array_assume_init(array) - }; - + let array = unsafe { MaybeUninit::array_assume_init(array) }; + assert_eq!(array, [3, 1, 4, 1, 5]); } From 39e1331cfabeef842081c599bebb6c10042008e7 Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Mon, 11 Jan 2021 12:10:16 +0900 Subject: [PATCH 06/14] Add another test case for #79808 Taken from #80293. --- library/alloc/src/collections/vec_deque/tests.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/library/alloc/src/collections/vec_deque/tests.rs b/library/alloc/src/collections/vec_deque/tests.rs index 21f52af056b2a..27dc59ae64411 100644 --- a/library/alloc/src/collections/vec_deque/tests.rs +++ b/library/alloc/src/collections/vec_deque/tests.rs @@ -224,6 +224,21 @@ fn make_contiguous_head_to_end() { assert_eq!((&['A', 'B', 'C'] as &[_], &[] as &[_]), dq.as_slices()); } +#[test] +fn make_contiguous_head_to_end_2() { + // Another test case for #79808, taken from #80293. + + let mut dq = VecDeque::from_iter(0..6); + dq.pop_front(); + dq.pop_front(); + dq.push_back(6); + dq.push_back(7); + dq.push_back(8); + dq.make_contiguous(); + let collected: Vec<_> = dq.iter().copied().collect(); + assert_eq!(dq.as_slices(), (&collected[..], &[] as &[_])); +} + #[test] fn test_remove() { // This test checks that every single combination of tail position, length, and From dec8c033a3d3287cdb9e0b9f4888b21bbb86d60c Mon Sep 17 00:00:00 2001 From: CoffeeBlend Date: Mon, 11 Jan 2021 10:07:29 +0100 Subject: [PATCH 07/14] Add tracking issue for array_assume_init --- library/core/src/mem/maybe_uninit.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/mem/maybe_uninit.rs b/library/core/src/mem/maybe_uninit.rs index a39f93466377a..3d96ad32a1277 100644 --- a/library/core/src/mem/maybe_uninit.rs +++ b/library/core/src/mem/maybe_uninit.rs @@ -830,7 +830,7 @@ impl MaybeUninit { /// /// assert_eq!(array, [0, 1, 2]); /// ``` - #[unstable(feature = "maybe_uninit_array_assume_init", issue = "none")] + #[unstable(feature = "maybe_uninit_array_assume_init", issue = "80908")] #[inline(always)] pub unsafe fn array_assume_init(array: [Self; N]) -> [T; N] { // Convert using a union because mem::transmute does not support const_generics From 0be9d39336d8ec775d7effe12b91bf2e51bb7a85 Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Mon, 11 Jan 2021 18:55:35 +0200 Subject: [PATCH 08/14] core/slice: remove doc comment about scoped borrow There's no need to scope the borrow in the doc example due to NLL. Playground link where changed code compiles https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=fn%20main()%20%7B%0A%20%20%20%20let%20mut%20v%20%3D%20%5B1%2C%200%2C%203%2C%200%2C%205%2C%206%5D%3B%0A%0A%20%20%20%20let%20(left%2C%20right)%20%3D%20v.split_at_mut(2)%3B%0A%20%20%20%20assert_eq!(left%2C%20%5B1%2C%200%5D)%3B%0A%20%20%20%20assert_eq!(right%2C%20%5B3%2C%200%2C%205%2C%206%5D)%3B%0A%20%20%20%20left%5B1%5D%20%3D%202%3B%0A%20%20%20%20right%5B1%5D%20%3D%204%3B%0A%0A%20%20%20%20assert_eq!(v%2C%20%5B1%2C%202%2C%203%2C%204%2C%205%2C%206%5D)%3B%0A%7D%0A --- library/core/src/slice/mod.rs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs index bd01271ec150f..439a39b8276a2 100644 --- a/library/core/src/slice/mod.rs +++ b/library/core/src/slice/mod.rs @@ -1357,14 +1357,11 @@ impl [T] { /// /// ``` /// let mut v = [1, 0, 3, 0, 5, 6]; - /// // scoped to restrict the lifetime of the borrows - /// { - /// let (left, right) = v.split_at_mut(2); - /// assert_eq!(left, [1, 0]); - /// assert_eq!(right, [3, 0, 5, 6]); - /// left[1] = 2; - /// right[1] = 4; - /// } + /// let (left, right) = v.split_at_mut(2); + /// assert_eq!(left, [1, 0]); + /// assert_eq!(right, [3, 0, 5, 6]); + /// left[1] = 2; + /// right[1] = 4; /// assert_eq!(v, [1, 2, 3, 4, 5, 6]); /// ``` #[stable(feature = "rust1", since = "1.0.0")] From 5d65b7e055f469ee5bcc733860c6ebe37649bfa5 Mon Sep 17 00:00:00 2001 From: CoffeeBlend Date: Mon, 11 Jan 2021 23:32:03 +0100 Subject: [PATCH 09/14] Simplify array_assume_init --- library/core/src/mem/maybe_uninit.rs | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/library/core/src/mem/maybe_uninit.rs b/library/core/src/mem/maybe_uninit.rs index 3d96ad32a1277..b186aa61eb0eb 100644 --- a/library/core/src/mem/maybe_uninit.rs +++ b/library/core/src/mem/maybe_uninit.rs @@ -833,21 +833,14 @@ impl MaybeUninit { #[unstable(feature = "maybe_uninit_array_assume_init", issue = "80908")] #[inline(always)] pub unsafe fn array_assume_init(array: [Self; N]) -> [T; N] { - // Convert using a union because mem::transmute does not support const_generics - union ArrayInit { - maybe_uninit: ManuallyDrop<[MaybeUninit; N]>, - init: ManuallyDrop<[T; N]>, - } - // SAFETY: - // * The caller guarantees that all elements of the array are initialized, - // * `MaybeUninit` and T are guaranteed to have the same layout, - // Therefore the conversion is safe + // * The caller guarantees that all elements of the array are initialized + // * `MaybeUninit` and T are guaranteed to have the same layout + // * MaybeUnint does not drop, so there are no double-frees + // And thus the conversion is safe unsafe { intrinsics::assert_inhabited::(); - - let array = ArrayInit { maybe_uninit: ManuallyDrop::new(array) }; - ManuallyDrop::into_inner(array.init) + (&array as *const _ as *const T).read() } } From 2055e4c2faa0b10befb66a170ef0125a6b0d2000 Mon Sep 17 00:00:00 2001 From: Caio Date: Mon, 11 Jan 2021 19:49:51 -0300 Subject: [PATCH 10/14] Move some tests to more reasonable directories --- src/test/ui/{issues => drop}/auxiliary/issue-10028.rs | 0 src/test/ui/{issues => drop}/issue-10028.rs | 0 src/test/ui/{issues => extern}/issue-10025.rs | 0 src/test/ui/issues/auxiliary/issue-10031-aux.rs | 1 - src/test/ui/issues/issue-10031.rs | 9 --------- src/test/ui/{issues => never_type}/issue-10176.rs | 0 src/test/ui/{issues => never_type}/issue-10176.stderr | 0 src/test/ui/{issues => resolve}/issue-10200.rs | 0 src/test/ui/{issues => resolve}/issue-10200.stderr | 0 9 files changed, 10 deletions(-) rename src/test/ui/{issues => drop}/auxiliary/issue-10028.rs (100%) rename src/test/ui/{issues => drop}/issue-10028.rs (100%) rename src/test/ui/{issues => extern}/issue-10025.rs (100%) delete mode 100644 src/test/ui/issues/auxiliary/issue-10031-aux.rs delete mode 100644 src/test/ui/issues/issue-10031.rs rename src/test/ui/{issues => never_type}/issue-10176.rs (100%) rename src/test/ui/{issues => never_type}/issue-10176.stderr (100%) rename src/test/ui/{issues => resolve}/issue-10200.rs (100%) rename src/test/ui/{issues => resolve}/issue-10200.stderr (100%) diff --git a/src/test/ui/issues/auxiliary/issue-10028.rs b/src/test/ui/drop/auxiliary/issue-10028.rs similarity index 100% rename from src/test/ui/issues/auxiliary/issue-10028.rs rename to src/test/ui/drop/auxiliary/issue-10028.rs diff --git a/src/test/ui/issues/issue-10028.rs b/src/test/ui/drop/issue-10028.rs similarity index 100% rename from src/test/ui/issues/issue-10028.rs rename to src/test/ui/drop/issue-10028.rs diff --git a/src/test/ui/issues/issue-10025.rs b/src/test/ui/extern/issue-10025.rs similarity index 100% rename from src/test/ui/issues/issue-10025.rs rename to src/test/ui/extern/issue-10025.rs diff --git a/src/test/ui/issues/auxiliary/issue-10031-aux.rs b/src/test/ui/issues/auxiliary/issue-10031-aux.rs deleted file mode 100644 index e2abeb99ea829..0000000000000 --- a/src/test/ui/issues/auxiliary/issue-10031-aux.rs +++ /dev/null @@ -1 +0,0 @@ -pub struct Wrap(pub A); diff --git a/src/test/ui/issues/issue-10031.rs b/src/test/ui/issues/issue-10031.rs deleted file mode 100644 index 136df05c2397a..0000000000000 --- a/src/test/ui/issues/issue-10031.rs +++ /dev/null @@ -1,9 +0,0 @@ -// run-pass -// aux-build:issue-10031-aux.rs -// pretty-expanded FIXME #23616 - -extern crate issue_10031_aux; - -pub fn main() { - let _ = issue_10031_aux::Wrap(()); -} diff --git a/src/test/ui/issues/issue-10176.rs b/src/test/ui/never_type/issue-10176.rs similarity index 100% rename from src/test/ui/issues/issue-10176.rs rename to src/test/ui/never_type/issue-10176.rs diff --git a/src/test/ui/issues/issue-10176.stderr b/src/test/ui/never_type/issue-10176.stderr similarity index 100% rename from src/test/ui/issues/issue-10176.stderr rename to src/test/ui/never_type/issue-10176.stderr diff --git a/src/test/ui/issues/issue-10200.rs b/src/test/ui/resolve/issue-10200.rs similarity index 100% rename from src/test/ui/issues/issue-10200.rs rename to src/test/ui/resolve/issue-10200.rs diff --git a/src/test/ui/issues/issue-10200.stderr b/src/test/ui/resolve/issue-10200.stderr similarity index 100% rename from src/test/ui/issues/issue-10200.stderr rename to src/test/ui/resolve/issue-10200.stderr From b6dc03b93dfe421a42d8dec910775a601fbd3d50 Mon Sep 17 00:00:00 2001 From: Andy Russell Date: Mon, 11 Jan 2021 19:27:38 -0500 Subject: [PATCH 11/14] fix typo in trait method mutability mismatch help --- compiler/rustc_typeck/src/check/compare_method.rs | 2 +- src/test/ui/issues/issue-13033.stderr | 9 ++++----- src/test/ui/mismatched_types/E0053.stderr | 9 ++++----- .../trait-impl-fn-incompatibility.stderr | 9 ++++----- 4 files changed, 13 insertions(+), 16 deletions(-) diff --git a/compiler/rustc_typeck/src/check/compare_method.rs b/compiler/rustc_typeck/src/check/compare_method.rs index bb324d0d8bc1e..320ded5334e21 100644 --- a/compiler/rustc_typeck/src/check/compare_method.rs +++ b/compiler/rustc_typeck/src/check/compare_method.rs @@ -296,7 +296,7 @@ fn compare_predicate_entailment<'tcx>( { diag.span_suggestion( impl_err_span, - "consider change the type to match the mutability in trait", + "consider changing the mutability to match the trait", trait_err_str, Applicability::MachineApplicable, ); diff --git a/src/test/ui/issues/issue-13033.stderr b/src/test/ui/issues/issue-13033.stderr index a8473c8a52413..57447fa48aacc 100644 --- a/src/test/ui/issues/issue-13033.stderr +++ b/src/test/ui/issues/issue-13033.stderr @@ -5,14 +5,13 @@ LL | fn bar(&mut self, other: &mut dyn Foo); | ------------ type in trait ... LL | fn bar(&mut self, other: &dyn Foo) {} - | ^^^^^^^^ types differ in mutability + | ^^^^^^^^ + | | + | types differ in mutability + | help: consider changing the mutability to match the trait: `&mut dyn Foo` | = note: expected fn pointer `fn(&mut Baz, &mut dyn Foo)` found fn pointer `fn(&mut Baz, &dyn Foo)` -help: consider change the type to match the mutability in trait - | -LL | fn bar(&mut self, other: &mut dyn Foo) {} - | ^^^^^^^^^^^^ error: aborting due to previous error diff --git a/src/test/ui/mismatched_types/E0053.stderr b/src/test/ui/mismatched_types/E0053.stderr index fef83e6bbe2b6..e0a3ce922b970 100644 --- a/src/test/ui/mismatched_types/E0053.stderr +++ b/src/test/ui/mismatched_types/E0053.stderr @@ -17,14 +17,13 @@ LL | fn bar(&self); | ----- type in trait ... LL | fn bar(&mut self) { } - | ^^^^^^^^^ types differ in mutability + | ^^^^^^^^^ + | | + | types differ in mutability + | help: consider changing the mutability to match the trait: `&self` | = note: expected fn pointer `fn(&Bar)` found fn pointer `fn(&mut Bar)` -help: consider change the type to match the mutability in trait - | -LL | fn bar(&self) { } - | ^^^^^ error: aborting due to 2 previous errors diff --git a/src/test/ui/mismatched_types/trait-impl-fn-incompatibility.stderr b/src/test/ui/mismatched_types/trait-impl-fn-incompatibility.stderr index 5735120f7104a..161843473b6c1 100644 --- a/src/test/ui/mismatched_types/trait-impl-fn-incompatibility.stderr +++ b/src/test/ui/mismatched_types/trait-impl-fn-incompatibility.stderr @@ -17,14 +17,13 @@ LL | fn bar(&mut self, bar: &mut Bar); | -------- type in trait ... LL | fn bar(&mut self, bar: &Bar) { } - | ^^^^ types differ in mutability + | ^^^^ + | | + | types differ in mutability + | help: consider changing the mutability to match the trait: `&mut Bar` | = note: expected fn pointer `fn(&mut Bar, &mut Bar)` found fn pointer `fn(&mut Bar, &Bar)` -help: consider change the type to match the mutability in trait - | -LL | fn bar(&mut self, bar: &mut Bar) { } - | ^^^^^^^^ error: aborting due to 2 previous errors From 985071b08f5c03e4f18d43c15f3ea82395588a5e Mon Sep 17 00:00:00 2001 From: CoffeeBlend Date: Tue, 12 Jan 2021 01:39:10 +0100 Subject: [PATCH 12/14] Fix implementation --- library/core/src/mem/maybe_uninit.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/mem/maybe_uninit.rs b/library/core/src/mem/maybe_uninit.rs index b186aa61eb0eb..fda0553f94c5f 100644 --- a/library/core/src/mem/maybe_uninit.rs +++ b/library/core/src/mem/maybe_uninit.rs @@ -840,7 +840,7 @@ impl MaybeUninit { // And thus the conversion is safe unsafe { intrinsics::assert_inhabited::(); - (&array as *const _ as *const T).read() + (&array as *const _ as *const [T; N]).read() } } From 6bd661ef1763fc41cba6ae4971ea877dd47b082b Mon Sep 17 00:00:00 2001 From: LingMan Date: Mon, 11 Jan 2021 22:22:04 +0100 Subject: [PATCH 13/14] Replace a simple `if let` with the `matches` macro --- compiler/rustc_resolve/src/late.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index 8544e1d8ee55a..6219d1b08eb68 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -1947,8 +1947,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> { _ => report_errors(self, None), }; - if let PathSource::TraitItem(..) = source { - } else { + if !matches!(source, PathSource::TraitItem(..)) { // Avoid recording definition of `A::B` in `::B::C`. self.r.record_partial_res(id, partial_res); } From 8c431607440d454e15c456b14f0282d3487c411e Mon Sep 17 00:00:00 2001 From: Camelid Date: Sun, 10 Jan 2021 18:07:05 -0800 Subject: [PATCH 14/14] driver: Use `atty` instead of rolling our own Rationale: - `atty` is widely used in the Rust ecosystem - We already use it (in `rustc_errors` and other places) - We shouldn't be rolling our own TTY detector when there's a widely-used, well-tested package that we can use --- Cargo.lock | 1 + compiler/rustc_driver/Cargo.toml | 1 + compiler/rustc_driver/src/lib.rs | 35 ++------------------------------ 3 files changed, 4 insertions(+), 33 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 322b163203100..ab452c97e7b37 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3652,6 +3652,7 @@ dependencies = [ name = "rustc_driver" version = "0.0.0" dependencies = [ + "atty", "libc", "rustc_ast", "rustc_ast_pretty", diff --git a/compiler/rustc_driver/Cargo.toml b/compiler/rustc_driver/Cargo.toml index 0adc006b6244e..b88b556d143d9 100644 --- a/compiler/rustc_driver/Cargo.toml +++ b/compiler/rustc_driver/Cargo.toml @@ -9,6 +9,7 @@ crate-type = ["dylib"] [dependencies] libc = "0.2" +atty = "0.2" tracing = { version = "0.1.18" } tracing-subscriber = { version = "0.2.13", default-features = false, features = ["fmt", "env-filter", "smallvec", "parking_lot", "ansi"] } tracing-tree = "0.1.6" diff --git a/compiler/rustc_driver/src/lib.rs b/compiler/rustc_driver/src/lib.rs index c2a0d8ef7ea11..509f81e16536b 100644 --- a/compiler/rustc_driver/src/lib.rs +++ b/compiler/rustc_driver/src/lib.rs @@ -546,43 +546,12 @@ impl Compilation { #[derive(Copy, Clone)] pub struct RustcDefaultCalls; -// FIXME remove these and use winapi 0.3 instead -// Duplicates: bootstrap/compile.rs, librustc_errors/emitter.rs -#[cfg(unix)] -fn stdout_isatty() -> bool { - unsafe { libc::isatty(libc::STDOUT_FILENO) != 0 } -} - -#[cfg(windows)] fn stdout_isatty() -> bool { - use winapi::um::consoleapi::GetConsoleMode; - use winapi::um::processenv::GetStdHandle; - use winapi::um::winbase::STD_OUTPUT_HANDLE; - - unsafe { - let handle = GetStdHandle(STD_OUTPUT_HANDLE); - let mut out = 0; - GetConsoleMode(handle, &mut out) != 0 - } + atty::is(atty::Stream::Stdout) } -// FIXME remove these and use winapi 0.3 instead -#[cfg(unix)] -fn stderr_isatty() -> bool { - unsafe { libc::isatty(libc::STDERR_FILENO) != 0 } -} - -#[cfg(windows)] fn stderr_isatty() -> bool { - use winapi::um::consoleapi::GetConsoleMode; - use winapi::um::processenv::GetStdHandle; - use winapi::um::winbase::STD_ERROR_HANDLE; - - unsafe { - let handle = GetStdHandle(STD_ERROR_HANDLE); - let mut out = 0; - GetConsoleMode(handle, &mut out) != 0 - } + atty::is(atty::Stream::Stderr) } fn handle_explain(registry: Registry, code: &str, output: ErrorOutputType) {