|
| 1 | +// Copyright 2014 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 infer::at::At; |
| 12 | +use infer::canonical::{Canonical, Canonicalize, QueryResult}; |
| 13 | +use infer::InferOk; |
| 14 | +use std::iter::FromIterator; |
| 15 | +use traits::query::CanonicalTyGoal; |
| 16 | +use ty::{self, Ty, TyCtxt}; |
| 17 | +use ty::subst::Kind; |
| 18 | +use std::rc::Rc; |
| 19 | + |
| 20 | +impl<'cx, 'gcx, 'tcx> At<'cx, 'gcx, 'tcx> { |
| 21 | + /// Given a type `ty` of some value being dropped, computes a set |
| 22 | + /// of "kinds" (types, regions) that must be outlive the execution |
| 23 | + /// of the destructor. These basically correspond to data that the |
| 24 | + /// destructor might access. This is used during regionck to |
| 25 | + /// impose "outlives" constraints on any lifetimes referenced |
| 26 | + /// within. |
| 27 | + /// |
| 28 | + /// The rules here are given by the "dropck" RFCs, notably [#1238] |
| 29 | + /// and [#1327]. This is a fixed-point computation, where we |
| 30 | + /// explore all the data that will be dropped (transitively) when |
| 31 | + /// a value of type `ty` is dropped. For each type T that will be |
| 32 | + /// dropped and which has a destructor, we must assume that all |
| 33 | + /// the types/regions of T are live during the destructor, unless |
| 34 | + /// they are marked with a special attribute (`#[may_dangle]`). |
| 35 | + /// |
| 36 | + /// [#1238]: https://github.com/rust-lang/rfcs/blob/master/text/1238-nonparametric-dropck.md |
| 37 | + /// [#1327]: https://github.com/rust-lang/rfcs/blob/master/text/1327-dropck-param-eyepatch.md |
| 38 | + pub fn dropck_outlives(&self, ty: Ty<'tcx>) -> InferOk<'tcx, Vec<Kind<'tcx>>> { |
| 39 | + debug!( |
| 40 | + "dropck_outlives(ty={:?}, param_env={:?})", |
| 41 | + ty, self.param_env, |
| 42 | + ); |
| 43 | + |
| 44 | + let tcx = self.infcx.tcx; |
| 45 | + let gcx = tcx.global_tcx(); |
| 46 | + let (c_ty, orig_values) = self.infcx.canonicalize_query(&self.param_env.and(ty)); |
| 47 | + let span = self.cause.span; |
| 48 | + match &gcx.dropck_outlives(c_ty) { |
| 49 | + Ok(result) if result.is_proven() => { |
| 50 | + match self.infcx.instantiate_query_result( |
| 51 | + self.cause, |
| 52 | + self.param_env, |
| 53 | + &orig_values, |
| 54 | + result, |
| 55 | + ) { |
| 56 | + Ok(InferOk { |
| 57 | + value: DropckOutlivesResult { kinds, overflows }, |
| 58 | + obligations, |
| 59 | + }) => { |
| 60 | + for overflow_ty in overflows.into_iter().take(1) { |
| 61 | + let mut err = struct_span_err!( |
| 62 | + tcx.sess, |
| 63 | + span, |
| 64 | + E0320, |
| 65 | + "overflow while adding drop-check rules for {}", |
| 66 | + self.infcx.resolve_type_vars_if_possible(&ty), |
| 67 | + ); |
| 68 | + err.note(&format!("overflowed on {}", overflow_ty)); |
| 69 | + err.emit(); |
| 70 | + } |
| 71 | + |
| 72 | + return InferOk { |
| 73 | + value: kinds, |
| 74 | + obligations, |
| 75 | + }; |
| 76 | + } |
| 77 | + |
| 78 | + Err(_) => { /* fallthrough to error-handling code below */ } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + _ => { /* fallthrough to error-handling code below */ } |
| 83 | + } |
| 84 | + |
| 85 | + // Errors and ambiuity in dropck occur in two cases: |
| 86 | + // - unresolved inference variables at the end of typeck |
| 87 | + // - non well-formed types where projections cannot be resolved |
| 88 | + // Either of these should hvae created an error before. |
| 89 | + tcx.sess |
| 90 | + .delay_span_bug(span, "dtorck encountered internal error"); |
| 91 | + return InferOk { |
| 92 | + value: vec![], |
| 93 | + obligations: vec![], |
| 94 | + }; |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +#[derive(Clone, Debug)] |
| 99 | +pub struct DropckOutlivesResult<'tcx> { |
| 100 | + pub kinds: Vec<Kind<'tcx>>, |
| 101 | + pub overflows: Vec<Ty<'tcx>>, |
| 102 | +} |
| 103 | + |
| 104 | +/// A set of constraints that need to be satisfied in order for |
| 105 | +/// a type to be valid for destruction. |
| 106 | +#[derive(Clone, Debug)] |
| 107 | +pub struct DtorckConstraint<'tcx> { |
| 108 | + /// Types that are required to be alive in order for this |
| 109 | + /// type to be valid for destruction. |
| 110 | + pub outlives: Vec<ty::subst::Kind<'tcx>>, |
| 111 | + |
| 112 | + /// Types that could not be resolved: projections and params. |
| 113 | + pub dtorck_types: Vec<Ty<'tcx>>, |
| 114 | + |
| 115 | + /// If, during the computation of the dtorck constraint, we |
| 116 | + /// overflow, that gets recorded here. The caller is expected to |
| 117 | + /// report an error. |
| 118 | + pub overflows: Vec<Ty<'tcx>>, |
| 119 | +} |
| 120 | + |
| 121 | +impl<'tcx> DtorckConstraint<'tcx> { |
| 122 | + pub fn empty() -> DtorckConstraint<'tcx> { |
| 123 | + DtorckConstraint { |
| 124 | + outlives: vec![], |
| 125 | + dtorck_types: vec![], |
| 126 | + overflows: vec![], |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +impl<'tcx> FromIterator<DtorckConstraint<'tcx>> for DtorckConstraint<'tcx> { |
| 132 | + fn from_iter<I: IntoIterator<Item = DtorckConstraint<'tcx>>>(iter: I) -> Self { |
| 133 | + let mut result = Self::empty(); |
| 134 | + |
| 135 | + for DtorckConstraint { |
| 136 | + outlives, |
| 137 | + dtorck_types, |
| 138 | + overflows, |
| 139 | + } in iter |
| 140 | + { |
| 141 | + result.outlives.extend(outlives); |
| 142 | + result.dtorck_types.extend(dtorck_types); |
| 143 | + result.overflows.extend(overflows); |
| 144 | + } |
| 145 | + |
| 146 | + result |
| 147 | + } |
| 148 | +} |
| 149 | +impl<'gcx: 'tcx, 'tcx> Canonicalize<'gcx, 'tcx> for ty::ParamEnvAnd<'tcx, Ty<'tcx>> { |
| 150 | + type Canonicalized = CanonicalTyGoal<'gcx>; |
| 151 | + |
| 152 | + fn intern( |
| 153 | + _gcx: TyCtxt<'_, 'gcx, 'gcx>, |
| 154 | + value: Canonical<'gcx, Self::Lifted>, |
| 155 | + ) -> Self::Canonicalized { |
| 156 | + value |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +BraceStructTypeFoldableImpl! { |
| 161 | + impl<'tcx> TypeFoldable<'tcx> for DropckOutlivesResult<'tcx> { |
| 162 | + kinds, overflows |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +BraceStructLiftImpl! { |
| 167 | + impl<'a, 'tcx> Lift<'tcx> for DropckOutlivesResult<'a> { |
| 168 | + type Lifted = DropckOutlivesResult<'tcx>; |
| 169 | + kinds, overflows |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +impl_stable_hash_for!(struct DropckOutlivesResult<'tcx> { |
| 174 | + kinds, overflows |
| 175 | +}); |
| 176 | + |
| 177 | +impl<'gcx: 'tcx, 'tcx> Canonicalize<'gcx, 'tcx> for QueryResult<'tcx, DropckOutlivesResult<'tcx>> { |
| 178 | + // we ought to intern this, but I'm too lazy just now |
| 179 | + type Canonicalized = Rc<Canonical<'gcx, QueryResult<'gcx, DropckOutlivesResult<'gcx>>>>; |
| 180 | + |
| 181 | + fn intern( |
| 182 | + _gcx: TyCtxt<'_, 'gcx, 'gcx>, |
| 183 | + value: Canonical<'gcx, Self::Lifted>, |
| 184 | + ) -> Self::Canonicalized { |
| 185 | + Rc::new(value) |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +impl_stable_hash_for!(struct DtorckConstraint<'tcx> { |
| 190 | + outlives, |
| 191 | + dtorck_types, |
| 192 | + overflows |
| 193 | +}); |
0 commit comments