Skip to content

Commit 54c76e6

Browse files
author
Jakub Bukaj
committed
Fix an ICE when using struct patterns with traits
Fixes #18986.
1 parent f092793 commit 54c76e6

File tree

3 files changed

+47
-10
lines changed

3 files changed

+47
-10
lines changed

src/librustc/diagnostics.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,5 +143,6 @@ register_diagnostics!(
143143
E0164,
144144
E0165,
145145
E0166,
146-
E0167
146+
E0167,
147+
E0168
147148
)

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

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -297,22 +297,38 @@ pub fn check_pat_struct(pcx: &pat_ctxt, pat: &ast::Pat,
297297
let tcx = pcx.fcx.ccx.tcx;
298298

299299
let def = tcx.def_map.borrow()[pat.id].clone();
300-
let def_type = ty::lookup_item_type(tcx, def.def_id());
301-
let (enum_def_id, variant_def_id) = match ty::get(def_type.ty).sty {
302-
ty::ty_struct(struct_def_id, _) =>
303-
(struct_def_id, struct_def_id),
304-
ty::ty_enum(enum_def_id, _) if def == def::DefVariant(enum_def_id, def.def_id(), true) =>
305-
(enum_def_id, def.def_id()),
306-
_ => {
300+
let (enum_def_id, variant_def_id) = match def {
301+
def::DefTrait(_) => {
307302
let name = pprust::path_to_string(path);
308-
span_err!(tcx.sess, pat.span, E0163,
309-
"`{}` does not name a struct or a struct variant", name);
303+
span_err!(tcx.sess, pat.span, E0168,
304+
"use of trait `{}` in a struct pattern", name);
310305
fcx.write_error(pat.id);
311306

312307
for field in fields.iter() {
313308
check_pat(pcx, &*field.node.pat, ty::mk_err());
314309
}
315310
return;
311+
},
312+
_ => {
313+
let def_type = ty::lookup_item_type(tcx, def.def_id());
314+
match ty::get(def_type.ty).sty {
315+
ty::ty_struct(struct_def_id, _) =>
316+
(struct_def_id, struct_def_id),
317+
ty::ty_enum(enum_def_id, _)
318+
if def == def::DefVariant(enum_def_id, def.def_id(), true) =>
319+
(enum_def_id, def.def_id()),
320+
_ => {
321+
let name = pprust::path_to_string(path);
322+
span_err!(tcx.sess, pat.span, E0163,
323+
"`{}` does not name a struct or a struct variant", name);
324+
fcx.write_error(pat.id);
325+
326+
for field in fields.iter() {
327+
check_pat(pcx, &*field.node.pat, ty::mk_err());
328+
}
329+
return;
330+
}
331+
}
316332
}
317333
};
318334

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
// aux-build:use_from_trait_xc.rs
12+
13+
extern crate use_from_trait_xc;
14+
pub use use_from_trait_xc::Trait;
15+
16+
fn main() {
17+
match () {
18+
Trait { x: 42u } => () //~ ERROR use of trait `Trait` in a struct pattern
19+
}
20+
}

0 commit comments

Comments
 (0)