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.
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.
12 use check::{FnCtxt, Inherited, blank_fn_ctxt, vtable, regionck};
17 use middle::ty::{self, Ty};
18 use middle::ty::liberate_late_bound_regions;
19 use middle::ty_fold::{TypeFolder, TypeFoldable, super_fold_ty};
20 use util::ppaux::Repr;
22 use std::collections::HashSet;
24 use syntax::ast_util::{local_def};
26 use syntax::codemap::Span;
28 use syntax::visit::Visitor;
30 pub struct CheckTypeWellFormedVisitor<'ccx, 'tcx:'ccx> {
31 ccx: &'ccx CrateCtxt<'ccx, 'tcx>,
32 cache: HashSet<Ty<'tcx>>
35 impl<'ccx, 'tcx> CheckTypeWellFormedVisitor<'ccx, 'tcx> {
36 pub fn new(ccx: &'ccx CrateCtxt<'ccx, 'tcx>) -> CheckTypeWellFormedVisitor<'ccx, 'tcx> {
37 CheckTypeWellFormedVisitor { ccx: ccx, cache: HashSet::new() }
40 /// Checks that the field types (in a struct def'n) or argument types (in an enum def'n) are
41 /// well-formed, meaning that they do not require any constraints not declared in the struct
42 /// definition itself. For example, this definition would be illegal:
44 /// struct Ref<'a, T> { x: &'a T }
46 /// because the type did not declare that `T:'a`.
48 /// We do this check as a pre-pass before checking fn bodies because if these constraints are
49 /// not included it frequently leads to confusing errors in fn bodies. So it's better to check
51 fn check_item_well_formed(&mut self, item: &ast::Item) {
53 debug!("check_item_well_formed(it.id={}, it.ident={})",
55 ty::item_path_str(ccx.tcx, local_def(item.id)));
58 ast::ItemImpl(..) => {
59 self.check_impl(item);
62 self.check_item_type(item);
64 ast::ItemStatic(..) => {
65 self.check_item_type(item);
67 ast::ItemConst(..) => {
68 self.check_item_type(item);
70 ast::ItemStruct(ref struct_def, _) => {
71 self.check_type_defn(item, |fcx| {
72 vec![struct_variant(fcx, &**struct_def)]
75 ast::ItemEnum(ref enum_def, _) => {
76 self.check_type_defn(item, |fcx| {
77 enum_variants(fcx, enum_def)
84 fn with_fcx<F>(&mut self, item: &ast::Item, mut f: F) where
85 F: for<'fcx> FnMut(&mut CheckTypeWellFormedVisitor<'ccx, 'tcx>, &FnCtxt<'fcx, 'tcx>),
88 let item_def_id = local_def(item.id);
89 let type_scheme = ty::lookup_item_type(ccx.tcx, item_def_id);
90 reject_non_type_param_bounds(ccx.tcx, item.span, &type_scheme.generics);
92 ty::construct_parameter_environment(ccx.tcx,
93 &type_scheme.generics,
95 let inh = Inherited::new(ccx.tcx, param_env);
96 let fcx = blank_fn_ctxt(ccx, &inh, ty::FnConverging(type_scheme.ty), item.id);
98 vtable::select_all_fcx_obligations_or_error(&fcx);
99 regionck::regionck_item(&fcx, item);
102 /// In a type definition, we check that to ensure that the types of the fields are well-formed.
103 fn check_type_defn<F>(&mut self, item: &ast::Item, mut lookup_fields: F) where
104 F: for<'fcx> FnMut(&FnCtxt<'fcx, 'tcx>) -> Vec<AdtVariant<'tcx>>,
106 self.with_fcx(item, |this, fcx| {
107 let variants = lookup_fields(fcx);
108 let mut bounds_checker = BoundsChecker::new(fcx,
110 region::CodeExtent::from_node_id(item.id),
111 Some(&mut this.cache));
112 for variant in variants.iter() {
113 for field in variant.fields.iter() {
114 // Regions are checked below.
115 bounds_checker.check_traits_in_ty(field.ty);
118 // For DST, all intermediate types must be sized.
119 if variant.fields.len() > 0 {
120 for field in variant.fields.init().iter() {
121 fcx.register_builtin_bound(
124 traits::ObligationCause::new(field.span,
126 traits::FieldSized));
131 let field_tys: Vec<Ty> =
132 variants.iter().flat_map(|v| v.fields.iter().map(|f| f.ty)).collect();
134 regionck::regionck_ensure_component_tys_wf(
135 fcx, item.span, field_tys.as_slice());
139 fn check_item_type(&mut self,
142 self.with_fcx(item, |this, fcx| {
143 let mut bounds_checker = BoundsChecker::new(fcx,
145 region::CodeExtent::from_node_id(item.id),
146 Some(&mut this.cache));
148 let type_scheme = ty::lookup_item_type(fcx.tcx(), local_def(item.id));
149 let item_ty = fcx.instantiate_type_scheme(item.span,
150 &fcx.inh.param_env.free_substs,
153 bounds_checker.check_traits_in_ty(item_ty);
157 fn check_impl(&mut self,
160 self.with_fcx(item, |this, fcx| {
161 let item_scope = region::CodeExtent::from_node_id(item.id);
163 let mut bounds_checker = BoundsChecker::new(fcx,
166 Some(&mut this.cache));
168 // Find the impl self type as seen from the "inside" --
169 // that is, with all type parameters converted from bound
171 let self_ty = ty::node_id_to_type(fcx.tcx(), item.id);
172 let self_ty = fcx.instantiate_type_scheme(item.span,
173 &fcx.inh.param_env.free_substs,
176 bounds_checker.check_traits_in_ty(self_ty);
178 // Similarly, obtain an "inside" reference to the trait
179 // that the impl implements.
180 let trait_ref = match ty::impl_trait_ref(fcx.tcx(), local_def(item.id)) {
185 let trait_ref = fcx.instantiate_type_scheme(item.span,
186 &fcx.inh.param_env.free_substs,
189 // There are special rules that apply to drop.
191 fcx.tcx().lang_items.drop_trait() == Some(trait_ref.def_id) &&
192 !attr::contains_name(item.attrs.as_slice(), "unsafe_destructor")
195 ty::ty_struct(def_id, _) |
196 ty::ty_enum(def_id, _) => {
197 check_struct_safe_for_destructor(fcx, item.span, def_id);
200 // Coherence already reports an error in this case.
205 if fcx.tcx().lang_items.copy_trait() == Some(trait_ref.def_id) {
206 // This is checked in coherence.
210 // We are stricter on the trait-ref in an impl than the
211 // self-type. In particular, we enforce region
212 // relationships. The reason for this is that (at least
213 // presently) "applying" an impl does not require that the
214 // application site check the well-formedness constraints on the
215 // trait reference. Instead, this is done at the impl site.
216 // Arguably this is wrong and we should treat the trait-reference
217 // the same way as we treat the self-type.
218 bounds_checker.check_trait_ref(&*trait_ref);
221 traits::ObligationCause::new(
224 traits::ItemObligation(trait_ref.def_id));
226 // Find the supertrait bounds. This will add `int:Bar`.
227 let poly_trait_ref = ty::Binder(trait_ref);
228 let predicates = ty::predicates_for_trait_ref(fcx.tcx(), &poly_trait_ref);
229 for predicate in predicates.into_iter() {
230 fcx.register_predicate(traits::Obligation::new(cause.clone(), predicate));
236 // Reject any predicates that do not involve a type parameter.
237 fn reject_non_type_param_bounds<'tcx>(tcx: &ty::ctxt<'tcx>,
239 generics: &ty::Generics<'tcx>) {
240 for predicate in generics.predicates.iter() {
242 &ty::Predicate::Trait(ty::Binder(ref tr)) => {
243 let self_ty = tr.self_ty();
244 if !self_ty.walk().any(|t| is_ty_param(t)) {
247 format!("cannot bound type `{}`, where clause \
248 bounds may only be attached to types involving \
250 self_ty.repr(tcx)).as_slice())
257 fn is_ty_param(ty: ty::Ty) -> bool {
259 &ty::sty::ty_param(_) => true,
265 impl<'ccx, 'tcx, 'v> Visitor<'v> for CheckTypeWellFormedVisitor<'ccx, 'tcx> {
266 fn visit_item(&mut self, i: &ast::Item) {
267 self.check_item_well_formed(i);
268 visit::walk_item(self, i);
272 pub struct BoundsChecker<'cx,'tcx:'cx> {
273 fcx: &'cx FnCtxt<'cx,'tcx>,
275 scope: region::CodeExtent,
277 cache: Option<&'cx mut HashSet<Ty<'tcx>>>,
280 impl<'cx,'tcx> BoundsChecker<'cx,'tcx> {
281 pub fn new(fcx: &'cx FnCtxt<'cx,'tcx>,
283 scope: region::CodeExtent,
284 cache: Option<&'cx mut HashSet<Ty<'tcx>>>)
285 -> BoundsChecker<'cx,'tcx> {
286 BoundsChecker { fcx: fcx, span: span, scope: scope,
287 cache: cache, binding_count: 0 }
290 /// Given a trait ref like `A : Trait<B>`, where `Trait` is defined as (say):
292 /// trait Trait<B:OtherTrait> : Copy { ... }
294 /// This routine will check that `B : OtherTrait` and `A : Trait<B>`. It will also recursively
295 /// check that the types `A` and `B` are well-formed.
297 /// Note that it does not (currently, at least) check that `A : Copy` (that check is delegated
298 /// to the point where impl `A : Trait<B>` is implemented).
299 pub fn check_trait_ref(&mut self, trait_ref: &ty::TraitRef<'tcx>) {
300 let trait_def = ty::lookup_trait_def(self.fcx.tcx(), trait_ref.def_id);
302 let bounds = self.fcx.instantiate_bounds(self.span, trait_ref.substs, &trait_def.generics);
304 self.fcx.add_obligations_for_parameters(
305 traits::ObligationCause::new(
308 traits::ItemObligation(trait_ref.def_id)),
311 for &ty in trait_ref.substs.types.iter() {
312 self.check_traits_in_ty(ty);
316 pub fn check_ty(&mut self, ty: Ty<'tcx>) {
320 fn check_traits_in_ty(&mut self, ty: Ty<'tcx>) {
321 // When checking types outside of a type def'n, we ignore
322 // region obligations. See discussion below in fold_ty().
323 self.binding_count += 1;
325 self.binding_count -= 1;
329 impl<'cx,'tcx> TypeFolder<'tcx> for BoundsChecker<'cx,'tcx> {
330 fn tcx(&self) -> &ty::ctxt<'tcx> {
334 fn fold_binder<T>(&mut self, binder: &ty::Binder<T>) -> ty::Binder<T>
335 where T : TypeFoldable<'tcx> + Repr<'tcx>
337 self.binding_count += 1;
338 let value = liberate_late_bound_regions(self.fcx.tcx(), self.scope, binder);
339 debug!("BoundsChecker::fold_binder: late-bound regions replaced: {}",
340 value.repr(self.tcx()));
341 let value = value.fold_with(self);
342 self.binding_count -= 1;
346 fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
347 debug!("BoundsChecker t={}",
351 Some(ref mut cache) => {
352 if !cache.insert(t) {
353 // Already checked this type! Don't check again.
362 ty::ty_struct(type_id, substs) |
363 ty::ty_enum(type_id, substs) => {
364 let type_scheme = ty::lookup_item_type(self.fcx.tcx(), type_id);
365 let bounds = self.fcx.instantiate_bounds(self.span, substs, &type_scheme.generics);
367 if self.binding_count == 0 {
368 self.fcx.add_obligations_for_parameters(
369 traits::ObligationCause::new(self.span,
371 traits::ItemObligation(type_id)),
374 // There are two circumstances in which we ignore
375 // region obligations.
377 // The first is when we are inside of a closure
378 // type. This is because in that case the region
379 // obligations for the parameter types are things
380 // that the closure body gets to assume and the
381 // caller must prove at the time of call. In other
382 // words, if there is a type like `<'a, 'b> | &'a
383 // &'b int |`, it is well-formed, and caller will
384 // have to show that `'b : 'a` at the time of
387 // The second is when we are checking for
388 // well-formedness outside of a type def'n or fn
389 // body. This is for a similar reason: in general,
390 // we only do WF checking for regions in the
391 // result of expressions and type definitions, so
392 // to as allow for implicit where clauses.
394 // (I believe we should do the same for traits, but
395 // that will require an RFC. -nmatsakis)
396 let bounds = filter_to_trait_obligations(bounds);
397 self.fcx.add_obligations_for_parameters(
398 traits::ObligationCause::new(self.span,
400 traits::ItemObligation(type_id)),
404 self.fold_substs(substs);
407 super_fold_ty(self, t);
411 t // we're not folding to produce a new type, so just return `t` here
415 ///////////////////////////////////////////////////////////////////////////
418 struct AdtVariant<'tcx> {
419 fields: Vec<AdtField<'tcx>>,
422 struct AdtField<'tcx> {
427 fn struct_variant<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
428 struct_def: &ast::StructDef)
429 -> AdtVariant<'tcx> {
434 let field_ty = ty::node_id_to_type(fcx.tcx(), field.node.id);
435 let field_ty = fcx.instantiate_type_scheme(field.span,
436 &fcx.inh.param_env.free_substs,
438 AdtField { ty: field_ty, span: field.span }
441 AdtVariant { fields: fields }
444 fn enum_variants<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
445 enum_def: &ast::EnumDef)
446 -> Vec<AdtVariant<'tcx>> {
447 enum_def.variants.iter()
449 match variant.node.kind {
450 ast::TupleVariantKind(ref args) if args.len() > 0 => {
451 let ctor_ty = ty::node_id_to_type(fcx.tcx(), variant.node.id);
453 // the regions in the argument types come from the
454 // enum def'n, and hence will all be early bound
456 ty::assert_no_late_bound_regions(
457 fcx.tcx(), &ty::ty_fn_args(ctor_ty));
460 fields: args.iter().enumerate().map(|(index, arg)| {
461 let arg_ty = arg_tys[index];
463 fcx.instantiate_type_scheme(variant.span,
464 &fcx.inh.param_env.free_substs,
473 ast::TupleVariantKind(_) => {
478 ast::StructVariantKind(ref struct_def) => {
479 struct_variant(fcx, &**struct_def)
486 fn filter_to_trait_obligations<'tcx>(bounds: ty::GenericBounds<'tcx>)
487 -> ty::GenericBounds<'tcx>
489 let mut result = ty::GenericBounds::empty();
490 for (space, _, predicate) in bounds.predicates.iter_enumerated() {
492 ty::Predicate::Trait(..) |
493 ty::Predicate::Projection(..) => {
494 result.predicates.push(space, predicate.clone())
496 ty::Predicate::Equate(..) |
497 ty::Predicate::TypeOutlives(..) |
498 ty::Predicate::RegionOutlives(..) => {
505 ///////////////////////////////////////////////////////////////////////////
506 // Special drop trait checking
508 fn check_struct_safe_for_destructor<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
510 struct_did: ast::DefId) {
511 let struct_tpt = ty::lookup_item_type(fcx.tcx(), struct_did);
512 if struct_tpt.generics.has_type_params(subst::TypeSpace)
513 || struct_tpt.generics.has_region_params(subst::TypeSpace)
515 span_err!(fcx.tcx().sess, span, E0141,
516 "cannot implement a destructor on a structure \
517 with type parameters");
518 span_note!(fcx.tcx().sess, span,
519 "use \"#[unsafe_destructor]\" on the implementation \
520 to force the compiler to allow this");