about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2022-06-22 11:04:36 -0700
committerEsteban Küber <esteban@kuber.com.ar>2022-07-07 12:25:56 -0700
commit95923d1676f565e6358183d5428513c1803f79c2 (patch)
tree367d9fbc100645cda18aefcecdb25d470af908dd
parent9cb1874cd6567b68ee6d3aba60449771cf868d5a (diff)
downloadrust-95923d1676f565e6358183d5428513c1803f79c2.tar.gz
rust-95923d1676f565e6358183d5428513c1803f79c2.zip
Review comments: wording
-rw-r--r--compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs69
-rw-r--r--src/test/ui/async-await/no-non-guaranteed-initialization.stderr10
-rw-r--r--src/test/ui/borrowck/borrowck-and-init.stderr4
-rw-r--r--src/test/ui/borrowck/borrowck-if-no-else.stderr9
-rw-r--r--src/test/ui/borrowck/borrowck-if-with-else.stderr6
-rw-r--r--src/test/ui/borrowck/borrowck-or-init.stderr4
-rw-r--r--src/test/ui/borrowck/borrowck-while-break.stderr6
-rw-r--r--src/test/ui/borrowck/borrowck-while.stderr6
-rw-r--r--src/test/ui/nll/match-cfg-fake-edges.stderr4
-rw-r--r--src/test/ui/rfc-2497-if-let-chains/chains-without-let.stderr12
-rw-r--r--src/test/ui/try-block/try-block-opt-init.stderr4
11 files changed, 74 insertions, 60 deletions
diff --git a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
index 08b22a6e0e1..8bdc45409f5 100644
--- a/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
+++ b/compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
@@ -329,7 +329,9 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
         for init_idx in inits {
             let init = &self.move_data.inits[*init_idx];
             let span = init.span(&self.body);
-            spans.push(span);
+            if !span.is_dummy() {
+                spans.push(span);
+            }
         }
 
         let (binding, name, desc) =
@@ -337,24 +339,26 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
                 Some(name) => (format!("`{name}`"), format!("`{name}`"), format!("`{name}` ")),
                 None => ("value".to_string(), "the variable".to_string(), String::new()),
             };
-        let initialized = if let InitializationRequiringAction::PartialAssignment = desired_action {
-            // The same error is emitted for bindings that are *sometimes* initialized and the ones
-            // that are *partially* initialized by assigning to a field of an uninitialized
-            // binding. We differentiate between them for more accurate wording here.
-            "fully initialized"
-        } else if spans.iter().filter(|i| !i.contains(span)).count() == 0 {
-            // We filter above to avoid misleading wording in cases like:
-            // ```
-            // let x;
-            // x += 1;
-            // ```
-            "initialized"
-        } else {
-            "initialized in all conditions"
-        };
+        let isnt_initialized =
+            if let InitializationRequiringAction::PartialAssignment = desired_action {
+                // The same error is emitted for bindings that are *sometimes* initialized and the ones
+                // that are *partially* initialized by assigning to a field of an uninitialized
+                // binding. We differentiate between them for more accurate wording here.
+                "isn't fully initialized"
+            } else if spans.iter().filter(|i| !i.contains(span)).count() == 0 {
+                // We filter above to avoid misleading wording in cases like the following, where `x`
+                // has an `init`, but it is in the same place we're looking at:
+                // ```
+                // let x;
+                // x += 1;
+                // ```
+                "isn't initialized"
+            } else {
+                "is possibly-uninitialized"
+            };
         let used = desired_action.as_general_verb_in_past_tense();
         let mut err =
-            struct_span_err!(self, span, E0381, "{used} binding {desc}isn't {initialized}");
+            struct_span_err!(self, span, E0381, "{used} binding {desc}{isnt_initialized}");
         use_spans.var_span_label_path_only(
             &mut err,
             format!("{} occurs due to use{}", desired_action.as_noun(), use_spans.describe()),
@@ -366,7 +370,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
                  default value and mutate it, or use `std::mem::MaybeUninit`",
             );
         }
-        err.span_label(span, format!("{binding} {used} here but it isn't {initialized}"));
+        err.span_label(span, format!("{binding} {used} here but it {isnt_initialized}"));
 
         // We use the statements were the binding was initialized, and inspect the HIR to look
         // for the branching codepaths that aren't covered, to point at them.
