Skip to content

Commit 95923d1

Browse files
committed
Review comments: wording
1 parent 9cb1874 commit 95923d1

11 files changed

+74
-60
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+40-29
Original file line numberDiff line numberDiff line change
@@ -329,32 +329,36 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
329329
for init_idx in inits {
330330
let init = &self.move_data.inits[*init_idx];
331331
let span = init.span(&self.body);
332-
spans.push(span);
332+
if !span.is_dummy() {
333+
spans.push(span);
334+
}
333335
}
334336

335337
let (binding, name, desc) =
336338
match self.describe_place_with_options(used_place, IncludingDowncast(true)) {
337339
Some(name) => (format!("`{name}`"), format!("`{name}`"), format!("`{name}` ")),
338340
None => ("value".to_string(), "the variable".to_string(), String::new()),
339341
};
340-
let initialized = if let InitializationRequiringAction::PartialAssignment = desired_action {
341-
// The same error is emitted for bindings that are *sometimes* initialized and the ones
342-
// that are *partially* initialized by assigning to a field of an uninitialized
343-
// binding. We differentiate between them for more accurate wording here.
344-
"fully initialized"
345-
} else if spans.iter().filter(|i| !i.contains(span)).count() == 0 {
346-
// We filter above to avoid misleading wording in cases like:
347-
// ```
348-
// let x;
349-
// x += 1;
350-
// ```
351-
"initialized"
352-
} else {
353-
"initialized in all conditions"
354-
};
342+
let isnt_initialized =
343+
if let InitializationRequiringAction::PartialAssignment = desired_action {
344+
// The same error is emitted for bindings that are *sometimes* initialized and the ones
345+
// that are *partially* initialized by assigning to a field of an uninitialized
346+
// binding. We differentiate between them for more accurate wording here.
347+
"isn't fully initialized"
348+
} else if spans.iter().filter(|i| !i.contains(span)).count() == 0 {
349+
// We filter above to avoid misleading wording in cases like the following, where `x`
350+
// has an `init`, but it is in the same place we're looking at:
351+
// ```
352+
// let x;
353+
// x += 1;
354+
// ```
355+
"isn't initialized"
356+
} else {
357+
"is possibly-uninitialized"
358+
};
355359
let used = desired_action.as_general_verb_in_past_tense();
356360
let mut err =
357-
struct_span_err!(self, span, E0381, "{used} binding {desc}isn't {initialized}");
361+
struct_span_err!(self, span, E0381, "{used} binding {desc}{isnt_initialized}");
358362
use_spans.var_span_label_path_only(
359363
&mut err,
360364
format!("{} occurs due to use{}", desired_action.as_noun(), use_spans.describe()),
@@ -366,7 +370,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
366370
default value and mutate it, or use `std::mem::MaybeUninit`",
367371
);
368372
}
369-
err.span_label(span, format!("{binding} {used} here but it isn't {initialized}"));
373+
err.span_label(span, format!("{binding} {used} here but it {isnt_initialized}"));
370374

