@@ -362,36 +362,16 @@ pub fn check_struct_pat_fields(pcx: &pat_ctxt,
362362 }
363363}
364364
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 ,
367367 fields : & [ ast:: FieldPat ] , etc : bool ,
368368 struct_id : ast:: DefId ,
369369 substitutions : & subst:: Substs ) {
370- let fcx = pcx. fcx ;
370+ let _fcx = pcx. fcx ;
371371 let tcx = pcx. fcx . ccx . tcx ;
372372
373373 let class_fields = ty:: lookup_struct_fields ( tcx, struct_id) ;
374374
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-
395375 check_struct_pat_fields ( pcx, span, fields, class_fields, struct_id,
396376 substitutions, etc) ;
397377}
@@ -535,6 +515,21 @@ pub fn check_pat(pcx: &pat_ctxt, pat: &ast::Pat, expected: ty::t) {
535515 let mut error_happened = false ;
536516 match * structure {
537517 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+
538533 check_struct_pat ( pcx, pat. id , pat. span , expected, path,
539534 fields. as_slice ( ) , etc, cid, substs) ;
540535 }
@@ -562,18 +557,22 @@ pub fn check_pat(pcx: &pat_ctxt, pat: &ast::Pat, expected: ty::t) {
562557 "a structure pattern" . to_string ( ) ,
563558 None ) ;
564559 match tcx. def_map . borrow ( ) . find ( & pat. id ) {
565- Some ( & def:: DefStruct ( supplied_def_id ) ) => {
560+ Some ( def) => {
566561 check_struct_pat ( pcx,
567562 pat. id ,
568563 pat. span ,
569564 ty:: mk_err ( ) ,
570565 path,
571566 fields. as_slice ( ) ,
572567 etc,
573- supplied_def_id ,
568+ def . def_id ( ) ,
574569 & subst:: Substs :: empty ( ) ) ;
575570 }
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+ }
577576 }
578577 error_happened = true ;
579578 }
0 commit comments