Skip to content

Detect traits being used as structs in check_expr_with_unifier #16867

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 1, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/librustc/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,5 +169,6 @@ register_diagnostics!(
E0155,
E0156,
E0157,
E0158
E0158,
E0159
)
41 changes: 30 additions & 11 deletions src/librustc/middle/typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3463,6 +3463,22 @@ fn check_expr_with_unifier(fcx: &FnCtxt,
fcx.write_ty(id, enum_type);
}

fn check_struct_fields_on_error(fcx: &FnCtxt,
id: ast::NodeId,
fields: &[ast::Field],
base_expr: Option<Gc<ast::Expr>>) {
// Make sure to still write the types
// otherwise we might ICE
fcx.write_error(id);
for field in fields.iter() {
check_expr(fcx, &*field.expr);
}
match base_expr {
Some(ref base) => check_expr(fcx, &**base),
None => {}
}
}

type ExprCheckerWithTy = fn(&FnCtxt, &ast::Expr, ty::t);

let tcx = fcx.ccx.tcx;
Expand Down Expand Up @@ -3982,6 +3998,16 @@ fn check_expr_with_unifier(fcx: &FnCtxt,
variant_id, fields.as_slice());
enum_id
}
Some(def::DefTrait(def_id)) => {
span_err!(tcx.sess, path.span, E0159,
"`{}` is a trait not a structure",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this could be "use of trait {} as a struct constructor"

pprust::path_to_string(path));
check_struct_fields_on_error(fcx,
id,
fields.as_slice(),
base_expr);
def_id
},
Some(def) => {
// Verify that this was actually a struct.
let typ = ty::lookup_item_type(fcx.ccx.tcx, def.def_id());
Expand All @@ -3998,17 +4024,10 @@ fn check_expr_with_unifier(fcx: &FnCtxt,
span_err!(tcx.sess, path.span, E0071,
"`{}` does not name a structure",
pprust::path_to_string(path));

// Make sure to still write the types
// otherwise we might ICE
fcx.write_error(id);
for field in fields.iter() {
check_expr(fcx, &*field.expr);
}
match base_expr {
Some(ref base) => check_expr(fcx, &**base),
None => {}
}
check_struct_fields_on_error(fcx,
id,
fields.as_slice(),
base_expr);
}
}

Expand Down