about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2015-12-13 07:56:03 +0000
committerbors <bors@rust-lang.org>2015-12-13 07:56:03 +0000
commitd382fcdb82dd07c739cb48cdd6a768e273b47256 (patch)
tree39fe26468fbf8aeb80675d2b95fca6697e6bbe48
parentc4c191afae23be19cc845f67de2a36a2862c4fa1 (diff)
parent15743919dcb60b9fa2c7ef6b698cc0ef1c972ed8 (diff)
downloadrust-d382fcdb82dd07c739cb48cdd6a768e273b47256.tar.gz
rust-d382fcdb82dd07c739cb48cdd6a768e273b47256.zip
Auto merge of #30314 - fhahn:issue-30299-missing-fields, r=pnkfelix
This PR for #30299 adds the name of the type where the field is missing.

The span that's used for the error seems correct. What may be confusing is when the initializer with the missing field contains other intializers. These are then included in the span. For example, consider the following listing.

    struct A {
        a1: i32,
        a2: B,
    }

    struct B {
        b1: i32,
        b2: i32
    }

    fn main() {
        let x = A {
            a2: B {
                b1: 1,
                b2: 1
            },
        };
    }

It will display the following code snippet along with the message that field `a2` is missing:

        let x = A {
            a2: B {
                b1: 1,
                b2: 1
            },
        };

By adding the name of the type it's clearer where the field is missing.
-rw-r--r--src/librustc_typeck/check/mod.rs5
-rw-r--r--src/test/compile-fail/struct-fields-missing.rs2
2 files changed, 4 insertions, 3 deletions
diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs
index a50213202b8..a985e355281 100644
--- a/src/librustc_typeck/check/mod.rs
+++ b/src/librustc_typeck/check/mod.rs
@@ -3156,12 +3156,13 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
             !remaining_fields.is_empty()
         {
             span_err!(tcx.sess, span, E0063,
-                      "missing field{}: {}",
+                      "missing field{} {} in initializer of `{}`",
                       if remaining_fields.len() == 1 {""} else {"s"},
                       remaining_fields.keys()
                                       .map(|n| format!("`{}`", n))
                                       .collect::<Vec<_>>()
-                                      .join(", "));
+                                      .join(", "),
+                      adt_ty);
         }
 
     }
diff --git a/src/test/compile-fail/struct-fields-missing.rs b/src/test/compile-fail/struct-fields-missing.rs
index 1fd9357cf2d..d5ab13affaf 100644
--- a/src/test/compile-fail/struct-fields-missing.rs
+++ b/src/test/compile-fail/struct-fields-missing.rs
@@ -15,7 +15,7 @@ struct BuildData {
 }
 
 fn main() {
-    let foo = BuildData { //~ ERROR missing field: `bar`
+    let foo = BuildData { //~ ERROR missing field `bar` in initializer of `BuildData`
         foo: 0
     };
 }