|
| 1 | +// Copyright 2014-2015 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 | +use check::regionck::{Rcx}; |
| 12 | + |
| 13 | +use middle::infer; |
| 14 | +use middle::region; |
| 15 | +use middle::subst; |
| 16 | +use middle::ty::{self, Ty}; |
| 17 | +use util::ppaux::{Repr}; |
| 18 | + |
| 19 | +use syntax::codemap::Span; |
| 20 | + |
| 21 | +pub fn check_safety_of_destructor_if_necessary<'a, 'tcx>(rcx: &mut Rcx<'a, 'tcx>, |
| 22 | + typ: ty::Ty<'tcx>, |
| 23 | + span: Span, |
| 24 | + scope: region::CodeExtent) { |
| 25 | + debug!("check_safety_of_destructor_if_necessary typ: {} scope: {:?}", |
| 26 | + typ.repr(rcx.tcx()), scope); |
| 27 | + |
| 28 | + // types that have been traversed so far by `traverse_type_if_unseen` |
| 29 | + let mut breadcrumbs: Vec<Ty<'tcx>> = Vec::new(); |
| 30 | + |
| 31 | + iterate_over_potentially_unsafe_regions_in_type( |
| 32 | + rcx, |
| 33 | + &mut breadcrumbs, |
| 34 | + typ, |
| 35 | + span, |
| 36 | + scope, |
| 37 | + false, |
| 38 | + 0); |
| 39 | +} |
| 40 | + |
| 41 | +fn constrain_region_for_destructor_safety(rcx: &mut Rcx, |
| 42 | + region: ty::Region, |
| 43 | + inner_scope: region::CodeExtent, |
| 44 | + span: Span) { |
| 45 | + debug!("constrain_region_for_destructor_safety region: {:?} inner_scope: {:?}", |
| 46 | + region, inner_scope); |
| 47 | + |
| 48 | + // Ignore bound regions. |
| 49 | + match region { |
| 50 | + ty::ReEarlyBound(..) | ty::ReLateBound(..) => return, |
| 51 | + ty::ReFree(_) | ty::ReScope(_) | ty::ReStatic | |
| 52 | + ty::ReInfer(_) | ty::ReEmpty => {} |
| 53 | + } |
| 54 | + |
| 55 | + // Get the parent scope. |
| 56 | + let parent_inner_region = |
| 57 | + match rcx.tcx().region_maps.opt_encl_scope(inner_scope) { |
| 58 | + Some(parent_inner_scope) => ty::ReScope(parent_inner_scope), |
| 59 | + None => |
| 60 | + rcx.tcx().sess.span_bug( |
| 61 | + span, format!("no enclosing scope found for inner_scope: {:?}", |
| 62 | + inner_scope).as_slice()), |
| 63 | + }; |
| 64 | + |
| 65 | + rcx.mk_subr(infer::SafeDestructor(span), |
| 66 | + parent_inner_region, |
| 67 | + region); |
| 68 | +} |
| 69 | + |
| 70 | +fn traverse_type_if_unseen<'a, 'tcx, P>(rcx: &mut Rcx<'a, 'tcx>, |
| 71 | + breadcrumbs: &mut Vec<Ty<'tcx>>, |
| 72 | + typ: ty::Ty<'tcx>, |
| 73 | + keep_going: P) -> bool where |
| 74 | + P: Fn(&mut Rcx<'a, 'tcx>, &mut Vec<Ty<'tcx>>) -> bool, |
| 75 | +{ |
| 76 | + // Avoid recursing forever. |
| 77 | + if !breadcrumbs.contains(&typ) { |
| 78 | + breadcrumbs.push(typ); |
| 79 | + let keep_going = keep_going(rcx, breadcrumbs); |
| 80 | + |
| 81 | + // You might be tempted to pop breadcrumbs here after the |
| 82 | + // `keep_going` call, but then you hit exponential time |
| 83 | + // blowup e.g. on compile-fail/huge-struct.rs. Instead, we |
| 84 | + // do not remove anything from the breadcrumbs vector |
| 85 | + // during any particular traversal, and instead clear it |
| 86 | + // after the whole traversal is done. |
| 87 | + |
| 88 | + keep_going |
| 89 | + } else { |
| 90 | + false |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | + |
| 95 | +fn iterate_over_potentially_unsafe_regions_in_type<'a, 'tcx>( |
| 96 | + rcx: &mut Rcx<'a, 'tcx>, |
| 97 | + breadcrumbs: &mut Vec<Ty<'tcx>>, |
| 98 | + typ: ty::Ty<'tcx>, |
| 99 | + span: Span, |
| 100 | + scope: region::CodeExtent, |
| 101 | + reachable_by_destructor: bool, |
| 102 | + depth: uint) |
| 103 | +{ |
| 104 | + ty::maybe_walk_ty(typ, |typ| { |
| 105 | + // Avoid recursing forever. |
| 106 | + traverse_type_if_unseen(rcx, breadcrumbs, typ, |rcx, breadcrumbs| { |
| 107 | + debug!("iterate_over_potentially_unsafe_regions_in_type \ |
| 108 | + {}typ: {} scope: {:?} reachable_by_destructor: {}", |
| 109 | + (0..depth).map(|_| ' ').collect::<String>(), |
| 110 | + typ.repr(rcx.tcx()), scope, reachable_by_destructor); |
| 111 | + |
| 112 | + let keep_going = match typ.sty { |
| 113 | + ty::ty_struct(structure_id, substitutions) => { |
| 114 | + let reachable_by_destructor = |
| 115 | + reachable_by_destructor || |
| 116 | + ty::has_dtor(rcx.tcx(), structure_id); |
| 117 | + |
| 118 | + let fields = |
| 119 | + ty::lookup_struct_fields(rcx.tcx(), |
| 120 | + structure_id); |
| 121 | + for field in fields.iter() { |
| 122 | + let field_type = |
| 123 | + ty::lookup_field_type(rcx.tcx(), |
| 124 | + structure_id, |
| 125 | + field.id, |
| 126 | + substitutions); |
| 127 | + iterate_over_potentially_unsafe_regions_in_type( |
| 128 | + rcx, |
| 129 | + breadcrumbs, |
| 130 | + field_type, |
| 131 | + span, |
| 132 | + scope, |
| 133 | + reachable_by_destructor, depth+1) |
| 134 | + } |
| 135 | + |
| 136 | + false |
| 137 | + } |
| 138 | + ty::ty_enum(enumeration_id, substitutions) => { |
| 139 | + let reachable_by_destructor = reachable_by_destructor || |
| 140 | + ty::has_dtor(rcx.tcx(), enumeration_id); |
| 141 | + |
| 142 | + let all_variant_info = |
| 143 | + ty::substd_enum_variants(rcx.tcx(), |
| 144 | + enumeration_id, |
| 145 | + substitutions); |
| 146 | + for variant_info in all_variant_info.iter() { |
| 147 | + for argument_type in variant_info.args.iter() { |
| 148 | + iterate_over_potentially_unsafe_regions_in_type( |
| 149 | + rcx, |
| 150 | + breadcrumbs, |
| 151 | + *argument_type, |
| 152 | + span, |
| 153 | + scope, |
| 154 | + reachable_by_destructor, depth+1) |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + false |
| 159 | + } |
| 160 | + ty::ty_rptr(region, _) => { |
| 161 | + if reachable_by_destructor { |
| 162 | + constrain_region_for_destructor_safety(rcx, |
| 163 | + *region, |
| 164 | + scope, |
| 165 | + span) |
| 166 | + } |
| 167 | + // Don't recurse, since references do not own their |
| 168 | + // contents. |
| 169 | + false |
| 170 | + } |
| 171 | + ty::ty_unboxed_closure(..) => { |
| 172 | + true |
| 173 | + } |
| 174 | + ty::ty_trait(ref trait_type) => { |
| 175 | + if reachable_by_destructor { |
| 176 | + match trait_type.principal.substs().regions { |
| 177 | + subst::NonerasedRegions(ref regions) => { |
| 178 | + for region in regions.iter() { |
| 179 | + constrain_region_for_destructor_safety( |
| 180 | + rcx, |
| 181 | + *region, |
| 182 | + scope, |
| 183 | + span) |
| 184 | + } |
| 185 | + } |
| 186 | + subst::ErasedRegions => {} |
| 187 | + } |
| 188 | + |
| 189 | + // FIXME (pnkfelix): Added by pnkfelix, but |
| 190 | + // need to double-check that this additional |
| 191 | + // constraint is necessary. |
| 192 | + constrain_region_for_destructor_safety( |
| 193 | + rcx, |
| 194 | + trait_type.bounds.region_bound, |
| 195 | + scope, |
| 196 | + span) |
| 197 | + } |
| 198 | + true |
| 199 | + } |
| 200 | + ty::ty_ptr(_) | ty::ty_bare_fn(..) => { |
| 201 | + // Don't recurse, since pointers, boxes, and bare |
| 202 | + // functions don't own instances of the types appearing |
| 203 | + // within them. |
| 204 | + false |
| 205 | + } |
| 206 | + ty::ty_bool | ty::ty_char | ty::ty_int(_) | ty::ty_uint(_) | |
| 207 | + ty::ty_float(_) | ty::ty_uniq(_) | ty::ty_str | |
| 208 | + ty::ty_vec(..) | ty::ty_tup(_) | ty::ty_param(_) | |
| 209 | + ty::ty_infer(_) | ty::ty_open(_) | ty::ty_err => true, |
| 210 | + |
| 211 | + ty::ty_projection(_) => { |
| 212 | + // We keep going, since we want to descend into |
| 213 | + // the substructure `Trait<..>` within the |
| 214 | + // projection `<T as Trait<..>>::N`. |
| 215 | + // |
| 216 | + // Furthermore, in the future, we are likely to |
| 217 | + // support higher-kinded projections (i.e. an |
| 218 | + // associated item that is parameterized over a |
| 219 | + // lifetime). When that is supported, we will need |
| 220 | + // to ensure that we constrain the input regions |
| 221 | + // accordingly (which might go here, or might end |
| 222 | + // up in some recursive part of the traversal). |
| 223 | + true |
| 224 | + } |
| 225 | + }; |
| 226 | + |
| 227 | + keep_going |
| 228 | + }) |
| 229 | + }); |
| 230 | +} |
0 commit comments