1 // Copyright 2012-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 * Conversion from AST representation of types to the ty.rs
13 * representation. The main routine here is `ast_ty_to_ty()`: each use
14 * is parameterized by an instance of `AstConv` and a `RegionScope`.
16 * The parameterization of `ast_ty_to_ty()` is because it behaves
17 * somewhat differently during the collect and check phases,
18 * particularly with respect to looking up the types of top-level
19 * items. In the collect phase, the crate context is used as the
20 * `AstConv` instance; in this phase, the `get_item_ty()` function
21 * triggers a recursive call to `ty_of_item()` (note that
22 * `ast_ty_to_ty()` will detect recursive types and report an error).
23 * In the check phase, when the FnCtxt is used as the `AstConv`,
24 * `get_item_ty()` just looks up the item type in `tcx.tcache`.
26 * The `RegionScope` trait controls what happens when the user does
27 * not specify a region in some location where a region is required
28 * (e.g., if the user writes `&Foo` as a type rather than `&'a Foo`).
29 * See the `rscope` module for more details.
31 * Unlike the `AstConv` trait, the region scope can change as we descend
32 * the type. This is to accommodate the fact that (a) fn types are binding
33 * scopes and (b) the default region may change. To understand case (a),
34 * consider something like:
36 * type foo = { x: &a.int, y: |&a.int| }
38 * The type of `x` is an error because there is no region `a` in scope.
39 * In the type of `y`, however, region `a` is considered a bound region
40 * as it does not already appear in scope.
42 * Case (b) says that if you have a type:
44 * type bar = fn(&foo, &a.foo)
45 * The fully expanded version of type bar is:
46 * type bar = fn(&'foo &, &a.foo<'a>)
47 * Note that the self region for the `foo` defaulted to `&` in the first
48 * case but `&a` in the second. Basically, defaults that appear inside
49 * an rptr (`&r.T`) use the region `r` that appears in the rptr.
53 use middle::const_eval;
54 use middle::subst::Subst;
55 use middle::ty::{substs};
56 use middle::ty::{ty_param_substs_and_ty};
58 use middle::typeck::rscope;
59 use middle::typeck::rscope::{RegionScope};
60 use middle::typeck::lookup_def_tcx;
61 use util::ppaux::Repr;
65 use syntax::{ast, ast_util};
66 use syntax::codemap::Span;
67 use syntax::owned_slice::OwnedSlice;
68 use syntax::print::pprust::{lifetime_to_str, path_to_str};
71 fn tcx<'a>(&'a self) -> &'a ty::ctxt;
72 fn get_item_ty(&self, id: ast::DefId) -> ty::ty_param_bounds_and_ty;
73 fn get_trait_def(&self, id: ast::DefId) -> Rc<ty::TraitDef>;
75 // what type should we use when a type is omitted?
76 fn ty_infer(&self, span: Span) -> ty::t;
79 pub fn ast_region_to_region(tcx: &ty::ctxt, lifetime: &ast::Lifetime)
81 let r = match tcx.named_region_map.find(&lifetime.id) {
83 // should have been recorded by the `resolve_lifetime` pass
84 tcx.sess.span_bug(lifetime.span, "unresolved lifetime");
87 Some(&ast::DefStaticRegion) => {
91 Some(&ast::DefLateBoundRegion(binder_id, _, id)) => {
92 ty::ReLateBound(binder_id, ty::BrNamed(ast_util::local_def(id),
96 Some(&ast::DefEarlyBoundRegion(index, id)) => {
97 ty::ReEarlyBound(id, index, lifetime.name)
100 Some(&ast::DefFreeRegion(scope_id, id)) => {
101 ty::ReFree(ty::FreeRegion {
103 bound_region: ty::BrNamed(ast_util::local_def(id),
109 debug!("ast_region_to_region(lifetime={} id={}) yields {}",
110 lifetime_to_str(lifetime),
111 lifetime.id, r.repr(tcx));
116 pub fn opt_ast_region_to_region<AC:AstConv,RS:RegionScope>(
120 opt_lifetime: &Option<ast::Lifetime>) -> ty::Region
122 let r = match *opt_lifetime {
123 Some(ref lifetime) => {
124 ast_region_to_region(this.tcx(), lifetime)
128 match rscope.anon_regions(default_span, 1) {
130 debug!("optional region in illegal location");
131 this.tcx().sess.span_err(
132 default_span, "missing lifetime specifier");
143 debug!("opt_ast_region_to_region(opt_lifetime={:?}) yields {}",
144 opt_lifetime.as_ref().map(|e| lifetime_to_str(e)),
150 fn ast_path_substs<AC:AstConv,RS:RegionScope>(
153 decl_generics: &ty::Generics,
154 self_ty: Option<ty::t>,
155 path: &ast::Path) -> ty::substs
158 * Given a path `path` that refers to an item `I` with the
159 * declared generics `decl_generics`, returns an appropriate
160 * set of substitutions for this particular reference to `I`.
163 let tcx = this.tcx();
165 // If the type is parameterized by the this region, then replace this
166 // region with the current anon region binding (in other words,
167 // whatever & would get replaced with).
168 let expected_num_region_params = decl_generics.region_param_defs().len();
169 let supplied_num_region_params = path.segments.last().unwrap().lifetimes.len();
170 let regions = if expected_num_region_params == supplied_num_region_params {
171 path.segments.last().unwrap().lifetimes.iter().map(
172 |l| ast_region_to_region(this.tcx(), l)).collect::<Vec<_>>()
175 rscope.anon_regions(path.span, expected_num_region_params);
177 if supplied_num_region_params != 0 || anon_regions.is_err() {
180 format!("wrong number of lifetime parameters: \
181 expected {} but found {}",
182 expected_num_region_params,
183 supplied_num_region_params));
187 Ok(v) => v.move_iter().collect(),
188 Err(()) => Vec::from_fn(expected_num_region_params,
189 |_| ty::ReStatic) // hokey
193 // Convert the type parameters supplied by the user.
194 let supplied_ty_param_count = path.segments.iter().flat_map(|s| s.types.iter()).len();
195 let formal_ty_param_count = decl_generics.type_param_defs().len();
196 let required_ty_param_count = decl_generics.type_param_defs().iter()
197 .take_while(|x| x.default.is_none())
199 if supplied_ty_param_count < required_ty_param_count {
200 let expected = if required_ty_param_count < formal_ty_param_count {
205 this.tcx().sess.span_fatal(path.span,
206 format!("wrong number of type arguments: {} {} but found {}",
207 expected, required_ty_param_count, supplied_ty_param_count));
208 } else if supplied_ty_param_count > formal_ty_param_count {
209 let expected = if required_ty_param_count < formal_ty_param_count {
214 this.tcx().sess.span_fatal(path.span,
215 format!("wrong number of type arguments: {} {} but found {}",
216 expected, formal_ty_param_count, supplied_ty_param_count));
219 if supplied_ty_param_count > required_ty_param_count
220 && !this.tcx().sess.features.default_type_params.get() {
221 this.tcx().sess.span_err(path.span, "default type parameters are \
222 experimental and possibly buggy");
223 this.tcx().sess.span_note(path.span, "add #![feature(default_type_params)] \
224 to the crate attributes to enable");
227 let tps = path.segments.iter().flat_map(|s| s.types.iter())
228 .map(|&a_t| ast_ty_to_ty(this, rscope, a_t))
231 let mut substs = substs {
232 regions: ty::NonerasedRegions(OwnedSlice::from_vec(regions)),
237 for param in decl_generics.type_param_defs()
238 .slice_from(supplied_ty_param_count).iter() {
239 let ty = param.default.unwrap().subst_spanned(tcx, &substs, Some(path.span));
246 pub fn ast_path_to_substs_and_ty<AC:AstConv,
252 -> ty_param_substs_and_ty {
253 let tcx = this.tcx();
254 let ty::ty_param_bounds_and_ty {
257 } = this.get_item_ty(did);
259 let substs = ast_path_substs(this, rscope, &generics, None, path);
260 let ty = ty::subst(tcx, &substs, decl_ty);
261 ty_param_substs_and_ty { substs: substs, ty: ty }
264 pub fn ast_path_to_trait_ref<AC:AstConv,RS:RegionScope>(
267 trait_def_id: ast::DefId,
268 self_ty: Option<ty::t>,
269 path: &ast::Path) -> Rc<ty::TraitRef> {
270 let trait_def = this.get_trait_def(trait_def_id);
271 Rc::new(ty::TraitRef {
272 def_id: trait_def_id,
273 substs: ast_path_substs(this, rscope, &trait_def.generics, self_ty, path)
277 pub fn ast_path_to_ty<AC:AstConv,RS:RegionScope>(
282 -> ty_param_substs_and_ty
284 // Look up the polytype of the item and then substitute the provided types
285 // for any type/region parameters.
286 let ty::ty_param_substs_and_ty {
289 } = ast_path_to_substs_and_ty(this, rscope, did, path);
290 ty_param_substs_and_ty { substs: substs, ty: ty }
293 pub static NO_REGIONS: uint = 1;
294 pub static NO_TPS: uint = 2;
296 fn check_path_args(tcx: &ty::ctxt,
299 if (flags & NO_TPS) != 0u {
300 if !path.segments.iter().all(|s| s.types.is_empty()) {
303 "type parameters are not allowed on this type");
307 if (flags & NO_REGIONS) != 0u {
308 if !path.segments.last().unwrap().lifetimes.is_empty() {
311 "region parameters are not allowed on this type");
316 pub fn ast_ty_to_prim_ty(tcx: &ty::ctxt, ast_ty: &ast::Ty) -> Option<ty::t> {
318 ast::TyPath(ref path, _, id) => {
319 let a_def = match tcx.def_map.borrow().find(&id) {
320 None => tcx.sess.span_bug(
321 ast_ty.span, format!("unbound path {}", path_to_str(path))),
325 ast::DefPrimTy(nty) => {
328 check_path_args(tcx, path, NO_TPS | NO_REGIONS);
332 check_path_args(tcx, path, NO_TPS | NO_REGIONS);
336 check_path_args(tcx, path, NO_TPS | NO_REGIONS);
337 Some(ty::mk_mach_int(it))
339 ast::TyUint(uit) => {
340 check_path_args(tcx, path, NO_TPS | NO_REGIONS);
341 Some(ty::mk_mach_uint(uit))
343 ast::TyFloat(ft) => {
344 if ft == ast::TyF128 && !tcx.sess.features.quad_precision_float.get() {
345 tcx.sess.span_err(path.span, "quadruple precision floats are \
346 missing complete runtime support");
347 tcx.sess.span_note(path.span, "add \
348 #[feature(quad_precision_float)] \
349 to the crate attributes to enable");
351 check_path_args(tcx, path, NO_TPS | NO_REGIONS);
352 Some(ty::mk_mach_float(ft))
355 tcx.sess.span_err(ast_ty.span,
356 "bare `str` is not a type");
357 // return /something/ so they can at least get more errors
358 Some(ty::mk_uniq(tcx, ty::mk_str(tcx)))
369 /// Converts the given AST type to a built-in type. A "built-in type" is, at
370 /// present, either a core numeric type, a string, or `Box`.
371 pub fn ast_ty_to_builtin_ty<AC:AstConv,
377 match ast_ty_to_prim_ty(this.tcx(), ast_ty) {
378 Some(typ) => return Some(typ),
383 ast::TyPath(ref path, _, id) => {
384 let a_def = match this.tcx().def_map.borrow().find(&id) {
385 None => this.tcx().sess.span_bug(
386 ast_ty.span, format!("unbound path {}", path_to_str(path))),
390 // FIXME(#12938): This is a hack until we have full support for
393 ast::DefTy(did) | ast::DefStruct(did)
394 if Some(did) == this.tcx().lang_items.owned_box() => {
397 .flat_map(|s| s.types.iter())
402 "`Box` has only one type parameter")
405 for inner_ast_type in path.segments
407 .flat_map(|s| s.types.iter()) {
408 let mt = ast::MutTy {
410 mutbl: ast::MutImmutable,
412 return Some(mk_pointer(this,
417 match ty::get(typ).sty {
422 "`Box<str>` is not a type");
425 ty::ty_vec(_, None) => {
429 "`Box<[T]>` is not a type");
432 _ => ty::mk_uniq(this.tcx(), typ),
436 this.tcx().sess.span_bug(path.span,
437 "not enough type parameters \
438 supplied to `Box<T>`")
453 fn ast_ty_to_mt<AC:AstConv, RS:RegionScope>(this: &AC,
455 ty: &ast::Ty) -> ty::mt {
456 ty::mt {ty: ast_ty_to_ty(this, rscope, ty), mutbl: ast::MutImmutable}
459 // Handle `~`, `Box`, and `&` being able to mean strs and vecs.
460 // If a_seq_ty is a str or a vec, make it a str/vec.
461 // Also handle first-class trait types.
462 fn mk_pointer<AC:AstConv,
466 a_seq_ty: &ast::MutTy,
468 constr: |ty::t| -> ty::t)
470 let tcx = this.tcx();
471 debug!("mk_pointer(ptr_ty={:?})", ptr_ty);
473 match a_seq_ty.ty.node {
475 let mut mt = ast_ty_to_mt(this, rscope, ty);
476 if a_seq_ty.mutbl == ast::MutMutable {
477 mt.mutbl = ast::MutMutable;
479 return constr(ty::mk_vec(tcx, mt, None));
481 ast::TyPath(ref path, ref bounds, id) => {
482 // Note that the "bounds must be empty if path is not a trait"
483 // restriction is enforced in the below case for ty_path, which
484 // will run after this as long as the path isn't a trait.
485 match tcx.def_map.borrow().find(&id) {
486 Some(&ast::DefPrimTy(ast::TyStr)) => {
487 check_path_args(tcx, path, NO_TPS | NO_REGIONS);
490 return constr(ty::mk_str(tcx));
493 return ty::mk_str_slice(tcx, r, ast::MutImmutable);
495 _ => tcx.sess.span_err(path.span,
496 format!("managed strings are not supported")),
499 Some(&ast::DefTrait(trait_def_id)) => {
500 let result = ast_path_to_trait_ref(
501 this, rscope, trait_def_id, None, path);
502 let trait_store = match ptr_ty {
503 Uniq => ty::UniqTraitStore,
505 ty::RegionTraitStore(r, a_seq_ty.mutbl)
510 "~trait or &trait are the only supported \
511 forms of casting-to-trait");
515 let bounds = conv_builtin_bounds(this.tcx(), bounds, trait_store);
516 return ty::mk_trait(tcx,
518 result.substs.clone(),
528 constr(ast_ty_to_ty(this, rscope, a_seq_ty.ty))
531 // Parses the programmer's textual representation of a type into our
532 // internal notion of a type.
533 pub fn ast_ty_to_ty<AC:AstConv, RS:RegionScope>(
534 this: &AC, rscope: &RS, ast_ty: &ast::Ty) -> ty::t {
536 let tcx = this.tcx();
538 let mut ast_ty_to_ty_cache = tcx.ast_ty_to_ty_cache.borrow_mut();
539 match ast_ty_to_ty_cache.find(&ast_ty.id) {
540 Some(&ty::atttce_resolved(ty)) => return ty,
541 Some(&ty::atttce_unresolved) => {
542 tcx.sess.span_fatal(ast_ty.span,
543 "illegal recursive type; insert an enum \
544 or struct in the cycle, if this is \
547 None => { /* go on */ }
549 ast_ty_to_ty_cache.insert(ast_ty.id, ty::atttce_unresolved);
550 drop(ast_ty_to_ty_cache);
552 let typ = ast_ty_to_builtin_ty(this, rscope, ast_ty).unwrap_or_else(|| {
554 ast::TyNil => ty::mk_nil(),
555 ast::TyBot => ty::mk_bot(),
557 let mt = ast::MutTy { ty: ty, mutbl: ast::MutImmutable };
558 mk_pointer(this, rscope, &mt, Box, |ty| ty::mk_box(tcx, ty))
561 let mt = ast::MutTy { ty: ty, mutbl: ast::MutImmutable };
562 mk_pointer(this, rscope, &mt, Uniq,
563 |ty| ty::mk_uniq(tcx, ty))
566 tcx.sess.span_err(ast_ty.span, "bare `[]` is not a type");
567 // return /something/ so they can at least get more errors
568 let vec_ty = ty::mk_vec(tcx, ast_ty_to_mt(this, rscope, ty), None);
569 ty::mk_uniq(tcx, vec_ty)
571 ast::TyPtr(ref mt) => {
572 ty::mk_ptr(tcx, ty::mt {
573 ty: ast_ty_to_ty(this, rscope, mt.ty),
577 ast::TyRptr(ref region, ref mt) => {
578 let r = opt_ast_region_to_region(this, rscope, ast_ty.span, region);
579 debug!("ty_rptr r={}", r.repr(this.tcx()));
580 mk_pointer(this, rscope, mt, RPtr(r),
581 |ty| ty::mk_rptr(tcx, r, ty::mt {ty: ty, mutbl: mt.mutbl}))
583 ast::TyTup(ref fields) => {
584 let flds = fields.iter()
585 .map(|&t| ast_ty_to_ty(this, rscope, t))
587 ty::mk_tup(tcx, flds)
589 ast::TyBareFn(ref bf) => {
590 if bf.decl.variadic && bf.abi != abi::C {
591 tcx.sess.span_err(ast_ty.span,
592 "variadic function must have C calling convention");
594 ty::mk_bare_fn(tcx, ty_of_bare_fn(this, ast_ty.id, bf.fn_style,
597 ast::TyClosure(ref f, ref region) => {
599 // resolve the function bound region in the original region
600 // scope `rscope`, not the scope of the function parameters
601 let bound_region = opt_ast_region_to_region(this, rscope,
602 ast_ty.span, region);
604 let store = ty::RegionTraitStore(bound_region, ast::MutMutable);
606 // Use corresponding trait store to figure out default bounds
607 // if none were specified.
608 let bounds = conv_builtin_bounds(this.tcx(), &f.bounds, store);
610 let fn_decl = ty_of_closure(this,
618 ty::mk_closure(tcx, fn_decl)
620 ast::TyProc(ref f) => {
621 // Use corresponding trait store to figure out default bounds
622 // if none were specified.
623 let bounds = conv_builtin_bounds(this.tcx(), &f.bounds, ty::UniqTraitStore);
625 let fn_decl = ty_of_closure(this,
633 ty::mk_closure(tcx, fn_decl)
635 ast::TyPath(ref path, ref bounds, id) => {
636 let a_def = match tcx.def_map.borrow().find(&id) {
637 None => tcx.sess.span_bug(
638 ast_ty.span, format!("unbound path {}", path_to_str(path))),
641 // Kind bounds on path types are only supported for traits.
643 // But don't emit the error if the user meant to do a trait anyway.
644 ast::DefTrait(..) => { },
645 _ if bounds.is_some() =>
646 tcx.sess.span_err(ast_ty.span,
647 "kind bounds can only be used on trait types"),
651 ast::DefTrait(_) => {
652 let path_str = path_to_str(path);
655 format!("reference to trait `{name}` where a type is expected; \
656 try `~{name}` or `&{name}`", name=path_str));
659 ast::DefTy(did) | ast::DefStruct(did) => {
660 ast_path_to_ty(this, rscope, did, path).ty
662 ast::DefTyParam(id, n) => {
663 check_path_args(tcx, path, NO_TPS | NO_REGIONS);
664 ty::mk_param(tcx, n, id)
666 ast::DefSelfTy(id) => {
667 // n.b.: resolve guarantees that the this type only appears in a
668 // trait, which we rely upon in various places when creating
670 check_path_args(tcx, path, NO_TPS | NO_REGIONS);
671 let did = ast_util::local_def(id);
672 ty::mk_self(tcx, did)
675 tcx.sess.span_fatal(ast_ty.span,
676 format!("found module name used as a type: {}",
677 tcx.map.node_to_str(id.node)));
679 ast::DefPrimTy(_) => {
680 fail!("DefPrimTy arm missed in previous ast_ty_to_prim_ty call");
683 tcx.sess.span_fatal(ast_ty.span,
684 format!("found value name used as a type: {:?}", a_def));
688 ast::TyFixedLengthVec(ty, e) => {
689 match const_eval::eval_const_expr_partial(tcx, e) {
692 const_eval::const_int(i) =>
693 ty::mk_vec(tcx, ast_ty_to_mt(this, rscope, ty),
695 const_eval::const_uint(i) =>
696 ty::mk_vec(tcx, ast_ty_to_mt(this, rscope, ty),
700 ast_ty.span, "expected constant expr for vector length");
707 format!("expected constant expr for vector length: {}", *r));
711 ast::TyTypeof(_e) => {
712 tcx.sess.span_bug(ast_ty.span, "typeof is reserved but unimplemented");
715 // TyInfer also appears as the type of arguments or return
716 // values in a ExprFnBlock or ExprProc, or as the type of
717 // local variables. Both of these cases are handled specially
718 // and will not descend into this routine.
719 this.ty_infer(ast_ty.span)
724 tcx.ast_ty_to_ty_cache.borrow_mut().insert(ast_ty.id, ty::atttce_resolved(typ));
728 pub fn ty_of_arg<AC: AstConv, RS: RegionScope>(this: &AC, rscope: &RS, a: &ast::Arg,
729 expected_ty: Option<ty::t>) -> ty::t {
731 ast::TyInfer if expected_ty.is_some() => expected_ty.unwrap(),
732 ast::TyInfer => this.ty_infer(a.ty.span),
733 _ => ast_ty_to_ty(this, rscope, a.ty),
738 untransformed_self_ty: ty::t,
739 explicit_self: ast::ExplicitSelf
742 pub fn ty_of_method<AC:AstConv>(
745 fn_style: ast::FnStyle,
746 untransformed_self_ty: ty::t,
747 explicit_self: ast::ExplicitSelf,
748 decl: &ast::FnDecl) -> ty::BareFnTy {
749 ty_of_method_or_bare_fn(this, id, fn_style, abi::Rust, Some(SelfInfo {
750 untransformed_self_ty: untransformed_self_ty,
751 explicit_self: explicit_self
755 pub fn ty_of_bare_fn<AC:AstConv>(this: &AC, id: ast::NodeId,
756 fn_style: ast::FnStyle, abi: abi::Abi,
757 decl: &ast::FnDecl) -> ty::BareFnTy {
758 ty_of_method_or_bare_fn(this, id, fn_style, abi, None, decl)
761 fn ty_of_method_or_bare_fn<AC:AstConv>(this: &AC, id: ast::NodeId,
762 fn_style: ast::FnStyle, abi: abi::Abi,
763 opt_self_info: Option<SelfInfo>,
764 decl: &ast::FnDecl) -> ty::BareFnTy {
765 debug!("ty_of_method_or_bare_fn");
767 // new region names that appear inside of the fn decl are bound to
768 // that function type
769 let rb = rscope::BindingRscope::new(id);
771 let self_ty = opt_self_info.and_then(|self_info| {
772 match self_info.explicit_self.node {
773 ast::SelfStatic => None,
775 Some(self_info.untransformed_self_ty)
777 ast::SelfRegion(ref lifetime, mutability) => {
779 opt_ast_region_to_region(this, &rb,
780 self_info.explicit_self.span,
782 Some(ty::mk_rptr(this.tcx(), region,
783 ty::mt {ty: self_info.untransformed_self_ty,
787 Some(ty::mk_uniq(this.tcx(), self_info.untransformed_self_ty))
792 // HACK(eddyb) replace the fake self type in the AST with the actual type.
793 let input_tys = if self_ty.is_some() {
794 decl.inputs.slice_from(1)
796 decl.inputs.as_slice()
798 let input_tys = input_tys.iter().map(|a| ty_of_arg(this, &rb, a, None));
800 let self_and_input_tys = self_ty.move_iter().chain(input_tys).collect();
802 let output_ty = match decl.output.node {
803 ast::TyInfer => this.ty_infer(decl.output.span),
804 _ => ast_ty_to_ty(this, &rb, decl.output)
807 return ty::BareFnTy {
812 inputs: self_and_input_tys,
814 variadic: decl.variadic
819 pub fn ty_of_closure<AC:AstConv>(
822 fn_style: ast::FnStyle,
823 onceness: ast::Onceness,
824 bounds: ty::BuiltinBounds,
825 store: ty::TraitStore,
827 expected_sig: Option<ty::FnSig>)
830 debug!("ty_of_fn_decl");
832 // new region names that appear inside of the fn decl are bound to
833 // that function type
834 let rb = rscope::BindingRscope::new(id);
836 let input_tys = decl.inputs.iter().enumerate().map(|(i, a)| {
837 let expected_arg_ty = expected_sig.as_ref().and_then(|e| {
838 // no guarantee that the correct number of expected args
840 if i < e.inputs.len() {
841 Some(*e.inputs.get(i))
846 ty_of_arg(this, &rb, a, expected_arg_ty)
849 let expected_ret_ty = expected_sig.map(|e| e.output);
850 let output_ty = match decl.output.node {
851 ast::TyInfer if expected_ret_ty.is_some() => expected_ret_ty.unwrap(),
852 ast::TyInfer => this.ty_infer(decl.output.span),
853 _ => ast_ty_to_ty(this, &rb, decl.output)
861 sig: ty::FnSig {binder_id: id,
864 variadic: decl.variadic}
868 fn conv_builtin_bounds(tcx: &ty::ctxt, ast_bounds: &Option<OwnedSlice<ast::TyParamBound>>,
869 store: ty::TraitStore)
870 -> ty::BuiltinBounds {
871 //! Converts a list of bounds from the AST into a `BuiltinBounds`
872 //! struct. Reports an error if any of the bounds that appear
873 //! in the AST refer to general traits and not the built-in traits
874 //! like `Send`. Used to translate the bounds that
875 //! appear in closure and trait types, where only builtin bounds are
877 //! If no bounds were specified, we choose a "default" bound based on
878 //! the allocation type of the fn/trait, as per issue #7264. The user can
879 //! override this with an empty bounds list, e.g. "Box<fn:()>" or
882 match (ast_bounds, store) {
883 (&Some(ref bound_vec), _) => {
884 let mut builtin_bounds = ty::EmptyBuiltinBounds();
885 for ast_bound in bound_vec.iter() {
887 ast::TraitTyParamBound(ref b) => {
888 match lookup_def_tcx(tcx, b.path.span, b.ref_id) {
889 ast::DefTrait(trait_did) => {
890 if ty::try_add_builtin_trait(tcx, trait_did,
891 &mut builtin_bounds) {
899 format!("only the builtin traits can be used \
900 as closure or object bounds"));
902 ast::StaticRegionTyParamBound => {
903 builtin_bounds.add(ty::BoundStatic);
905 ast::OtherRegionTyParamBound(span) => {
906 if !tcx.sess.features.issue_5723_bootstrap.get() {
909 format!("only the 'static lifetime is \
917 // &'static Trait is sugar for &'static Trait:'static.
918 (&None, ty::RegionTraitStore(ty::ReStatic, _)) => {
919 let mut set = ty::EmptyBuiltinBounds(); set.add(ty::BoundStatic); set
921 // No bounds are automatically applied for &'r Trait or ~Trait
922 (&None, ty::RegionTraitStore(..)) |
923 (&None, ty::UniqTraitStore) => ty::EmptyBuiltinBounds(),