]> git.lizzy.rs Git - rust.git/commitdiff
Review comments: wording
authorEsteban Küber <esteban@kuber.com.ar>
Wed, 22 Jun 2022 18:04:36 +0000 (11:04 -0700)
committerEsteban Küber <esteban@kuber.com.ar>
Thu, 7 Jul 2022 19:25:56 +0000 (12:25 -0700)
compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
src/test/ui/async-await/no-non-guaranteed-initialization.stderr
src/test/ui/borrowck/borrowck-and-init.stderr
src/test/ui/borrowck/borrowck-if-no-else.stderr
src/test/ui/borrowck/borrowck-if-with-else.stderr
src/test/ui/borrowck/borrowck-or-init.stderr
src/test/ui/borrowck/borrowck-while-break.stderr
src/test/ui/borrowck/borrowck-while.stderr
src/test/ui/nll/match-cfg-fake-edges.stderr
src/test/ui/rfc-2497-if-let-chains/chains-without-let.stderr
src/test/ui/try-block/try-block-opt-init.stderr

index 08b22a6e0e182e2d58c140cda6f248765b80d046..8bdc45409f52ca0c3e40bb932499c7c8783e1cf5 100644 (file)
@@ -329,7 +329,9 @@ fn report_use_of_uninitialized(
         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 @@ fn report_use_of_uninitialized(
                 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 @@ fn report_use_of_uninitialized(
                  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 @@ fn visit_expr(&mut self, ex: &'v hir::Expr<'v>) {
                 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 @@ fn visit_expr(&mut self, ex: &'v hir::Expr<'v>) {
                             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 @@ fn visit_expr(&mut self, ex: &'v hir::Expr<'v>) {
                             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 @@ fn visit_expr(&mut self, ex: &'v hir::Expr<'v>) {
                     (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 @@ fn visit_expr(&mut self, ex: &'v hir::Expr<'v>) {
                                 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 @@ fn visit_expr(&mut self, ex: &'v hir::Expr<'v>) {
                                 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 @@ fn visit_expr(&mut self, ex: &'v hir::Expr<'v>) {
                                 self.errors.push((
                                     arm.pat.span,
                                     format!(
-                                        "{} is uninitialized if this pattern is matched",
+                                        "if this pattern is matched, {} is not initialized",
                                         self.name
                                     ),
                                 ));
index b23e2da0e0920195290696484cb530fdd871680f..12c15bf56ce22aa1b7b512b03e9922ed49d063c3 100644 (file)
@@ -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
 
index a78ac1e593a05fc6e799015a1d3ceb6856c4f905..7f3d27d6091d83e8aec5fe9122e7318629861bd7 100644 (file)
@@ -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)
 
index 134e7d5df4b09dc23b60706a6ae312779448183f..9eafc2c2a86e6b4051adef9e3e5a231c28e666c8 100644 (file)
@@ -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
 
index f4b0c3930745232ada60b46b67e8eea5f816f8c9..3f0fe291ca2508e44f723ca6ab88129117ad1bb3 100644 (file)
@@ -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
 
index 633c4017d16ac1ef24375ee2d29d513030c5e1ff..0bc24f1b6932f2e56988f9d1f3d84a470ac37b55 100644 (file)
@@ -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)
 
index ab7d50b834a4ee9d4b37a2695a937f39375a52e1..44674febf4973b4c3929e779e62e4a8554a7947d 100644 (file)
@@ -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)
 
index 5bb86b11ba50338d03589751e27e4bf98214a981..c45235990c383c56783321d61b0e665adab05bf8 100644 (file)
@@ -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
 
index 39933db36480389b824164fcf6440d43841f4962..b7529389f0276a6f832e3e037ce449ab6b33d3f8 100644 (file)
@@ -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
index a3d8c608f4b8b4527aba0612cf9de23f86f79a61..30d5a6779fcd7ba8de7b439272d0284c2a2905e4 100644 (file)
@@ -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
 
index 72e732599c00ead65a2ca33894fcadf1ad55ea18..c397385017ff487cdb2de28f6a57c40579394e12 100644 (file)
@@ -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)