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.
15 The collect phase of type check has the job of visiting all items,
16 determining their type, and writing that type into the `tcx.types`
17 table. Despite its name, this table does not really operate as a
18 *cache*, at least not for the types of items defined within the
19 current crate: we assume that after the collect phase, the types of
20 all local items will be present in the table.
22 Unlike most of the types that are present in Rust, the types computed
23 for each item are in fact type schemes. This means that they are
24 generic types that may have type parameters. TypeSchemes are
25 represented by a pair of `Generics` and `Ty`. Type
26 parameters themselves are represented as `ty_param()` instances.
28 The phasing of type conversion is somewhat complicated. There is no
29 clear set of phases we can enforce (e.g., converting traits first,
30 then types, or something like that) because the user can introduce
31 arbitrary interdependencies. So instead we generally convert things
32 lazilly and on demand, and include logic that checks for cycles.
33 Demand is driven by calls to `AstConv::get_item_type_scheme` or
34 `AstConv::lookup_trait_def`.
36 Currently, we "convert" types and traits in two phases (note that
37 conversion only affects the types of items / enum variants / methods;
38 it does not e.g. compute the types of individual expressions):
41 1. Trait/Type definitions
43 Conversion itself is done by simply walking each of the items in turn
44 and invoking an appropriate function (e.g., `trait_def_of_item` or
45 `convert_item`). However, it is possible that while converting an
46 item, we may need to compute the *type scheme* or *trait definition*
49 There are some shortcomings in this design:
50 - Because the item generics include defaults, cycles through type
51 parameter defaults are illegal even if those defaults are never
52 employed. This is not necessarily a bug.
56 use astconv::{AstConv, Bounds};
58 use constrained_type_params as ctp;
59 use middle::lang_items::SizedTraitLangItem;
60 use middle::const_val::ConstVal;
61 use middle::resolve_lifetime as rl;
62 use rustc::ty::subst::Substs;
63 use rustc::ty::{ToPredicate, ReprOptions};
64 use rustc::ty::{self, AdtKind, ToPolyTraitRef, Ty, TyCtxt};
65 use rustc::ty::maps::Providers;
66 use rustc::ty::util::IntTypeExt;
67 use util::nodemap::{NodeMap, FxHashMap};
69 use rustc_const_math::ConstInt;
71 use std::cell::RefCell;
72 use std::collections::BTreeMap;
74 use syntax::{abi, ast};
75 use syntax::codemap::Spanned;
76 use syntax::symbol::{Symbol, keywords};
77 use syntax_pos::{Span, DUMMY_SP};
79 use rustc::hir::{self, map as hir_map};
80 use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap};
81 use rustc::hir::def::{Def, CtorKind};
82 use rustc::hir::def_id::DefId;
84 ///////////////////////////////////////////////////////////////////////////
87 pub fn collect_item_types<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
88 let mut visitor = CollectItemTypesVisitor { tcx: tcx };
89 tcx.hir.krate().visit_all_item_likes(&mut visitor.as_deep_visitor());
92 pub fn provide(providers: &mut Providers) {
93 *providers = Providers {
98 type_param_predicates,
106 ///////////////////////////////////////////////////////////////////////////
108 /// Context specific to some particular item. This is what implements
109 /// AstConv. It has information about the predicates that are defined
110 /// on the trait. Unfortunately, this predicate information is
111 /// available in various different forms at various points in the
112 /// process. So we can't just store a pointer to e.g. the AST or the
113 /// parsed ty form, we have to be more flexible. To this end, the
114 /// `ItemCtxt` is parameterized by a `DefId` that it uses to satisfy
115 /// `get_type_parameter_bounds` requests, drawing the information from
116 /// the AST (`hir::Generics`), recursively.
117 struct ItemCtxt<'a,'tcx:'a> {
118 tcx: TyCtxt<'a, 'tcx, 'tcx>,
122 ///////////////////////////////////////////////////////////////////////////
124 struct CollectItemTypesVisitor<'a, 'tcx: 'a> {
125 tcx: TyCtxt<'a, 'tcx, 'tcx>
128 impl<'a, 'tcx> Visitor<'tcx> for CollectItemTypesVisitor<'a, 'tcx> {
129 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
130 NestedVisitorMap::OnlyBodies(&self.tcx.hir)
133 fn visit_item(&mut self, item: &'tcx hir::Item) {
134 convert_item(self.tcx, item.id);
135 intravisit::walk_item(self, item);
138 fn visit_generics(&mut self, generics: &'tcx hir::Generics) {
139 for param in &generics.ty_params {
140 if param.default.is_some() {
141 let def_id = self.tcx.hir.local_def_id(param.id);
142 self.tcx.item_type(def_id);
145 intravisit::walk_generics(self, generics);
148 fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
149 if let hir::ExprClosure(..) = expr.node {
150 let def_id = self.tcx.hir.local_def_id(expr.id);
151 self.tcx.item_generics(def_id);
152 self.tcx.item_type(def_id);
154 intravisit::walk_expr(self, expr);
157 fn visit_ty(&mut self, ty: &'tcx hir::Ty) {
158 if let hir::TyImplTrait(..) = ty.node {
159 let def_id = self.tcx.hir.local_def_id(ty.id);
160 self.tcx.item_generics(def_id);
161 self.tcx.item_predicates(def_id);
163 intravisit::walk_ty(self, ty);
166 fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem) {
167 convert_trait_item(self.tcx, trait_item.id);
168 intravisit::walk_trait_item(self, trait_item);
171 fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem) {
172 convert_impl_item(self.tcx, impl_item.id);
173 intravisit::walk_impl_item(self, impl_item);
177 ///////////////////////////////////////////////////////////////////////////
178 // Utility types and common code for the above passes.
180 impl<'a, 'tcx> ItemCtxt<'a, 'tcx> {
181 fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>, item_def_id: DefId)
182 -> ItemCtxt<'a,'tcx> {
185 item_def_id: item_def_id,
190 impl<'a,'tcx> ItemCtxt<'a,'tcx> {
191 fn to_ty(&self, ast_ty: &hir::Ty) -> Ty<'tcx> {
192 AstConv::ast_ty_to_ty(self, ast_ty)
196 impl<'a, 'tcx> AstConv<'tcx, 'tcx> for ItemCtxt<'a, 'tcx> {
197 fn tcx<'b>(&'b self) -> TyCtxt<'b, 'tcx, 'tcx> { self.tcx }
199 fn ast_ty_to_ty_cache(&self) -> &RefCell<NodeMap<Ty<'tcx>>> {
200 &self.tcx.ast_ty_to_ty_cache
203 fn get_type_parameter_bounds(&self,
206 -> ty::GenericPredicates<'tcx>
208 ty::queries::type_param_predicates::get(self.tcx, span, (self.item_def_id, def_id))
211 fn get_free_substs(&self) -> Option<&Substs<'tcx>> {
215 fn re_infer(&self, _span: Span, _def: Option<&ty::RegionParameterDef>)
216 -> Option<&'tcx ty::Region> {
220 fn ty_infer(&self, span: Span) -> Ty<'tcx> {
225 "the type placeholder `_` is not allowed within types on item signatures"
226 ).span_label(span, &format!("not allowed in type signatures"))
231 fn projected_ty_from_poly_trait_ref(&self,
233 poly_trait_ref: ty::PolyTraitRef<'tcx>,
234 item_name: ast::Name)
237 if let Some(trait_ref) = self.tcx().no_late_bound_regions(&poly_trait_ref) {
238 self.tcx().mk_projection(trait_ref, item_name)
240 // no late-bound regions, we can just ignore the binder
241 span_err!(self.tcx().sess, span, E0212,
242 "cannot extract an associated type from a higher-ranked trait bound \
248 fn normalize_ty(&self, _span: Span, ty: Ty<'tcx>) -> Ty<'tcx> {
249 // types in item signatures are not normalized, to avoid undue
254 fn set_tainted_by_errors(&self) {
255 // no obvious place to track this, just let it go
259 fn type_param_predicates<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
260 (item_def_id, def_id): (DefId, DefId))
261 -> ty::GenericPredicates<'tcx> {
262 use rustc::hir::map::*;
265 // In the AST, bounds can derive from two places. Either
266 // written inline like `<T:Foo>` or in a where clause like
269 let param_id = tcx.hir.as_local_node_id(def_id).unwrap();
270 let param_owner = tcx.hir.ty_param_owner(param_id);
271 let param_owner_def_id = tcx.hir.local_def_id(param_owner);
272 let generics = tcx.item_generics(param_owner_def_id);
273 let index = generics.type_param_to_index[&def_id.index];
274 let ty = tcx.mk_param(index, tcx.hir.ty_param_name(param_id));
276 // Don't look for bounds where the type parameter isn't in scope.
277 let parent = if item_def_id == param_owner_def_id {
280 tcx.item_generics(item_def_id).parent
283 let mut result = parent.map_or(ty::GenericPredicates {
287 let icx = ItemCtxt::new(tcx, parent);
288 icx.get_type_parameter_bounds(DUMMY_SP, def_id)
291 let item_node_id = tcx.hir.as_local_node_id(item_def_id).unwrap();
292 let ast_generics = match tcx.hir.get(item_node_id) {
293 NodeTraitItem(item) => {
295 TraitItemKind::Method(ref sig, _) => &sig.generics,
300 NodeImplItem(item) => {
302 ImplItemKind::Method(ref sig, _) => &sig.generics,
309 ItemFn(.., ref generics, _) |
310 ItemImpl(_, _, ref generics, ..) |
311 ItemTy(_, ref generics) |
312 ItemEnum(_, ref generics) |
313 ItemStruct(_, ref generics) |
314 ItemUnion(_, ref generics) => generics,
315 ItemTrait(_, ref generics, ..) => {
316 // Implied `Self: Trait` and supertrait bounds.
317 if param_id == item_node_id {
318 result.predicates.push(ty::TraitRef {
320 substs: Substs::identity_for_item(tcx, item_def_id)
329 NodeForeignItem(item) => {
331 ForeignItemFn(_, _, ref generics) => generics,
339 let icx = ItemCtxt::new(tcx, item_def_id);
340 result.predicates.extend(
341 icx.type_parameter_bounds_in_generics(ast_generics, param_id, ty));
345 impl<'a, 'tcx> ItemCtxt<'a, 'tcx> {
346 /// Find bounds from hir::Generics. This requires scanning through the
347 /// AST. We do this to avoid having to convert *all* the bounds, which
348 /// would create artificial cycles. Instead we can only convert the
349 /// bounds for a type parameter `X` if `X::Foo` is used.
350 fn type_parameter_bounds_in_generics(&self,
351 ast_generics: &hir::Generics,
352 param_id: ast::NodeId,
354 -> Vec<ty::Predicate<'tcx>>
357 ast_generics.ty_params
359 .filter(|p| p.id == param_id)
360 .flat_map(|p| p.bounds.iter())
361 .flat_map(|b| predicates_from_bound(self, ty, b));
363 let from_where_clauses =
364 ast_generics.where_clause
367 .filter_map(|wp| match *wp {
368 hir::WherePredicate::BoundPredicate(ref bp) => Some(bp),
371 .filter(|bp| is_param(self.tcx, &bp.bounded_ty, param_id))
372 .flat_map(|bp| bp.bounds.iter())
373 .flat_map(|b| predicates_from_bound(self, ty, b));
375 from_ty_params.chain(from_where_clauses).collect()
379 /// Tests whether this is the AST for a reference to the type
380 /// parameter with id `param_id`. We use this so as to avoid running
381 /// `ast_ty_to_ty`, because we want to avoid triggering an all-out
382 /// conversion of the type to avoid inducing unnecessary cycles.
383 fn is_param<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
385 param_id: ast::NodeId)
388 if let hir::TyPath(hir::QPath::Resolved(None, ref path)) = ast_ty.node {
390 Def::SelfTy(Some(def_id), None) |
391 Def::TyParam(def_id) => {
392 def_id == tcx.hir.local_def_id(param_id)
401 fn ensure_no_ty_param_bounds(tcx: TyCtxt,
403 generics: &hir::Generics,
404 thing: &'static str) {
405 let mut warn = false;
407 for ty_param in generics.ty_params.iter() {
408 for bound in ty_param.bounds.iter() {
410 hir::TraitTyParamBound(..) => {
413 hir::RegionTyParamBound(..) => { }
418 for predicate in generics.where_clause.predicates.iter() {
420 hir::WherePredicate::BoundPredicate(..) => {
423 hir::WherePredicate::RegionPredicate(..) => { }
424 hir::WherePredicate::EqPredicate(..) => { }
429 // According to accepted RFC #XXX, we should
430 // eventually accept these, but it will not be
431 // part of this PR. Still, convert to warning to
432 // make bootstrapping easier.
433 span_warn!(tcx.sess, span, E0122,
434 "trait bounds are not (yet) enforced \
440 fn convert_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item_id: ast::NodeId) {
441 let it = tcx.hir.expect_item(item_id);
442 debug!("convert: item {} with id {}", it.name, it.id);
443 let def_id = tcx.hir.local_def_id(item_id);
445 // These don't define types.
446 hir::ItemExternCrate(_) |
449 hir::ItemGlobalAsm(_) => {}
450 hir::ItemForeignMod(ref foreign_mod) => {
451 for item in &foreign_mod.items {
452 let def_id = tcx.hir.local_def_id(item.id);
453 tcx.item_generics(def_id);
454 tcx.item_type(def_id);
455 tcx.item_predicates(def_id);
458 hir::ItemEnum(ref enum_definition, _) => {
459 tcx.item_generics(def_id);
460 tcx.item_type(def_id);
461 tcx.item_predicates(def_id);
462 convert_enum_variant_types(tcx, def_id, &enum_definition.variants);
464 hir::ItemDefaultImpl(..) => {
465 tcx.impl_trait_ref(def_id);
467 hir::ItemImpl(..) => {
468 tcx.item_generics(def_id);
469 tcx.item_type(def_id);
470 tcx.impl_trait_ref(def_id);
471 tcx.item_predicates(def_id);
473 hir::ItemTrait(..) => {
474 tcx.item_generics(def_id);
475 tcx.lookup_trait_def(def_id);
476 ty::queries::super_predicates::get(tcx, it.span, def_id);
477 tcx.item_predicates(def_id);
479 hir::ItemStruct(ref struct_def, _) |
480 hir::ItemUnion(ref struct_def, _) => {
481 tcx.item_generics(def_id);
482 tcx.item_type(def_id);
483 tcx.item_predicates(def_id);
485 for f in struct_def.fields() {
486 let def_id = tcx.hir.local_def_id(f.id);
487 tcx.item_generics(def_id);
488 tcx.item_type(def_id);
489 tcx.item_predicates(def_id);
492 if !struct_def.is_struct() {
493 convert_variant_ctor(tcx, struct_def.id());
496 hir::ItemTy(_, ref generics) => {
497 ensure_no_ty_param_bounds(tcx, it.span, generics, "type");
498 tcx.item_generics(def_id);
499 tcx.item_type(def_id);
500 tcx.item_predicates(def_id);
502 hir::ItemStatic(..) | hir::ItemConst(..) | hir::ItemFn(..) => {
503 tcx.item_generics(def_id);
504 tcx.item_type(def_id);
505 tcx.item_predicates(def_id);
510 fn convert_trait_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, trait_item_id: ast::NodeId) {
511 let trait_item = tcx.hir.expect_trait_item(trait_item_id);
512 let def_id = tcx.hir.local_def_id(trait_item.id);
513 tcx.item_generics(def_id);
515 match trait_item.node {
516 hir::TraitItemKind::Const(..) |
517 hir::TraitItemKind::Type(_, Some(_)) |
518 hir::TraitItemKind::Method(..) => {
519 tcx.item_type(def_id);
522 hir::TraitItemKind::Type(_, None) => {}
525 tcx.item_predicates(def_id);
528 fn convert_impl_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, impl_item_id: ast::NodeId) {
529 let def_id = tcx.hir.local_def_id(impl_item_id);
530 tcx.item_generics(def_id);
531 tcx.item_type(def_id);
532 tcx.item_predicates(def_id);
535 fn convert_variant_ctor<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
536 ctor_id: ast::NodeId) {
537 let def_id = tcx.hir.local_def_id(ctor_id);
538 tcx.item_generics(def_id);
539 tcx.item_type(def_id);
540 tcx.item_predicates(def_id);
543 fn convert_enum_variant_types<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
545 variants: &[hir::Variant]) {
546 let def = tcx.lookup_adt_def(def_id);
547 let repr_type = def.repr.discr_type();
548 let initial = repr_type.initial_discriminant(tcx);
549 let mut prev_discr = None::<ConstInt>;
551 // fill the discriminant values and field types
552 for variant in variants {
553 let wrapped_discr = prev_discr.map_or(initial, |d| d.wrap_incr());
554 prev_discr = Some(if let Some(e) = variant.node.disr_expr {
555 let expr_did = tcx.hir.local_def_id(e.node_id);
556 let result = ty::queries::monomorphic_const_eval::get(tcx, variant.span, expr_did);
558 // enum variant evaluation happens before the global constant check
559 // so we need to report the real error
560 if let Err(ref err) = result {
561 err.report(tcx, variant.span, "enum discriminant");
565 Ok(ConstVal::Integral(x)) => Some(x),
568 } else if let Some(discr) = repr_type.disr_incr(tcx, prev_discr) {
571 struct_span_err!(tcx.sess, variant.span, E0370,
572 "enum discriminant overflowed")
573 .span_label(variant.span, &format!("overflowed on value after {}",
574 prev_discr.unwrap()))
575 .note(&format!("explicitly set `{} = {}` if that is desired outcome",
576 variant.node.name, wrapped_discr))
579 }.unwrap_or(wrapped_discr));
581 for f in variant.node.data.fields() {
582 let def_id = tcx.hir.local_def_id(f.id);
583 tcx.item_generics(def_id);
584 tcx.item_type(def_id);
585 tcx.item_predicates(def_id);
588 // Convert the ctor, if any. This also registers the variant as
590 convert_variant_ctor(tcx, variant.node.data.id());
594 fn convert_struct_variant<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
597 discr: ty::VariantDiscr,
598 def: &hir::VariantData)
600 let mut seen_fields: FxHashMap<ast::Name, Span> = FxHashMap();
601 let node_id = tcx.hir.as_local_node_id(did).unwrap();
602 let fields = def.fields().iter().map(|f| {
603 let fid = tcx.hir.local_def_id(f.id);
604 let dup_span = seen_fields.get(&f.name).cloned();
605 if let Some(prev_span) = dup_span {
606 struct_span_err!(tcx.sess, f.span, E0124,
607 "field `{}` is already declared",
609 .span_label(f.span, &"field already declared")
610 .span_label(prev_span, &format!("`{}` first declared here", f.name))
613 seen_fields.insert(f.name, f.span);
619 vis: ty::Visibility::from_hir(&f.vis, node_id, tcx)
627 ctor_kind: CtorKind::from_hir(def),
631 fn adt_def<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
633 -> &'tcx ty::AdtDef {
634 use rustc::hir::map::*;
637 let node_id = tcx.hir.as_local_node_id(def_id).unwrap();
638 let item = match tcx.hir.get(node_id) {
639 NodeItem(item) => item,
643 let repr = ReprOptions::new(tcx, def_id);
644 let (kind, variants) = match item.node {
645 ItemEnum(ref def, _) => {
646 let mut distance_from_explicit = 0;
647 (AdtKind::Enum, def.variants.iter().map(|v| {
648 let did = tcx.hir.local_def_id(v.node.data.id());
649 let discr = if let Some(e) = v.node.disr_expr {
650 distance_from_explicit = 0;
651 ty::VariantDiscr::Explicit(tcx.hir.local_def_id(e.node_id))
653 ty::VariantDiscr::Relative(distance_from_explicit)
655 distance_from_explicit += 1;
657 convert_struct_variant(tcx, did, v.node.name, discr, &v.node.data)
660 ItemStruct(ref def, _) => {
661 // Use separate constructor id for unit/tuple structs and reuse did for braced structs.
662 let ctor_id = if !def.is_struct() {
663 Some(tcx.hir.local_def_id(def.id()))
667 (AdtKind::Struct, vec![
668 convert_struct_variant(tcx, ctor_id.unwrap_or(def_id), item.name,
669 ty::VariantDiscr::Relative(0), def)
672 ItemUnion(ref def, _) => {
673 (AdtKind::Union, vec![
674 convert_struct_variant(tcx, def_id, item.name,
675 ty::VariantDiscr::Relative(0), def)
680 tcx.alloc_adt_def(def_id, kind, variants, repr)
683 /// Ensures that the super-predicates of the trait with def-id
684 /// trait_def_id are converted and stored. This also ensures that
685 /// the transitive super-predicates are converted;
686 fn super_predicates<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
688 -> ty::GenericPredicates<'tcx> {
689 debug!("super_predicates(trait_def_id={:?})", trait_def_id);
690 let trait_node_id = tcx.hir.as_local_node_id(trait_def_id).unwrap();
692 let item = match tcx.hir.get(trait_node_id) {
693 hir_map::NodeItem(item) => item,
694 _ => bug!("trait_node_id {} is not an item", trait_node_id)
697 let (generics, bounds) = match item.node {
698 hir::ItemTrait(_, ref generics, ref supertraits, _) => (generics, supertraits),
699 _ => span_bug!(item.span,
700 "super_predicates invoked on non-trait"),
703 let icx = ItemCtxt::new(tcx, trait_def_id);
705 // Convert the bounds that follow the colon, e.g. `Bar+Zed` in `trait Foo : Bar+Zed`.
706 let self_param_ty = tcx.mk_self_type();
707 let superbounds1 = compute_bounds(&icx,
713 let superbounds1 = superbounds1.predicates(tcx, self_param_ty);
715 // Convert any explicit superbounds in the where clause,
716 // e.g. `trait Foo where Self : Bar`:
717 let superbounds2 = icx.type_parameter_bounds_in_generics(generics, item.id, self_param_ty);
719 // Combine the two lists to form the complete set of superbounds:
720 let superbounds: Vec<_> = superbounds1.into_iter().chain(superbounds2).collect();
722 // Now require that immediate supertraits are converted,
723 // which will, in turn, reach indirect supertraits.
724 for bound in superbounds.iter().filter_map(|p| p.to_opt_poly_trait_ref()) {
725 ty::queries::super_predicates::get(tcx, item.span, bound.def_id());
728 ty::GenericPredicates {
730 predicates: superbounds
734 fn trait_def<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
736 -> &'tcx ty::TraitDef {
737 let node_id = tcx.hir.as_local_node_id(def_id).unwrap();
738 let item = tcx.hir.expect_item(node_id);
740 let unsafety = match item.node {
741 hir::ItemTrait(unsafety, ..) => unsafety,
742 _ => span_bug!(item.span, "trait_def_of_item invoked on non-trait"),
745 let paren_sugar = tcx.has_attr(def_id, "rustc_paren_sugar");
746 if paren_sugar && !tcx.sess.features.borrow().unboxed_closures {
747 let mut err = tcx.sess.struct_span_err(
749 "the `#[rustc_paren_sugar]` attribute is a temporary means of controlling \
750 which traits can use parenthetical notation");
752 "add `#![feature(unboxed_closures)]` to \
753 the crate attributes to use it");
757 let def_path_hash = tcx.def_path_hash(def_id);
758 let def = ty::TraitDef::new(def_id, unsafety, paren_sugar, def_path_hash);
760 if tcx.hir.trait_is_auto(def_id) {
761 def.record_has_default_impl();
764 tcx.alloc_trait_def(def)
767 fn generics<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
769 -> &'tcx ty::Generics {
770 use rustc::hir::map::*;
773 let node_id = tcx.hir.as_local_node_id(def_id).unwrap();
775 let node = tcx.hir.get(node_id);
776 let parent_def_id = match node {
782 let parent_id = tcx.hir.get_parent(node_id);
783 Some(tcx.hir.local_def_id(parent_id))
785 NodeExpr(&hir::Expr { node: hir::ExprClosure(..), .. }) => {
786 Some(tcx.closure_base_def_id(def_id))
788 NodeTy(&hir::Ty { node: hir::TyImplTrait(..), .. }) => {
789 let mut parent_id = node_id;
791 match tcx.hir.get(parent_id) {
792 NodeItem(_) | NodeImplItem(_) | NodeTraitItem(_) => break,
794 parent_id = tcx.hir.get_parent_node(parent_id);
798 Some(tcx.hir.local_def_id(parent_id))
803 let mut opt_self = None;
804 let mut allow_defaults = false;
806 let no_generics = hir::Generics::empty();
807 let ast_generics = match node {
808 NodeTraitItem(item) => {
810 TraitItemKind::Method(ref sig, _) => &sig.generics,
815 NodeImplItem(item) => {
817 ImplItemKind::Method(ref sig, _) => &sig.generics,
824 ItemFn(.., ref generics, _) |
825 ItemImpl(_, _, ref generics, ..) => generics,
827 ItemTy(_, ref generics) |
828 ItemEnum(_, ref generics) |
829 ItemStruct(_, ref generics) |
830 ItemUnion(_, ref generics) => {
831 allow_defaults = true;
835 ItemTrait(_, ref generics, ..) => {
836 // Add in the self type parameter.
838 // Something of a hack: use the node id for the trait, also as
839 // the node id for the Self type parameter.
840 let param_id = item.id;
842 opt_self = Some(ty::TypeParameterDef {
844 name: keywords::SelfType.name(),
845 def_id: tcx.hir.local_def_id(param_id),
847 object_lifetime_default: rl::Set1::Empty,
848 pure_wrt_drop: false,
851 allow_defaults = true;
859 NodeForeignItem(item) => {
861 ForeignItemStatic(..) => &no_generics,
862 ForeignItemFn(_, _, ref generics) => generics
869 let has_self = opt_self.is_some();
870 let mut parent_has_self = false;
871 let mut own_start = has_self as u32;
872 let (parent_regions, parent_types) = parent_def_id.map_or((0, 0), |def_id| {
873 let generics = tcx.item_generics(def_id);
874 assert_eq!(has_self, false);
875 parent_has_self = generics.has_self;
876 own_start = generics.count() as u32;
877 (generics.parent_regions + generics.regions.len() as u32,
878 generics.parent_types + generics.types.len() as u32)
881 let early_lifetimes = early_bound_lifetimes_from_generics(tcx, ast_generics);
882 let regions = early_lifetimes.enumerate().map(|(i, l)| {
883 let issue_32330 = tcx.named_region_map.issue_32330.get(&l.lifetime.id).cloned();
884 ty::RegionParameterDef {
885 name: l.lifetime.name,
886 index: own_start + i as u32,
887 def_id: tcx.hir.local_def_id(l.lifetime.id),
888 pure_wrt_drop: l.pure_wrt_drop,
889 issue_32330: issue_32330,
891 }).collect::<Vec<_>>();
893 let object_lifetime_defaults =
894 tcx.named_region_map.object_lifetime_defaults.get(&node_id);
896 // Now create the real type parameters.
897 let type_start = own_start + regions.len() as u32;
898 let types = ast_generics.ty_params.iter().enumerate().map(|(i, p)| {
899 if p.name == keywords::SelfType.name() {
900 span_bug!(p.span, "`Self` should not be the name of a regular parameter");
903 if !allow_defaults && p.default.is_some() {
904 if !tcx.sess.features.borrow().default_type_parameter_fallback {
906 lint::builtin::INVALID_TYPE_PARAM_DEFAULT,
909 format!("defaults for type parameters are only allowed in `struct`, \
910 `enum`, `type`, or `trait` definitions."));
914 ty::TypeParameterDef {
915 index: type_start + i as u32,
917 def_id: tcx.hir.local_def_id(p.id),
918 has_default: p.default.is_some(),
919 object_lifetime_default:
920 object_lifetime_defaults.map_or(rl::Set1::Empty, |o| o[i]),
921 pure_wrt_drop: p.pure_wrt_drop,
924 let mut types: Vec<_> = opt_self.into_iter().chain(types).collect();
926 // provide junk type parameter defs - the only place that
927 // cares about anything but the length is instantiation,
928 // and we don't do that for closures.
929 if let NodeExpr(&hir::Expr { node: hir::ExprClosure(..), .. }) = node {
930 tcx.with_freevars(node_id, |fv| {
931 types.extend(fv.iter().enumerate().map(|(i, _)| ty::TypeParameterDef {
932 index: type_start + i as u32,
933 name: Symbol::intern("<upvar>"),
936 object_lifetime_default: rl::Set1::Empty,
937 pure_wrt_drop: false,
942 let mut type_param_to_index = BTreeMap::new();
943 for param in &types {
944 type_param_to_index.insert(param.def_id.index, param.index);
947 tcx.alloc_generics(ty::Generics {
948 parent: parent_def_id,
949 parent_regions: parent_regions,
950 parent_types: parent_types,
953 type_param_to_index: type_param_to_index,
954 has_self: has_self || parent_has_self
958 fn ty<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
961 use rustc::hir::map::*;
964 let node_id = tcx.hir.as_local_node_id(def_id).unwrap();
966 let icx = ItemCtxt::new(tcx, def_id);
968 match tcx.hir.get(node_id) {
969 NodeTraitItem(item) => {
971 TraitItemKind::Method(ref sig, _) => {
972 let fty = AstConv::ty_of_fn(&icx, sig.unsafety, sig.abi, &sig.decl);
973 let substs = Substs::identity_for_item(tcx, def_id);
974 tcx.mk_fn_def(def_id, substs, fty)
976 TraitItemKind::Const(ref ty, _) |
977 TraitItemKind::Type(_, Some(ref ty)) => icx.to_ty(ty),
978 TraitItemKind::Type(_, None) => {
979 span_bug!(item.span, "associated type missing default");
984 NodeImplItem(item) => {
986 ImplItemKind::Method(ref sig, _) => {
987 let fty = AstConv::ty_of_fn(&icx, sig.unsafety, sig.abi, &sig.decl);
988 let substs = Substs::identity_for_item(tcx, def_id);
989 tcx.mk_fn_def(def_id, substs, fty)
991 ImplItemKind::Const(ref ty, _) => icx.to_ty(ty),
992 ImplItemKind::Type(ref ty) => {
993 if tcx.impl_trait_ref(tcx.hir.get_parent_did(node_id)).is_none() {
994 span_err!(tcx.sess, item.span, E0202,
995 "associated types are not allowed in inherent impls");
1005 ItemStatic(ref t, ..) | ItemConst(ref t, _) |
1006 ItemTy(ref t, _) | ItemImpl(.., ref t, _) => {
1009 ItemFn(ref decl, unsafety, _, abi, _, _) => {
1010 let tofd = AstConv::ty_of_fn(&icx, unsafety, abi, &decl);
1011 let substs = Substs::identity_for_item(tcx, def_id);
1012 tcx.mk_fn_def(def_id, substs, tofd)
1017 let def = tcx.lookup_adt_def(def_id);
1018 let substs = Substs::identity_for_item(tcx, def_id);
1019 tcx.mk_adt(def, substs)
1021 ItemDefaultImpl(..) |
1024 ItemForeignMod(..) |
1026 ItemExternCrate(..) |
1030 "compute_type_of_item: unexpected item type: {:?}",
1036 NodeForeignItem(foreign_item) => {
1037 let abi = tcx.hir.get_foreign_abi(node_id);
1039 match foreign_item.node {
1040 ForeignItemFn(ref fn_decl, _, _) => {
1041 compute_type_of_foreign_fn_decl(tcx, def_id, fn_decl, abi)
1043 ForeignItemStatic(ref t, _) => icx.to_ty(t)
1047 NodeStructCtor(&ref def) |
1048 NodeVariant(&Spanned { node: hir::Variant_ { data: ref def, .. }, .. }) => {
1049 let ty = tcx.item_type(tcx.hir.get_parent_did(node_id));
1051 VariantData::Unit(..) | VariantData::Struct(..) => ty,
1052 VariantData::Tuple(ref fields, _) => {
1053 let inputs = fields.iter().map(|f| {
1054 tcx.item_type(tcx.hir.local_def_id(f.id))
1056 let substs = Substs::identity_for_item(tcx, def_id);
1057 tcx.mk_fn_def(def_id, substs, ty::Binder(tcx.mk_fn_sig(
1061 hir::Unsafety::Normal,
1068 NodeField(field) => icx.to_ty(&field.ty),
1070 NodeExpr(&hir::Expr { node: hir::ExprClosure(..), .. }) => {
1071 tcx.mk_closure(def_id, Substs::for_item(
1074 let region = def.to_early_bound_region_data();
1075 tcx.mk_region(ty::ReEarlyBound(region))
1077 |def, _| tcx.mk_param_from_def(def)
1081 NodeExpr(_) => match tcx.hir.get(tcx.hir.get_parent_node(node_id)) {
1082 NodeTy(&hir::Ty { node: TyArray(_, body), .. }) |
1083 NodeTy(&hir::Ty { node: TyTypeof(body), .. }) |
1084 NodeExpr(&hir::Expr { node: ExprRepeat(_, body), .. })
1085 if body.node_id == node_id => tcx.types.usize,
1087 NodeVariant(&Spanned { node: Variant_ { disr_expr: Some(e), .. }, .. })
1088 if e.node_id == node_id => {
1089 tcx.lookup_adt_def(tcx.hir.get_parent_did(node_id))
1090 .repr.discr_type().to_ty(tcx)
1094 bug!("unexpected expr parent in type_of_def_id(): {:?}", x);
1098 NodeTyParam(&hir::TyParam { default: Some(ref ty), .. }) => {
1102 NodeTy(&hir::Ty { node: TyImplTrait(..), .. }) => {
1103 let owner = tcx.hir.get_parent_did(node_id);
1104 tcx.item_tables(owner).node_id_to_type(node_id)
1108 bug!("unexpected sort of node in type_of_def_id(): {:?}", x);
1113 fn impl_trait_ref<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
1115 -> Option<ty::TraitRef<'tcx>> {
1116 let icx = ItemCtxt::new(tcx, def_id);
1118 let node_id = tcx.hir.as_local_node_id(def_id).unwrap();
1119 match tcx.hir.expect_item(node_id).node {
1120 hir::ItemDefaultImpl(_, ref ast_trait_ref) => {
1121 Some(AstConv::instantiate_mono_trait_ref(&icx,
1123 tcx.mk_self_type()))
1125 hir::ItemImpl(.., ref opt_trait_ref, _, _) => {
1126 opt_trait_ref.as_ref().map(|ast_trait_ref| {
1127 let selfty = tcx.item_type(def_id);
1128 AstConv::instantiate_mono_trait_ref(&icx, ast_trait_ref, selfty)
1135 // Is it marked with ?Sized
1136 fn is_unsized<'gcx: 'tcx, 'tcx>(astconv: &AstConv<'gcx, 'tcx>,
1137 ast_bounds: &[hir::TyParamBound],
1140 let tcx = astconv.tcx();
1142 // Try to find an unbound in bounds.
1143 let mut unbound = None;
1144 for ab in ast_bounds {
1145 if let &hir::TraitTyParamBound(ref ptr, hir::TraitBoundModifier::Maybe) = ab {
1146 if unbound.is_none() {
1147 unbound = Some(ptr.trait_ref.clone());
1149 span_err!(tcx.sess, span, E0203,
1150 "type parameter has more than one relaxed default \
1151 bound, only one is supported");
1156 let kind_id = tcx.lang_items.require(SizedTraitLangItem);
1159 // FIXME(#8559) currently requires the unbound to be built-in.
1160 if let Ok(kind_id) = kind_id {
1161 if tpb.path.def != Def::Trait(kind_id) {
1162 tcx.sess.span_warn(span,
1163 "default bound relaxed for a type parameter, but \
1164 this does nothing because the given bound is not \
1165 a default. Only `?Sized` is supported");
1169 _ if kind_id.is_ok() => {
1172 // No lang item for Sized, so we can't add it as a bound.
1179 /// Returns the early-bound lifetimes declared in this generics
1180 /// listing. For anything other than fns/methods, this is just all
1181 /// the lifetimes that are declared. For fns or methods, we have to
1182 /// screen out those that do not appear in any where-clauses etc using
1183 /// `resolve_lifetime::early_bound_lifetimes`.
1184 fn early_bound_lifetimes_from_generics<'a, 'tcx>(
1185 tcx: TyCtxt<'a, 'tcx, 'tcx>,
1186 ast_generics: &'a hir::Generics)
1187 -> impl Iterator<Item=&'a hir::LifetimeDef>
1192 .filter(move |l| !tcx.named_region_map.late_bound.contains(&l.lifetime.id))
1195 fn predicates<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
1197 -> ty::GenericPredicates<'tcx> {
1198 use rustc::hir::map::*;
1201 let node_id = tcx.hir.as_local_node_id(def_id).unwrap();
1202 let node = tcx.hir.get(node_id);
1204 let mut is_trait = None;
1206 let icx = ItemCtxt::new(tcx, def_id);
1207 let no_generics = hir::Generics::empty();
1208 let ast_generics = match node {
1209 NodeTraitItem(item) => {
1211 TraitItemKind::Method(ref sig, _) => &sig.generics,
1216 NodeImplItem(item) => {
1218 ImplItemKind::Method(ref sig, _) => &sig.generics,
1225 ItemFn(.., ref generics, _) |
1226 ItemImpl(_, _, ref generics, ..) |
1227 ItemTy(_, ref generics) |
1228 ItemEnum(_, ref generics) |
1229 ItemStruct(_, ref generics) |
1230 ItemUnion(_, ref generics) => {
1234 ItemTrait(_, ref generics, .., ref items) => {
1235 is_trait = Some((ty::TraitRef {
1237 substs: Substs::identity_for_item(tcx, def_id)
1246 NodeForeignItem(item) => {
1248 ForeignItemStatic(..) => &no_generics,
1249 ForeignItemFn(_, _, ref generics) => generics
1253 NodeTy(&Ty { node: TyImplTrait(ref bounds), span, .. }) => {
1254 let substs = Substs::identity_for_item(tcx, def_id);
1255 let anon_ty = tcx.mk_anon(def_id, substs);
1257 // Collect the bounds, i.e. the `A+B+'c` in `impl A+B+'c`.
1258 let bounds = compute_bounds(&icx, anon_ty, bounds,
1259 SizedByDefault::Yes,
1261 return ty::GenericPredicates {
1263 predicates: bounds.predicates(tcx, anon_ty)
1270 let generics = tcx.item_generics(def_id);
1271 let parent_count = generics.parent_count() as u32;
1272 let has_own_self = generics.has_self && parent_count == 0;
1274 let mut predicates = vec![];
1276 // Below we'll consider the bounds on the type parameters (including `Self`)
1277 // and the explicit where-clauses, but to get the full set of predicates
1278 // on a trait we need to add in the supertrait bounds and bounds found on
1279 // associated types.
1280 if let Some((trait_ref, _)) = is_trait {
1281 predicates = tcx.item_super_predicates(def_id).predicates;
1283 // Add in a predicate that `Self:Trait` (where `Trait` is the
1284 // current trait). This is needed for builtin bounds.
1285 predicates.push(trait_ref.to_poly_trait_ref().to_predicate());
1288 // Collect the region predicates that were declared inline as
1289 // well. In the case of parameters declared on a fn or method, we
1290 // have to be careful to only iterate over early-bound regions.
1291 let mut index = parent_count + has_own_self as u32;
1292 for param in early_bound_lifetimes_from_generics(tcx, ast_generics) {
1293 let region = tcx.mk_region(ty::ReEarlyBound(ty::EarlyBoundRegion {
1295 name: param.lifetime.name
1299 for bound in ¶m.bounds {
1300 let bound_region = AstConv::ast_region_to_region(&icx, bound, None);
1301 let outlives = ty::Binder(ty::OutlivesPredicate(region, bound_region));
1302 predicates.push(outlives.to_predicate());
1306 // Collect the predicates that were written inline by the user on each
1307 // type parameter (e.g., `<T:Foo>`).
1308 for param in &ast_generics.ty_params {
1309 let param_ty = ty::ParamTy::new(index, param.name).to_ty(tcx);
1312 let bounds = compute_bounds(&icx,
1315 SizedByDefault::Yes,
1317 predicates.extend(bounds.predicates(tcx, param_ty));
1320 // Add in the bounds that appear in the where-clause
1321 let where_clause = &ast_generics.where_clause;
1322 for predicate in &where_clause.predicates {
1324 &hir::WherePredicate::BoundPredicate(ref bound_pred) => {
1325 let ty = icx.to_ty(&bound_pred.bounded_ty);
1327 for bound in bound_pred.bounds.iter() {
1329 &hir::TyParamBound::TraitTyParamBound(ref poly_trait_ref, _) => {
1330 let mut projections = Vec::new();
1333 AstConv::instantiate_poly_trait_ref(&icx,
1338 predicates.push(trait_ref.to_predicate());
1340 for projection in &projections {
1341 predicates.push(projection.to_predicate());
1345 &hir::TyParamBound::RegionTyParamBound(ref lifetime) => {
1346 let region = AstConv::ast_region_to_region(&icx,
1349 let pred = ty::Binder(ty::OutlivesPredicate(ty, region));
1350 predicates.push(ty::Predicate::TypeOutlives(pred))
1356 &hir::WherePredicate::RegionPredicate(ref region_pred) => {
1357 let r1 = AstConv::ast_region_to_region(&icx, ®ion_pred.lifetime, None);
1358 for bound in ®ion_pred.bounds {
1359 let r2 = AstConv::ast_region_to_region(&icx, bound, None);
1360 let pred = ty::Binder(ty::OutlivesPredicate(r1, r2));
1361 predicates.push(ty::Predicate::RegionOutlives(pred))
1365 &hir::WherePredicate::EqPredicate(..) => {
1371 // Add predicates from associated type bounds.
1372 if let Some((self_trait_ref, trait_items)) = is_trait {
1373 predicates.extend(trait_items.iter().flat_map(|trait_item_ref| {
1374 let trait_item = tcx.hir.trait_item(trait_item_ref.id);
1375 let bounds = match trait_item.node {
1376 hir::TraitItemKind::Type(ref bounds, _) => bounds,
1378 return vec![].into_iter();
1382 let assoc_ty = tcx.mk_projection(self_trait_ref, trait_item.name);
1384 let bounds = compute_bounds(&ItemCtxt::new(tcx, def_id),
1387 SizedByDefault::Yes,
1390 bounds.predicates(tcx, assoc_ty).into_iter()
1394 // Subtle: before we store the predicates into the tcx, we
1395 // sort them so that predicates like `T: Foo<Item=U>` come
1396 // before uses of `U`. This avoids false ambiguity errors
1397 // in trait checking. See `setup_constraining_predicates`
1399 if let NodeItem(&Item { node: ItemImpl(..), .. }) = node {
1400 let self_ty = tcx.item_type(def_id);
1401 let trait_ref = tcx.impl_trait_ref(def_id);
1402 ctp::setup_constraining_predicates(&mut predicates,
1404 &mut ctp::parameters_for_impl(self_ty, trait_ref));
1407 ty::GenericPredicates {
1408 parent: generics.parent,
1409 predicates: predicates
1413 pub enum SizedByDefault { Yes, No, }
1415 /// Translate the AST's notion of ty param bounds (which are an enum consisting of a newtyped Ty or
1416 /// a region) to ty's notion of ty param bounds, which can either be user-defined traits, or the
1417 /// built-in trait (formerly known as kind): Send.
1418 pub fn compute_bounds<'gcx: 'tcx, 'tcx>(astconv: &AstConv<'gcx, 'tcx>,
1419 param_ty: ty::Ty<'tcx>,
1420 ast_bounds: &[hir::TyParamBound],
1421 sized_by_default: SizedByDefault,
1425 let mut region_bounds = vec![];
1426 let mut trait_bounds = vec![];
1427 for ast_bound in ast_bounds {
1429 hir::TraitTyParamBound(ref b, hir::TraitBoundModifier::None) => {
1430 trait_bounds.push(b);
1432 hir::TraitTyParamBound(_, hir::TraitBoundModifier::Maybe) => {}
1433 hir::RegionTyParamBound(ref l) => {
1434 region_bounds.push(l);
1439 let mut projection_bounds = vec![];
1441 let mut trait_bounds: Vec<_> = trait_bounds.iter().map(|&bound| {
1442 astconv.instantiate_poly_trait_ref(bound,
1444 &mut projection_bounds)
1447 let region_bounds = region_bounds.into_iter().map(|r| {
1448 astconv.ast_region_to_region(r, None)
1451 trait_bounds.sort_by(|a,b| a.def_id().cmp(&b.def_id()));
1453 let implicitly_sized = if let SizedByDefault::Yes = sized_by_default {
1454 !is_unsized(astconv, ast_bounds, span)
1460 region_bounds: region_bounds,
1461 implicitly_sized: implicitly_sized,
1462 trait_bounds: trait_bounds,
1463 projection_bounds: projection_bounds,
1467 /// Converts a specific TyParamBound from the AST into a set of
1468 /// predicates that apply to the self-type. A vector is returned
1469 /// because this can be anywhere from 0 predicates (`T:?Sized` adds no
1470 /// predicates) to 1 (`T:Foo`) to many (`T:Bar<X=i32>` adds `T:Bar`
1471 /// and `<T as Bar>::X == i32`).
1472 fn predicates_from_bound<'tcx>(astconv: &AstConv<'tcx, 'tcx>,
1474 bound: &hir::TyParamBound)
1475 -> Vec<ty::Predicate<'tcx>>
1478 hir::TraitTyParamBound(ref tr, hir::TraitBoundModifier::None) => {
1479 let mut projections = Vec::new();
1480 let pred = astconv.instantiate_poly_trait_ref(tr,
1483 projections.into_iter()
1484 .map(|p| p.to_predicate())
1485 .chain(Some(pred.to_predicate()))
1488 hir::RegionTyParamBound(ref lifetime) => {
1489 let region = astconv.ast_region_to_region(lifetime, None);
1490 let pred = ty::Binder(ty::OutlivesPredicate(param_ty, region));
1491 vec![ty::Predicate::TypeOutlives(pred)]
1493 hir::TraitTyParamBound(_, hir::TraitBoundModifier::Maybe) => {
1499 fn compute_type_of_foreign_fn_decl<'a, 'tcx>(
1500 tcx: TyCtxt<'a, 'tcx, 'tcx>,
1506 let fty = AstConv::ty_of_fn(&ItemCtxt::new(tcx, def_id), hir::Unsafety::Unsafe, abi, decl);
1508 // feature gate SIMD types in FFI, since I (huonw) am not sure the
1509 // ABIs are handled at all correctly.
1510 if abi != abi::Abi::RustIntrinsic && abi != abi::Abi::PlatformIntrinsic
1511 && !tcx.sess.features.borrow().simd_ffi {
1512 let check = |ast_ty: &hir::Ty, ty: ty::Ty| {
1514 tcx.sess.struct_span_err(ast_ty.span,
1515 &format!("use of SIMD type `{}` in FFI is highly experimental and \
1516 may result in invalid code",
1517 tcx.hir.node_to_pretty_string(ast_ty.id)))
1518 .help("add #![feature(simd_ffi)] to the crate attributes to enable")
1522 for (input, ty) in decl.inputs.iter().zip(*fty.inputs().skip_binder()) {
1525 if let hir::Return(ref ty) = decl.output {
1526 check(&ty, *fty.output().skip_binder())
1530 let substs = Substs::identity_for_item(tcx, def_id);
1531 tcx.mk_fn_def(def_id, substs, fty)