]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc_typeck/check/dropck.rs
Auto merge of #30641 - tsion:match-range, r=eddyb
[rust.git] / src / librustc_typeck / check / dropck.rs
index 0fbe83674931c2bbf89be4ba3107840afcb4b1b9..0cf552b6efecb9165a4f9ba8f9ff75c324f5fdf4 100644 (file)
@@ -17,6 +17,7 @@
 use middle::subst::{self, Subst};
 use middle::traits;
 use middle::ty::{self, Ty};
+use util::nodemap::FnvHashSet;
 
 use syntax::ast;
 use syntax::codemap::{self, Span};
@@ -94,12 +95,13 @@ fn ensure_drop_params_and_item_params_correspond<'tcx>(
 
     if let Err(_) = infer::mk_eqty(&infcx, true, infer::TypeOrigin::Misc(drop_impl_span),
                                    named_type, fresh_impl_self_ty) {
-        span_err!(tcx.sess, drop_impl_span, E0366,
-                  "Implementations of Drop cannot be specialized");
         let item_span = tcx.map.span(self_type_node_id);
-        tcx.sess.span_note(item_span,
-                           "Use same sequence of generic type and region \
-                            parameters that is on the struct/enum definition");
+        struct_span_err!(tcx.sess, drop_impl_span, E0366,
+                         "Implementations of Drop cannot be specialized")
+            .span_note(item_span,
+                       "Use same sequence of generic type and region \
+                        parameters that is on the struct/enum definition")
+            .emit();
         return Err(());
     }
 
@@ -196,11 +198,12 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>(
 
         if !assumptions_in_impl_context.contains(&predicate) {
             let item_span = tcx.map.span(self_type_node_id);
-            span_err!(tcx.sess, drop_impl_span, E0367,
-                      "The requirement `{}` is added only by the Drop impl.", predicate);
-            tcx.sess.span_note(item_span,
-                               "The same requirement must be part of \
-                                the struct/enum definition");
+            struct_span_err!(tcx.sess, drop_impl_span, E0367,
+                             "The requirement `{}` is added only by the Drop impl.", predicate)
+                .span_note(item_span,
+                           "The same requirement must be part of \
+                            the struct/enum definition")
+                .emit();
         }
     }
 
@@ -279,7 +282,7 @@ pub fn check_safety_of_destructor_if_necessary<'a, 'tcx>(rcx: &mut Rcx<'a, 'tcx>
             rcx: rcx,
             span: span,
             parent_scope: parent_scope,
-            breadcrumbs: Vec::new(),
+            breadcrumbs: FnvHashSet()
         },
         TypeContext::Root,
         typ,
@@ -288,8 +291,8 @@ pub fn check_safety_of_destructor_if_necessary<'a, 'tcx>(rcx: &mut Rcx<'a, 'tcx>
         Ok(()) => {}
         Err(Error::Overflow(ref ctxt, ref detected_on_typ)) => {
             let tcx = rcx.tcx();
-            span_err!(tcx.sess, span, E0320,
-                      "overflow while adding drop-check rules for {}", typ);
+            let mut err = struct_span_err!(tcx.sess, span, E0320,
+                                           "overflow while adding drop-check rules for {}", typ);
             match *ctxt {
                 TypeContext::Root => {
                     // no need for an additional note if the overflow
@@ -310,7 +313,7 @@ pub fn check_safety_of_destructor_if_necessary<'a, 'tcx>(rcx: &mut Rcx<'a, 'tcx>
                         format!("`{}`", field)
                     };
                     span_note!(
-                        rcx.tcx().sess,
+                        &mut err,
                         span,
                         "overflowed on {} field {} type: {}",
                         variant_name,
@@ -318,6 +321,7 @@ pub fn check_safety_of_destructor_if_necessary<'a, 'tcx>(rcx: &mut Rcx<'a, 'tcx>
                         detected_on_typ);
                 }
             }
+            err.emit();
         }
     }
 }
@@ -340,7 +344,7 @@ enum TypeContext {
 struct DropckContext<'a, 'b: 'a, 'tcx: 'b> {
     rcx: &'a mut Rcx<'b, 'tcx>,
     /// types that have already been traversed
-    breadcrumbs: Vec<Ty<'tcx>>,
+    breadcrumbs: FnvHashSet<Ty<'tcx>>,
     /// span for error reporting
     span: Span,
     /// the scope reachable dtorck types must outlive
@@ -355,8 +359,6 @@ fn iterate_over_potentially_unsafe_regions_in_type<'a, 'b, 'tcx>(
     depth: usize) -> Result<(), Error<'tcx>>
 {
     let tcx = cx.rcx.tcx();
-    let ty = cx.rcx.infcx().resolve_type_and_region_vars_if_possible(&ty);
-
     // Issue #22443: Watch out for overflow. While we are careful to
     // handle regular types properly, non-regular ones cause problems.
     let recursion_limit = tcx.sess.recursion_limit.get();
@@ -367,19 +369,17 @@ fn iterate_over_potentially_unsafe_regions_in_type<'a, 'b, 'tcx>(
         return Err(Error::Overflow(context, ty))
     }
 
-    for breadcrumb in &mut cx.breadcrumbs {
-        *breadcrumb =
-            cx.rcx.infcx().resolve_type_and_region_vars_if_possible(breadcrumb);
-        if *breadcrumb == ty {
-            debug!("iterate_over_potentially_unsafe_regions_in_type \
-                   {}ty: {} scope: {:?} - cached",
-                   (0..depth).map(|_| ' ').collect::<String>(),
-                   ty, cx.parent_scope);
-            return Ok(()); // we already visited this type
-        }
-    }
-    cx.breadcrumbs.push(ty);
+    // canoncialize the regions in `ty` before inserting - infinitely many
+    // region variables can refer to the same region.
+    let ty = cx.rcx.infcx().resolve_type_and_region_vars_if_possible(&ty);
 
+    if !cx.breadcrumbs.insert(ty) {
+        debug!("iterate_over_potentially_unsafe_regions_in_type \
+               {}ty: {} scope: {:?} - cached",
+               (0..depth).map(|_| ' ').collect::<String>(),
+               ty, cx.parent_scope);
+        return Ok(()); // we already visited this type
+    }
     debug!("iterate_over_potentially_unsafe_regions_in_type \
            {}ty: {} scope: {:?}",
            (0..depth).map(|_| ' ').collect::<String>(),