1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use middle::region;
use middle::subst;
use middle::subst::{Subst};
use middle::traits;
use middle::ty::{mod, Ty};
use middle::ty::liberate_late_bound_regions;
use middle::ty_fold::{TypeFolder, TypeFoldable};
use middle::typeck::astconv::AstConv;
use middle::typeck::check::{FnCtxt, Inherited, blank_fn_ctxt, vtable, regionck};
use middle::typeck::CrateCtxt;
use util::ppaux::Repr;

use std::collections::HashSet;
use syntax::ast;
use syntax::ast_util::{local_def};
use syntax::attr;
use syntax::codemap::Span;
use syntax::visit;
use syntax::visit::Visitor;

pub struct CheckTypeWellFormedVisitor<'ccx, 'tcx:'ccx> {
    ccx: &'ccx CrateCtxt<'ccx, 'tcx>,
    cache: HashSet<Ty<'tcx>>
}

impl<'ccx, 'tcx> CheckTypeWellFormedVisitor<'ccx, 'tcx> {
    pub fn new(ccx: &'ccx CrateCtxt<'ccx, 'tcx>) -> CheckTypeWellFormedVisitor<'ccx, 'tcx> {
        CheckTypeWellFormedVisitor { ccx: ccx, cache: HashSet::new() }
    }

    fn check_item_well_formed(&mut self, item: &ast::Item) {
        /*!
         * Checks that the field types (in a struct def'n) or
         * argument types (in an enum def'n) are well-formed,
         * meaning that they do not require any constraints not
         * declared in the struct definition itself.
         * For example, this definition would be illegal:
         *
         *     struct Ref<'a, T> { x: &'a T }
         *
         * because the type did not declare that `T:'a`.
         *
         * We do this check as a pre-pass before checking fn bodies
         * because if these constraints are not included it frequently
         * leads to confusing errors in fn bodies. So it's better to check
         * the types first.
         */

        let ccx = self.ccx;
        debug!("check_item_well_formed(it.id={}, it.ident={})",
               item.id,
               ty::item_path_str(ccx.tcx, local_def(item.id)));

        match item.node {
            ast::ItemImpl(..) => {
                self.check_impl(item);
            }
            ast::ItemFn(..) => {
                self.check_item_type(item);
            }
            ast::ItemStatic(..) => {
                self.check_item_type(item);
            }
            ast::ItemConst(..) => {
                self.check_item_type(item);
            }
            ast::ItemStruct(ref struct_def, _) => {
                self.check_type_defn(item, |fcx| {
                    vec![struct_variant(fcx, &**struct_def)]
                });
            }
            ast::ItemEnum(ref enum_def, _) => {
                self.check_type_defn(item, |fcx| {
                    enum_variants(fcx, enum_def)
                });
            }
            _ => {}
        }
    }

    fn with_fcx(&mut self,
                item: &ast::Item,
                f: for<'fcx> |&mut CheckTypeWellFormedVisitor<'ccx, 'tcx>,
                              &FnCtxt<'fcx, 'tcx>|) {
        let ccx = self.ccx;
        let item_def_id = local_def(item.id);
        let polytype = ty::lookup_item_type(ccx.tcx, item_def_id);
        let param_env =
            ty::construct_parameter_environment(ccx.tcx,
                                                item.span,
                                                &polytype.generics,
                                                item.id);
        let inh = Inherited::new(ccx.tcx, param_env);
        let fcx = blank_fn_ctxt(ccx, &inh, ty::FnConverging(polytype.ty), item.id);
        f(self, &fcx);
        vtable::select_all_fcx_obligations_or_error(&fcx);
        regionck::regionck_item(&fcx, item);
    }

