Skip to content

Commit f5776f8

Browse files
author
wickerwaka
committed
Detect a traits being used as structs in check_expr_with_unifier
Fixes rust-lang#16750 Fixes rust-lang#15812
1 parent 5419b2c commit f5776f8

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

src/librustc/diagnostics.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,5 +169,6 @@ register_diagnostics!(
169169
E0155,
170170
E0156,
171171
E0157,
172-
E0158
172+
E0158,
173+
E0159
173174
)

src/librustc/middle/typeck/check/mod.rs

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3463,6 +3463,22 @@ fn check_expr_with_unifier(fcx: &FnCtxt,
34633463
fcx.write_ty(id, enum_type);
34643464
}
34653465

3466+
fn check_struct_fields_on_error(fcx: &FnCtxt,
3467+
id: ast::NodeId,
3468+
fields: &[ast::Field],
3469+
base_expr: Option<Gc<ast::Expr>>) {
3470+
// Make sure to still write the types
3471+
// otherwise we might ICE
3472+
fcx.write_error(id);
3473+
for field in fields.iter() {
3474+
check_expr(fcx, &*field.expr);
3475+
}
3476+
match base_expr {
3477+
Some(ref base) => check_expr(fcx, &**base),
3478+
None => {}
3479+
}
3480+
}
3481+
34663482
type ExprCheckerWithTy = fn(&FnCtxt, &ast::Expr, ty::t);
34673483

34683484
let tcx = fcx.ccx.tcx;
@@ -3982,6 +3998,16 @@ fn check_expr_with_unifier(fcx: &FnCtxt,
39823998
variant_id, fields.as_slice());
39833999
enum_id
39844000
}
4001+
Some(def::DefTrait(def_id)) => {
4002+
span_err!(tcx.sess, path.span, E0159,
4003+
"`{}` is a trait not a structure",
4004+
pprust::path_to_string(path));
4005+
check_struct_fields_on_error(fcx,
4006+
id,
4007+
fields.as_slice(),
4008+
base_expr);
4009+
def_id
4010+
},
39854011
Some(def) => {
39864012
// Verify that this was actually a struct.
39874013
let typ = ty::lookup_item_type(fcx.ccx.tcx, def.def_id());
@@ -3998,17 +4024,10 @@ fn check_expr_with_unifier(fcx: &FnCtxt,
39984024
span_err!(tcx.sess, path.span, E0071,
39994025
"`{}` does not name a structure",
40004026
pprust::path_to_string(path));
4001-
4002-
// Make sure to still write the types
4003-
// otherwise we might ICE
4004-
fcx.write_error(id);
4005-
for field in fields.iter() {
4006-
check_expr(fcx, &*field.expr);
4007-
}
4008-
match base_expr {
4009-
Some(ref base) => check_expr(fcx, &**base),
4010-
None => {}
4011-
}
4027+
check_struct_fields_on_error(fcx,
4028+
id,
4029+
fields.as_slice(),
4030+
base_expr);
40124031
}
40134032
}
40144033

0 commit comments

Comments
 (0)