@@ -362,36 +362,16 @@ pub fn check_struct_pat_fields(pcx: &pat_ctxt,
362
362
}
363
363
}
364
364
365
- pub fn check_struct_pat ( pcx : & pat_ctxt , pat_id : ast:: NodeId , span : Span ,
366
- expected : ty:: t , path : & ast:: Path ,
365
+ pub fn check_struct_pat ( pcx : & pat_ctxt , _pat_id : ast:: NodeId , span : Span ,
366
+ _expected : ty:: t , _path : & ast:: Path ,
367
367
fields : & [ ast:: FieldPat ] , etc : bool ,
368
368
struct_id : ast:: DefId ,
369
369
substitutions : & subst:: Substs ) {
370
- let fcx = pcx. fcx ;
370
+ let _fcx = pcx. fcx ;
371
371
let tcx = pcx. fcx . ccx . tcx ;
372
372
373
373
let class_fields = ty:: lookup_struct_fields ( tcx, struct_id) ;
374
374
375
- // Check to ensure that the struct is the one specified.
376
- match tcx. def_map . borrow ( ) . find ( & pat_id) {
377
- Some ( & def:: DefStruct ( supplied_def_id) )
378
- if supplied_def_id == struct_id => {
379
- // OK.
380
- }
381
- Some ( & def:: DefStruct ( ..) ) | Some ( & def:: DefVariant ( ..) ) => {
382
- let name = pprust:: path_to_str ( path) ;
383
- tcx. sess
384
- . span_err ( span,
385
- format ! ( "mismatched types: expected `{}` but found \
386
- `{}`",
387
- fcx. infcx( ) . ty_to_str( expected) ,
388
- name) . as_slice ( ) ) ;
389
- }
390
- _ => {
391
- tcx. sess . span_bug ( span, "resolve didn't write in struct ID" ) ;
392
- }
393
- }
394
-
395
375
check_struct_pat_fields ( pcx, span, fields, class_fields, struct_id,
396
376
substitutions, etc) ;
397
377
}
@@ -535,6 +515,21 @@ pub fn check_pat(pcx: &pat_ctxt, pat: &ast::Pat, expected: ty::t) {
535
515
let mut error_happened = false ;
536
516
match * structure {
537
517
ty:: ty_struct( cid, ref substs) => {
518
+ // Verify that the pattern named the right structure.
519
+ let item_did = tcx. def_map . borrow ( ) . get ( & pat. id ) . def_id ( ) ;
520
+ let struct_did =
521
+ ty:: ty_to_def_id (
522
+ ty:: lookup_item_type ( tcx, item_did) . ty ) . unwrap ( ) ;
523
+ if struct_did != cid {
524
+ tcx. sess
525
+ . span_err ( path. span ,
526
+ format ! ( "`{}` does not name the \
527
+ structure `{}`",
528
+ pprust:: path_to_str( path) ,
529
+ fcx. infcx( )
530
+ . ty_to_str( expected) ) . as_slice ( ) )
531
+ }
532
+
538
533
check_struct_pat ( pcx, pat. id , pat. span , expected, path,
539
534
fields. as_slice ( ) , etc, cid, substs) ;
540
535
}
@@ -562,18 +557,22 @@ pub fn check_pat(pcx: &pat_ctxt, pat: &ast::Pat, expected: ty::t) {
562
557
"a structure pattern" . to_string ( ) ,
563
558
None ) ;
564
559
match tcx. def_map . borrow ( ) . find ( & pat. id ) {
565
- Some ( & def:: DefStruct ( supplied_def_id ) ) => {
560
+ Some ( def) => {
566
561
check_struct_pat ( pcx,
567
562
pat. id ,
568
563
pat. span ,
569
564
ty:: mk_err ( ) ,
570
565
path,
571
566
fields. as_slice ( ) ,
572
567
etc,
573
- supplied_def_id ,
568
+ def . def_id ( ) ,
574
569
& subst:: Substs :: empty ( ) ) ;
575
570
}
576
- _ => ( ) // Error, but we're already in an error case
571
+ None => {
572
+ tcx. sess . span_bug ( pat. span ,
573
+ "whoops, looks like resolve didn't \
574
+ write a def in here")
575
+ }
577
576
}
578
577
error_happened = true ;
579
578
}
0 commit comments