    fn check_type_defn(&mut self,
                       item: &ast::Item,
                       lookup_fields: for<'fcx> |&FnCtxt<'fcx, 'tcx>|
                                                 -> Vec<AdtVariant<'tcx>>)
    {
        /*!
         * In a type definition, we check that to ensure that the types of the fields are
         * well-formed.
         */

        self.with_fcx(item, |this, fcx| {
            let variants = lookup_fields(fcx);
            let mut bounds_checker = BoundsChecker::new(fcx,
                                                        item.span,
                                                        region::CodeExtent::from_node_id(item.id),
                                                        Some(&mut this.cache));
            for variant in variants.iter() {
                for field in variant.fields.iter() {
                    // Regions are checked below.
                    bounds_checker.check_traits_in_ty(field.ty);
                }

                // For DST, all intermediate types must be sized.
                if variant.fields.len() > 0 {
                    for field in variant.fields.init().iter() {
                        let cause = traits::ObligationCause::new(field.span, traits::FieldSized);
                        let obligation = traits::obligation_for_builtin_bound(fcx.tcx(),
                                                                              cause,
                                                                              field.ty,
                                                                              ty::BoundSized);
                        match obligation {
                            Ok(obligation) => fcx.register_obligation(obligation),
                            _ => {}
                        }
                    }
                }
            }

            let field_tys: Vec<Ty> =
                variants.iter().flat_map(|v| v.fields.iter().map(|f| f.ty)).collect();

            regionck::regionck_ensure_component_tys_wf(
                fcx, item.span, field_tys.as_slice());
        });
    }

    fn check_item_type(&mut self,
                       item: &ast::Item)
    {
        self.with_fcx(item, |this, fcx| {
            let mut bounds_checker = BoundsChecker::new(fcx,
                                                        item.span,
                                                        region::CodeExtent::from_node_id(item.id),
                                                        Some(&mut this.cache));
            let polytype = ty::lookup_item_type(fcx.tcx(), local_def(item.id));
            let item_ty = polytype.ty.subst(fcx.tcx(), &fcx.inh.param_env.free_substs);
            bounds_checker.check_traits_in_ty(item_ty);
        });
    }

    fn check_impl(&mut self,
                  item: &ast::Item)
    {
        self.with_fcx(item, |this, fcx| {
            let item_scope = region::CodeExtent::from_node_id(item.id);

            let mut bounds_checker = BoundsChecker::new(fcx,
                                                        item.span,
                                                        item_scope,
                                                        Some(&mut this.cache));

            // Find the impl self type as seen from the "inside" --
            // that is, with all type parameters converted from bound
            // to free, and any late-bound regions on the impl
            // liberated.
            let self_ty = ty::node_id_to_type(fcx.tcx(), item.id);
            let self_ty = self_ty.subst(fcx.tcx(), &fcx.inh.param_env.free_substs);
            let self_ty = liberate_late_bound_regions(
                fcx.tcx(), item_scope, &ty::bind(self_ty)).value;

            bounds_checker.check_traits_in_ty(self_ty);

            // Similarly, obtain an "inside" reference to the trait
            // that the impl implements.
            let trait_ref = match ty::impl_trait_ref(fcx.tcx(), local_def(item.id)) {
                None => { return; }
                Some(t) => { t }
            };
            let trait_ref = (*trait_ref).subst(fcx.tcx(), &fcx.inh.param_env.free_substs);
            let trait_ref = liberate_late_bound_regions(fcx.tcx(), item_scope, &trait_ref);

            // There are special rules that apply to drop.
            if
                fcx.tcx().lang_items.drop_trait() == Some(trait_ref.def_id) &&
                !attr::contains_name(item.attrs.as_slice(), "unsafe_destructor")
            {
                match self_ty.sty {
                    ty::ty_struct(def_id, _) |
                    ty::ty_enum(def_id, _) => {
                        check_struct_safe_for_destructor(fcx, item.span, self_ty, def_id);
                    }
                    _ => {
                        // Coherence already reports an error in this case.
                    }
                }
            }

            // We are stricter on the trait-ref in an impl than the
            // self-type.  In particular, we enforce region
            // relationships. The reason for this is that (at least
            // presently) "appyling" an impl does not require that the
            // application site check the well-formedness constraints on the
            // trait reference. Instead, this is done at the impl site.
            // Arguably this is wrong and we should treat the trait-reference
            // the same way as we treat the self-type.
            bounds_checker.check_trait_ref(&trait_ref);

            let trait_def = ty::lookup_trait_def(fcx.tcx(), trait_ref.def_id);

            let cause =
                traits::ObligationCause::new(
                    item.span,
                    traits::ItemObligation(trait_ref.def_id));

            // Find the supertrait bounds. This will add `int:Bar`.
            //
            // FIXME -- This is a bit ill-factored. There is very similar
            // code in traits::util::obligations_for_generics.
            fcx.add_region_obligations_for_type_parameter(item.span,
                                                          &trait_def.bounds,
                                                          trait_ref.self_ty());
            for builtin_bound in trait_def.bounds.builtin_bounds.iter() {
                let obligation = traits::obligation_for_builtin_bound(fcx.tcx(),
                                                                      cause,
                                                                      trait_ref.self_ty(),
                                                                      builtin_bound);
                match obligation {
                    Ok (obligation) => fcx.register_obligation(obligation),
                    _ => {}
                }
            }
            for trait_bound in trait_def.bounds.trait_bounds.iter() {
                let trait_bound = trait_bound.subst(fcx.tcx(), &trait_ref.substs);
                fcx.register_obligation(
                    traits::Obligation::new(cause, trait_bound));
            }
        });
    }
}

impl<'ccx, 'tcx, 'v> Visitor<'v> for CheckTypeWellFormedVisitor<'ccx, 'tcx> {
    fn visit_item(&mut self, i: &ast::Item) {
        self.check_item_well_formed(i);
        visit::walk_item(self, i);
    }
}

pub struct BoundsChecker<'cx,'tcx:'cx> {
    fcx: &'cx FnCtxt<'cx,'tcx>,
    span: Span,
    scope: region::CodeExtent,
    binding_count: uint,
    cache: Option<&'cx mut HashSet<Ty<'tcx>>>,
}

