Skip to content

Commit c9010bf

Browse files
committed
Fix error message on invalid field names for a struct variant
Fixes #19922.
1 parent cc19e33 commit c9010bf

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
@@ -3419,7 +3419,8 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
34193419
substitutions: subst::Substs<'tcx>,
34203420
field_types: &[ty::field_ty],
34213421
ast_fields: &[ast::Field],
3422-
check_completeness: bool) {
3422+
check_completeness: bool,
3423+
enum_id_opt: Option<ast::DefId>) {
34233424
let tcx = fcx.ccx.tcx;
34243425

34253426
let mut class_field_map = FnvHashMap::new();
@@ -3438,13 +3439,24 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
34383439
match pair {
34393440
None => {
34403441
fcx.type_error_message(
3441-
field.ident.span,
3442-
|actual| {
3443-
format!("structure `{}` has no field named `{}`",
3444-
actual, token::get_ident(field.ident.node))
3445-
},
3446-
struct_ty,
3447-
None);
3442+
field.ident.span,
3443+
|actual| match enum_id_opt {
3444+
Some(enum_id) => {
3445+
let variant_type = ty::enum_variant_with_id(tcx,
3446+
enum_id,
3447+
class_id);
3448+
format!("struct variant `{}::{}` has no field named `{}`",
3449+
actual, variant_type.name.as_str(),
3450+
token::get_ident(field.ident.node))
3451+
}
3452+
None => {
3453+
format!("structure `{}` has no field named `{}`",
3454+
actual,
3455+
token::get_ident(field.ident.node))
3456+
}
3457+
},
3458+
struct_ty,
3459+
None);
34483460
error_happened = true;
34493461
}
34503462
Some((_, true)) => {
@@ -3525,7 +3537,8 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
35253537
struct_substs,
35263538
class_fields.as_slice(),
35273539
fields,
3528-
base_expr.is_none());
3540+
base_expr.is_none(),
3541+
None);
35293542
if ty::type_is_error(fcx.node_ty(id)) {
35303543
struct_type = ty::mk_err();
35313544
}
@@ -3567,7 +3580,8 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
35673580
substitutions,
35683581
variant_fields.as_slice(),
35693582
fields,
3570-
true);
3583+
true,
3584+
Some(enum_id));
35713585
fcx.write_ty(id, enum_type);
35723586
}
35733587

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)