Skip to content

Commit 9cb1874

Browse files
committed
Tweak wording and spans
1 parent 29e2aa1 commit 9cb1874

File tree

60 files changed

+368
-326
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+368
-326
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+58-24
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use rustc_middle::ty::{
2121
self, subst::Subst, suggest_constraining_type_params, EarlyBinder, PredicateKind, Ty,
2222
};
2323
use rustc_mir_dataflow::move_paths::{InitKind, MoveOutIndex, MovePathIndex};
24+
use rustc_span::hygiene::DesugaringKind;
2425
use rustc_span::symbol::sym;
2526
use rustc_span::{BytePos, Span};
2627
use rustc_trait_selection::infer::InferCtxtExt;
@@ -331,7 +332,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
331332
spans.push(span);
332333
}
333334

334-
let (item_msg, name, desc) =
335+
let (binding, name, desc) =
335336
match self.describe_place_with_options(used_place, IncludingDowncast(true)) {
336337
Some(name) => (format!("`{name}`"), format!("`{name}`"), format!("`{name}` ")),
337338
None => ("value".to_string(), "the variable".to_string(), String::new()),
@@ -351,20 +352,21 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
351352
} else {
352353
"initialized in all conditions"
353354
};
354-
let mut err = struct_span_err!(self, span, E0381, "binding {desc}isn't {initialized}");
355+
let used = desired_action.as_general_verb_in_past_tense();
356+
let mut err =
357+
struct_span_err!(self, span, E0381, "{used} binding {desc}isn't {initialized}");
355358
use_spans.var_span_label_path_only(
356359
&mut err,
357360
format!("{} occurs due to use{}", desired_action.as_noun(), use_spans.describe()),
358361
);
359362

360363
if let InitializationRequiringAction::PartialAssignment = desired_action {
361364
err.help(
362-
"partial initialization isn't supported, fully initialize the binding with \
363-
a default value and mutate, or use `std::mem::MaybeUninit`",
365+
"partial initialization isn't supported, fully initialize the binding with a \
366+
default value and mutate it, or use `std::mem::MaybeUninit`",
364367
);
365368
}
366-
let verb = desired_action.as_verb_in_past_tense();
367-
err.span_label(span, format!("{item_msg} {verb} here but it isn't {initialized}",));
369+
err.span_label(span, format!("{binding} {used} here but it isn't {initialized}"));
368370

369371
// We use the statements were the binding was initialized, and inspect the HIR to look
370372
// for the branching codepaths that aren't covered, to point at them.
@@ -400,7 +402,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
400402
err.span_label(sp, &label);
401403
}
402404
}
403-
err.span_label(decl_span, "variable declared here");
405+
err.span_label(decl_span, "binding declared here but left uninitialized");
404406
err
405407
}
406408