impl<'cx,'tcx> BoundsChecker<'cx,'tcx> {
    pub fn new(fcx: &'cx FnCtxt<'cx,'tcx>,
               span: Span,
               scope: region::CodeExtent,
               cache: Option<&'cx mut HashSet<Ty<'tcx>>>)
               -> BoundsChecker<'cx,'tcx> {
        BoundsChecker { fcx: fcx, span: span, scope: scope,
                        cache: cache, binding_count: 0 }
    }

    pub fn check_trait_ref(&mut self, trait_ref: &ty::TraitRef<'tcx>) {
        /*!
         * Given a trait ref like `A : Trait<B>`, where `Trait` is
         * defined as (say):
         *
         *     trait Trait<B:OtherTrait> : Copy { ... }
         *
         * This routine will check that `B : OtherTrait` and `A :
         * Trait<B>`. It will also recursively check that the types
         * `A` and `B` are well-formed.
         *
         * Note that it does not (currently, at least)
         * check that `A : Copy` (that check is delegated to the point
         * where impl `A : Trait<B>` is implemented).
         */

        let trait_def = ty::lookup_trait_def(self.fcx.tcx(), trait_ref.def_id);

        let bounds = trait_def.generics.to_bounds(self.tcx(), &trait_ref.substs);
        self.fcx.add_obligations_for_parameters(
            traits::ObligationCause::new(
                self.span,
                traits::ItemObligation(trait_ref.def_id)),
            &trait_ref.substs,
            &bounds);

        for &ty in trait_ref.substs.types.iter() {
            self.check_traits_in_ty(ty);
        }
    }

    pub fn check_ty(&mut self, ty: Ty<'tcx>) {
        ty.fold_with(self);
    }

    fn check_traits_in_ty(&mut self, ty: Ty<'tcx>) {
        // When checking types outside of a type def'n, we ignore
        // region obligations. See discussion below in fold_ty().
        self.binding_count += 1;
        ty.fold_with(self);
        self.binding_count -= 1;
    }
}