371375
// We use the statements were the binding was initialized, and inspect the HIR to look
372376
// for the branching codepaths that aren't covered, to point at them.
@@ -2561,13 +2565,16 @@ impl<'b, 'v> Visitor<'v> for ConditionVisitor<'b> {
25612565
v.visit_expr(body);
25622566
if v.1 {
25632567
self.errors.push((
2564-
ex.span.to(cond.span),
2568+
cond.span,
25652569
format!(
2566-
"this `if` expression might be missing an `else` arm that initializes \
2567-
{}",
2570+
"if this `if` condition is `false`, {} is not initialized",
25682571
self.name,
25692572
),
25702573
));
2574+
self.errors.push((
2575+
ex.span.shrink_to_hi(),
2576+
format!("an `else` arm might be missing here, initializing {}", self.name),
2577+
));
25712578
}
25722579
}
25732580
hir::ExprKind::If(cond, body, Some(other)) => {
@@ -2584,16 +2591,17 @@ impl<'b, 'v> Visitor<'v> for ConditionVisitor<'b> {
25842591
self.errors.push((
25852592
cond.span,
25862593
format!(
2587-
"{} is uninitialized if this condition isn't met and the \
2588-
`while` loop runs 0 times",
2594+
"if this condition isn't met and the `while` loop runs 0 \
2595+
times, {} is not initialized",
25892596
self.name
25902597
),
25912598
));
25922599
} else {
25932600
self.errors.push((
25942601
body.span.shrink_to_hi().until(other.span),
25952602
format!(
2596-
"{} is uninitialized if this `else` arm is executed",
2603+
"if the `if` condition is `false` and this `else` arm is \
2604+
executed, {} is not initialized",
25972605
self.name
25982606
),
25992607
));
@@ -2602,7 +2610,10 @@ impl<'b, 'v> Visitor<'v> for ConditionVisitor<'b> {
26022610
(false, true) => {
26032611
self.errors.push((
26042612
cond.span,
2605-
format!("{} is uninitialized if this condition is met", self.name),
2613+
format!(
2614+
"if this condition is `true`, {} is not initialized",
2615+
self.name
2616+
),
26062617
));
26072618
}
26082619
}
@@ -2625,24 +2636,24 @@ impl<'b, 'v> Visitor<'v> for ConditionVisitor<'b> {
26252636
self.errors.push((
26262637
e.span,
26272638
format!(
2628-
"{} is uninitialized if the `for` loop runs 0 times",
2639+
"if the `for` loop runs 0 times, {} is not initialized ",
26292640
self.name
26302641
),
26312642
));
26322643
} else if let Some(guard) = &arm.guard {
26332644
self.errors.push((
26342645
arm.pat.span.to(guard.body().span),
26352646
format!(
2636-
"{} is uninitialized if this pattern and condition are \
2637-
matched",
2647+
"if this pattern and condition are matched, {} is not \
2648+
initialized",
26382649
self.name
26392650
),
26402651
));
26412652
} else {
26422653
self.errors.push((
26432654
arm.pat.span,
26442655
format!(
2645-
"{} is uninitialized if this pattern is matched",
2656+
"if this pattern is matched, {} is not initialized",
26462657
self.name
26472658
),
26482659
));

src/test/ui/async-await/no-non-guaranteed-initialization.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
error[E0381]: used binding `y` isn't initialized in all conditions
1+
error[E0381]: used binding `y` is possibly-uninitialized
22
--> $DIR/no-non-guaranteed-initialization.rs:9:5
33
|
44
LL | let y;
55
| - binding declared here but left uninitialized
66
LL | if x > 5 {
7-
| ----- this `if` expression might be missing an `else` arm that initializes `y`
8-
...
7+
| ----- if this `if` condition is `false`, `y` is not initialized
8+
LL | y = echo(10).await;
9+
LL | }
10+
| - an `else` arm might be missing here, initializing `y`
911
LL | y
10-
| ^ `y` used here but it isn't initialized in all conditions
12+
| ^ `y` used here but it is possibly-uninitialized
1113

1214
error: aborting due to previous error
1315

src/test/ui/borrowck/borrowck-and-init.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0381]: used binding `i` isn't initialized in all conditions
1+
error[E0381]: used binding `i` is possibly-uninitialized
22
--> $DIR/borrowck-and-init.rs:5:20
33
|
44
LL | let i: isize;
@@ -7,7 +7,7 @@ LL |
77
LL | println!("{}", false && { i = 5; true });
88
| ----- binding initialized here in some conditions
99
LL | println!("{}", i);
10-
| ^ `i` used here but it isn't initialized in all conditions
10+
| ^ `i` used here but it is possibly-uninitialized
1111
|
1212
= note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)
1313

src/test/ui/borrowck/borrowck-if-no-else.stderr

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
error[E0381]: used binding `x` isn't initialized in all conditions
1+
error[E0381]: used binding `x` is possibly-uninitialized
22
--> $DIR/borrowck-if-no-else.rs:5:9
33
|
44
LL | let x: isize; if 1 > 2 { x = 10; }
5-
| - ----- this `if` expression might be missing an `else` arm that initializes `x`
6-
| |
5+
| - ----- - an `else` arm might be missing here, initializing `x`
6+
| | |
7+
| | if this `if` condition is `false`, `x` is not initialized
78
| binding declared here but left uninitialized
89
LL | foo(x);
9-
| ^ `x` used here but it isn't initialized in all conditions
10+
| ^ `x` used here but it is possibly-uninitialized
1011

1112
error: aborting due to previous error
1213

src/test/ui/borrowck/borrowck-if-with-else.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
error[E0381]: used binding `x` isn't initialized in all conditions
1+
error[E0381]: used binding `x` is possibly-uninitialized
22
--> $DIR/borrowck-if-with-else.rs:10:9
33
|
44
LL | let x: isize;
55
| - binding declared here but left uninitialized
66
LL | if 1 > 2 {
7-
| ----- `x` is uninitialized if this condition is met
7+
| ----- if this condition is `true`, `x` is not initialized
88
...
99
LL | foo(x);
10-
| ^ `x` used here but it isn't initialized in all conditions
10+
| ^ `x` used here but it is possibly-uninitialized
1111

1212
error: aborting due to previous error
1313

src/test/ui/borrowck/borrowck-or-init.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0381]: used binding `i` isn't initialized in all conditions
1+
error[E0381]: used binding `i` is possibly-uninitialized
22
--> $DIR/borrowck-or-init.rs:5:20
33
|
44
LL | let i: isize;
@@ -7,7 +7,7 @@ LL |
77
LL | println!("{}", false || { i = 5; true });
88
| ----- binding initialized here in some conditions
99
LL | println!("{}", i);
10-
| ^ `i` used here but it isn't initialized in all conditions
10+
| ^ `i` used here but it is possibly-uninitialized
1111
|
1212
= note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)
1313