@@ -2559,10 +2561,10 @@ impl<'b, 'v> Visitor<'v> for ConditionVisitor<'b> {
25592561
v.visit_expr(body);
25602562
if v.1 {
25612563
self.errors.push((
2562-
cond.span,
2564+
ex.span.to(cond.span),
25632565
format!(
2564-
"this `if` expression might be missing an `else` arm where {} is \
2565-
initialized",
2566+
"this `if` expression might be missing an `else` arm that initializes \
2567+
{}",
25662568
self.name,
25672569
),
25682570
));
@@ -2578,10 +2580,24 @@ impl<'b, 'v> Visitor<'v> for ConditionVisitor<'b> {
25782580
match (a.1, b.1) {
25792581
(true, true) | (false, false) => {}
25802582
(true, false) => {
2581-
self.errors.push((
2582-
cond.span,
2583-
format!("{} is uninitialized if this condition isn't met", self.name,),
2584-
));
2583+
if other.span.is_desugaring(DesugaringKind::WhileLoop) {
2584+
self.errors.push((
2585+
cond.span,
2586+
format!(
2587+
"{} is uninitialized if this condition isn't met and the \
2588+
`while` loop runs 0 times",
2589+
self.name
2590+
),
2591+
));
2592+
} else {
2593+
self.errors.push((
2594+
body.span.shrink_to_hi().until(other.span),
2595+
format!(
2596+
"{} is uninitialized if this `else` arm is executed",
2597+
self.name
2598+
),
2599+
));
2600+
}
25852601
}
25862602
(false, true) => {
25872603
self.errors.push((
@@ -2591,7 +2607,7 @@ impl<'b, 'v> Visitor<'v> for ConditionVisitor<'b> {
25912607
}
25922608
}
25932609
}
2594-
hir::ExprKind::Match(_, arms, _) => {
2610+
hir::ExprKind::Match(e, arms, loop_desugar) => {
25952611
// If the binding is initialized in one of the match arms, then the other match
25962612
// arms might be missing an initialization.
25972613
let results: Vec<bool> = arms
@@ -2605,22 +2621,40 @@ impl<'b, 'v> Visitor<'v> for ConditionVisitor<'b> {
26052621
if results.iter().any(|x| *x) && !results.iter().all(|x| *x) {
26062622
for (arm, seen) in arms.iter().zip(results) {
26072623
if !seen {
2608-
self.errors.push((
2609-
arm.pat.span,
2610-
format!(
2611-
"{} is uninitialized if this pattern is matched",
2612-
self.name
2613-
),
2614-
));
2624+
if loop_desugar == hir::MatchSource::ForLoopDesugar {
2625+
self.errors.push((
2626+
e.span,
2627+
format!(
2628+
"{} is uninitialized if the `for` loop runs 0 times",
2629+
self.name
2630+
),
2631+
));
2632+
} else if let Some(guard) = &arm.guard {
2633+
self.errors.push((
2634+
arm.pat.span.to(guard.body().span),
2635+
format!(
2636+
"{} is uninitialized if this pattern and condition are \
2637+
matched",
2638+
self.name
2639+
),
2640+
));
2641+
} else {
2642+
self.errors.push((
2643+
arm.pat.span,
2644+
format!(
2645+
"{} is uninitialized if this pattern is matched",
2646+
self.name
2647+
),
2648+
));
2649+
}
26152650
}
26162651
}
26172652
}
26182653
}
26192654
// FIXME: should we also account for binops, particularly `&&` and `||`? `try` should
26202655
// also be accounted for. For now it is fine, as if we don't find *any* relevant
26212656
// branching code paths, we point at the places where the binding *is* initialized for
2622-
// *some* context. We should also specialize the output for `while` and `for` loops,
2623-
// but for now we can rely on their desugaring to provide appropriate output.
2657+
// *some* context.
26242658
_ => {}
26252659
}
26262660
walk_expr(self, ex);

compiler/rustc_borrowck/src/lib.rs

+10
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,16 @@ impl InitializationRequiringAction {
907907
InitializationRequiringAction::PartialAssignment => "partially assigned",
908908
}
909909
}
910+
911+
fn as_general_verb_in_past_tense(self) -> &'static str {
912+
match self {
913+
InitializationRequiringAction::Borrow
914+
| InitializationRequiringAction::MatchOn
915+
| InitializationRequiringAction::Use => "used",
916+
InitializationRequiringAction::Assignment => "assigned",
917+
InitializationRequiringAction::PartialAssignment => "partially assigned",
918+
}
919+
}
910920
}
911921

912922
impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {

src/test/ui/asm/x86_64/type-check-5.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
error[E0381]: binding `x` isn't initialized
1+
error[E0381]: used binding `x` isn't initialized
22
--> $DIR/type-check-5.rs:15:28
33
|
44
LL | let x: u64;
5-
| - variable declared here
5+
| - binding declared here but left uninitialized
66
LL | asm!("{}", in(reg) x);
77
| ^ `x` used here but it isn't initialized
88

9-
error[E0381]: binding `y` isn't initialized
9+
error[E0381]: used binding `y` isn't initialized
1010
--> $DIR/type-check-5.rs:18:9
1111
|
1212
LL | let mut y: u64;
13-
| ----- variable declared here
13+
| ----- binding declared here but left uninitialized
1414
LL | asm!("{}", inout(reg) y);
1515
| ^^^^^^^^^^^^^^^^^^^^^^^^ `y` used here but it isn't initialized
1616

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error[E0381]: binding `y` isn't initialized in all conditions
1+
error[E0381]: used binding `y` isn't initialized in all conditions
22
--> $DIR/no-non-guaranteed-initialization.rs:9:5
33
|
44
LL | let y;
5-
| - variable declared here
5+
| - binding declared here but left uninitialized
66
LL | if x > 5 {
7-
| ----- this `if` expression might be missing an `else` arm where `y` is initialized
7+
| ----- this `if` expression might be missing an `else` arm that initializes `y`
88
...
99
LL | y
1010
| ^ `y` used here but it isn't initialized in all conditions

src/test/ui/async-await/partial-initialization-across-await.stderr

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
1-
error[E0381]: binding `t` isn't fully initialized
1+
error[E0381]: partially assigned binding `t` isn't fully initialized
22
--> $DIR/partial-initialization-across-await.rs:13:5
33
|
44
LL | let mut t: (i32, i32);
5-
| ----- variable declared here
5+
| ----- binding declared here but left uninitialized
66
LL | t.0 = 42;
77
| ^^^^^^^^ `t` partially assigned here but it isn't fully initialized
88
|
9-
= help: partial initialization isn't supported, fully initialize the binding with a default value and mutate, or use `std::mem::MaybeUninit`
9+
= help: partial initialization isn't supported, fully initialize the binding with a default value and mutate it, or use `std::mem::MaybeUninit`
1010

11-
error[E0381]: binding `t` isn't fully initialized
11+
error[E0381]: partially assigned binding `t` isn't fully initialized
1212
--> $DIR/partial-initialization-across-await.rs:21:5
1313
|
1414
LL | let mut t: T;
15-
| ----- variable declared here
15+
| ----- binding declared here but left uninitialized
1616
LL | t.0 = 42;
1717
| ^^^^^^^^ `t` partially assigned here but it isn't fully initialized
1818
|
19-
= help: partial initialization isn't supported, fully initialize the binding with a default value and mutate, or use `std::mem::MaybeUninit`
19+
= help: partial initialization isn't supported, fully initialize the binding with a default value and mutate it, or use `std::mem::MaybeUninit`
2020

21-
error[E0381]: binding `t` isn't fully initialized
21+
error[E0381]: partially assigned binding `t` isn't fully initialized
2222
--> $DIR/partial-initialization-across-await.rs:29:5
2323
|
2424
LL | let mut t: S;
25-
| ----- variable declared here
25+
| ----- binding declared here but left uninitialized
2626
LL | t.x = 42;
2727
| ^^^^^^^^ `t` partially assigned here but it isn't fully initialized
2828
|
29-
= help: partial initialization isn't supported, fully initialize the binding with a default value and mutate, or use `std::mem::MaybeUninit`
29+
= help: partial initialization isn't supported, fully initialize the binding with a default value and mutate it, or use `std::mem::MaybeUninit`
3030

3131
error: aborting due to 3 previous errors
3232

src/test/ui/borrowck/assign_mutable_fields.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
error[E0381]: binding `x` isn't fully initialized
1+
error[E0381]: partially assigned binding `x` isn't fully initialized
22
--> $DIR/assign_mutable_fields.rs:9:5
33
|
44
LL | let mut x: (u32, u32);
5-
| ----- variable declared here
5+
| ----- binding declared here but left uninitialized
66
LL | x.0 = 1;
77
| ^^^^^^^ `x` partially assigned here but it isn't fully initialized
88
|
9-
= help: partial initialization isn't supported, fully initialize the binding with a default value and mutate, or use `std::mem::MaybeUninit`
9+
= help: partial initialization isn't supported, fully initialize the binding with a default value and mutate it, or use `std::mem::MaybeUninit`
1010

11-
error[E0381]: binding `x` isn't fully initialized
11+
error[E0381]: partially assigned binding `x` isn't fully initialized
1212
--> $DIR/assign_mutable_fields.rs:17:5
1313
|
1414
LL | let mut x: (u32, u32);
15-
| ----- variable declared here
15+
| ----- binding declared here but left uninitialized
1616
LL | x.0 = 1;
1717
| ^^^^^^^ `x` partially assigned here but it isn't fully initialized
1818
|
19-
= help: partial initialization isn't supported, fully initialize the binding with a default value and mutate, or use `std::mem::MaybeUninit`
19+
= help: partial initialization isn't supported, fully initialize the binding with a default value and mutate it, or use `std::mem::MaybeUninit`
2020

2121
error: aborting due to 2 previous errors
2222

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
error[E0381]: binding `i` isn't initialized in all conditions
1+
error[E0381]: used binding `i` isn't initialized in all conditions
22
--> $DIR/borrowck-and-init.rs:5:20
33
|
44
LL | let i: isize;
5-
| - variable declared here
5+
| - binding declared here but left uninitialized
66
LL |
77
LL | println!("{}", false && { i = 5; true });
88
| ----- binding initialized here in some conditions
99
LL | println!("{}", i);
10-
| ^ `i` borrowed here but it isn't initialized in all conditions
10+
| ^ `i` used here but it isn't initialized in all conditions
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-block-unint.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error[E0381]: binding `x` isn't initialized
1+
error[E0381]: used binding `x` isn't initialized
22
--> $DIR/borrowck-block-unint.rs:4:11
33
|
44
LL | let x: isize;
5-
| - variable declared here
5+
| - binding declared here but left uninitialized
66
LL | force(|| {
7-
| ^^ `x` borrowed here but it isn't initialized
7+
| ^^ `x` used here but it isn't initialized
88
LL | println!("{}", x);
99
| - borrow occurs due to use in closure
1010

src/test/ui/borrowck/borrowck-break-uninit-2.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
error[E0381]: binding `x` isn't initialized
1+
error[E0381]: used binding `x` isn't initialized
22
--> $DIR/borrowck-break-uninit-2.rs:9:20
33
|
44
LL | let x: isize;
5-
| - variable declared here
5+
| - binding declared here but left uninitialized
66
...
77
LL | println!("{}", x);
8-
| ^ `x` borrowed here but it isn't initialized
8+
| ^ `x` used here but it isn't initialized
99
|
1010
= note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)
1111

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
error[E0381]: binding `x` isn't initialized
1+
error[E0381]: used binding `x` isn't initialized
22
--> $DIR/borrowck-break-uninit.rs:9:20
33
|
44
LL | let x: isize;
5-
| - variable declared here
5+
| - binding declared here but left uninitialized
66
...
77
LL | println!("{}", x);
8-
| ^ `x` borrowed here but it isn't initialized
8+
| ^ `x` used here but it isn't initialized
99
|
1010
= note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)
1111

src/test/ui/borrowck/borrowck-field-sensitivity.stderr

+9-9
Original file line numberDiff line numberDiff line change
@@ -108,35 +108,35 @@ LL | let _z = A { a: 4, .. x };
108108
|
109109
= note: move occurs because `x.b` has type `Box<isize>`, which does not implement the `Copy` trait
110110

111-
error[E0381]: binding `x` isn't fully initialized
111+
error[E0381]: partially assigned binding `x` isn't fully initialized
112112
--> $DIR/borrowck-field-sensitivity.rs:81:5
113113
|
114114
LL | let mut x: A;
115-
| ----- variable declared here
115+
| ----- binding declared here but left uninitialized
116116
LL | x.a = 1;
117117
| ^^^^^^^ `x` partially assigned here but it isn't fully initialized
118118
|
119-
= help: partial initialization isn't supported, fully initialize the binding with a default value and mutate, or use `std::mem::MaybeUninit`
119+
= help: partial initialization isn't supported, fully initialize the binding with a default value and mutate it, or use `std::mem::MaybeUninit`
120120

121-
error[E0381]: binding `x` isn't fully initialized
121+
error[E0381]: partially assigned binding `x` isn't fully initialized
122122
--> $DIR/borrowck-field-sensitivity.rs:87:5
123123
|
124124
LL | let mut x: A;
125-
| ----- variable declared here
125+
| ----- binding declared here but left uninitialized
126126
LL | x.a = 1;
127127
| ^^^^^^^ `x` partially assigned here but it isn't fully initialized
128128
|
129-
= help: partial initialization isn't supported, fully initialize the binding with a default value and mutate, or use `std::mem::MaybeUninit`
129+
= help: partial initialization isn't supported, fully initialize the binding with a default value and mutate it, or use `std::mem::MaybeUninit`
130130

131-
error[E0381]: binding `x` isn't fully initialized
131+
error[E0381]: partially assigned binding `x` isn't fully initialized
132132
--> $DIR/borrowck-field-sensitivity.rs:94:5
133133
|
134134
LL | let mut x: A;
135-
| ----- variable declared here
135+
| ----- binding declared here but left uninitialized
136136
LL | x.b = Box::new(1);
137137
| ^^^ `x` partially assigned here but it isn't fully initialized
138138
|
139-
= help: partial initialization isn't supported, fully initialize the binding with a default value and mutate, or use `std::mem::MaybeUninit`
139+
= help: partial initialization isn't supported, fully initialize the binding with a default value and mutate it, or use `std::mem::MaybeUninit`
140140

141141
error: aborting due to 14 previous errors
142142

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
error[E0381]: binding `x` isn't initialized in all conditions
1+
error[E0381]: used binding `x` isn't initialized in all conditions
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 where `x` is initialized
5+
| - ----- this `if` expression might be missing an `else` arm that initializes `x`
66
| |
7-
| variable declared here
7+
| binding declared here but left uninitialized
88
LL | foo(x);
99
| ^ `x` used here but it isn't initialized in all conditions
1010

0 commit comments

Comments
 (0)