impl<'cx,'tcx> TypeFolder<'tcx> for BoundsChecker<'cx,'tcx> {
    fn tcx(&self) -> &ty::ctxt<'tcx> {
        self.fcx.tcx()
    }

    fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
        debug!("BoundsChecker t={}",
               t.repr(self.tcx()));

        match self.cache {
            Some(ref mut cache) => {
                if !cache.insert(t) {
                    // Already checked this type! Don't check again.
                    debug!("cached");
                    return t;
                }
            }
            None => { }
        }

        match t.sty{
            ty::ty_struct(type_id, ref substs) |
            ty::ty_enum(type_id, ref substs) => {
                let polytype = ty::lookup_item_type(self.fcx.tcx(), type_id);

                if self.binding_count == 0 {
                    self.fcx.add_obligations_for_parameters(
                        traits::ObligationCause::new(self.span,
                                                     traits::ItemObligation(type_id)),
                        substs,
                        &polytype.generics.to_bounds(self.tcx(), substs));
                } else {
                    // There are two circumstances in which we ignore
                    // region obligations.
                    //
                    // The first is when we are inside of a closure
                    // type. This is because in that case the region
                    // obligations for the parameter types are things
                    // that the closure body gets to assume and the
                    // caller must prove at the time of call. In other
                    // words, if there is a type like `<'a, 'b> | &'a
                    // &'b int |`, it is well-formed, and caller will
                    // have to show that `'b : 'a` at the time of
                    // call.
                    //
                    // The second is when we are checking for
                    // well-formedness outside of a type def'n or fn
                    // body. This is for a similar reason: in general,
                    // we only do WF checking for regions in the
                    // result of expressions and type definitions, so
                    // to as allow for implicit where clauses.
                    //
                    // (I believe we should do the same for traits, but
                    // that will require an RFC. -nmatsakis)
                    self.fcx.add_trait_obligations_for_generics(
                        traits::ObligationCause::new(self.span,
                                                     traits::ItemObligation(type_id)),
                        substs,
                        &polytype.generics.to_bounds(self.tcx(), substs));
                }

                self.fold_substs(substs);
            }
            ty::ty_bare_fn(ty::BareFnTy{sig: ref fn_sig, ..}) |
            ty::ty_closure(box ty::ClosureTy{sig: ref fn_sig, ..}) => {
                self.binding_count += 1;

                let fn_sig = liberate_late_bound_regions(self.fcx.tcx(), self.scope, fn_sig);

                debug!("late-bound regions replaced: {}",
                       fn_sig.repr(self.tcx()));

                self.fold_fn_sig(&fn_sig);

                self.binding_count -= 1;
            }
            ref sty => {
                self.fold_sty(sty);
            }
        }

        t // we're not folding to produce a new type, so just return `t` here
    }
}

///////////////////////////////////////////////////////////////////////////
// ADT

struct AdtVariant<'tcx> {
    fields: Vec<AdtField<'tcx>>,
}

struct AdtField<'tcx> {
    ty: Ty<'tcx>,
    span: Span,
}

fn struct_variant<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
                            struct_def: &ast::StructDef)
                            -> AdtVariant<'tcx> {
    let fields =
        struct_def.fields
        .iter()
        .map(|field| {
            let field_ty = ty::node_id_to_type(fcx.tcx(), field.node.id);
            let field_ty = field_ty.subst(fcx.tcx(), &fcx.inh.param_env.free_substs);
            AdtField { ty: field_ty, span: field.span }
        })
        .collect();
    AdtVariant { fields: fields }
}

fn enum_variants<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
                           enum_def: &ast::EnumDef)
                           -> Vec<AdtVariant<'tcx>> {
    enum_def.variants.iter()
        .map(|variant| {
            match variant.node.kind {
                ast::TupleVariantKind(ref args) if args.len() > 0 => {
                    let ctor_ty = ty::node_id_to_type(fcx.tcx(), variant.node.id);
                    let arg_tys = ty::ty_fn_args(ctor_ty);
                    AdtVariant {
                        fields: args.iter().enumerate().map(|(index, arg)| {
                            let arg_ty = arg_tys[index];
                            let arg_ty = arg_ty.subst(fcx.tcx(), &fcx.inh.param_env.free_substs);
                            AdtField {
                                ty: arg_ty,
                                span: arg.ty.span
                            }
                        }).collect()
                    }
                }
                ast::TupleVariantKind(_) => {
                    AdtVariant {
                        fields: Vec::new()
                    }
                }
                ast::StructVariantKind(ref struct_def) => {
                    struct_variant(fcx, &**struct_def)
                }
            }
        })
        .collect()
}

///////////////////////////////////////////////////////////////////////////
// Special drop trait checking

fn check_struct_safe_for_destructor<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
                                              span: Span,
                                              self_ty: Ty<'tcx>,
                                              struct_did: ast::DefId) {
    let struct_tpt = ty::lookup_item_type(fcx.tcx(), struct_did);
    if !struct_tpt.generics.has_type_params(subst::TypeSpace)
        && !struct_tpt.generics.has_region_params(subst::TypeSpace)
    {
        let cause = traits::ObligationCause::new(span, traits::DropTrait);
        let obligation = traits::obligation_for_builtin_bound(fcx.tcx(),
                                                              cause,
                                                              self_ty,
                                                              ty::BoundSend);
        match obligation {
            Ok(obligation) => fcx.register_obligation(obligation),
            _ => {}
        }
    } else {
        span_err!(fcx.tcx().sess, span, E0141,
                  "cannot implement a destructor on a structure \
                       with type parameters");
            span_note!(fcx.tcx().sess, span,
                       "use \"#[unsafe_destructor]\" on the implementation \
                        to force the compiler to allow this");
    }
}