src/test/ui/borrowck/borrowck-while-break.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
error[E0381]: used binding `v` isn't initialized in all conditions
1+
error[E0381]: used binding `v` is possibly-uninitialized
22
--> $DIR/borrowck-while-break.rs:7:20
33
|
44
LL | let v;
55
| - binding declared here but left uninitialized
66
LL | while cond {
7-
| ---- `v` is uninitialized if this condition isn't met and the `while` loop runs 0 times
7+
| ---- if this condition isn't met and the `while` loop runs 0 times, `v` is not initialized
88
...
99
LL | println!("{}", v);
10-
| ^ `v` used here but it isn't initialized in all conditions
10+
| ^ `v` used here but it is possibly-uninitialized
1111
|
1212
= note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)
1313

src/test/ui/borrowck/borrowck-while.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
error[E0381]: used binding `x` isn't initialized in all conditions
1+
error[E0381]: used binding `x` is possibly-uninitialized
22
--> $DIR/borrowck-while.rs:4:12
33
|
44
LL | let mut x: isize;
55
| ----- binding declared here but left uninitialized
66
LL | while 1 == 1 { x = 10; }
7-
| ------ `x` is uninitialized if this condition isn't met and the `while` loop runs 0 times
7+
| ------ if this condition isn't met and the `while` loop runs 0 times, `x` is not initialized
88
LL | return x;
9-
| ^ `x` used here but it isn't initialized in all conditions
9+
| ^ `x` used here but it is possibly-uninitialized
1010

1111
error: aborting due to previous error
1212

src/test/ui/nll/match-cfg-fake-edges.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
error[E0381]: used binding `x` isn't initialized in all conditions
1+
error[E0381]: used binding `x` is possibly-uninitialized
22
--> $DIR/match-cfg-fake-edges.rs:21:13
33
|
44
LL | let x;
55
| - binding declared here but left uninitialized
66
...
77
LL | x;
8-
| ^ `x` used here but it isn't initialized in all conditions
8+
| ^ `x` used here but it is possibly-uninitialized
99

1010
error[E0382]: use of moved value: `x`
1111
--> $DIR/match-cfg-fake-edges.rs:35:13

src/test/ui/rfc-2497-if-let-chains/chains-without-let.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
1-
error[E0381]: used binding `z` isn't initialized in all conditions
1+
error[E0381]: used binding `z` is possibly-uninitialized
22
--> $DIR/chains-without-let.rs:3:34
33
|
44
LL | let z;
55
| - binding declared here but left uninitialized
66
LL | if true && { z = 3; true} && z == 3 {}
7-
| ----- ^ `z` used here but it isn't initialized in all conditions
7+
| ----- ^ `z` used here but it is possibly-uninitialized
88
| |
99
| binding initialized here in some conditions
1010

11-
error[E0381]: used binding `z` isn't initialized in all conditions
11+
error[E0381]: used binding `z` is possibly-uninitialized
1212
--> $DIR/chains-without-let.rs:9:31
1313
|
1414
LL | let z;
1515
| - binding declared here but left uninitialized
1616
LL | true && { z = 3; true} && z == 3;
17-
| ----- ^ `z` used here but it isn't initialized in all conditions
17+
| ----- ^ `z` used here but it is possibly-uninitialized
1818
| |
1919
| binding initialized here in some conditions
2020

21-
error[E0381]: used binding `z` isn't initialized in all conditions
21+
error[E0381]: used binding `z` is possibly-uninitialized
2222
--> $DIR/chains-without-let.rs:15:36
2323
|
2424
LL | let z;
2525
| - binding declared here but left uninitialized
2626
LL | if false || { z = 3; false} || z == 3 {}
27-
| ----- ^ `z` used here but it isn't initialized in all conditions
27+
| ----- ^ `z` used here but it is possibly-uninitialized
2828
| |
2929
| binding initialized here in some conditions
3030

src/test/ui/try-block/try-block-opt-init.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0381]: used binding `cfg_res` isn't initialized in all conditions
1+
error[E0381]: used binding `cfg_res` is possibly-uninitialized
22
--> $DIR/try-block-opt-init.rs:15:5
33
|
44
LL | let cfg_res;
@@ -8,7 +8,7 @@ LL | cfg_res = 5;
88
| ----------- binding initialized here in some conditions
99
...
1010
LL | assert_eq!(cfg_res, 5);
11-
| ^^^^^^^^^^^^^^^^^^^^^^ `cfg_res` used here but it isn't initialized in all conditions
11+
| ^^^^^^^^^^^^^^^^^^^^^^ `cfg_res` used here but it is possibly-uninitialized
1212
|
1313
= note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
1414

0 commit comments

Comments
 (0)