Skip to content

Commit 01364c4

Browse files
committed
auto merge of #16867 : wickerwaka/rust/ice-16750, r=alexcrichton
Not sure if this is addressing the root cause or just patching up a symptom. Also not sure if I should be adding a diagnostic code for this. Fixes #16750 Fixes #15812
2 parents 3768ef4 + d3d14d6 commit 01364c4

File tree

3 files changed

+49
-12
lines changed

3 files changed

+49
-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+
"use of trait `{}` as a struct constructor",
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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
trait TraitNotAStruct { }
12+
13+
fn main() {
14+
TraitNotAStruct{ value: 0 };
15+
//~^ ERROR: use of trait `TraitNotAStruct` as a struct constructor [E0159]
16+
}
17+

0 commit comments

Comments
 (0)