Skip to content

Commit d3f3ae3

Browse files
committed
rollup merge of rust-lang#20084: barosl/struct-variant-field-err
Fixes rust-lang#19922.
2 parents 4baeace + c9010bf commit d3f3ae3

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

src/librustc_typeck/check/mod.rs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3418,7 +3418,8 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
34183418
substitutions: subst::Substs<'tcx>,
34193419
field_types: &[ty::field_ty],
34203420
ast_fields: &[ast::Field],
3421-
check_completeness: bool) {
3421+
check_completeness: bool,
3422+
enum_id_opt: Option<ast::DefId>) {
34223423
let tcx = fcx.ccx.tcx;
34233424

34243425
let mut class_field_map = FnvHashMap::new();
@@ -3437,13 +3438,24 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
34373438
match pair {
34383439
None => {
34393440
fcx.type_error_message(
3440-
field.ident.span,
3441-
|actual| {
3442-
format!("structure `{}` has no field named `{}`",
3443-
actual, token::get_ident(field.ident.node))
3444-
},
3445-
struct_ty,
3446-
None);
3441+
field.ident.span,
3442+
|actual| match enum_id_opt {
3443+
Some(enum_id) => {
3444+
let variant_type = ty::enum_variant_with_id(tcx,
3445+
enum_id,
3446+
class_id);
3447+
format!("struct variant `{}::{}` has no field named `{}`",
3448+
actual, variant_type.name.as_str(),
3449+
token::get_ident(field.ident.node))
3450+
}
3451+
None => {
3452+
format!("structure `{}` has no field named `{}`",
3453+
actual,
3454+
token::get_ident(field.ident.node))
3455+
}
3456+
},
3457+
struct_ty,
3458+
None);
34473459
error_happened = true;
34483460
}
34493461
Some((_, true)) => {
@@ -3524,7 +3536,8 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
35243536
struct_substs,
35253537
class_fields[],
35263538
fields,
3527-
base_expr.is_none());
3539+
base_expr.is_none(),
3540+
None);
35283541
if ty::type_is_error(fcx.node_ty(id)) {
35293542
struct_type = ty::mk_err();
35303543
}
@@ -3566,7 +3579,8 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
35663579
substitutions,
35673580
variant_fields[],
35683581
fields,
3569-
true);
3582+
true,
3583+
Some(enum_id));
35703584
fcx.write_ty(id, enum_type);
35713585
}
35723586

src/test/compile-fail/issue-19922.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
enum Homura {
12+
Akemi { madoka: () }
13+
}
14+
15+
fn main() {
16+
let homura = Homura::Akemi { kaname: () };
17+
//~^ ERROR struct variant `Homura::Akemi` has no field named `kaname`
18+
}

0 commit comments

Comments
 (0)