@@ -156,6 +156,7 @@ pub fn check_pat_variant(pcx: &pat_ctxt, pat: @ast::pat, path: &ast::Path,
156
156
kind_name = "variant" ;
157
157
}
158
158
None => {
159
+ // See [Note-Type-error-reporting] in middle/typeck/infer/mod.rs
159
160
fcx. infcx ( ) . type_error_message_str_with_expected ( pat. span ,
160
161
|expected, actual| {
161
162
expected. map_move_default ( ~"", |e| {
@@ -199,6 +200,7 @@ pub fn check_pat_variant(pcx: &pat_ctxt, pat: @ast::pat, path: &ast::Path,
199
200
kind_name = "structure" ;
200
201
}
201
202
_ => {
203
+ // See [Note-Type-error-reporting] in middle/typeck/infer/mod.rs
202
204
fcx. infcx ( ) . type_error_message_str_with_expected ( pat. span ,
203
205
|expected, actual| {
204
206
expected. map_move_default ( ~"", |e| {
@@ -302,10 +304,13 @@ pub fn check_struct_pat_fields(pcx: &pat_ctxt,
302
304
}
303
305
None => {
304
306
let name = pprust::path_to_str(path, tcx.sess.intr());
307
+ // Check the pattern anyway, so that attempts to look
308
+ // up its type won't fail
309
+ check_pat(pcx, field.pat, ty::mk_err());
305
310
tcx.sess.span_err(span,
306
- fmt!(" struct `%s` does not have a field
307
- named `%s`" , name,
308
- tcx. sess. str_of( field. ident) ) ) ;
311
+ fmt!(" struct `%s` does not have a field named `%s`" ,
312
+ name,
313
+ tcx. sess. str_of( field. ident) ) ) ;
309
314
}
310
315
}
311
316
}
@@ -326,16 +331,17 @@ pub fn check_struct_pat_fields(pcx: &pat_ctxt,
326
331
pub fn check_struct_pat( pcx: & pat_ctxt, pat_id: ast:: NodeId , span: span,
327
332
expected: ty:: t, path: & ast:: Path ,
328
333
fields: & [ ast:: field_pat] , etc: bool ,
329
- class_id: ast:: def_id, substitutions: & ty:: substs) {
334
+ struct_id: ast:: def_id,
335
+ substitutions: & ty:: substs) {
330
336
let fcx = pcx. fcx;
331
337
let tcx = pcx. fcx. ccx. tcx;
332
338
333
- let class_fields = ty:: lookup_struct_fields( tcx, class_id ) ;
339
+ let class_fields = ty:: lookup_struct_fields( tcx, struct_id ) ;
334
340
335
341
// Check to ensure that the struct is the one specified.
336
342
match tcx. def_map. find( & pat_id) {
337
343
Some ( & ast:: def_struct( supplied_def_id) )
338
- if supplied_def_id == class_id => {
344
+ if supplied_def_id == struct_id => {
339
345
// OK.
340
346
}
341
347
Some ( & ast:: def_struct( * ) ) | Some ( & ast:: def_variant( * ) ) => {
@@ -346,11 +352,11 @@ pub fn check_struct_pat(pcx: &pat_ctxt, pat_id: ast::NodeId, span: span,
346
352
name) ) ;
347
353
}
348
354
_ => {
349
- tcx. sess. span_bug( span, "resolve didn't write in class " ) ;
355
+ tcx. sess. span_bug( span, "resolve didn't write in struct ID " ) ;
350
356
}
351
357
}
352
358
353
- check_struct_pat_fields( pcx, span, path, fields, class_fields, class_id ,
359
+ check_struct_pat_fields( pcx, span, path, fields, class_fields, struct_id ,
354
360
substitutions, etc) ;
355
361
}
356
362
@@ -499,9 +505,22 @@ pub fn check_pat(pcx: &pat_ctxt, pat: @ast::pat, expected: ty::t) {
499
505
substs);
500
506
}
501
507
_ => {
502
- tcx.sess.span_err(pat.span,
503
- fmt!(" mismatched types: expected `%s` but found struct ",
504
- fcx.infcx().ty_to_str(expected)));
508
+ // See [Note-Type-error-reporting] in middle/typeck/infer/mod.rs
509
+ fcx.infcx().type_error_message_str_with_expected(pat.span,
510
+ |expected, actual| {
511
+ expected.map_move_default(~" ", |e| {
512
+ fmt!(" mismatched types: expected `%s` but found %s",
513
+ e, actual)})},
514
+ Some(expected), ~" a structure pattern",
515
+ None);
516
+ match tcx.def_map.find(&pat.id) {
517
+ Some(&ast::def_struct(supplied_def_id)) => {
518
+ check_struct_pat(pcx, pat.id, pat.span, ty::mk_err(), path, *fields, etc,
519
+ supplied_def_id,
520
+ &ty::substs { self_ty: None, tps: ~[], regions: ty::ErasedRegions} );
521
+ }
522
+ _ => () // Error, but we're already in an error case
523
+ }
505
524
error_happened = true;
506
525
}
507
526
}
@@ -534,6 +553,7 @@ pub fn check_pat(pcx: &pat_ctxt, pat: @ast::pat, expected: ty::t) {
534
553
found: e_count}),
535
554
_ => ty::terr_mismatch
536
555
};
556
+ // See [Note-Type-error-reporting] in middle/typeck/infer/mod.rs
537
557
fcx.infcx().type_error_message_str_with_expected(pat.span, |expected, actual| {
538
558
expected.map_move_default(~" ", |e| {
539
559
fmt!(" mismatched types: expected `%s` but found %s",
@@ -581,6 +601,7 @@ pub fn check_pat(pcx: &pat_ctxt, pat: @ast::pat, expected: ty::t) {
581
601
for &elt in after.iter() {
582
602
check_pat(pcx, elt, ty::mk_err());
583
603
}
604
+ // See [Note-Type-error-reporting] in middle/typeck/infer/mod.rs
584
605
fcx.infcx().type_error_message_str_with_expected(
585
606
pat.span,
586
607
|expected, actual| {
@@ -639,6 +660,7 @@ pub fn check_pointer_pat(pcx: &pat_ctxt,
639
660
}
640
661
_ => {
641
662
check_pat(pcx, inner, ty::mk_err());
663
+ // See [Note-Type-error-reporting] in middle/typeck/infer/mod.rs
642
664
fcx.infcx().type_error_message_str_with_expected(
643
665
span,
644
666
|expected, actual| {
0 commit comments