@@ -2561,13 +2565,16 @@ impl<'b, 'v> Visitor<'v> for ConditionVisitor<'b> {
                 v.visit_expr(body);
                 if v.1 {
                     self.errors.push((
-                        ex.span.to(cond.span),
+                        cond.span,
                         format!(
-                            "this `if` expression might be missing an `else` arm that initializes \
-                             {}",
+                            "if this `if` condition is `false`, {} is not initialized",
                             self.name,
                         ),
                     ));
+                    self.errors.push((
+                        ex.span.shrink_to_hi(),
+                        format!("an `else` arm might be missing here, initializing {}", self.name),
+                    ));
                 }
             }
             hir::ExprKind::If(cond, body, Some(other)) => {
@@ -2584,8 +2591,8 @@ impl<'b, 'v> Visitor<'v> for ConditionVisitor<'b> {
                             self.errors.push((
                                 cond.span,
                                 format!(
-                                    "{} is uninitialized if this condition isn't met and the \
-                                     `while` loop runs 0 times",
+                                    "if this condition isn't met and the `while` loop runs 0 \
+                                     times, {} is not initialized",
                                     self.name
                                 ),
                             ));
@@ -2593,7 +2600,8 @@ impl<'b, 'v> Visitor<'v> for ConditionVisitor<'b> {
                             self.errors.push((
                                 body.span.shrink_to_hi().until(other.span),
                                 format!(
-                                    "{} is uninitialized if this `else` arm is executed",
+                                    "if the `if` condition is `false` and this `else` arm is \
+                                     executed, {} is not initialized",
                                     self.name
                                 ),
                             ));
@@ -2602,7 +2610,10 @@ impl<'b, 'v> Visitor<'v> for ConditionVisitor<'b> {
                     (false, true) => {
                         self.errors.push((
                             cond.span,
-                            format!("{} is uninitialized if this condition is met", self.name),
+                            format!(
+                                "if this condition is `true`, {} is not initialized",
+                                self.name
+                            ),
                         ));
                     }
                 }
@@ -2625,7 +2636,7 @@ impl<'b, 'v> Visitor<'v> for ConditionVisitor<'b> {
                                 self.errors.push((
                                     e.span,
                                     format!(
-                                        "{} is uninitialized if the `for` loop runs 0 times",
+                                        "if the `for` loop runs 0 times, {} is not initialized ",
                                         self.name
                                     ),
                                 ));
@@ -2633,8 +2644,8 @@ impl<'b, 'v> Visitor<'v> for ConditionVisitor<'b> {
                                 self.errors.push((
                                     arm.pat.span.to(guard.body().span),
                                     format!(
-                                        "{} is uninitialized if this pattern and condition are \
-                                         matched",
+                                        "if this pattern and condition are matched, {} is not \
+                                         initialized",
                                         self.name
                                     ),
                                 ));
@@ -2642,7 +2653,7 @@ impl<'b, 'v> Visitor<'v> for ConditionVisitor<'b> {
                                 self.errors.push((
                                     arm.pat.span,
                                     format!(
-                                        "{} is uninitialized if this pattern is matched",
+                                        "if this pattern is matched, {} is not initialized",
                                         self.name
                                     ),
                                 ));
diff --git a/src/test/ui/async-await/no-non-guaranteed-initialization.stderr b/src/test/ui/async-await/no-non-guaranteed-initialization.stderr
index b23e2da0e09..12c15bf56ce 100644
--- a/src/test/ui/async-await/no-non-guaranteed-initialization.stderr
+++ b/src/test/ui/async-await/no-non-guaranteed-initialization.stderr
@@ -1,13 +1,15 @@
-error[E0381]: used binding `y` isn't initialized in all conditions
+error[E0381]: used binding `y` is possibly-uninitialized
   --> $DIR/no-non-guaranteed-initialization.rs:9:5
    |
 LL |     let y;
    |         - binding declared here but left uninitialized
 LL |     if x > 5 {
-   |        ----- this `if` expression might be missing an `else` arm that initializes `y`
-...
+   |        ----- if this `if` condition is `false`, `y` is not initialized
+LL |         y = echo(10).await;
+LL |     }
+   |      - an `else` arm might be missing here, initializing `y`
 LL |     y
-   |     ^ `y` used here but it isn't initialized in all conditions
+   |     ^ `y` used here but it is possibly-uninitialized
 
 error: aborting due to previous error
 
diff --git a/src/test/ui/borrowck/borrowck-and-init.stderr b/src/test/ui/borrowck/borrowck-and-init.stderr
index a78ac1e593a..7f3d27d6091 100644
--- a/src/test/ui/borrowck/borrowck-and-init.stderr
+++ b/src/test/ui/borrowck/borrowck-and-init.stderr
@@ -1,4 +1,4 @@
-error[E0381]: used binding `i` isn't initialized in all conditions
+error[E0381]: used binding `i` is possibly-uninitialized
   --> $DIR/borrowck-and-init.rs:5:20
    |
 LL |     let i: isize;
@@ -7,7 +7,7 @@ LL |
 LL |     println!("{}", false && { i = 5; true });
    |                               ----- binding initialized here in some conditions
 LL |     println!("{}", i);
-   |                    ^ `i` used here but it isn't initialized in all conditions
+   |                    ^ `i` used here but it is possibly-uninitialized
    |
    = note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)
 
diff --git a/src/test/ui/borrowck/borrowck-if-no-else.stderr b/src/test/ui/borrowck/borrowck-if-no-else.stderr
index 134e7d5df4b..9eafc2c2a86 100644
--- a/src/test/ui/borrowck/borrowck-if-no-else.stderr
+++ b/src/test/ui/borrowck/borrowck-if-no-else.stderr
@@ -1,12 +1,13 @@
-error[E0381]: used binding `x` isn't initialized in all conditions
+error[E0381]: used binding `x` is possibly-uninitialized
   --> $DIR/borrowck-if-no-else.rs:5:9
    |
 LL |     let x: isize; if 1 > 2 { x = 10; }
-   |         -            ----- this `if` expression might be missing an `else` arm that initializes `x`
-   |         |
+   |         -            -----            - an `else` arm might be missing here, initializing `x`
+   |         |            |
+   |         |            if this `if` condition is `false`, `x` is not initialized
    |         binding declared here but left uninitialized
 LL |     foo(x);
-   |         ^ `x` used here but it isn't initialized in all conditions
+   |         ^ `x` used here but it is possibly-uninitialized
 
 error: aborting due to previous error
 
diff --git a/src/test/ui/borrowck/borrowck-if-with-else.stderr b/src/test/ui/borrowck/borrowck-if-with-else.stderr
index f4b0c393074..3f0fe291ca2 100644
--- a/src/test/ui/borrowck/borrowck-if-with-else.stderr
+++ b/src/test/ui/borrowck/borrowck-if-with-else.stderr
@@ -1,13 +1,13 @@
-error[E0381]: used binding `x` isn't initialized in all conditions
+error[E0381]: used binding `x` is possibly-uninitialized
   --> $DIR/borrowck-if-with-else.rs:10:9
    |
 LL |     let x: isize;
    |         - binding declared here but left uninitialized
 LL |     if 1 > 2 {
-   |        ----- `x` is uninitialized if this condition is met
+   |        ----- if this condition is `true`, `x` is not initialized
 ...
 LL |     foo(x);
-   |         ^ `x` used here but it isn't initialized in all conditions
+   |         ^ `x` used here but it is possibly-uninitialized
 
 error: aborting due to previous error
 
diff --git a/src/test/ui/borrowck/borrowck-or-init.stderr b/src/test/ui/borrowck/borrowck-or-init.stderr
index 633c4017d16..0bc24f1b693 100644
--- a/src/test/ui/borrowck/borrowck-or-init.stderr
+++ b/src/test/ui/borrowck/borrowck-or-init.stderr
@@ -1,4 +1,4 @@
-error[E0381]: used binding `i` isn't initialized in all conditions
+error[E0381]: used binding `i` is possibly-uninitialized
   --> $DIR/borrowck-or-init.rs:5:20
    |
 LL |     let i: isize;
@@ -7,7 +7,7 @@ LL |
 LL |     println!("{}", false || { i = 5; true });
    |                               ----- binding initialized here in some conditions
 LL |     println!("{}", i);
-   |                    ^ `i` used here but it isn't initialized in all conditions
+   |                    ^ `i` used here but it is possibly-uninitialized
    |
    = note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)
 
diff --git a/src/test/ui/borrowck/borrowck-while-break.stderr b/src/test/ui/borrowck/borrowck-while-break.stderr
index ab7d50b834a..44674febf49 100644
--- a/src/test/ui/borrowck/borrowck-while-break.stderr
+++ b/src/test/ui/borrowck/borrowck-while-break.stderr
@@ -1,13 +1,13 @@
-error[E0381]: used binding `v` isn't initialized in all conditions
+error[E0381]: used binding `v` is possibly-uninitialized
   --> $DIR/borrowck-while-break.rs:7:20
    |
 LL |     let v;
    |         - binding declared here but left uninitialized
 LL |     while cond {
-   |           ---- `v` is uninitialized if this condition isn't met and the `while` loop runs 0 times
+   |           ---- if this condition isn't met and the `while` loop runs 0 times, `v` is not initialized
 ...
 LL |     println!("{}", v);
-   |                    ^ `v` used here but it isn't initialized in all conditions
+   |                    ^ `v` used here but it is possibly-uninitialized
    |
    = note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)
 
diff --git a/src/test/ui/borrowck/borrowck-while.stderr b/src/test/ui/borrowck/borrowck-while.stderr
index 5bb86b11ba5..c45235990c3 100644
--- a/src/test/ui/borrowck/borrowck-while.stderr
+++ b/src/test/ui/borrowck/borrowck-while.stderr
@@ -1,12 +1,12 @@
-error[E0381]: used binding `x` isn't initialized in all conditions
+error[E0381]: used binding `x` is possibly-uninitialized
   --> $DIR/borrowck-while.rs:4:12
    |
 LL |     let mut x: isize;
    |         ----- binding declared here but left uninitialized
 LL |     while 1 == 1 { x = 10; }
-   |           ------ `x` is uninitialized if this condition isn't met and the `while` loop runs 0 times
+   |           ------ if this condition isn't met and the `while` loop runs 0 times, `x` is not initialized
 LL |     return x;
-   |            ^ `x` used here but it isn't initialized in all conditions
+   |            ^ `x` used here but it is possibly-uninitialized
 
 error: aborting due to previous error
 
diff --git a/src/test/ui/nll/match-cfg-fake-edges.stderr b/src/test/ui/nll/match-cfg-fake-edges.stderr
index 39933db3648..b7529389f02 100644
--- a/src/test/ui/nll/match-cfg-fake-edges.stderr
+++ b/src/test/ui/nll/match-cfg-fake-edges.stderr
@@ -1,11 +1,11 @@
-error[E0381]: used binding `x` isn't initialized in all conditions
+error[E0381]: used binding `x` is possibly-uninitialized
   --> $DIR/match-cfg-fake-edges.rs:21:13
    |
 LL |     let x;
    |         - binding declared here but left uninitialized
 ...
 LL |             x;
-   |             ^ `x` used here but it isn't initialized in all conditions
+   |             ^ `x` used here but it is possibly-uninitialized
 
 error[E0382]: use of moved value: `x`
   --> $DIR/match-cfg-fake-edges.rs:35:13
diff --git a/src/test/ui/rfc-2497-if-let-chains/chains-without-let.stderr b/src/test/ui/rfc-2497-if-let-chains/chains-without-let.stderr
index a3d8c608f4b..30d5a6779fc 100644
--- a/src/test/ui/rfc-2497-if-let-chains/chains-without-let.stderr
+++ b/src/test/ui/rfc-2497-if-let-chains/chains-without-let.stderr
@@ -1,30 +1,30 @@
-error[E0381]: used binding `z` isn't initialized in all conditions
+error[E0381]: used binding `z` is possibly-uninitialized
   --> $DIR/chains-without-let.rs:3:34
    |
 LL |     let z;
    |         - binding declared here but left uninitialized
 LL |     if true && { z = 3; true} && z == 3 {}
-   |                  -----           ^ `z` used here but it isn't initialized in all conditions
+   |                  -----           ^ `z` used here but it is possibly-uninitialized
    |                  |
    |                  binding initialized here in some conditions
 
-error[E0381]: used binding `z` isn't initialized in all conditions
+error[E0381]: used binding `z` is possibly-uninitialized
   --> $DIR/chains-without-let.rs:9:31
    |
 LL |     let z;
    |         - binding declared here but left uninitialized
 LL |     true && { z = 3; true} && z == 3;
-   |               -----           ^ `z` used here but it isn't initialized in all conditions
+   |               -----           ^ `z` used here but it is possibly-uninitialized
    |               |
    |               binding initialized here in some conditions
 
-error[E0381]: used binding `z` isn't initialized in all conditions
+error[E0381]: used binding `z` is possibly-uninitialized
   --> $DIR/chains-without-let.rs:15:36
    |
 LL |     let z;
    |         - binding declared here but left uninitialized
 LL |     if false || { z = 3; false} || z == 3 {}
-   |                   -----            ^ `z` used here but it isn't initialized in all conditions
+   |                   -----            ^ `z` used here but it is possibly-uninitialized
    |                   |
    |                   binding initialized here in some conditions
 
diff --git a/src/test/ui/try-block/try-block-opt-init.stderr b/src/test/ui/try-block/try-block-opt-init.stderr
index 72e732599c0..c397385017f 100644
--- a/src/test/ui/try-block/try-block-opt-init.stderr
+++ b/src/test/ui/try-block/try-block-opt-init.stderr
@@ -1,4 +1,4 @@
-error[E0381]: used binding `cfg_res` isn't initialized in all conditions
+error[E0381]: used binding `cfg_res` is possibly-uninitialized
   --> $DIR/try-block-opt-init.rs:15:5
    |
 LL |     let cfg_res;
@@ -8,7 +8,7 @@ LL |         cfg_res = 5;
    |         ----------- binding initialized here in some conditions
 ...
 LL |     assert_eq!(cfg_res, 5);
-   |     ^^^^^^^^^^^^^^^^^^^^^^ `cfg_res` used here but it isn't initialized in all conditions
+   |     ^^^^^^^^^^^^^^^^^^^^^^ `cfg_res` used here but it is possibly-uninitialized
    |
    = note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)