Skip to content

Commit

Permalink
Rollup merge of rust-lang#35691 - jaredwy:update-error-63, r=jonathan…
Browse files Browse the repository at this point in the history
…dturner

Update the wording for E0063. This will truncate the fields to 3.

Instead of listing every field it will now show missing `a`, `z`, `b`, and 1 other field
This is for rust-lang#35218 as part of rust-lang#35233

r? @jonathandturner
  • Loading branch information
Jonathan Turner committed Aug 17, 2016
2 parents e20915f + 65b8be7 commit 9126b08
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 11 deletions.
32 changes: 24 additions & 8 deletions src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3207,14 +3207,30 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
!error_happened &&
!remaining_fields.is_empty()
{
span_err!(tcx.sess, span, E0063,
"missing field{} {} in initializer of `{}`",
if remaining_fields.len() == 1 {""} else {"s"},
remaining_fields.keys()
.map(|n| format!("`{}`", n))
.collect::<Vec<_>>()
.join(", "),
adt_ty);
let len = remaining_fields.len();

let truncated_fields = if len <= 3 {
(remaining_fields.keys().take(len), "".to_string())
} else {
(remaining_fields.keys().take(3), format!(", and {} other field{}",
(len-3), if len-3 == 1 {""} else {"s"}))
};

let remaining_fields_names = truncated_fields.0
.map(|n| format!("`{}`", n))
.collect::<Vec<_>>()
.join(", ");

struct_span_err!(tcx.sess, span, E0063,
"missing field{} {}{} in initializer of `{}`",
if remaining_fields.len() == 1 {""} else {"s"},
remaining_fields_names,
truncated_fields.1,
adt_ty)
.span_label(span, &format!("missing {}{}",
remaining_fields_names,
truncated_fields.1))
.emit();
}

}
Expand Down
42 changes: 39 additions & 3 deletions src/test/compile-fail/E0063.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,47 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

struct Foo {
// ignore-tidy-linelength

struct SingleFoo {
x: i32
}

struct PluralFoo {
x: i32,
y: i32,
z: i32
}

struct TruncatedFoo {
a: i32,
b: i32,
x: i32,
y: i32
y: i32,
z: i32
}

struct TruncatedPluralFoo {
a: i32,
b: i32,
c: i32,
x: i32,
y: i32,
z: i32
}


fn main() {
let x = Foo { x: 0 }; //~ ERROR E0063
let w = SingleFoo { };
//~^ ERROR missing field `x` in initializer of `SingleFoo`
//~| NOTE missing `x`
let x = PluralFoo {x: 1};
//~^ ERROR missing fields `z`, `y` in initializer of `PluralFoo`
//~| NOTE missing `z`, `y`
let y = TruncatedFoo{x:1};
//~^ ERROR missing fields `a`, `z`, `b`, and 1 other field in initializer of `TruncatedFoo`
//~| NOTE missing `a`, `z`, `b`, and 1 other field
let z = TruncatedPluralFoo{x:1};
//~^ ERROR missing fields `c`, `a`, `z`, and 2 other fields in initializer of `TruncatedPluralFoo`
//~| NOTE missing `c`, `a`, `z`, and 2 other fields
}

0 comments on commit 9126b08

Please sign in to comment.