Skip to content

Commit b6b4616

Browse files
committed
Auto merge of #80939 - JohnTitor:rollup-pymns4q, r=JohnTitor
Rollup of 8 pull requests Successful merges: - #79757 (Replace tabs earlier in diagnostics) - #80600 (Add `MaybeUninit` method `array_assume_init`) - #80880 (Move some tests to more reasonable directories) - #80897 (driver: Use `atty` instead of rolling our own) - #80898 (Add another test case for #79808) - #80917 (core/slice: remove doc comment about scoped borrow) - #80927 (Replace a simple `if let` with the `matches` macro) - #80930 (fix typo in trait method mutability mismatch help) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 8234db5 + 139daf5 commit b6b4616

File tree

26 files changed

+142
-98
lines changed

26 files changed

+142
-98
lines changed

Diff for: Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3652,6 +3652,7 @@ dependencies = [
36523652
name = "rustc_driver"
36533653
version = "0.0.0"
36543654
dependencies = [
3655+
"atty",
36553656
"libc",
36563657
"rustc_ast",
36573658
"rustc_ast_pretty",

Diff for: compiler/rustc_driver/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ crate-type = ["dylib"]
99

1010
[dependencies]
1111
libc = "0.2"
12+
atty = "0.2"
1213
tracing = { version = "0.1.18" }
1314
tracing-subscriber = { version = "0.2.13", default-features = false, features = ["fmt", "env-filter", "smallvec", "parking_lot", "ansi"] }
1415
tracing-tree = "0.1.6"

Diff for: compiler/rustc_driver/src/lib.rs

+2-33
Original file line numberDiff line numberDiff line change
@@ -546,43 +546,12 @@ impl Compilation {
546546
#[derive(Copy, Clone)]
547547
pub struct RustcDefaultCalls;
548548

549-
// FIXME remove these and use winapi 0.3 instead
550-
// Duplicates: bootstrap/compile.rs, librustc_errors/emitter.rs
551-
#[cfg(unix)]
552-
fn stdout_isatty() -> bool {
553-
unsafe { libc::isatty(libc::STDOUT_FILENO) != 0 }
554-
}
555-
556-
#[cfg(windows)]
557549
fn stdout_isatty() -> bool {
558-
use winapi::um::consoleapi::GetConsoleMode;
559-
use winapi::um::processenv::GetStdHandle;
560-
use winapi::um::winbase::STD_OUTPUT_HANDLE;
561-
562-
unsafe {
563-
let handle = GetStdHandle(STD_OUTPUT_HANDLE);
564-
let mut out = 0;
565-
GetConsoleMode(handle, &mut out) != 0
566-
}
550+
atty::is(atty::Stream::Stdout)
567551
}
568552

569-
// FIXME remove these and use winapi 0.3 instead
570-
#[cfg(unix)]
571-
fn stderr_isatty() -> bool {
572-
unsafe { libc::isatty(libc::STDERR_FILENO) != 0 }
573-
}
574-
575-
#[cfg(windows)]
576553
fn stderr_isatty() -> bool {
577-
use winapi::um::consoleapi::GetConsoleMode;
578-
use winapi::um::processenv::GetStdHandle;
579-
use winapi::um::winbase::STD_ERROR_HANDLE;
580-
581-
unsafe {
582-
let handle = GetStdHandle(STD_ERROR_HANDLE);
583-
let mut out = 0;
584-
GetConsoleMode(handle, &mut out) != 0
585-
}
554+
atty::is(atty::Stream::Stderr)
586555
}
587556

588557
fn handle_explain(registry: Registry, code: &str, output: ErrorOutputType) {

Diff for: compiler/rustc_errors/src/emitter.rs

+20-5
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,8 @@ impl EmitterWriter {
644644
code_offset: usize,
645645
margin: Margin,
646646
) {
647+
// Tabs are assumed to have been replaced by spaces in calling code.
648+
assert!(!source_string.contains('\t'));
647649
let line_len = source_string.len();
648650
// Create the source line we will highlight.
649651
let left = margin.left(line_len);
@@ -707,7 +709,7 @@ impl EmitterWriter {
707709
}
708710

709711
let source_string = match file.get_line(line.line_index - 1) {
710-
Some(s) => s,
712+
Some(s) => replace_tabs(&*s),
711713
None => return Vec::new(),
712714
};
713715

@@ -1376,8 +1378,17 @@ impl EmitterWriter {
13761378
let file = annotated_file.file.clone();
13771379
let line = &annotated_file.lines[line_idx];
13781380
if let Some(source_string) = file.get_line(line.line_index - 1) {
1379-
let leading_whitespace =
1380-
source_string.chars().take_while(|c| c.is_whitespace()).count();
1381+
let leading_whitespace = source_string
1382+
.chars()
1383+
.take_while(|c| c.is_whitespace())
1384+
.map(|c| {
1385+
match c {
1386+
// Tabs are displayed as 4 spaces
1387+
'\t' => 4,
1388+
_ => 1,
1389+
}
1390+
})
1391+
.sum();
13811392
if source_string.chars().any(|c| !c.is_whitespace()) {
13821393
whitespace_margin = min(whitespace_margin, leading_whitespace);
13831394
}
@@ -1502,7 +1513,7 @@ impl EmitterWriter {
15021513

15031514
self.draw_line(
15041515
&mut buffer,
1505-
&unannotated_line,
1516+
&replace_tabs(&unannotated_line),
15061517
annotated_file.lines[line_idx + 1].line_index - 1,
15071518
last_buffer_line_num,
15081519
width_offset,
@@ -1598,7 +1609,7 @@ impl EmitterWriter {
15981609
);
15991610
// print the suggestion
16001611
draw_col_separator(&mut buffer, row_num, max_line_num_len + 1);
1601-
buffer.append(row_num, line, Style::NoStyle);
1612+
buffer.append(row_num, &replace_tabs(line), Style::NoStyle);
16021613
row_num += 1;
16031614
}
16041615

@@ -1930,6 +1941,10 @@ impl FileWithAnnotatedLines {
19301941
}
19311942
}
19321943

1944+
fn replace_tabs(str: &str) -> String {
1945+
str.replace('\t', " ")
1946+
}
1947+
19331948
fn draw_col_separator(buffer: &mut StyledBuffer, line: usize, col: usize) {
19341949
buffer.puts(line, col, "| ", Style::LineNumber);
19351950
}

Diff for: compiler/rustc_errors/src/styled_buffer.rs

+3-24
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,13 @@ impl StyledBuffer {
1313
StyledBuffer { text: vec![], styles: vec![] }
1414
}
1515

16-
fn replace_tabs(&mut self) {
17-
for (line_pos, line) in self.text.iter_mut().enumerate() {
18-
let mut tab_pos = vec![];
19-
for (pos, c) in line.iter().enumerate() {
20-
if *c == '\t' {
21-
tab_pos.push(pos);
22-
}
23-
}
24-
// start with the tabs at the end of the line to replace them with 4 space chars
25-
for pos in tab_pos.iter().rev() {
26-
assert_eq!(line.remove(*pos), '\t');
27-
// fix the position of the style to match up after replacing the tabs
28-
let s = self.styles[line_pos].remove(*pos);
29-
for _ in 0..4 {
30-
line.insert(*pos, ' ');
31-
self.styles[line_pos].insert(*pos, s);
32-
}
33-
}
34-
}
35-
}
16+
pub fn render(&self) -> Vec<Vec<StyledString>> {
17+
// Tabs are assumed to have been replaced by spaces in calling code.
18+
assert!(self.text.iter().all(|r| !r.contains(&'\t')));
3619

37-
pub fn render(&mut self) -> Vec<Vec<StyledString>> {
3820
let mut output: Vec<Vec<StyledString>> = vec![];
3921
let mut styled_vec: Vec<StyledString> = vec![];
4022

41-
// before we render, replace tabs with spaces
42-
self.replace_tabs();
43-
4423
for (row, row_style) in self.text.iter().zip(&self.styles) {
4524
let mut current_style = Style::NoStyle;
4625
let mut current_text = String::new();

Diff for: compiler/rustc_resolve/src/late.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1947,8 +1947,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
19471947
_ => report_errors(self, None),
19481948
};
19491949

1950-
if let PathSource::TraitItem(..) = source {
1951-
} else {
1950+
if !matches!(source, PathSource::TraitItem(..)) {
19521951
// Avoid recording definition of `A::B` in `<T as A>::B::C`.
19531952
self.r.record_partial_res(id, partial_res);
19541953
}

Diff for: compiler/rustc_typeck/src/check/compare_method.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ fn compare_predicate_entailment<'tcx>(
296296
{
297297
diag.span_suggestion(
298298
impl_err_span,
299-
"consider change the type to match the mutability in trait",
299+
"consider changing the mutability to match the trait",
300300
trait_err_str,
301301
Applicability::MachineApplicable,
302302
);

Diff for: library/alloc/src/collections/vec_deque/tests.rs

+15
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,21 @@ fn make_contiguous_head_to_end() {
224224
assert_eq!((&['A', 'B', 'C'] as &[_], &[] as &[_]), dq.as_slices());
225225
}
226226

227+
#[test]
228+
fn make_contiguous_head_to_end_2() {
229+
// Another test case for #79808, taken from #80293.
230+
231+
let mut dq = VecDeque::from_iter(0..6);
232+
dq.pop_front();
233+
dq.pop_front();
234+
dq.push_back(6);
235+
dq.push_back(7);
236+
dq.push_back(8);
237+
dq.make_contiguous();
238+
let collected: Vec<_> = dq.iter().copied().collect();
239+
assert_eq!(dq.as_slices(), (&collected[..], &[] as &[_]));
240+
}
241+
227242
#[test]
228243
fn test_remove() {
229244
// This test checks that every single combination of tail position, length, and

Diff for: library/core/src/mem/maybe_uninit.rs

+40
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,46 @@ impl<T> MaybeUninit<T> {
804804
}
805805
}
806806

807+
/// Extracts the values from an array of `MaybeUninit` containers.
808+
///
809+
/// # Safety
810+
///
811+
/// It is up to the caller to guarantee that all elements of the array are
812+
/// in an initialized state.
813+
///
814+
/// # Examples
815+
///
816+
/// ```
817+
/// #![feature(maybe_uninit_uninit_array)]
818+
/// #![feature(maybe_uninit_array_assume_init)]
819+
/// use std::mem::MaybeUninit;
820+
///
821+
/// let mut array: [MaybeUninit<i32>; 3] = MaybeUninit::uninit_array();
822+
/// array[0] = MaybeUninit::new(0);
823+
/// array[1] = MaybeUninit::new(1);
824+
/// array[2] = MaybeUninit::new(2);
825+
///
826+
/// // SAFETY: Now safe as we initialised all elements
827+
/// let array = unsafe {
828+
/// MaybeUninit::array_assume_init(array)
829+
/// };
830+
///
831+
/// assert_eq!(array, [0, 1, 2]);
832+
/// ```
833+
#[unstable(feature = "maybe_uninit_array_assume_init", issue = "80908")]
834+
#[inline(always)]
835+
pub unsafe fn array_assume_init<const N: usize>(array: [Self; N]) -> [T; N] {
836+
// SAFETY:
837+
// * The caller guarantees that all elements of the array are initialized
838+
// * `MaybeUninit<T>` and T are guaranteed to have the same layout
839+
// * MaybeUnint does not drop, so there are no double-frees
840+
// And thus the conversion is safe
841+
unsafe {
842+
intrinsics::assert_inhabited::<T>();
843+
(&array as *const _ as *const [T; N]).read()
844+
}
845+
}
846+
807847
/// Assuming all the elements are initialized, get a slice to them.
808848
///
809849
/// # Safety

Diff for: library/core/src/slice/mod.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -1357,14 +1357,11 @@ impl<T> [T] {
13571357
///
13581358
/// ```
13591359
/// let mut v = [1, 0, 3, 0, 5, 6];
1360-
/// // scoped to restrict the lifetime of the borrows
1361-
/// {
1362-
/// let (left, right) = v.split_at_mut(2);
1363-
/// assert_eq!(left, [1, 0]);
1364-
/// assert_eq!(right, [3, 0, 5, 6]);
1365-
/// left[1] = 2;
1366-
/// right[1] = 4;
1367-
/// }
1360+
/// let (left, right) = v.split_at_mut(2);
1361+
/// assert_eq!(left, [1, 0]);
1362+
/// assert_eq!(right, [3, 0, 5, 6]);
1363+
/// left[1] = 2;
1364+
/// right[1] = 4;
13681365
/// assert_eq!(v, [1, 2, 3, 4, 5, 6]);
13691366
/// ```
13701367
#[stable(feature = "rust1", since = "1.0.0")]

Diff for: library/core/tests/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
#![feature(raw)]
3737
#![feature(sort_internals)]
3838
#![feature(slice_partition_at_index)]
39+
#![feature(maybe_uninit_uninit_array)]
40+
#![feature(maybe_uninit_array_assume_init)]
3941
#![feature(maybe_uninit_extra)]
4042
#![feature(maybe_uninit_write_slice)]
4143
#![feature(min_specialization)]

Diff for: library/core/tests/mem.rs

+14
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,20 @@ fn assume_init_good() {
140140
assert!(TRUE);
141141
}
142142

143+
#[test]
144+
fn uninit_array_assume_init() {
145+
let mut array: [MaybeUninit<i16>; 5] = MaybeUninit::uninit_array();
146+
array[0].write(3);
147+
array[1].write(1);
148+
array[2].write(4);
149+
array[3].write(1);
150+
array[4].write(5);
151+
152+
let array = unsafe { MaybeUninit::array_assume_init(array) };
153+
154+
assert_eq!(array, [3, 1, 4, 1, 5]);
155+
}
156+
143157
#[test]
144158
fn uninit_write_slice() {
145159
let mut dst = [MaybeUninit::new(255); 64];
File renamed without changes.
File renamed without changes.

Diff for: src/test/ui/issues/auxiliary/issue-10031-aux.rs

-1
This file was deleted.

Diff for: src/test/ui/issues/issue-10031.rs

-9
This file was deleted.

Diff for: src/test/ui/issues/issue-13033.stderr

+4-5
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@ LL | fn bar(&mut self, other: &mut dyn Foo);
55
| ------------ type in trait
66
...
77
LL | fn bar(&mut self, other: &dyn Foo) {}
8-
| ^^^^^^^^ types differ in mutability
8+
| ^^^^^^^^
9+
| |
10+
| types differ in mutability
11+
| help: consider changing the mutability to match the trait: `&mut dyn Foo`
912
|
1013
= note: expected fn pointer `fn(&mut Baz, &mut dyn Foo)`
1114
found fn pointer `fn(&mut Baz, &dyn Foo)`
12-
help: consider change the type to match the mutability in trait
13-
|
14-
LL | fn bar(&mut self, other: &mut dyn Foo) {}
15-
| ^^^^^^^^^^^^
1615

1716
error: aborting due to previous error
1817

Diff for: src/test/ui/mismatched_types/E0053.stderr

+4-5
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@ LL | fn bar(&self);
1717
| ----- type in trait
1818
...
1919
LL | fn bar(&mut self) { }
20-
| ^^^^^^^^^ types differ in mutability
20+
| ^^^^^^^^^
21+
| |
22+
| types differ in mutability
23+
| help: consider changing the mutability to match the trait: `&self`
2124
|
2225
= note: expected fn pointer `fn(&Bar)`
2326
found fn pointer `fn(&mut Bar)`
24-
help: consider change the type to match the mutability in trait
25-
|
26-
LL | fn bar(&self) { }
27-
| ^^^^^
2827

2928
error: aborting due to 2 previous errors
3029

Diff for: src/test/ui/mismatched_types/trait-impl-fn-incompatibility.stderr

+4-5
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@ LL | fn bar(&mut self, bar: &mut Bar);
1717
| -------- type in trait
1818
...
1919
LL | fn bar(&mut self, bar: &Bar) { }
20-
| ^^^^ types differ in mutability
20+
| ^^^^
21+
| |
22+
| types differ in mutability
23+
| help: consider changing the mutability to match the trait: `&mut Bar`
2124
|
2225
= note: expected fn pointer `fn(&mut Bar, &mut Bar)`
2326
found fn pointer `fn(&mut Bar, &Bar)`
24-
help: consider change the type to match the mutability in trait
25-
|
26-
LL | fn bar(&mut self, bar: &mut Bar) { }
27-
| ^^^^^^^^
2827

2928
error: aborting due to 2 previous errors
3029

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

Diff for: src/test/ui/terminal-width/tabs-trimming.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Test for #78438: ensure underline alignment with many tabs on the left, long line on the right
2+
3+
// ignore-tidy-linelength
4+
// ignore-tidy-tab
5+
6+
fn main() {
7+
let money = 42i32;
8+
match money {
9+
v @ 1 | 2 | 3 => panic!("You gave me too little money {}", v), // Long text here: TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT
10+
//~^ ERROR variable `v` is not bound in all patterns
11+
v => println!("Enough money {}", v),
12+
}
13+
}

0 commit comments

Comments
 (0)