]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_typeck/src/collect.rs
walk hir to get const evaluatable predicates
[rust.git] / compiler / rustc_typeck / src / collect.rs
1 //! "Collection" is the process of determining the type and other external
2 //! details of each item in Rust. Collection is specifically concerned
3 //! with *inter-procedural* things -- for example, for a function
4 //! definition, collection will figure out the type and signature of the
5 //! function, but it will not visit the *body* of the function in any way,
6 //! nor examine type annotations on local variables (that's the job of
7 //! type *checking*).
8 //!
9 //! Collecting is ultimately defined by a bundle of queries that
10 //! inquire after various facts about the items in the crate (e.g.,
11 //! `type_of`, `generics_of`, `predicates_of`, etc). See the `provide` function
12 //! for the full set.
13 //!
14 //! At present, however, we do run collection across all items in the
15 //! crate as a kind of pass. This should eventually be factored away.
16
17 use crate::astconv::{AstConv, SizedByDefault};
18 use crate::bounds::Bounds;
19 use crate::check::intrinsic::intrinsic_operation_unsafety;
20 use crate::constrained_generic_params as cgp;
21 use crate::errors;
22 use crate::middle::resolve_lifetime as rl;
23 use rustc_ast as ast;
24 use rustc_ast::MetaItemKind;
25 use rustc_attr::{list_contains_name, InlineAttr, OptimizeAttr};
26 use rustc_data_structures::captures::Captures;
27 use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet};
28 use rustc_errors::{struct_span_err, Applicability};
29 use rustc_hir as hir;
30 use rustc_hir::def::{CtorKind, DefKind, Res};
31 use rustc_hir::def_id::{DefId, LocalDefId, LOCAL_CRATE};
32 use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
33 use rustc_hir::weak_lang_items;
34 use rustc_hir::{GenericParamKind, HirId, Node};
35 use rustc_middle::hir::map::blocks::FnLikeNode;
36 use rustc_middle::hir::map::Map;
37 use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
38 use rustc_middle::mir::mono::Linkage;
39 use rustc_middle::ty::query::Providers;
40 use rustc_middle::ty::subst::InternalSubsts;
41 use rustc_middle::ty::util::Discr;
42 use rustc_middle::ty::util::IntTypeExt;
43 use rustc_middle::ty::{self, AdtKind, Const, ToPolyTraitRef, Ty, TyCtxt};
44 use rustc_middle::ty::{ReprOptions, ToPredicate, WithConstness};
45 use rustc_session::config::SanitizerSet;
46 use rustc_session::lint;
47 use rustc_session::parse::feature_err;
48 use rustc_span::symbol::{kw, sym, Ident, Symbol};
49 use rustc_span::{Span, DUMMY_SP};
50 use rustc_target::spec::abi;
51 use rustc_trait_selection::traits::error_reporting::suggestions::NextTypeParamName;
52
53 mod type_of;
54
55 struct OnlySelfBounds(bool);
56
57 ///////////////////////////////////////////////////////////////////////////
58 // Main entry point
59
60 fn collect_mod_item_types(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
61     tcx.hir().visit_item_likes_in_module(
62         module_def_id,
63         &mut CollectItemTypesVisitor { tcx }.as_deep_visitor(),
64     );
65 }
66
67 pub fn provide(providers: &mut Providers) {
68     *providers = Providers {
69         opt_const_param_of: type_of::opt_const_param_of,
70         type_of: type_of::type_of,
71         generics_of,
72         predicates_of,
73         predicates_defined_on,
74         projection_ty_from_predicates,
75         explicit_predicates_of,
76         super_predicates_of,
77         type_param_predicates,
78         trait_def,
79         adt_def,
80         fn_sig,
81         impl_trait_ref,
82         impl_polarity,
83         is_foreign_item,
84         static_mutability,
85         generator_kind,
86         codegen_fn_attrs,
87         collect_mod_item_types,
88         ..*providers
89     };
90 }
91
92 ///////////////////////////////////////////////////////////////////////////
93
94 /// Context specific to some particular item. This is what implements
95 /// `AstConv`. It has information about the predicates that are defined
96 /// on the trait. Unfortunately, this predicate information is
97 /// available in various different forms at various points in the
98 /// process. So we can't just store a pointer to e.g., the AST or the
99 /// parsed ty form, we have to be more flexible. To this end, the
100 /// `ItemCtxt` is parameterized by a `DefId` that it uses to satisfy
101 /// `get_type_parameter_bounds` requests, drawing the information from
102 /// the AST (`hir::Generics`), recursively.
103 pub struct ItemCtxt<'tcx> {
104     tcx: TyCtxt<'tcx>,
105     item_def_id: DefId,
106 }
107
108 ///////////////////////////////////////////////////////////////////////////
109
110 #[derive(Default)]
111 crate struct PlaceholderHirTyCollector(crate Vec<Span>);
112
113 impl<'v> Visitor<'v> for PlaceholderHirTyCollector {
114     type Map = intravisit::ErasedMap<'v>;
115
116     fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
117         NestedVisitorMap::None
118     }
119     fn visit_ty(&mut self, t: &'v hir::Ty<'v>) {
120         if let hir::TyKind::Infer = t.kind {
121             self.0.push(t.span);
122         }
123         intravisit::walk_ty(self, t)
124     }
125 }
126
127 struct CollectItemTypesVisitor<'tcx> {
128     tcx: TyCtxt<'tcx>,
129 }
130
131 /// If there are any placeholder types (`_`), emit an error explaining that this is not allowed
132 /// and suggest adding type parameters in the appropriate place, taking into consideration any and
133 /// all already existing generic type parameters to avoid suggesting a name that is already in use.
134 crate fn placeholder_type_error(
135     tcx: TyCtxt<'tcx>,
136     span: Option<Span>,
137     generics: &[hir::GenericParam<'_>],
138     placeholder_types: Vec<Span>,
139     suggest: bool,
140 ) {
141     if placeholder_types.is_empty() {
142         return;
143     }
144
145     let type_name = generics.next_type_param_name(None);
146     let mut sugg: Vec<_> =
147         placeholder_types.iter().map(|sp| (*sp, (*type_name).to_string())).collect();
148
149     if generics.is_empty() {
150         if let Some(span) = span {
151             sugg.push((span, format!("<{}>", type_name)));
152         }
153     } else if let Some(arg) = generics.iter().find(|arg| match arg.name {
154         hir::ParamName::Plain(Ident { name: kw::Underscore, .. }) => true,
155         _ => false,
156     }) {
157         // Account for `_` already present in cases like `struct S<_>(_);` and suggest
158         // `struct S<T>(T);` instead of `struct S<_, T>(T);`.
159         sugg.push((arg.span, (*type_name).to_string()));
160     } else {
161         let last = generics.iter().last().unwrap();
162         sugg.push((
163             // Account for bounds, we want `fn foo<T: E, K>(_: K)` not `fn foo<T, K: E>(_: K)`.
164             last.bounds_span().unwrap_or(last.span).shrink_to_hi(),
165             format!(", {}", type_name),
166         ));
167     }
168
169     let mut err = bad_placeholder_type(tcx, placeholder_types);
170     if suggest {
171         err.multipart_suggestion(
172             "use type parameters instead",
173             sugg,
174             Applicability::HasPlaceholders,
175         );
176     }
177     err.emit();
178 }
179
180 fn reject_placeholder_type_signatures_in_item(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) {
181     let (generics, suggest) = match &item.kind {
182         hir::ItemKind::Union(_, generics)
183         | hir::ItemKind::Enum(_, generics)
184         | hir::ItemKind::TraitAlias(generics, _)
185         | hir::ItemKind::Trait(_, _, generics, ..)
186         | hir::ItemKind::Impl { generics, .. }
187         | hir::ItemKind::Struct(_, generics) => (generics, true),
188         hir::ItemKind::OpaqueTy(hir::OpaqueTy { generics, .. })
189         | hir::ItemKind::TyAlias(_, generics) => (generics, false),
190         // `static`, `fn` and `const` are handled elsewhere to suggest appropriate type.
191         _ => return,
192     };
193
194     let mut visitor = PlaceholderHirTyCollector::default();
195     visitor.visit_item(item);
196
197     placeholder_type_error(tcx, Some(generics.span), &generics.params[..], visitor.0, suggest);
198 }
199
200 impl Visitor<'tcx> for CollectItemTypesVisitor<'tcx> {
201     type Map = Map<'tcx>;
202
203     fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
204         NestedVisitorMap::OnlyBodies(self.tcx.hir())
205     }
206
207     fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
208         convert_item(self.tcx, item.hir_id);
209         reject_placeholder_type_signatures_in_item(self.tcx, item);
210         intravisit::walk_item(self, item);
211     }
212
213     fn visit_generics(&mut self, generics: &'tcx hir::Generics<'tcx>) {
214         for param in generics.params {
215             match param.kind {
216                 hir::GenericParamKind::Lifetime { .. } => {}
217                 hir::GenericParamKind::Type { default: Some(_), .. } => {
218                     let def_id = self.tcx.hir().local_def_id(param.hir_id);
219                     self.tcx.ensure().type_of(def_id);
220                 }
221                 hir::GenericParamKind::Type { .. } => {}
222                 hir::GenericParamKind::Const { .. } => {
223                     let def_id = self.tcx.hir().local_def_id(param.hir_id);
224                     self.tcx.ensure().type_of(def_id);
225                 }
226             }
227         }
228         intravisit::walk_generics(self, generics);
229     }
230
231     fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
232         if let hir::ExprKind::Closure(..) = expr.kind {
233             let def_id = self.tcx.hir().local_def_id(expr.hir_id);
234             self.tcx.ensure().generics_of(def_id);
235             self.tcx.ensure().type_of(def_id);
236         }
237         intravisit::walk_expr(self, expr);
238     }
239
240     fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>) {
241         convert_trait_item(self.tcx, trait_item.hir_id);
242         intravisit::walk_trait_item(self, trait_item);
243     }
244
245     fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) {
246         convert_impl_item(self.tcx, impl_item.hir_id);
247         intravisit::walk_impl_item(self, impl_item);
248     }
249 }
250
251 ///////////////////////////////////////////////////////////////////////////
252 // Utility types and common code for the above passes.
253
254 fn bad_placeholder_type(
255     tcx: TyCtxt<'tcx>,
256     mut spans: Vec<Span>,
257 ) -> rustc_errors::DiagnosticBuilder<'tcx> {
258     spans.sort();
259     let mut err = struct_span_err!(
260         tcx.sess,
261         spans.clone(),
262         E0121,
263         "the type placeholder `_` is not allowed within types on item signatures",
264     );
265     for span in spans {
266         err.span_label(span, "not allowed in type signatures");
267     }
268     err
269 }
270
271 impl ItemCtxt<'tcx> {
272     pub fn new(tcx: TyCtxt<'tcx>, item_def_id: DefId) -> ItemCtxt<'tcx> {
273         ItemCtxt { tcx, item_def_id }
274     }
275
276     pub fn to_ty(&self, ast_ty: &'tcx hir::Ty<'tcx>) -> Ty<'tcx> {
277         AstConv::ast_ty_to_ty(self, ast_ty)
278     }
279
280     pub fn hir_id(&self) -> hir::HirId {
281         self.tcx.hir().local_def_id_to_hir_id(self.item_def_id.expect_local())
282     }
283
284     pub fn node(&self) -> hir::Node<'tcx> {
285         self.tcx.hir().get(self.hir_id())
286     }
287 }
288
289 impl AstConv<'tcx> for ItemCtxt<'tcx> {
290     fn tcx(&self) -> TyCtxt<'tcx> {
291         self.tcx
292     }
293
294     fn item_def_id(&self) -> Option<DefId> {
295         Some(self.item_def_id)
296     }
297
298     fn default_constness_for_trait_bounds(&self) -> hir::Constness {
299         if let Some(fn_like) = FnLikeNode::from_node(self.node()) {
300             fn_like.constness()
301         } else {
302             hir::Constness::NotConst
303         }
304     }
305
306     fn get_type_parameter_bounds(&self, span: Span, def_id: DefId) -> ty::GenericPredicates<'tcx> {
307         self.tcx.at(span).type_param_predicates((self.item_def_id, def_id.expect_local()))
308     }
309
310     fn re_infer(&self, _: Option<&ty::GenericParamDef>, _: Span) -> Option<ty::Region<'tcx>> {
311         None
312     }
313
314     fn allow_ty_infer(&self) -> bool {
315         false
316     }
317
318     fn ty_infer(&self, _: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx> {
319         self.tcx().ty_error_with_message(span, "bad_placeholder_type")
320     }
321
322     fn ct_infer(
323         &self,
324         ty: Ty<'tcx>,
325         _: Option<&ty::GenericParamDef>,
326         span: Span,
327     ) -> &'tcx Const<'tcx> {
328         bad_placeholder_type(self.tcx(), vec![span]).emit();
329         self.tcx().const_error(ty)
330     }
331
332     fn projected_ty_from_poly_trait_ref(
333         &self,
334         span: Span,
335         item_def_id: DefId,
336         item_segment: &hir::PathSegment<'_>,
337         poly_trait_ref: ty::PolyTraitRef<'tcx>,
338     ) -> Ty<'tcx> {
339         if let Some(trait_ref) = poly_trait_ref.no_bound_vars() {
340             let item_substs = <dyn AstConv<'tcx>>::create_substs_for_associated_item(
341                 self,
342                 self.tcx,
343                 span,
344                 item_def_id,
345                 item_segment,
346                 trait_ref.substs,
347             );
348             self.tcx().mk_projection(item_def_id, item_substs)
349         } else {
350             // There are no late-bound regions; we can just ignore the binder.
351             let mut err = struct_span_err!(
352                 self.tcx().sess,
353                 span,
354                 E0212,
355                 "cannot extract an associated type from a higher-ranked trait bound \
356                  in this context"
357             );
358
359             match self.node() {
360                 hir::Node::Field(_) | hir::Node::Ctor(_) | hir::Node::Variant(_) => {
361                     let item =
362                         self.tcx.hir().expect_item(self.tcx.hir().get_parent_item(self.hir_id()));
363                     match &item.kind {
364                         hir::ItemKind::Enum(_, generics)
365                         | hir::ItemKind::Struct(_, generics)
366                         | hir::ItemKind::Union(_, generics) => {
367                             let lt_name = get_new_lifetime_name(self.tcx, poly_trait_ref, generics);
368                             let (lt_sp, sugg) = match &generics.params[..] {
369                                 [] => (generics.span, format!("<{}>", lt_name)),
370                                 [bound, ..] => {
371                                     (bound.span.shrink_to_lo(), format!("{}, ", lt_name))
372                                 }
373                             };
374                             let suggestions = vec![
375                                 (lt_sp, sugg),
376                                 (
377                                     span,
378                                     format!(
379                                         "{}::{}",
380                                         // Replace the existing lifetimes with a new named lifetime.
381                                         self.tcx
382                                             .replace_late_bound_regions(&poly_trait_ref, |_| {
383                                                 self.tcx.mk_region(ty::ReEarlyBound(
384                                                     ty::EarlyBoundRegion {
385                                                         def_id: item_def_id,
386                                                         index: 0,
387                                                         name: Symbol::intern(&lt_name),
388                                                     },
389                                                 ))
390                                             })
391                                             .0,
392                                         item_segment.ident
393                                     ),
394                                 ),
395                             ];
396                             err.multipart_suggestion(
397                                 "use a fully qualified path with explicit lifetimes",
398                                 suggestions,
399                                 Applicability::MaybeIncorrect,
400                             );
401                         }
402                         _ => {}
403                     }
404                 }
405                 hir::Node::Item(hir::Item {
406                     kind:
407                         hir::ItemKind::Struct(..) | hir::ItemKind::Enum(..) | hir::ItemKind::Union(..),
408                     ..
409                 }) => {}
410                 hir::Node::Item(_)
411                 | hir::Node::ForeignItem(_)
412                 | hir::Node::TraitItem(_)
413                 | hir::Node::ImplItem(_) => {
414                     err.span_suggestion(
415                         span,
416                         "use a fully qualified path with inferred lifetimes",
417                         format!(
418                             "{}::{}",
419                             // Erase named lt, we want `<A as B<'_>::C`, not `<A as B<'a>::C`.
420                             self.tcx.anonymize_late_bound_regions(&poly_trait_ref).skip_binder(),
421                             item_segment.ident
422                         ),
423                         Applicability::MaybeIncorrect,
424                     );
425                 }
426                 _ => {}
427             }
428             err.emit();
429             self.tcx().ty_error()
430         }
431     }
432
433     fn normalize_ty(&self, _span: Span, ty: Ty<'tcx>) -> Ty<'tcx> {
434         // Types in item signatures are not normalized to avoid undue dependencies.
435         ty
436     }
437
438     fn set_tainted_by_errors(&self) {
439         // There's no obvious place to track this, so just let it go.
440     }
441
442     fn record_ty(&self, _hir_id: hir::HirId, _ty: Ty<'tcx>, _span: Span) {
443         // There's no place to record types from signatures?
444     }
445 }
446
447 /// Synthesize a new lifetime name that doesn't clash with any of the lifetimes already present.
448 fn get_new_lifetime_name<'tcx>(
449     tcx: TyCtxt<'tcx>,
450     poly_trait_ref: ty::PolyTraitRef<'tcx>,
451     generics: &hir::Generics<'tcx>,
452 ) -> String {
453     let existing_lifetimes = tcx
454         .collect_referenced_late_bound_regions(&poly_trait_ref)
455         .into_iter()
456         .filter_map(|lt| {
457             if let ty::BoundRegion::BrNamed(_, name) = lt {
458                 Some(name.as_str().to_string())
459             } else {
460                 None
461             }
462         })
463         .chain(generics.params.iter().filter_map(|param| {
464             if let hir::GenericParamKind::Lifetime { .. } = &param.kind {
465                 Some(param.name.ident().as_str().to_string())
466             } else {
467                 None
468             }
469         }))
470         .collect::<FxHashSet<String>>();
471
472     let a_to_z_repeat_n = |n| {
473         (b'a'..=b'z').map(move |c| {
474             let mut s = '\''.to_string();
475             s.extend(std::iter::repeat(char::from(c)).take(n));
476             s
477         })
478     };
479
480     // If all single char lifetime names are present, we wrap around and double the chars.
481     (1..).flat_map(a_to_z_repeat_n).find(|lt| !existing_lifetimes.contains(lt.as_str())).unwrap()
482 }
483
484 /// Returns the predicates defined on `item_def_id` of the form
485 /// `X: Foo` where `X` is the type parameter `def_id`.
486 fn type_param_predicates(
487     tcx: TyCtxt<'_>,
488     (item_def_id, def_id): (DefId, LocalDefId),
489 ) -> ty::GenericPredicates<'_> {
490     use rustc_hir::*;
491
492     // In the AST, bounds can derive from two places. Either
493     // written inline like `<T: Foo>` or in a where-clause like
494     // `where T: Foo`.
495
496     let param_id = tcx.hir().local_def_id_to_hir_id(def_id);
497     let param_owner = tcx.hir().ty_param_owner(param_id);
498     let param_owner_def_id = tcx.hir().local_def_id(param_owner);
499     let generics = tcx.generics_of(param_owner_def_id);
500     let index = generics.param_def_id_to_index[&def_id.to_def_id()];
501     let ty = tcx.mk_ty_param(index, tcx.hir().ty_param_name(param_id));
502
503     // Don't look for bounds where the type parameter isn't in scope.
504     let parent = if item_def_id == param_owner_def_id.to_def_id() {
505         None
506     } else {
507         tcx.generics_of(item_def_id).parent
508     };
509
510     let mut result = parent
511         .map(|parent| {
512             let icx = ItemCtxt::new(tcx, parent);
513             icx.get_type_parameter_bounds(DUMMY_SP, def_id.to_def_id())
514         })
515         .unwrap_or_default();
516     let mut extend = None;
517
518     let item_hir_id = tcx.hir().local_def_id_to_hir_id(item_def_id.expect_local());
519     let ast_generics = match tcx.hir().get(item_hir_id) {
520         Node::TraitItem(item) => &item.generics,
521
522         Node::ImplItem(item) => &item.generics,
523
524         Node::Item(item) => {
525             match item.kind {
526                 ItemKind::Fn(.., ref generics, _)
527                 | ItemKind::Impl { ref generics, .. }
528                 | ItemKind::TyAlias(_, ref generics)
529                 | ItemKind::OpaqueTy(OpaqueTy { ref generics, impl_trait_fn: None, .. })
530                 | ItemKind::Enum(_, ref generics)
531                 | ItemKind::Struct(_, ref generics)
532                 | ItemKind::Union(_, ref generics) => generics,
533                 ItemKind::Trait(_, _, ref generics, ..) => {
534                     // Implied `Self: Trait` and supertrait bounds.
535                     if param_id == item_hir_id {
536                         let identity_trait_ref = ty::TraitRef::identity(tcx, item_def_id);
537                         extend =
538                             Some((identity_trait_ref.without_const().to_predicate(tcx), item.span));
539                     }
540                     generics
541                 }
542                 _ => return result,
543             }
544         }
545
546         Node::ForeignItem(item) => match item.kind {
547             ForeignItemKind::Fn(_, _, ref generics) => generics,
548             _ => return result,
549         },
550
551         _ => return result,
552     };
553
554     let icx = ItemCtxt::new(tcx, item_def_id);
555     let extra_predicates = extend.into_iter().chain(
556         icx.type_parameter_bounds_in_generics(ast_generics, param_id, ty, OnlySelfBounds(true))
557             .into_iter()
558             .filter(|(predicate, _)| match predicate.skip_binders() {
559                 ty::PredicateAtom::Trait(data, _) => data.self_ty().is_param(index),
560                 _ => false,
561             }),
562     );
563     result.predicates =
564         tcx.arena.alloc_from_iter(result.predicates.iter().copied().chain(extra_predicates));
565     result
566 }
567
568 impl ItemCtxt<'tcx> {
569     /// Finds bounds from `hir::Generics`. This requires scanning through the
570     /// AST. We do this to avoid having to convert *all* the bounds, which
571     /// would create artificial cycles. Instead, we can only convert the
572     /// bounds for a type parameter `X` if `X::Foo` is used.
573     fn type_parameter_bounds_in_generics(
574         &self,
575         ast_generics: &'tcx hir::Generics<'tcx>,
576         param_id: hir::HirId,
577         ty: Ty<'tcx>,
578         only_self_bounds: OnlySelfBounds,
579     ) -> Vec<(ty::Predicate<'tcx>, Span)> {
580         let constness = self.default_constness_for_trait_bounds();
581         let from_ty_params = ast_generics
582             .params
583             .iter()
584             .filter_map(|param| match param.kind {
585                 GenericParamKind::Type { .. } if param.hir_id == param_id => Some(&param.bounds),
586                 _ => None,
587             })
588             .flat_map(|bounds| bounds.iter())
589             .flat_map(|b| predicates_from_bound(self, ty, b, constness));
590
591         let from_where_clauses = ast_generics
592             .where_clause
593             .predicates
594             .iter()
595             .filter_map(|wp| match *wp {
596                 hir::WherePredicate::BoundPredicate(ref bp) => Some(bp),
597                 _ => None,
598             })
599             .flat_map(|bp| {
600                 let bt = if is_param(self.tcx, &bp.bounded_ty, param_id) {
601                     Some(ty)
602                 } else if !only_self_bounds.0 {
603                     Some(self.to_ty(&bp.bounded_ty))
604                 } else {
605                     None
606                 };
607                 bp.bounds.iter().filter_map(move |b| bt.map(|bt| (bt, b)))
608             })
609             .flat_map(|(bt, b)| predicates_from_bound(self, bt, b, constness));
610
611         from_ty_params.chain(from_where_clauses).collect()
612     }
613 }
614
615 /// Tests whether this is the AST for a reference to the type
616 /// parameter with ID `param_id`. We use this so as to avoid running
617 /// `ast_ty_to_ty`, because we want to avoid triggering an all-out
618 /// conversion of the type to avoid inducing unnecessary cycles.
619 fn is_param(tcx: TyCtxt<'_>, ast_ty: &hir::Ty<'_>, param_id: hir::HirId) -> bool {
620     if let hir::TyKind::Path(hir::QPath::Resolved(None, ref path)) = ast_ty.kind {
621         match path.res {
622             Res::SelfTy(Some(def_id), None) | Res::Def(DefKind::TyParam, def_id) => {
623                 def_id == tcx.hir().local_def_id(param_id).to_def_id()
624             }
625             _ => false,
626         }
627     } else {
628         false
629     }
630 }
631
632 fn convert_item(tcx: TyCtxt<'_>, item_id: hir::HirId) {
633     let it = tcx.hir().expect_item(item_id);
634     debug!("convert: item {} with id {}", it.ident, it.hir_id);
635     let def_id = tcx.hir().local_def_id(item_id);
636     match it.kind {
637         // These don't define types.
638         hir::ItemKind::ExternCrate(_)
639         | hir::ItemKind::Use(..)
640         | hir::ItemKind::Mod(_)
641         | hir::ItemKind::GlobalAsm(_) => {}
642         hir::ItemKind::ForeignMod(ref foreign_mod) => {
643             for item in foreign_mod.items {
644                 let def_id = tcx.hir().local_def_id(item.hir_id);
645                 tcx.ensure().generics_of(def_id);
646                 tcx.ensure().type_of(def_id);
647                 tcx.ensure().predicates_of(def_id);
648                 if let hir::ForeignItemKind::Fn(..) = item.kind {
649                     tcx.ensure().fn_sig(def_id);
650                 }
651             }
652         }
653         hir::ItemKind::Enum(ref enum_definition, _) => {
654             tcx.ensure().generics_of(def_id);
655             tcx.ensure().type_of(def_id);
656             tcx.ensure().predicates_of(def_id);
657             convert_enum_variant_types(tcx, def_id.to_def_id(), &enum_definition.variants);
658         }
659         hir::ItemKind::Impl { .. } => {
660             tcx.ensure().generics_of(def_id);
661             tcx.ensure().type_of(def_id);
662             tcx.ensure().impl_trait_ref(def_id);
663             tcx.ensure().predicates_of(def_id);
664         }
665         hir::ItemKind::Trait(..) => {
666             tcx.ensure().generics_of(def_id);
667             tcx.ensure().trait_def(def_id);
668             tcx.at(it.span).super_predicates_of(def_id);
669             tcx.ensure().predicates_of(def_id);
670         }
671         hir::ItemKind::TraitAlias(..) => {
672             tcx.ensure().generics_of(def_id);
673             tcx.at(it.span).super_predicates_of(def_id);
674             tcx.ensure().predicates_of(def_id);
675         }
676         hir::ItemKind::Struct(ref struct_def, _) | hir::ItemKind::Union(ref struct_def, _) => {
677             tcx.ensure().generics_of(def_id);
678             tcx.ensure().type_of(def_id);
679             tcx.ensure().predicates_of(def_id);
680
681             for f in struct_def.fields() {
682                 let def_id = tcx.hir().local_def_id(f.hir_id);
683                 tcx.ensure().generics_of(def_id);
684                 tcx.ensure().type_of(def_id);
685                 tcx.ensure().predicates_of(def_id);
686             }
687
688             if let Some(ctor_hir_id) = struct_def.ctor_hir_id() {
689                 convert_variant_ctor(tcx, ctor_hir_id);
690             }
691         }
692
693         // Desugared from `impl Trait`, so visited by the function's return type.
694         hir::ItemKind::OpaqueTy(hir::OpaqueTy { impl_trait_fn: Some(_), .. }) => {}
695
696         hir::ItemKind::OpaqueTy(..)
697         | hir::ItemKind::TyAlias(..)
698         | hir::ItemKind::Static(..)
699         | hir::ItemKind::Const(..)
700         | hir::ItemKind::Fn(..) => {
701             tcx.ensure().generics_of(def_id);
702             tcx.ensure().type_of(def_id);
703             tcx.ensure().predicates_of(def_id);
704             if let hir::ItemKind::Fn(..) = it.kind {
705                 tcx.ensure().fn_sig(def_id);
706             }
707         }
708     }
709 }
710
711 fn convert_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::HirId) {
712     let trait_item = tcx.hir().expect_trait_item(trait_item_id);
713     let def_id = tcx.hir().local_def_id(trait_item.hir_id);
714     tcx.ensure().generics_of(def_id);
715
716     match trait_item.kind {
717         hir::TraitItemKind::Fn(..) => {
718             tcx.ensure().type_of(def_id);
719             tcx.ensure().fn_sig(def_id);
720         }
721
722         hir::TraitItemKind::Const(.., Some(_)) => {
723             tcx.ensure().type_of(def_id);
724         }
725
726         hir::TraitItemKind::Const(..) | hir::TraitItemKind::Type(_, Some(_)) => {
727             tcx.ensure().type_of(def_id);
728             // Account for `const C: _;` and `type T = _;`.
729             let mut visitor = PlaceholderHirTyCollector::default();
730             visitor.visit_trait_item(trait_item);
731             placeholder_type_error(tcx, None, &[], visitor.0, false);
732         }
733
734         hir::TraitItemKind::Type(_, None) => {
735             // #74612: Visit and try to find bad placeholders
736             // even if there is no concrete type.
737             let mut visitor = PlaceholderHirTyCollector::default();
738             visitor.visit_trait_item(trait_item);
739             placeholder_type_error(tcx, None, &[], visitor.0, false);
740         }
741     };
742
743     tcx.ensure().predicates_of(def_id);
744 }
745
746 fn convert_impl_item(tcx: TyCtxt<'_>, impl_item_id: hir::HirId) {
747     let def_id = tcx.hir().local_def_id(impl_item_id);
748     tcx.ensure().generics_of(def_id);
749     tcx.ensure().type_of(def_id);
750     tcx.ensure().predicates_of(def_id);
751     let impl_item = tcx.hir().expect_impl_item(impl_item_id);
752     match impl_item.kind {
753         hir::ImplItemKind::Fn(..) => {
754             tcx.ensure().fn_sig(def_id);
755         }
756         hir::ImplItemKind::TyAlias(_) => {
757             // Account for `type T = _;`
758             let mut visitor = PlaceholderHirTyCollector::default();
759             visitor.visit_impl_item(impl_item);
760             placeholder_type_error(tcx, None, &[], visitor.0, false);
761         }
762         hir::ImplItemKind::Const(..) => {}
763     }
764 }
765
766 fn convert_variant_ctor(tcx: TyCtxt<'_>, ctor_id: hir::HirId) {
767     let def_id = tcx.hir().local_def_id(ctor_id);
768     tcx.ensure().generics_of(def_id);
769     tcx.ensure().type_of(def_id);
770     tcx.ensure().predicates_of(def_id);
771 }
772
773 fn convert_enum_variant_types(tcx: TyCtxt<'_>, def_id: DefId, variants: &[hir::Variant<'_>]) {
774     let def = tcx.adt_def(def_id);
775     let repr_type = def.repr.discr_type();
776     let initial = repr_type.initial_discriminant(tcx);
777     let mut prev_discr = None::<Discr<'_>>;
778
779     // fill the discriminant values and field types
780     for variant in variants {
781         let wrapped_discr = prev_discr.map_or(initial, |d| d.wrap_incr(tcx));
782         prev_discr = Some(
783             if let Some(ref e) = variant.disr_expr {
784                 let expr_did = tcx.hir().local_def_id(e.hir_id);
785                 def.eval_explicit_discr(tcx, expr_did.to_def_id())
786             } else if let Some(discr) = repr_type.disr_incr(tcx, prev_discr) {
787                 Some(discr)
788             } else {
789                 struct_span_err!(tcx.sess, variant.span, E0370, "enum discriminant overflowed")
790                     .span_label(
791                         variant.span,
792                         format!("overflowed on value after {}", prev_discr.unwrap()),
793                     )
794                     .note(&format!(
795                         "explicitly set `{} = {}` if that is desired outcome",
796                         variant.ident, wrapped_discr
797                     ))
798                     .emit();
799                 None
800             }
801             .unwrap_or(wrapped_discr),
802         );
803
804         for f in variant.data.fields() {
805             let def_id = tcx.hir().local_def_id(f.hir_id);
806             tcx.ensure().generics_of(def_id);
807             tcx.ensure().type_of(def_id);
808             tcx.ensure().predicates_of(def_id);
809         }
810
811         // Convert the ctor, if any. This also registers the variant as
812         // an item.
813         if let Some(ctor_hir_id) = variant.data.ctor_hir_id() {
814             convert_variant_ctor(tcx, ctor_hir_id);
815         }
816     }
817 }
818
819 fn convert_variant(
820     tcx: TyCtxt<'_>,
821     variant_did: Option<LocalDefId>,
822     ctor_did: Option<LocalDefId>,
823     ident: Ident,
824     discr: ty::VariantDiscr,
825     def: &hir::VariantData<'_>,
826     adt_kind: ty::AdtKind,
827     parent_did: LocalDefId,
828 ) -> ty::VariantDef {
829     let mut seen_fields: FxHashMap<Ident, Span> = Default::default();
830     let hir_id = tcx.hir().local_def_id_to_hir_id(variant_did.unwrap_or(parent_did));
831     let fields = def
832         .fields()
833         .iter()
834         .map(|f| {
835             let fid = tcx.hir().local_def_id(f.hir_id);
836             let dup_span = seen_fields.get(&f.ident.normalize_to_macros_2_0()).cloned();
837             if let Some(prev_span) = dup_span {
838                 tcx.sess.emit_err(errors::FieldAlreadyDeclared {
839                     field_name: f.ident,
840                     span: f.span,
841                     prev_span,
842                 });
843             } else {
844                 seen_fields.insert(f.ident.normalize_to_macros_2_0(), f.span);
845             }
846
847             ty::FieldDef {
848                 did: fid.to_def_id(),
849                 ident: f.ident,
850                 vis: ty::Visibility::from_hir(&f.vis, hir_id, tcx),
851             }
852         })
853         .collect();
854     let recovered = match def {
855         hir::VariantData::Struct(_, r) => *r,
856         _ => false,
857     };
858     ty::VariantDef::new(
859         ident,
860         variant_did.map(LocalDefId::to_def_id),
861         ctor_did.map(LocalDefId::to_def_id),
862         discr,
863         fields,
864         CtorKind::from_hir(def),
865         adt_kind,
866         parent_did.to_def_id(),
867         recovered,
868         adt_kind == AdtKind::Struct && tcx.has_attr(parent_did.to_def_id(), sym::non_exhaustive)
869             || variant_did.map_or(false, |variant_did| {
870                 tcx.has_attr(variant_did.to_def_id(), sym::non_exhaustive)
871             }),
872     )
873 }
874
875 fn adt_def(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::AdtDef {
876     use rustc_hir::*;
877
878     let def_id = def_id.expect_local();
879     let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
880     let item = match tcx.hir().get(hir_id) {
881         Node::Item(item) => item,
882         _ => bug!(),
883     };
884
885     let repr = ReprOptions::new(tcx, def_id.to_def_id());
886     let (kind, variants) = match item.kind {
887         ItemKind::Enum(ref def, _) => {
888             let mut distance_from_explicit = 0;
889             let variants = def
890                 .variants
891                 .iter()
892                 .map(|v| {
893                     let variant_did = Some(tcx.hir().local_def_id(v.id));
894                     let ctor_did =
895                         v.data.ctor_hir_id().map(|hir_id| tcx.hir().local_def_id(hir_id));
896
897                     let discr = if let Some(ref e) = v.disr_expr {
898                         distance_from_explicit = 0;
899                         ty::VariantDiscr::Explicit(tcx.hir().local_def_id(e.hir_id).to_def_id())
900                     } else {
901                         ty::VariantDiscr::Relative(distance_from_explicit)
902                     };
903                     distance_from_explicit += 1;
904
905                     convert_variant(
906                         tcx,
907                         variant_did,
908                         ctor_did,
909                         v.ident,
910                         discr,
911                         &v.data,
912                         AdtKind::Enum,
913                         def_id,
914                     )
915                 })
916                 .collect();
917
918             (AdtKind::Enum, variants)
919         }
920         ItemKind::Struct(ref def, _) => {
921             let variant_did = None::<LocalDefId>;
922             let ctor_did = def.ctor_hir_id().map(|hir_id| tcx.hir().local_def_id(hir_id));
923
924             let variants = std::iter::once(convert_variant(
925                 tcx,
926                 variant_did,
927                 ctor_did,
928                 item.ident,
929                 ty::VariantDiscr::Relative(0),
930                 def,
931                 AdtKind::Struct,
932                 def_id,
933             ))
934             .collect();
935
936             (AdtKind::Struct, variants)
937         }
938         ItemKind::Union(ref def, _) => {
939             let variant_did = None;
940             let ctor_did = def.ctor_hir_id().map(|hir_id| tcx.hir().local_def_id(hir_id));
941
942             let variants = std::iter::once(convert_variant(
943                 tcx,
944                 variant_did,
945                 ctor_did,
946                 item.ident,
947                 ty::VariantDiscr::Relative(0),
948                 def,
949                 AdtKind::Union,
950                 def_id,
951             ))
952             .collect();
953
954             (AdtKind::Union, variants)
955         }
956         _ => bug!(),
957     };
958     tcx.alloc_adt_def(def_id.to_def_id(), kind, variants, repr)
959 }
960
961 /// Ensures that the super-predicates of the trait with a `DefId`
962 /// of `trait_def_id` are converted and stored. This also ensures that
963 /// the transitive super-predicates are converted.
964 fn super_predicates_of(tcx: TyCtxt<'_>, trait_def_id: DefId) -> ty::GenericPredicates<'_> {
965     debug!("super_predicates(trait_def_id={:?})", trait_def_id);
966     let trait_hir_id = tcx.hir().local_def_id_to_hir_id(trait_def_id.expect_local());
967
968     let item = match tcx.hir().get(trait_hir_id) {
969         Node::Item(item) => item,
970         _ => bug!("trait_node_id {} is not an item", trait_hir_id),
971     };
972
973     let (generics, bounds) = match item.kind {
974         hir::ItemKind::Trait(.., ref generics, ref supertraits, _) => (generics, supertraits),
975         hir::ItemKind::TraitAlias(ref generics, ref supertraits) => (generics, supertraits),
976         _ => span_bug!(item.span, "super_predicates invoked on non-trait"),
977     };
978
979     let icx = ItemCtxt::new(tcx, trait_def_id);
980
981     // Convert the bounds that follow the colon, e.g., `Bar + Zed` in `trait Foo: Bar + Zed`.
982     let self_param_ty = tcx.types.self_param;
983     let superbounds1 =
984         AstConv::compute_bounds(&icx, self_param_ty, bounds, SizedByDefault::No, item.span);
985
986     let superbounds1 = superbounds1.predicates(tcx, self_param_ty);
987
988     // Convert any explicit superbounds in the where-clause,
989     // e.g., `trait Foo where Self: Bar`.
990     // In the case of trait aliases, however, we include all bounds in the where-clause,
991     // so e.g., `trait Foo = where u32: PartialEq<Self>` would include `u32: PartialEq<Self>`
992     // as one of its "superpredicates".
993     let is_trait_alias = tcx.is_trait_alias(trait_def_id);
994     let superbounds2 = icx.type_parameter_bounds_in_generics(
995         generics,
996         item.hir_id,
997         self_param_ty,
998         OnlySelfBounds(!is_trait_alias),
999     );
1000
1001     // Combine the two lists to form the complete set of superbounds:
1002     let superbounds = &*tcx.arena.alloc_from_iter(superbounds1.into_iter().chain(superbounds2));
1003
1004     // Now require that immediate supertraits are converted,
1005     // which will, in turn, reach indirect supertraits.
1006     for &(pred, span) in superbounds {
1007         debug!("superbound: {:?}", pred);
1008         if let ty::PredicateAtom::Trait(bound, _) = pred.skip_binders() {
1009             tcx.at(span).super_predicates_of(bound.def_id());
1010         }
1011     }
1012
1013     ty::GenericPredicates { parent: None, predicates: superbounds }
1014 }
1015
1016 fn trait_def(tcx: TyCtxt<'_>, def_id: DefId) -> ty::TraitDef {
1017     let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
1018     let item = tcx.hir().expect_item(hir_id);
1019
1020     let (is_auto, unsafety) = match item.kind {
1021         hir::ItemKind::Trait(is_auto, unsafety, ..) => (is_auto == hir::IsAuto::Yes, unsafety),
1022         hir::ItemKind::TraitAlias(..) => (false, hir::Unsafety::Normal),
1023         _ => span_bug!(item.span, "trait_def_of_item invoked on non-trait"),
1024     };
1025
1026     let paren_sugar = tcx.has_attr(def_id, sym::rustc_paren_sugar);
1027     if paren_sugar && !tcx.features().unboxed_closures {
1028         tcx.sess
1029             .struct_span_err(
1030                 item.span,
1031                 "the `#[rustc_paren_sugar]` attribute is a temporary means of controlling \
1032                  which traits can use parenthetical notation",
1033             )
1034             .help("add `#![feature(unboxed_closures)]` to the crate attributes to use it")
1035             .emit();
1036     }
1037
1038     let is_marker = tcx.has_attr(def_id, sym::marker);
1039     let spec_kind = if tcx.has_attr(def_id, sym::rustc_unsafe_specialization_marker) {
1040         ty::trait_def::TraitSpecializationKind::Marker
1041     } else if tcx.has_attr(def_id, sym::rustc_specialization_trait) {
1042         ty::trait_def::TraitSpecializationKind::AlwaysApplicable
1043     } else {
1044         ty::trait_def::TraitSpecializationKind::None
1045     };
1046     let def_path_hash = tcx.def_path_hash(def_id);
1047     ty::TraitDef::new(def_id, unsafety, paren_sugar, is_auto, is_marker, spec_kind, def_path_hash)
1048 }
1049
1050 fn has_late_bound_regions<'tcx>(tcx: TyCtxt<'tcx>, node: Node<'tcx>) -> Option<Span> {
1051     struct LateBoundRegionsDetector<'tcx> {
1052         tcx: TyCtxt<'tcx>,
1053         outer_index: ty::DebruijnIndex,
1054         has_late_bound_regions: Option<Span>,
1055     }
1056
1057     impl Visitor<'tcx> for LateBoundRegionsDetector<'tcx> {
1058         type Map = intravisit::ErasedMap<'tcx>;
1059
1060         fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
1061             NestedVisitorMap::None
1062         }
1063
1064         fn visit_ty(&mut self, ty: &'tcx hir::Ty<'tcx>) {
1065             if self.has_late_bound_regions.is_some() {
1066                 return;
1067             }
1068             match ty.kind {
1069                 hir::TyKind::BareFn(..) => {
1070                     self.outer_index.shift_in(1);
1071                     intravisit::walk_ty(self, ty);
1072                     self.outer_index.shift_out(1);
1073                 }
1074                 _ => intravisit::walk_ty(self, ty),
1075             }
1076         }
1077
1078         fn visit_poly_trait_ref(
1079             &mut self,
1080             tr: &'tcx hir::PolyTraitRef<'tcx>,
1081             m: hir::TraitBoundModifier,
1082         ) {
1083             if self.has_late_bound_regions.is_some() {
1084                 return;
1085             }
1086             self.outer_index.shift_in(1);
1087             intravisit::walk_poly_trait_ref(self, tr, m);
1088             self.outer_index.shift_out(1);
1089         }
1090
1091         fn visit_lifetime(&mut self, lt: &'tcx hir::Lifetime) {
1092             if self.has_late_bound_regions.is_some() {
1093                 return;
1094             }
1095
1096             match self.tcx.named_region(lt.hir_id) {
1097                 Some(rl::Region::Static | rl::Region::EarlyBound(..)) => {}
1098                 Some(
1099                     rl::Region::LateBound(debruijn, _, _) | rl::Region::LateBoundAnon(debruijn, _),
1100                 ) if debruijn < self.outer_index => {}
1101                 Some(
1102                     rl::Region::LateBound(..)
1103                     | rl::Region::LateBoundAnon(..)
1104                     | rl::Region::Free(..),
1105                 )
1106                 | None => {
1107                     self.has_late_bound_regions = Some(lt.span);
1108                 }
1109             }
1110         }
1111     }
1112
1113     fn has_late_bound_regions<'tcx>(
1114         tcx: TyCtxt<'tcx>,
1115         generics: &'tcx hir::Generics<'tcx>,
1116         decl: &'tcx hir::FnDecl<'tcx>,
1117     ) -> Option<Span> {
1118         let mut visitor = LateBoundRegionsDetector {
1119             tcx,
1120             outer_index: ty::INNERMOST,
1121             has_late_bound_regions: None,
1122         };
1123         for param in generics.params {
1124             if let GenericParamKind::Lifetime { .. } = param.kind {
1125                 if tcx.is_late_bound(param.hir_id) {
1126                     return Some(param.span);
1127                 }
1128             }
1129         }
1130         visitor.visit_fn_decl(decl);
1131         visitor.has_late_bound_regions
1132     }
1133
1134     match node {
1135         Node::TraitItem(item) => match item.kind {
1136             hir::TraitItemKind::Fn(ref sig, _) => {
1137                 has_late_bound_regions(tcx, &item.generics, &sig.decl)
1138             }
1139             _ => None,
1140         },
1141         Node::ImplItem(item) => match item.kind {
1142             hir::ImplItemKind::Fn(ref sig, _) => {
1143                 has_late_bound_regions(tcx, &item.generics, &sig.decl)
1144             }
1145             _ => None,
1146         },
1147         Node::ForeignItem(item) => match item.kind {
1148             hir::ForeignItemKind::Fn(ref fn_decl, _, ref generics) => {
1149                 has_late_bound_regions(tcx, generics, fn_decl)
1150             }
1151             _ => None,
1152         },
1153         Node::Item(item) => match item.kind {
1154             hir::ItemKind::Fn(ref sig, .., ref generics, _) => {
1155                 has_late_bound_regions(tcx, generics, &sig.decl)
1156             }
1157             _ => None,
1158         },
1159         _ => None,
1160     }
1161 }
1162
1163 struct AnonConstInParamListDetector {
1164     in_param_list: bool,
1165     found_anon_const_in_list: bool,
1166     ct: HirId,
1167 }
1168
1169 impl<'v> Visitor<'v> for AnonConstInParamListDetector {
1170     type Map = intravisit::ErasedMap<'v>;
1171
1172     fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
1173         NestedVisitorMap::None
1174     }
1175
1176     fn visit_generic_param(&mut self, p: &'v hir::GenericParam<'v>) {
1177         let prev = self.in_param_list;
1178         self.in_param_list = true;
1179         intravisit::walk_generic_param(self, p);
1180         self.in_param_list = prev;
1181     }
1182
1183     fn visit_anon_const(&mut self, c: &'v hir::AnonConst) {
1184         if self.in_param_list && self.ct == c.hir_id {
1185             self.found_anon_const_in_list = true;
1186         } else {
1187             intravisit::walk_anon_const(self, c)
1188         }
1189     }
1190 }
1191
1192 fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::Generics {
1193     use rustc_hir::*;
1194
1195     let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
1196
1197     let node = tcx.hir().get(hir_id);
1198     let parent_def_id = match node {
1199         Node::ImplItem(_)
1200         | Node::TraitItem(_)
1201         | Node::Variant(_)
1202         | Node::Ctor(..)
1203         | Node::Field(_) => {
1204             let parent_id = tcx.hir().get_parent_item(hir_id);
1205             Some(tcx.hir().local_def_id(parent_id).to_def_id())
1206         }
1207         // FIXME(#43408) always enable this once `lazy_normalization` is
1208         // stable enough and does not need a feature gate anymore.
1209         Node::AnonConst(_) => {
1210             let parent_id = tcx.hir().get_parent_item(hir_id);
1211             let parent_def_id = tcx.hir().local_def_id(parent_id);
1212
1213             let mut in_param_list = false;
1214             for (_parent, node) in tcx.hir().parent_iter(hir_id) {
1215                 if let Some(generics) = node.generics() {
1216                     let mut visitor = AnonConstInParamListDetector {
1217                         in_param_list: false,
1218                         found_anon_const_in_list: false,
1219                         ct: hir_id,
1220                     };
1221
1222                     visitor.visit_generics(generics);
1223                     in_param_list = visitor.found_anon_const_in_list;
1224                     break;
1225                 }
1226             }
1227
1228             if in_param_list {
1229                 // We do not allow generic parameters in anon consts if we are inside
1230                 // of a param list.
1231                 //
1232                 // This affects both default type bindings, e.g. `struct<T, U = [u8; std::mem::size_of::<T>()]>(T, U)`,
1233                 // and the types of const parameters, e.g. `struct V<const N: usize, const M: [u8; N]>();`.
1234                 None
1235             } else if tcx.lazy_normalization() {
1236                 // HACK(eddyb) this provides the correct generics when
1237                 // `feature(const_generics)` is enabled, so that const expressions
1238                 // used with const generics, e.g. `Foo<{N+1}>`, can work at all.
1239                 //
1240                 // Note that we do not supply the parent generics when using
1241                 // `feature(min_const_generics)`.
1242                 Some(parent_def_id.to_def_id())
1243             } else {
1244                 let parent_node = tcx.hir().get(tcx.hir().get_parent_node(hir_id));
1245                 match parent_node {
1246                     // HACK(eddyb) this provides the correct generics for repeat
1247                     // expressions' count (i.e. `N` in `[x; N]`), and explicit
1248                     // `enum` discriminants (i.e. `D` in `enum Foo { Bar = D }`),
1249                     // as they shouldn't be able to cause query cycle errors.
1250                     Node::Expr(&Expr { kind: ExprKind::Repeat(_, ref constant), .. })
1251                     | Node::Variant(Variant { disr_expr: Some(ref constant), .. })
1252                         if constant.hir_id == hir_id =>
1253                     {
1254                         Some(parent_def_id.to_def_id())
1255                     }
1256
1257                     _ => None,
1258                 }
1259             }
1260         }
1261         Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure(..), .. }) => {
1262             Some(tcx.closure_base_def_id(def_id))
1263         }
1264         Node::Item(item) => match item.kind {
1265             ItemKind::OpaqueTy(hir::OpaqueTy { impl_trait_fn, .. }) => {
1266                 impl_trait_fn.or_else(|| {
1267                     let parent_id = tcx.hir().get_parent_item(hir_id);
1268                     assert!(parent_id != hir_id && parent_id != CRATE_HIR_ID);
1269                     debug!("generics_of: parent of opaque ty {:?} is {:?}", def_id, parent_id);
1270                     // Opaque types are always nested within another item, and
1271                     // inherit the generics of the item.
1272                     Some(tcx.hir().local_def_id(parent_id).to_def_id())
1273                 })
1274             }
1275             _ => None,
1276         },
1277         _ => None,
1278     };
1279
1280     let mut opt_self = None;
1281     let mut allow_defaults = false;
1282
1283     let no_generics = hir::Generics::empty();
1284     let ast_generics = match node {
1285         Node::TraitItem(item) => &item.generics,
1286
1287         Node::ImplItem(item) => &item.generics,
1288
1289         Node::Item(item) => {
1290             match item.kind {
1291                 ItemKind::Fn(.., ref generics, _) | ItemKind::Impl { ref generics, .. } => generics,
1292
1293                 ItemKind::TyAlias(_, ref generics)
1294                 | ItemKind::Enum(_, ref generics)
1295                 | ItemKind::Struct(_, ref generics)
1296                 | ItemKind::OpaqueTy(hir::OpaqueTy { ref generics, .. })
1297                 | ItemKind::Union(_, ref generics) => {
1298                     allow_defaults = true;
1299                     generics
1300                 }
1301
1302                 ItemKind::Trait(_, _, ref generics, ..)
1303                 | ItemKind::TraitAlias(ref generics, ..) => {
1304                     // Add in the self type parameter.
1305                     //
1306                     // Something of a hack: use the node id for the trait, also as
1307                     // the node id for the Self type parameter.
1308                     let param_id = item.hir_id;
1309
1310                     opt_self = Some(ty::GenericParamDef {
1311                         index: 0,
1312                         name: kw::SelfUpper,
1313                         def_id: tcx.hir().local_def_id(param_id).to_def_id(),
1314                         pure_wrt_drop: false,
1315                         kind: ty::GenericParamDefKind::Type {
1316                             has_default: false,
1317                             object_lifetime_default: rl::Set1::Empty,
1318                             synthetic: None,
1319                         },
1320                     });
1321
1322                     allow_defaults = true;
1323                     generics
1324                 }
1325
1326                 _ => &no_generics,
1327             }
1328         }
1329
1330         Node::ForeignItem(item) => match item.kind {
1331             ForeignItemKind::Static(..) => &no_generics,
1332             ForeignItemKind::Fn(_, _, ref generics) => generics,
1333             ForeignItemKind::Type => &no_generics,
1334         },
1335
1336         _ => &no_generics,
1337     };
1338
1339     let has_self = opt_self.is_some();
1340     let mut parent_has_self = false;
1341     let mut own_start = has_self as u32;
1342     let parent_count = parent_def_id.map_or(0, |def_id| {
1343         let generics = tcx.generics_of(def_id);
1344         assert_eq!(has_self, false);
1345         parent_has_self = generics.has_self;
1346         own_start = generics.count() as u32;
1347         generics.parent_count + generics.params.len()
1348     });
1349
1350     let mut params: Vec<_> = opt_self.into_iter().collect();
1351
1352     let early_lifetimes = early_bound_lifetimes_from_generics(tcx, ast_generics);
1353     params.extend(early_lifetimes.enumerate().map(|(i, param)| ty::GenericParamDef {
1354         name: param.name.ident().name,
1355         index: own_start + i as u32,
1356         def_id: tcx.hir().local_def_id(param.hir_id).to_def_id(),
1357         pure_wrt_drop: param.pure_wrt_drop,
1358         kind: ty::GenericParamDefKind::Lifetime,
1359     }));
1360
1361     let object_lifetime_defaults = tcx.object_lifetime_defaults(hir_id);
1362
1363     // Now create the real type and const parameters.
1364     let type_start = own_start - has_self as u32 + params.len() as u32;
1365     let mut i = 0;
1366
1367     params.extend(ast_generics.params.iter().filter_map(|param| match param.kind {
1368         GenericParamKind::Lifetime { .. } => None,
1369         GenericParamKind::Type { ref default, synthetic, .. } => {
1370             if !allow_defaults && default.is_some() {
1371                 if !tcx.features().default_type_parameter_fallback {
1372                     tcx.struct_span_lint_hir(
1373                         lint::builtin::INVALID_TYPE_PARAM_DEFAULT,
1374                         param.hir_id,
1375                         param.span,
1376                         |lint| {
1377                             lint.build(
1378                                 "defaults for type parameters are only allowed in \
1379                                  `struct`, `enum`, `type`, or `trait` definitions.",
1380                             )
1381                             .emit();
1382                         },
1383                     );
1384                 }
1385             }
1386
1387             let kind = ty::GenericParamDefKind::Type {
1388                 has_default: default.is_some(),
1389                 object_lifetime_default: object_lifetime_defaults
1390                     .as_ref()
1391                     .map_or(rl::Set1::Empty, |o| o[i]),
1392                 synthetic,
1393             };
1394
1395             let param_def = ty::GenericParamDef {
1396                 index: type_start + i as u32,
1397                 name: param.name.ident().name,
1398                 def_id: tcx.hir().local_def_id(param.hir_id).to_def_id(),
1399                 pure_wrt_drop: param.pure_wrt_drop,
1400                 kind,
1401             };
1402             i += 1;
1403             Some(param_def)
1404         }
1405         GenericParamKind::Const { .. } => {
1406             let param_def = ty::GenericParamDef {
1407                 index: type_start + i as u32,
1408                 name: param.name.ident().name,
1409                 def_id: tcx.hir().local_def_id(param.hir_id).to_def_id(),
1410                 pure_wrt_drop: param.pure_wrt_drop,
1411                 kind: ty::GenericParamDefKind::Const,
1412             };
1413             i += 1;
1414             Some(param_def)
1415         }
1416     }));
1417
1418     // provide junk type parameter defs - the only place that
1419     // cares about anything but the length is instantiation,
1420     // and we don't do that for closures.
1421     if let Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure(.., gen), .. }) = node {
1422         let dummy_args = if gen.is_some() {
1423             &["<resume_ty>", "<yield_ty>", "<return_ty>", "<witness>", "<upvars>"][..]
1424         } else {
1425             &["<closure_kind>", "<closure_signature>", "<upvars>"][..]
1426         };
1427
1428         params.extend(dummy_args.iter().enumerate().map(|(i, &arg)| ty::GenericParamDef {
1429             index: type_start + i as u32,
1430             name: Symbol::intern(arg),
1431             def_id,
1432             pure_wrt_drop: false,
1433             kind: ty::GenericParamDefKind::Type {
1434                 has_default: false,
1435                 object_lifetime_default: rl::Set1::Empty,
1436                 synthetic: None,
1437             },
1438         }));
1439     }
1440
1441     let param_def_id_to_index = params.iter().map(|param| (param.def_id, param.index)).collect();
1442
1443     ty::Generics {
1444         parent: parent_def_id,
1445         parent_count,
1446         params,
1447         param_def_id_to_index,
1448         has_self: has_self || parent_has_self,
1449         has_late_bound_regions: has_late_bound_regions(tcx, node),
1450     }
1451 }
1452
1453 fn are_suggestable_generic_args(generic_args: &[hir::GenericArg<'_>]) -> bool {
1454     generic_args
1455         .iter()
1456         .filter_map(|arg| match arg {
1457             hir::GenericArg::Type(ty) => Some(ty),
1458             _ => None,
1459         })
1460         .any(is_suggestable_infer_ty)
1461 }
1462
1463 /// Whether `ty` is a type with `_` placeholders that can be inferred. Used in diagnostics only to
1464 /// use inference to provide suggestions for the appropriate type if possible.
1465 fn is_suggestable_infer_ty(ty: &hir::Ty<'_>) -> bool {
1466     use hir::TyKind::*;
1467     match &ty.kind {
1468         Infer => true,
1469         Slice(ty) | Array(ty, _) => is_suggestable_infer_ty(ty),
1470         Tup(tys) => tys.iter().any(is_suggestable_infer_ty),
1471         Ptr(mut_ty) | Rptr(_, mut_ty) => is_suggestable_infer_ty(mut_ty.ty),
1472         OpaqueDef(_, generic_args) => are_suggestable_generic_args(generic_args),
1473         Path(hir::QPath::TypeRelative(ty, segment)) => {
1474             is_suggestable_infer_ty(ty) || are_suggestable_generic_args(segment.generic_args().args)
1475         }
1476         Path(hir::QPath::Resolved(ty_opt, hir::Path { segments, .. })) => {
1477             ty_opt.map_or(false, is_suggestable_infer_ty)
1478                 || segments
1479                     .iter()
1480                     .any(|segment| are_suggestable_generic_args(segment.generic_args().args))
1481         }
1482         _ => false,
1483     }
1484 }
1485
1486 pub fn get_infer_ret_ty(output: &'hir hir::FnRetTy<'hir>) -> Option<&'hir hir::Ty<'hir>> {
1487     if let hir::FnRetTy::Return(ref ty) = output {
1488         if is_suggestable_infer_ty(ty) {
1489             return Some(&**ty);
1490         }
1491     }
1492     None
1493 }
1494
1495 fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> {
1496     use rustc_hir::Node::*;
1497     use rustc_hir::*;
1498
1499     let def_id = def_id.expect_local();
1500     let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
1501
1502     let icx = ItemCtxt::new(tcx, def_id.to_def_id());
1503
1504     match tcx.hir().get(hir_id) {
1505         TraitItem(hir::TraitItem {
1506             kind: TraitItemKind::Fn(sig, TraitFn::Provided(_)),
1507             ident,
1508             generics,
1509             ..
1510         })
1511         | ImplItem(hir::ImplItem { kind: ImplItemKind::Fn(sig, _), ident, generics, .. })
1512         | Item(hir::Item { kind: ItemKind::Fn(sig, generics, _), ident, .. }) => {
1513             match get_infer_ret_ty(&sig.decl.output) {
1514                 Some(ty) => {
1515                     let fn_sig = tcx.typeck(def_id).liberated_fn_sigs()[hir_id];
1516                     let mut visitor = PlaceholderHirTyCollector::default();
1517                     visitor.visit_ty(ty);
1518                     let mut diag = bad_placeholder_type(tcx, visitor.0);
1519                     let ret_ty = fn_sig.output();
1520                     if ret_ty != tcx.ty_error() {
1521                         diag.span_suggestion(
1522                             ty.span,
1523                             "replace with the correct return type",
1524                             ret_ty.to_string(),
1525                             Applicability::MaybeIncorrect,
1526                         );
1527                     }
1528                     diag.emit();
1529                     ty::Binder::bind(fn_sig)
1530                 }
1531                 None => AstConv::ty_of_fn(
1532                     &icx,
1533                     sig.header.unsafety,
1534                     sig.header.abi,
1535                     &sig.decl,
1536                     &generics,
1537                     Some(ident.span),
1538                 ),
1539             }
1540         }
1541
1542         TraitItem(hir::TraitItem {
1543             kind: TraitItemKind::Fn(FnSig { header, decl, span: _ }, _),
1544             ident,
1545             generics,
1546             ..
1547         }) => {
1548             AstConv::ty_of_fn(&icx, header.unsafety, header.abi, decl, &generics, Some(ident.span))
1549         }
1550
1551         ForeignItem(&hir::ForeignItem {
1552             kind: ForeignItemKind::Fn(ref fn_decl, _, _),
1553             ident,
1554             ..
1555         }) => {
1556             let abi = tcx.hir().get_foreign_abi(hir_id);
1557             compute_sig_of_foreign_fn_decl(tcx, def_id.to_def_id(), fn_decl, abi, ident)
1558         }
1559
1560         Ctor(data) | Variant(hir::Variant { data, .. }) if data.ctor_hir_id().is_some() => {
1561             let ty = tcx.type_of(tcx.hir().get_parent_did(hir_id).to_def_id());
1562             let inputs =
1563                 data.fields().iter().map(|f| tcx.type_of(tcx.hir().local_def_id(f.hir_id)));
1564             ty::Binder::bind(tcx.mk_fn_sig(
1565                 inputs,
1566                 ty,
1567                 false,
1568                 hir::Unsafety::Normal,
1569                 abi::Abi::Rust,
1570             ))
1571         }
1572
1573         Expr(&hir::Expr { kind: hir::ExprKind::Closure(..), .. }) => {
1574             // Closure signatures are not like other function
1575             // signatures and cannot be accessed through `fn_sig`. For
1576             // example, a closure signature excludes the `self`
1577             // argument. In any case they are embedded within the
1578             // closure type as part of the `ClosureSubsts`.
1579             //
1580             // To get the signature of a closure, you should use the
1581             // `sig` method on the `ClosureSubsts`:
1582             //
1583             //    substs.as_closure().sig(def_id, tcx)
1584             bug!(
1585                 "to get the signature of a closure, use `substs.as_closure().sig()` not `fn_sig()`",
1586             );
1587         }
1588
1589         x => {
1590             bug!("unexpected sort of node in fn_sig(): {:?}", x);
1591         }
1592     }
1593 }
1594
1595 fn impl_trait_ref(tcx: TyCtxt<'_>, def_id: DefId) -> Option<ty::TraitRef<'_>> {
1596     let icx = ItemCtxt::new(tcx, def_id);
1597
1598     let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
1599     match tcx.hir().expect_item(hir_id).kind {
1600         hir::ItemKind::Impl { ref of_trait, .. } => of_trait.as_ref().map(|ast_trait_ref| {
1601             let selfty = tcx.type_of(def_id);
1602             AstConv::instantiate_mono_trait_ref(&icx, ast_trait_ref, selfty)
1603         }),
1604         _ => bug!(),
1605     }
1606 }
1607
1608 fn impl_polarity(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ImplPolarity {
1609     let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
1610     let is_rustc_reservation = tcx.has_attr(def_id, sym::rustc_reservation_impl);
1611     let item = tcx.hir().expect_item(hir_id);
1612     match &item.kind {
1613         hir::ItemKind::Impl { polarity: hir::ImplPolarity::Negative(span), of_trait, .. } => {
1614             if is_rustc_reservation {
1615                 let span = span.to(of_trait.as_ref().map(|t| t.path.span).unwrap_or(*span));
1616                 tcx.sess.span_err(span, "reservation impls can't be negative");
1617             }
1618             ty::ImplPolarity::Negative
1619         }
1620         hir::ItemKind::Impl { polarity: hir::ImplPolarity::Positive, of_trait: None, .. } => {
1621             if is_rustc_reservation {
1622                 tcx.sess.span_err(item.span, "reservation impls can't be inherent");
1623             }
1624             ty::ImplPolarity::Positive
1625         }
1626         hir::ItemKind::Impl {
1627             polarity: hir::ImplPolarity::Positive, of_trait: Some(_), ..
1628         } => {
1629             if is_rustc_reservation {
1630                 ty::ImplPolarity::Reservation
1631             } else {
1632                 ty::ImplPolarity::Positive
1633             }
1634         }
1635         ref item => bug!("impl_polarity: {:?} not an impl", item),
1636     }
1637 }
1638
1639 /// Returns the early-bound lifetimes declared in this generics
1640 /// listing. For anything other than fns/methods, this is just all
1641 /// the lifetimes that are declared. For fns or methods, we have to
1642 /// screen out those that do not appear in any where-clauses etc using
1643 /// `resolve_lifetime::early_bound_lifetimes`.
1644 fn early_bound_lifetimes_from_generics<'a, 'tcx: 'a>(
1645     tcx: TyCtxt<'tcx>,
1646     generics: &'a hir::Generics<'a>,
1647 ) -> impl Iterator<Item = &'a hir::GenericParam<'a>> + Captures<'tcx> {
1648     generics.params.iter().filter(move |param| match param.kind {
1649         GenericParamKind::Lifetime { .. } => !tcx.is_late_bound(param.hir_id),
1650         _ => false,
1651     })
1652 }
1653
1654 /// Returns a list of type predicates for the definition with ID `def_id`, including inferred
1655 /// lifetime constraints. This includes all predicates returned by `explicit_predicates_of`, plus
1656 /// inferred constraints concerning which regions outlive other regions.
1657 fn predicates_defined_on(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicates<'_> {
1658     debug!("predicates_defined_on({:?})", def_id);
1659     let mut result = tcx.explicit_predicates_of(def_id);
1660     debug!("predicates_defined_on: explicit_predicates_of({:?}) = {:?}", def_id, result,);
1661     let inferred_outlives = tcx.inferred_outlives_of(def_id);
1662     if !inferred_outlives.is_empty() {
1663         debug!(
1664             "predicates_defined_on: inferred_outlives_of({:?}) = {:?}",
1665             def_id, inferred_outlives,
1666         );
1667         if result.predicates.is_empty() {
1668             result.predicates = inferred_outlives;
1669         } else {
1670             result.predicates = tcx
1671                 .arena
1672                 .alloc_from_iter(result.predicates.iter().chain(inferred_outlives).copied());
1673         }
1674     }
1675
1676     debug!("predicates_defined_on({:?}) = {:?}", def_id, result);
1677     result
1678 }
1679
1680 /// Returns a list of all type predicates (explicit and implicit) for the definition with
1681 /// ID `def_id`. This includes all predicates returned by `predicates_defined_on`, plus
1682 /// `Self: Trait` predicates for traits.
1683 fn predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicates<'_> {
1684     let mut result = tcx.predicates_defined_on(def_id);
1685
1686     if tcx.is_trait(def_id) {
1687         // For traits, add `Self: Trait` predicate. This is
1688         // not part of the predicates that a user writes, but it
1689         // is something that one must prove in order to invoke a
1690         // method or project an associated type.
1691         //
1692         // In the chalk setup, this predicate is not part of the
1693         // "predicates" for a trait item. But it is useful in
1694         // rustc because if you directly (e.g.) invoke a trait
1695         // method like `Trait::method(...)`, you must naturally
1696         // prove that the trait applies to the types that were
1697         // used, and adding the predicate into this list ensures
1698         // that this is done.
1699         let span = tcx.sess.source_map().guess_head_span(tcx.def_span(def_id));
1700         result.predicates =
1701             tcx.arena.alloc_from_iter(result.predicates.iter().copied().chain(std::iter::once((
1702                 ty::TraitRef::identity(tcx, def_id).without_const().to_predicate(tcx),
1703                 span,
1704             ))));
1705     }
1706     debug!("predicates_of(def_id={:?}) = {:?}", def_id, result);
1707     result
1708 }
1709
1710 /// Returns a list of user-specified type predicates for the definition with ID `def_id`.
1711 /// N.B., this does not include any implied/inferred constraints.
1712 fn explicit_predicates_of(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicates<'_> {
1713     use rustc_hir::*;
1714
1715     debug!("explicit_predicates_of(def_id={:?})", def_id);
1716
1717     let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
1718     let node = tcx.hir().get(hir_id);
1719
1720     let mut is_trait = None;
1721     let mut is_default_impl_trait = None;
1722     let mut is_trait_associated_type = None;
1723
1724     let icx = ItemCtxt::new(tcx, def_id);
1725     let constness = icx.default_constness_for_trait_bounds();
1726
1727     const NO_GENERICS: &hir::Generics<'_> = &hir::Generics::empty();
1728
1729     // We use an `IndexSet` to preserves order of insertion.
1730     // Preserving the order of insertion is important here so as not to break
1731     // compile-fail UI tests.
1732     let mut predicates: FxIndexSet<(ty::Predicate<'_>, Span)> = FxIndexSet::default();
1733
1734     let ast_generics = match node {
1735         Node::TraitItem(item) => {
1736             if let hir::TraitItemKind::Type(bounds, _) = item.kind {
1737                 is_trait_associated_type = Some((bounds, item.span));
1738             }
1739             &item.generics
1740         }
1741
1742         Node::ImplItem(item) => &item.generics,
1743
1744         Node::Item(item) => {
1745             match item.kind {
1746                 ItemKind::Impl { defaultness, ref generics, .. } => {
1747                     if defaultness.is_default() {
1748                         is_default_impl_trait = tcx.impl_trait_ref(def_id);
1749                     }
1750                     generics
1751                 }
1752                 ItemKind::Fn(.., ref generics, _)
1753                 | ItemKind::TyAlias(_, ref generics)
1754                 | ItemKind::Enum(_, ref generics)
1755                 | ItemKind::Struct(_, ref generics)
1756                 | ItemKind::Union(_, ref generics) => generics,
1757
1758                 ItemKind::Trait(_, _, ref generics, .., items) => {
1759                     is_trait = Some((ty::TraitRef::identity(tcx, def_id), items));
1760                     generics
1761                 }
1762                 ItemKind::TraitAlias(ref generics, _) => {
1763                     is_trait = Some((ty::TraitRef::identity(tcx, def_id), &[]));
1764                     generics
1765                 }
1766                 ItemKind::OpaqueTy(OpaqueTy {
1767                     ref bounds,
1768                     impl_trait_fn,
1769                     ref generics,
1770                     origin: _,
1771                 }) => {
1772                     let bounds_predicates = ty::print::with_no_queries(|| {
1773                         let substs = InternalSubsts::identity_for_item(tcx, def_id);
1774                         let opaque_ty = tcx.mk_opaque(def_id, substs);
1775
1776                         // Collect the bounds, i.e., the `A + B + 'c` in `impl A + B + 'c`.
1777                         let bounds = AstConv::compute_bounds(
1778                             &icx,
1779                             opaque_ty,
1780                             bounds,
1781                             SizedByDefault::Yes,
1782                             tcx.def_span(def_id),
1783                         );
1784
1785                         bounds.predicates(tcx, opaque_ty)
1786                     });
1787                     if impl_trait_fn.is_some() {
1788                         // opaque types
1789                         return ty::GenericPredicates {
1790                             parent: None,
1791                             predicates: tcx.arena.alloc_from_iter(bounds_predicates),
1792                         };
1793                     } else {
1794                         // named opaque types
1795                         predicates.extend(bounds_predicates);
1796                         generics
1797                     }
1798                 }
1799
1800                 _ => NO_GENERICS,
1801             }
1802         }
1803
1804         Node::ForeignItem(item) => match item.kind {
1805             ForeignItemKind::Static(..) => NO_GENERICS,
1806             ForeignItemKind::Fn(_, _, ref generics) => generics,
1807             ForeignItemKind::Type => NO_GENERICS,
1808         },
1809
1810         _ => NO_GENERICS,
1811     };
1812
1813     let generics = tcx.generics_of(def_id);
1814     let parent_count = generics.parent_count as u32;
1815     let has_own_self = generics.has_self && parent_count == 0;
1816
1817     // Below we'll consider the bounds on the type parameters (including `Self`)
1818     // and the explicit where-clauses, but to get the full set of predicates
1819     // on a trait we need to add in the supertrait bounds and bounds found on
1820     // associated types.
1821     if let Some((_trait_ref, _)) = is_trait {
1822         predicates.extend(tcx.super_predicates_of(def_id).predicates.iter().cloned());
1823     }
1824
1825     // In default impls, we can assume that the self type implements
1826     // the trait. So in:
1827     //
1828     //     default impl Foo for Bar { .. }
1829     //
1830     // we add a default where clause `Foo: Bar`. We do a similar thing for traits
1831     // (see below). Recall that a default impl is not itself an impl, but rather a
1832     // set of defaults that can be incorporated into another impl.
1833     if let Some(trait_ref) = is_default_impl_trait {
1834         predicates.insert((
1835             trait_ref.to_poly_trait_ref().without_const().to_predicate(tcx),
1836             tcx.def_span(def_id),
1837         ));
1838     }
1839
1840     // Collect the region predicates that were declared inline as
1841     // well. In the case of parameters declared on a fn or method, we
1842     // have to be careful to only iterate over early-bound regions.
1843     let mut index = parent_count + has_own_self as u32;
1844     for param in early_bound_lifetimes_from_generics(tcx, ast_generics) {
1845         let region = tcx.mk_region(ty::ReEarlyBound(ty::EarlyBoundRegion {
1846             def_id: tcx.hir().local_def_id(param.hir_id).to_def_id(),
1847             index,
1848             name: param.name.ident().name,
1849         }));
1850         index += 1;
1851
1852         match param.kind {
1853             GenericParamKind::Lifetime { .. } => {
1854                 param.bounds.iter().for_each(|bound| match bound {
1855                     hir::GenericBound::Outlives(lt) => {
1856                         let bound = AstConv::ast_region_to_region(&icx, &lt, None);
1857                         let outlives = ty::Binder::bind(ty::OutlivesPredicate(region, bound));
1858                         predicates.insert((outlives.to_predicate(tcx), lt.span));
1859                     }
1860                     _ => bug!(),
1861                 });
1862             }
1863             _ => bug!(),
1864         }
1865     }
1866
1867     // Collect the predicates that were written inline by the user on each
1868     // type parameter (e.g., `<T: Foo>`).
1869     for param in ast_generics.params {
1870         match param.kind {
1871             // We already dealt with early bound lifetimes above.
1872             GenericParamKind::Lifetime { .. } => (),
1873             GenericParamKind::Type { .. } => {
1874                 let name = param.name.ident().name;
1875                 let param_ty = ty::ParamTy::new(index, name).to_ty(tcx);
1876                 index += 1;
1877
1878                 let sized = SizedByDefault::Yes;
1879                 let bounds =
1880                     AstConv::compute_bounds(&icx, param_ty, &param.bounds, sized, param.span);
1881                 predicates.extend(bounds.predicates(tcx, param_ty));
1882             }
1883             GenericParamKind::Const { .. } => {
1884                 // Bounds on const parameters are currently not possible.
1885                 debug_assert!(param.bounds.is_empty());
1886                 index += 1;
1887             }
1888         }
1889     }
1890
1891     // Add in the bounds that appear in the where-clause.
1892     let where_clause = &ast_generics.where_clause;
1893     for predicate in where_clause.predicates {
1894         match predicate {
1895             &hir::WherePredicate::BoundPredicate(ref bound_pred) => {
1896                 let ty = icx.to_ty(&bound_pred.bounded_ty);
1897
1898                 // Keep the type around in a dummy predicate, in case of no bounds.
1899                 // That way, `where Ty:` is not a complete noop (see #53696) and `Ty`
1900                 // is still checked for WF.
1901                 if bound_pred.bounds.is_empty() {
1902                     if let ty::Param(_) = ty.kind() {
1903                         // This is a `where T:`, which can be in the HIR from the
1904                         // transformation that moves `?Sized` to `T`'s declaration.
1905                         // We can skip the predicate because type parameters are
1906                         // trivially WF, but also we *should*, to avoid exposing
1907                         // users who never wrote `where Type:,` themselves, to
1908                         // compiler/tooling bugs from not handling WF predicates.
1909                     } else {
1910                         let span = bound_pred.bounded_ty.span;
1911                         let re_root_empty = tcx.lifetimes.re_root_empty;
1912                         let predicate = ty::OutlivesPredicate(ty, re_root_empty);
1913                         predicates.insert((
1914                             ty::PredicateAtom::TypeOutlives(predicate)
1915                                 .potentially_quantified(tcx, ty::PredicateKind::ForAll),
1916                             span,
1917                         ));
1918                     }
1919                 }
1920
1921                 for bound in bound_pred.bounds.iter() {
1922                     match bound {
1923                         &hir::GenericBound::Trait(ref poly_trait_ref, modifier) => {
1924                             let constness = match modifier {
1925                                 hir::TraitBoundModifier::MaybeConst => hir::Constness::NotConst,
1926                                 hir::TraitBoundModifier::None => constness,
1927                                 hir::TraitBoundModifier::Maybe => bug!("this wasn't handled"),
1928                             };
1929
1930                             let mut bounds = Bounds::default();
1931                             let _ = AstConv::instantiate_poly_trait_ref(
1932                                 &icx,
1933                                 poly_trait_ref,
1934                                 constness,
1935                                 ty,
1936                                 &mut bounds,
1937                             );
1938                             predicates.extend(bounds.predicates(tcx, ty));
1939                         }
1940
1941                         &hir::GenericBound::LangItemTrait(lang_item, span, hir_id, args) => {
1942                             let mut bounds = Bounds::default();
1943                             AstConv::instantiate_lang_item_trait_ref(
1944                                 &icx,
1945                                 lang_item,
1946                                 span,
1947                                 hir_id,
1948                                 args,
1949                                 ty,
1950                                 &mut bounds,
1951                             );
1952                             predicates.extend(bounds.predicates(tcx, ty));
1953                         }
1954
1955                         &hir::GenericBound::Outlives(ref lifetime) => {
1956                             let region = AstConv::ast_region_to_region(&icx, lifetime, None);
1957                             predicates.insert((
1958                                 ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate(ty, region))
1959                                     .potentially_quantified(tcx, ty::PredicateKind::ForAll),
1960                                 lifetime.span,
1961                             ));
1962                         }
1963                     }
1964                 }
1965             }
1966
1967             &hir::WherePredicate::RegionPredicate(ref region_pred) => {
1968                 let r1 = AstConv::ast_region_to_region(&icx, &region_pred.lifetime, None);
1969                 predicates.extend(region_pred.bounds.iter().map(|bound| {
1970                     let (r2, span) = match bound {
1971                         hir::GenericBound::Outlives(lt) => {
1972                             (AstConv::ast_region_to_region(&icx, lt, None), lt.span)
1973                         }
1974                         _ => bug!(),
1975                     };
1976                     let pred = ty::PredicateAtom::RegionOutlives(ty::OutlivesPredicate(r1, r2));
1977
1978                     (pred.potentially_quantified(icx.tcx, ty::PredicateKind::ForAll), span)
1979                 }))
1980             }
1981
1982             &hir::WherePredicate::EqPredicate(..) => {
1983                 // FIXME(#20041)
1984             }
1985         }
1986     }
1987
1988     // Add predicates from associated type bounds (`type X: Bound`)
1989     if tcx.features().generic_associated_types {
1990         // New behavior: bounds declared on associate type are predicates of that
1991         // associated type. Not the default because it needs more testing.
1992         if let Some((bounds, span)) = is_trait_associated_type {
1993             let projection_ty =
1994                 tcx.mk_projection(def_id, InternalSubsts::identity_for_item(tcx, def_id));
1995
1996             predicates.extend(associated_item_bounds(tcx, def_id, bounds, projection_ty, span))
1997         }
1998     } else if let Some((self_trait_ref, trait_items)) = is_trait {
1999         // Current behavior: bounds declared on associate type are predicates
2000         // of its parent trait.
2001         predicates.extend(trait_items.iter().flat_map(|trait_item_ref| {
2002             trait_associated_item_predicates(tcx, def_id, self_trait_ref, trait_item_ref)
2003         }))
2004     }
2005
2006     if tcx.features().const_evaluatable_checked {
2007         predicates.extend(const_evaluatable_predicates_of(tcx, def_id.expect_local()));
2008     }
2009
2010     let mut predicates: Vec<_> = predicates.into_iter().collect();
2011
2012     // Subtle: before we store the predicates into the tcx, we
2013     // sort them so that predicates like `T: Foo<Item=U>` come
2014     // before uses of `U`.  This avoids false ambiguity errors
2015     // in trait checking. See `setup_constraining_predicates`
2016     // for details.
2017     if let Node::Item(&Item { kind: ItemKind::Impl { .. }, .. }) = node {
2018         let self_ty = tcx.type_of(def_id);
2019         let trait_ref = tcx.impl_trait_ref(def_id);
2020         cgp::setup_constraining_predicates(
2021             tcx,
2022             &mut predicates,
2023             trait_ref,
2024             &mut cgp::parameters_for_impl(self_ty, trait_ref),
2025         );
2026     }
2027
2028     let result = ty::GenericPredicates {
2029         parent: generics.parent,
2030         predicates: tcx.arena.alloc_from_iter(predicates),
2031     };
2032     debug!("explicit_predicates_of(def_id={:?}) = {:?}", def_id, result);
2033     result
2034 }
2035
2036 fn const_evaluatable_predicates_of<'tcx>(
2037     tcx: TyCtxt<'tcx>,
2038     def_id: LocalDefId,
2039 ) -> FxIndexSet<(ty::Predicate<'tcx>, Span)> {
2040     struct ConstCollector<'tcx> {
2041         tcx: TyCtxt<'tcx>,
2042         preds: FxIndexSet<(ty::Predicate<'tcx>, Span)>,
2043     }
2044
2045     impl<'tcx> intravisit::Visitor<'tcx> for ConstCollector<'tcx> {
2046         type Map = Map<'tcx>;
2047
2048         fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap<Self::Map> {
2049             intravisit::NestedVisitorMap::None
2050         }
2051
2052         fn visit_anon_const(&mut self, c: &'tcx hir::AnonConst) {
2053             let def_id = self.tcx.hir().local_def_id(c.hir_id);
2054             let ct = ty::Const::from_anon_const(self.tcx, def_id);
2055             if let ty::ConstKind::Unevaluated(def, substs, None) = ct.val {
2056                 let span = self.tcx.hir().span(c.hir_id);
2057                 self.preds.insert((
2058                     ty::PredicateAtom::ConstEvaluatable(def, substs).to_predicate(self.tcx),
2059                     span,
2060                 ));
2061             }
2062         }
2063
2064         // Look into `TyAlias`.
2065         fn visit_ty(&mut self, ty: &'tcx hir::Ty<'tcx>) {
2066             use ty::fold::{TypeFoldable, TypeVisitor};
2067             struct TyAliasVisitor<'a, 'tcx> {
2068                 tcx: TyCtxt<'tcx>,
2069                 preds: &'a mut FxIndexSet<(ty::Predicate<'tcx>, Span)>,
2070                 span: Span,
2071             }
2072
2073             impl<'a, 'tcx> TypeVisitor<'tcx> for TyAliasVisitor<'a, 'tcx> {
2074                 fn visit_const(&mut self, ct: &'tcx Const<'tcx>) -> bool {
2075                     if let ty::ConstKind::Unevaluated(def, substs, None) = ct.val {
2076                         self.preds.insert((
2077                             ty::PredicateAtom::ConstEvaluatable(def, substs).to_predicate(self.tcx),
2078                             self.span,
2079                         ));
2080                     }
2081                     false
2082                 }
2083             }
2084
2085             if let hir::TyKind::Path(hir::QPath::Resolved(None, path)) = ty.kind {
2086                 if let Res::Def(DefKind::TyAlias, def_id) = path.res {
2087                     let mut visitor =
2088                         TyAliasVisitor { tcx: self.tcx, preds: &mut self.preds, span: path.span };
2089                     self.tcx.type_of(def_id).visit_with(&mut visitor);
2090                 }
2091             }
2092
2093             intravisit::walk_ty(self, ty)
2094         }
2095     }
2096
2097     let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
2098     let node = tcx.hir().get(hir_id);
2099
2100     let mut collector = ConstCollector { tcx, preds: FxIndexSet::default() };
2101     if let Some(generics) = node.generics() {
2102         warn!("const_evaluatable_predicates_of({:?}): visit_generics", def_id);
2103         collector.visit_generics(generics);
2104     }
2105
2106     if let Some(fn_sig) = tcx.hir().fn_sig_by_hir_id(hir_id) {
2107         warn!("const_evaluatable_predicates_of({:?}): visit_fn_decl", def_id);
2108         collector.visit_fn_decl(fn_sig.decl);
2109     }
2110     warn!("const_evaluatable_predicates_of({:?}) = {:?}", def_id, collector.preds);
2111
2112     collector.preds
2113 }
2114
2115 fn projection_ty_from_predicates(
2116     tcx: TyCtxt<'tcx>,
2117     key: (
2118         // ty_def_id
2119         DefId,
2120         // def_id of `N` in `<T as Trait>::N`
2121         DefId,
2122     ),
2123 ) -> Option<ty::ProjectionTy<'tcx>> {
2124     let (ty_def_id, item_def_id) = key;
2125     let mut projection_ty = None;
2126     for (predicate, _) in tcx.predicates_of(ty_def_id).predicates {
2127         if let ty::PredicateAtom::Projection(projection_predicate) = predicate.skip_binders() {
2128             if item_def_id == projection_predicate.projection_ty.item_def_id {
2129                 projection_ty = Some(projection_predicate.projection_ty);
2130                 break;
2131             }
2132         }
2133     }
2134     projection_ty
2135 }
2136
2137 fn trait_associated_item_predicates(
2138     tcx: TyCtxt<'tcx>,
2139     def_id: DefId,
2140     self_trait_ref: ty::TraitRef<'tcx>,
2141     trait_item_ref: &hir::TraitItemRef,
2142 ) -> Vec<(ty::Predicate<'tcx>, Span)> {
2143     let trait_item = tcx.hir().trait_item(trait_item_ref.id);
2144     let item_def_id = tcx.hir().local_def_id(trait_item_ref.id.hir_id);
2145     let bounds = match trait_item.kind {
2146         hir::TraitItemKind::Type(ref bounds, _) => bounds,
2147         _ => return Vec::new(),
2148     };
2149
2150     if !tcx.generics_of(item_def_id).params.is_empty() {
2151         // For GATs the substs provided to the mk_projection call below are
2152         // wrong. We should emit a feature gate error if we get here so skip
2153         // this type.
2154         tcx.sess.delay_span_bug(trait_item.span, "gats used without feature gate");
2155         return Vec::new();
2156     }
2157
2158     let assoc_ty = tcx.mk_projection(
2159         tcx.hir().local_def_id(trait_item.hir_id).to_def_id(),
2160         self_trait_ref.substs,
2161     );
2162
2163     associated_item_bounds(tcx, def_id, bounds, assoc_ty, trait_item.span)
2164 }
2165
2166 fn associated_item_bounds(
2167     tcx: TyCtxt<'tcx>,
2168     def_id: DefId,
2169     bounds: &'tcx [hir::GenericBound<'tcx>],
2170     projection_ty: Ty<'tcx>,
2171     span: Span,
2172 ) -> Vec<(ty::Predicate<'tcx>, Span)> {
2173     let bounds = AstConv::compute_bounds(
2174         &ItemCtxt::new(tcx, def_id),
2175         projection_ty,
2176         bounds,
2177         SizedByDefault::Yes,
2178         span,
2179     );
2180
2181     let predicates = bounds.predicates(tcx, projection_ty);
2182
2183     predicates
2184 }
2185
2186 /// Converts a specific `GenericBound` from the AST into a set of
2187 /// predicates that apply to the self type. A vector is returned
2188 /// because this can be anywhere from zero predicates (`T: ?Sized` adds no
2189 /// predicates) to one (`T: Foo`) to many (`T: Bar<X = i32>` adds `T: Bar`
2190 /// and `<T as Bar>::X == i32`).
2191 fn predicates_from_bound<'tcx>(
2192     astconv: &dyn AstConv<'tcx>,
2193     param_ty: Ty<'tcx>,
2194     bound: &'tcx hir::GenericBound<'tcx>,
2195     constness: hir::Constness,
2196 ) -> Vec<(ty::Predicate<'tcx>, Span)> {
2197     match *bound {
2198         hir::GenericBound::Trait(ref tr, modifier) => {
2199             let constness = match modifier {
2200                 hir::TraitBoundModifier::Maybe => return vec![],
2201                 hir::TraitBoundModifier::MaybeConst => hir::Constness::NotConst,
2202                 hir::TraitBoundModifier::None => constness,
2203             };
2204
2205             let mut bounds = Bounds::default();
2206             let _ = astconv.instantiate_poly_trait_ref(tr, constness, param_ty, &mut bounds);
2207             bounds.predicates(astconv.tcx(), param_ty)
2208         }
2209         hir::GenericBound::LangItemTrait(lang_item, span, hir_id, args) => {
2210             let mut bounds = Bounds::default();
2211             astconv.instantiate_lang_item_trait_ref(
2212                 lang_item,
2213                 span,
2214                 hir_id,
2215                 args,
2216                 param_ty,
2217                 &mut bounds,
2218             );
2219             bounds.predicates(astconv.tcx(), param_ty)
2220         }
2221         hir::GenericBound::Outlives(ref lifetime) => {
2222             let region = astconv.ast_region_to_region(lifetime, None);
2223             let pred = ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate(param_ty, region))
2224                 .potentially_quantified(astconv.tcx(), ty::PredicateKind::ForAll);
2225             vec![(pred, lifetime.span)]
2226         }
2227     }
2228 }
2229
2230 fn compute_sig_of_foreign_fn_decl<'tcx>(
2231     tcx: TyCtxt<'tcx>,
2232     def_id: DefId,
2233     decl: &'tcx hir::FnDecl<'tcx>,
2234     abi: abi::Abi,
2235     ident: Ident,
2236 ) -> ty::PolyFnSig<'tcx> {
2237     let unsafety = if abi == abi::Abi::RustIntrinsic {
2238         intrinsic_operation_unsafety(tcx.item_name(def_id))
2239     } else {
2240         hir::Unsafety::Unsafe
2241     };
2242     let fty = AstConv::ty_of_fn(
2243         &ItemCtxt::new(tcx, def_id),
2244         unsafety,
2245         abi,
2246         decl,
2247         &hir::Generics::empty(),
2248         Some(ident.span),
2249     );
2250
2251     // Feature gate SIMD types in FFI, since I am not sure that the
2252     // ABIs are handled at all correctly. -huonw
2253     if abi != abi::Abi::RustIntrinsic
2254         && abi != abi::Abi::PlatformIntrinsic
2255         && !tcx.features().simd_ffi
2256     {
2257         let check = |ast_ty: &hir::Ty<'_>, ty: Ty<'_>| {
2258             if ty.is_simd() {
2259                 let snip = tcx
2260                     .sess
2261                     .source_map()
2262                     .span_to_snippet(ast_ty.span)
2263                     .map_or(String::new(), |s| format!(" `{}`", s));
2264                 tcx.sess
2265                     .struct_span_err(
2266                         ast_ty.span,
2267                         &format!(
2268                             "use of SIMD type{} in FFI is highly experimental and \
2269                              may result in invalid code",
2270                             snip
2271                         ),
2272                     )
2273                     .help("add `#![feature(simd_ffi)]` to the crate attributes to enable")
2274                     .emit();
2275             }
2276         };
2277         for (input, ty) in decl.inputs.iter().zip(fty.inputs().skip_binder()) {
2278             check(&input, ty)
2279         }
2280         if let hir::FnRetTy::Return(ref ty) = decl.output {
2281             check(&ty, fty.output().skip_binder())
2282         }
2283     }
2284
2285     fty
2286 }
2287
2288 fn is_foreign_item(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
2289     match tcx.hir().get_if_local(def_id) {
2290         Some(Node::ForeignItem(..)) => true,
2291         Some(_) => false,
2292         _ => bug!("is_foreign_item applied to non-local def-id {:?}", def_id),
2293     }
2294 }
2295
2296 fn static_mutability(tcx: TyCtxt<'_>, def_id: DefId) -> Option<hir::Mutability> {
2297     match tcx.hir().get_if_local(def_id) {
2298         Some(
2299             Node::Item(&hir::Item { kind: hir::ItemKind::Static(_, mutbl, _), .. })
2300             | Node::ForeignItem(&hir::ForeignItem {
2301                 kind: hir::ForeignItemKind::Static(_, mutbl),
2302                 ..
2303             }),
2304         ) => Some(mutbl),
2305         Some(_) => None,
2306         _ => bug!("static_mutability applied to non-local def-id {:?}", def_id),
2307     }
2308 }
2309
2310 fn generator_kind(tcx: TyCtxt<'_>, def_id: DefId) -> Option<hir::GeneratorKind> {
2311     match tcx.hir().get_if_local(def_id) {
2312         Some(Node::Expr(&rustc_hir::Expr {
2313             kind: rustc_hir::ExprKind::Closure(_, _, body_id, _, _),
2314             ..
2315         })) => tcx.hir().body(body_id).generator_kind(),
2316         Some(_) => None,
2317         _ => bug!("generator_kind applied to non-local def-id {:?}", def_id),
2318     }
2319 }
2320
2321 fn from_target_feature(
2322     tcx: TyCtxt<'_>,
2323     id: DefId,
2324     attr: &ast::Attribute,
2325     supported_target_features: &FxHashMap<String, Option<Symbol>>,
2326     target_features: &mut Vec<Symbol>,
2327 ) {
2328     let list = match attr.meta_item_list() {
2329         Some(list) => list,
2330         None => return,
2331     };
2332     let bad_item = |span| {
2333         let msg = "malformed `target_feature` attribute input";
2334         let code = "enable = \"..\"".to_owned();
2335         tcx.sess
2336             .struct_span_err(span, &msg)
2337             .span_suggestion(span, "must be of the form", code, Applicability::HasPlaceholders)
2338             .emit();
2339     };
2340     let rust_features = tcx.features();
2341     for item in list {
2342         // Only `enable = ...` is accepted in the meta-item list.
2343         if !item.has_name(sym::enable) {
2344             bad_item(item.span());
2345             continue;
2346         }
2347
2348         // Must be of the form `enable = "..."` (a string).
2349         let value = match item.value_str() {
2350             Some(value) => value,
2351             None => {
2352                 bad_item(item.span());
2353                 continue;
2354             }
2355         };
2356
2357         // We allow comma separation to enable multiple features.
2358         target_features.extend(value.as_str().split(',').filter_map(|feature| {
2359             let feature_gate = match supported_target_features.get(feature) {
2360                 Some(g) => g,
2361                 None => {
2362                     let msg =
2363                         format!("the feature named `{}` is not valid for this target", feature);
2364                     let mut err = tcx.sess.struct_span_err(item.span(), &msg);
2365                     err.span_label(
2366                         item.span(),
2367                         format!("`{}` is not valid for this target", feature),
2368                     );
2369                     if let Some(stripped) = feature.strip_prefix('+') {
2370                         let valid = supported_target_features.contains_key(stripped);
2371                         if valid {
2372                             err.help("consider removing the leading `+` in the feature name");
2373                         }
2374                     }
2375                     err.emit();
2376                     return None;
2377                 }
2378             };
2379
2380             // Only allow features whose feature gates have been enabled.
2381             let allowed = match feature_gate.as_ref().copied() {
2382                 Some(sym::arm_target_feature) => rust_features.arm_target_feature,
2383                 Some(sym::aarch64_target_feature) => rust_features.aarch64_target_feature,
2384                 Some(sym::hexagon_target_feature) => rust_features.hexagon_target_feature,
2385                 Some(sym::powerpc_target_feature) => rust_features.powerpc_target_feature,
2386                 Some(sym::mips_target_feature) => rust_features.mips_target_feature,
2387                 Some(sym::riscv_target_feature) => rust_features.riscv_target_feature,
2388                 Some(sym::avx512_target_feature) => rust_features.avx512_target_feature,
2389                 Some(sym::sse4a_target_feature) => rust_features.sse4a_target_feature,
2390                 Some(sym::tbm_target_feature) => rust_features.tbm_target_feature,
2391                 Some(sym::wasm_target_feature) => rust_features.wasm_target_feature,
2392                 Some(sym::cmpxchg16b_target_feature) => rust_features.cmpxchg16b_target_feature,
2393                 Some(sym::adx_target_feature) => rust_features.adx_target_feature,
2394                 Some(sym::movbe_target_feature) => rust_features.movbe_target_feature,
2395                 Some(sym::rtm_target_feature) => rust_features.rtm_target_feature,
2396                 Some(sym::f16c_target_feature) => rust_features.f16c_target_feature,
2397                 Some(name) => bug!("unknown target feature gate {}", name),
2398                 None => true,
2399             };
2400             if !allowed && id.is_local() {
2401                 feature_err(
2402                     &tcx.sess.parse_sess,
2403                     feature_gate.unwrap(),
2404                     item.span(),
2405                     &format!("the target feature `{}` is currently unstable", feature),
2406                 )
2407                 .emit();
2408             }
2409             Some(Symbol::intern(feature))
2410         }));
2411     }
2412 }
2413
2414 fn linkage_by_name(tcx: TyCtxt<'_>, def_id: DefId, name: &str) -> Linkage {
2415     use rustc_middle::mir::mono::Linkage::*;
2416
2417     // Use the names from src/llvm/docs/LangRef.rst here. Most types are only
2418     // applicable to variable declarations and may not really make sense for
2419     // Rust code in the first place but allow them anyway and trust that the
2420     // user knows what s/he's doing. Who knows, unanticipated use cases may pop
2421     // up in the future.
2422     //
2423     // ghost, dllimport, dllexport and linkonce_odr_autohide are not supported
2424     // and don't have to be, LLVM treats them as no-ops.
2425     match name {
2426         "appending" => Appending,
2427         "available_externally" => AvailableExternally,
2428         "common" => Common,
2429         "extern_weak" => ExternalWeak,
2430         "external" => External,
2431         "internal" => Internal,
2432         "linkonce" => LinkOnceAny,
2433         "linkonce_odr" => LinkOnceODR,
2434         "private" => Private,
2435         "weak" => WeakAny,
2436         "weak_odr" => WeakODR,
2437         _ => {
2438             let span = tcx.hir().span_if_local(def_id);
2439             if let Some(span) = span {
2440                 tcx.sess.span_fatal(span, "invalid linkage specified")
2441             } else {
2442                 tcx.sess.fatal(&format!("invalid linkage specified: {}", name))
2443             }
2444         }
2445     }
2446 }
2447
2448 fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
2449     let attrs = tcx.get_attrs(id);
2450
2451     let mut codegen_fn_attrs = CodegenFnAttrs::new();
2452     if should_inherit_track_caller(tcx, id) {
2453         codegen_fn_attrs.flags |= CodegenFnAttrFlags::TRACK_CALLER;
2454     }
2455
2456     let supported_target_features = tcx.supported_target_features(LOCAL_CRATE);
2457
2458     let mut inline_span = None;
2459     let mut link_ordinal_span = None;
2460     let mut no_sanitize_span = None;
2461     for attr in attrs.iter() {
2462         if tcx.sess.check_name(attr, sym::cold) {
2463             codegen_fn_attrs.flags |= CodegenFnAttrFlags::COLD;
2464         } else if tcx.sess.check_name(attr, sym::rustc_allocator) {
2465             codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR;
2466         } else if tcx.sess.check_name(attr, sym::unwind) {
2467             codegen_fn_attrs.flags |= CodegenFnAttrFlags::UNWIND;
2468         } else if tcx.sess.check_name(attr, sym::ffi_returns_twice) {
2469             if tcx.is_foreign_item(id) {
2470                 codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_RETURNS_TWICE;
2471             } else {
2472                 // `#[ffi_returns_twice]` is only allowed `extern fn`s.
2473                 struct_span_err!(
2474                     tcx.sess,
2475                     attr.span,
2476                     E0724,
2477                     "`#[ffi_returns_twice]` may only be used on foreign functions"
2478                 )
2479                 .emit();
2480             }
2481         } else if tcx.sess.check_name(attr, sym::ffi_pure) {
2482             if tcx.is_foreign_item(id) {
2483                 if attrs.iter().any(|a| tcx.sess.check_name(a, sym::ffi_const)) {
2484                     // `#[ffi_const]` functions cannot be `#[ffi_pure]`
2485                     struct_span_err!(
2486                         tcx.sess,
2487                         attr.span,
2488                         E0757,
2489                         "`#[ffi_const]` function cannot be `#[ffi_pure]`"
2490                     )
2491                     .emit();
2492                 } else {
2493                     codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_PURE;
2494                 }
2495             } else {
2496                 // `#[ffi_pure]` is only allowed on foreign functions
2497                 struct_span_err!(
2498                     tcx.sess,
2499                     attr.span,
2500                     E0755,
2501                     "`#[ffi_pure]` may only be used on foreign functions"
2502                 )
2503                 .emit();
2504             }
2505         } else if tcx.sess.check_name(attr, sym::ffi_const) {
2506             if tcx.is_foreign_item(id) {
2507                 codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_CONST;
2508             } else {
2509                 // `#[ffi_const]` is only allowed on foreign functions
2510                 struct_span_err!(
2511                     tcx.sess,
2512                     attr.span,
2513                     E0756,
2514                     "`#[ffi_const]` may only be used on foreign functions"
2515                 )
2516                 .emit();
2517             }
2518         } else if tcx.sess.check_name(attr, sym::rustc_allocator_nounwind) {
2519             codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_ALLOCATOR_NOUNWIND;
2520         } else if tcx.sess.check_name(attr, sym::naked) {
2521             codegen_fn_attrs.flags |= CodegenFnAttrFlags::NAKED;
2522         } else if tcx.sess.check_name(attr, sym::no_mangle) {
2523             codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_MANGLE;
2524         } else if tcx.sess.check_name(attr, sym::rustc_std_internal_symbol) {
2525             codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL;
2526         } else if tcx.sess.check_name(attr, sym::used) {
2527             codegen_fn_attrs.flags |= CodegenFnAttrFlags::USED;
2528         } else if tcx.sess.check_name(attr, sym::thread_local) {
2529             codegen_fn_attrs.flags |= CodegenFnAttrFlags::THREAD_LOCAL;
2530         } else if tcx.sess.check_name(attr, sym::track_caller) {
2531             if tcx.is_closure(id) || tcx.fn_sig(id).abi() != abi::Abi::Rust {
2532                 struct_span_err!(tcx.sess, attr.span, E0737, "`#[track_caller]` requires Rust ABI")
2533                     .emit();
2534             }
2535             codegen_fn_attrs.flags |= CodegenFnAttrFlags::TRACK_CALLER;
2536         } else if tcx.sess.check_name(attr, sym::export_name) {
2537             if let Some(s) = attr.value_str() {
2538                 if s.as_str().contains('\0') {
2539                     // `#[export_name = ...]` will be converted to a null-terminated string,
2540                     // so it may not contain any null characters.
2541                     struct_span_err!(
2542                         tcx.sess,
2543                         attr.span,
2544                         E0648,
2545                         "`export_name` may not contain null characters"
2546                     )
2547                     .emit();
2548                 }
2549                 codegen_fn_attrs.export_name = Some(s);
2550             }
2551         } else if tcx.sess.check_name(attr, sym::target_feature) {
2552             if !tcx.is_closure(id) && tcx.fn_sig(id).unsafety() == hir::Unsafety::Normal {
2553                 if !tcx.features().target_feature_11 {
2554                     let mut err = feature_err(
2555                         &tcx.sess.parse_sess,
2556                         sym::target_feature_11,
2557                         attr.span,
2558                         "`#[target_feature(..)]` can only be applied to `unsafe` functions",
2559                     );
2560                     err.span_label(tcx.def_span(id), "not an `unsafe` function");
2561                     err.emit();
2562                 } else if let Some(local_id) = id.as_local() {
2563                     check_target_feature_trait_unsafe(tcx, local_id, attr.span);
2564                 }
2565             }
2566             from_target_feature(
2567                 tcx,
2568                 id,
2569                 attr,
2570                 &supported_target_features,
2571                 &mut codegen_fn_attrs.target_features,
2572             );
2573         } else if tcx.sess.check_name(attr, sym::linkage) {
2574             if let Some(val) = attr.value_str() {
2575                 codegen_fn_attrs.linkage = Some(linkage_by_name(tcx, id, &val.as_str()));
2576             }
2577         } else if tcx.sess.check_name(attr, sym::link_section) {
2578             if let Some(val) = attr.value_str() {
2579                 if val.as_str().bytes().any(|b| b == 0) {
2580                     let msg = format!(
2581                         "illegal null byte in link_section \
2582                          value: `{}`",
2583                         &val
2584                     );
2585                     tcx.sess.span_err(attr.span, &msg);
2586                 } else {
2587                     codegen_fn_attrs.link_section = Some(val);
2588                 }
2589             }
2590         } else if tcx.sess.check_name(attr, sym::link_name) {
2591             codegen_fn_attrs.link_name = attr.value_str();
2592         } else if tcx.sess.check_name(attr, sym::link_ordinal) {
2593             link_ordinal_span = Some(attr.span);
2594             if let ordinal @ Some(_) = check_link_ordinal(tcx, attr) {
2595                 codegen_fn_attrs.link_ordinal = ordinal;
2596             }
2597         } else if tcx.sess.check_name(attr, sym::no_sanitize) {
2598             no_sanitize_span = Some(attr.span);
2599             if let Some(list) = attr.meta_item_list() {
2600                 for item in list.iter() {
2601                     if item.has_name(sym::address) {
2602                         codegen_fn_attrs.no_sanitize |= SanitizerSet::ADDRESS;
2603                     } else if item.has_name(sym::memory) {
2604                         codegen_fn_attrs.no_sanitize |= SanitizerSet::MEMORY;
2605                     } else if item.has_name(sym::thread) {
2606                         codegen_fn_attrs.no_sanitize |= SanitizerSet::THREAD;
2607                     } else {
2608                         tcx.sess
2609                             .struct_span_err(item.span(), "invalid argument for `no_sanitize`")
2610                             .note("expected one of: `address`, `memory` or `thread`")
2611                             .emit();
2612                     }
2613                 }
2614             }
2615         }
2616     }
2617
2618     codegen_fn_attrs.inline = attrs.iter().fold(InlineAttr::None, |ia, attr| {
2619         if !attr.has_name(sym::inline) {
2620             return ia;
2621         }
2622         match attr.meta().map(|i| i.kind) {
2623             Some(MetaItemKind::Word) => {
2624                 tcx.sess.mark_attr_used(attr);
2625                 InlineAttr::Hint
2626             }
2627             Some(MetaItemKind::List(ref items)) => {
2628                 tcx.sess.mark_attr_used(attr);
2629                 inline_span = Some(attr.span);
2630                 if items.len() != 1 {
2631                     struct_span_err!(
2632                         tcx.sess.diagnostic(),
2633                         attr.span,
2634                         E0534,
2635                         "expected one argument"
2636                     )
2637                     .emit();
2638                     InlineAttr::None
2639                 } else if list_contains_name(&items[..], sym::always) {
2640                     InlineAttr::Always
2641                 } else if list_contains_name(&items[..], sym::never) {
2642                     InlineAttr::Never
2643                 } else {
2644                     struct_span_err!(
2645                         tcx.sess.diagnostic(),
2646                         items[0].span(),
2647                         E0535,
2648                         "invalid argument"
2649                     )
2650                     .emit();
2651
2652                     InlineAttr::None
2653                 }
2654             }
2655             Some(MetaItemKind::NameValue(_)) => ia,
2656             None => ia,
2657         }
2658     });
2659
2660     codegen_fn_attrs.optimize = attrs.iter().fold(OptimizeAttr::None, |ia, attr| {
2661         if !attr.has_name(sym::optimize) {
2662             return ia;
2663         }
2664         let err = |sp, s| struct_span_err!(tcx.sess.diagnostic(), sp, E0722, "{}", s).emit();
2665         match attr.meta().map(|i| i.kind) {
2666             Some(MetaItemKind::Word) => {
2667                 err(attr.span, "expected one argument");
2668                 ia
2669             }
2670             Some(MetaItemKind::List(ref items)) => {
2671                 tcx.sess.mark_attr_used(attr);
2672                 inline_span = Some(attr.span);
2673                 if items.len() != 1 {
2674                     err(attr.span, "expected one argument");
2675                     OptimizeAttr::None
2676                 } else if list_contains_name(&items[..], sym::size) {
2677                     OptimizeAttr::Size
2678                 } else if list_contains_name(&items[..], sym::speed) {
2679                     OptimizeAttr::Speed
2680                 } else {
2681                     err(items[0].span(), "invalid argument");
2682                     OptimizeAttr::None
2683                 }
2684             }
2685             Some(MetaItemKind::NameValue(_)) => ia,
2686             None => ia,
2687         }
2688     });
2689
2690     // If a function uses #[target_feature] it can't be inlined into general
2691     // purpose functions as they wouldn't have the right target features
2692     // enabled. For that reason we also forbid #[inline(always)] as it can't be
2693     // respected.
2694     if !codegen_fn_attrs.target_features.is_empty() {
2695         if codegen_fn_attrs.inline == InlineAttr::Always {
2696             if let Some(span) = inline_span {
2697                 tcx.sess.span_err(
2698                     span,
2699                     "cannot use `#[inline(always)]` with \
2700                      `#[target_feature]`",
2701                 );
2702             }
2703         }
2704     }
2705
2706     if !codegen_fn_attrs.no_sanitize.is_empty() {
2707         if codegen_fn_attrs.inline == InlineAttr::Always {
2708             if let (Some(no_sanitize_span), Some(inline_span)) = (no_sanitize_span, inline_span) {
2709                 let hir_id = tcx.hir().local_def_id_to_hir_id(id.expect_local());
2710                 tcx.struct_span_lint_hir(
2711                     lint::builtin::INLINE_NO_SANITIZE,
2712                     hir_id,
2713                     no_sanitize_span,
2714                     |lint| {
2715                         lint.build("`no_sanitize` will have no effect after inlining")
2716                             .span_note(inline_span, "inlining requested here")
2717                             .emit();
2718                     },
2719                 )
2720             }
2721         }
2722     }
2723
2724     // Weak lang items have the same semantics as "std internal" symbols in the
2725     // sense that they're preserved through all our LTO passes and only
2726     // strippable by the linker.
2727     //
2728     // Additionally weak lang items have predetermined symbol names.
2729     if tcx.is_weak_lang_item(id) {
2730         codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL;
2731     }
2732     let check_name = |attr, sym| tcx.sess.check_name(attr, sym);
2733     if let Some(name) = weak_lang_items::link_name(check_name, &attrs) {
2734         codegen_fn_attrs.export_name = Some(name);
2735         codegen_fn_attrs.link_name = Some(name);
2736     }
2737     check_link_name_xor_ordinal(tcx, &codegen_fn_attrs, link_ordinal_span);
2738
2739     // Internal symbols to the standard library all have no_mangle semantics in
2740     // that they have defined symbol names present in the function name. This
2741     // also applies to weak symbols where they all have known symbol names.
2742     if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL) {
2743         codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_MANGLE;
2744     }
2745
2746     codegen_fn_attrs
2747 }
2748
2749 /// Checks if the provided DefId is a method in a trait impl for a trait which has track_caller
2750 /// applied to the method prototype.
2751 fn should_inherit_track_caller(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
2752     if let Some(impl_item) = tcx.opt_associated_item(def_id) {
2753         if let ty::AssocItemContainer::ImplContainer(impl_def_id) = impl_item.container {
2754             if let Some(trait_def_id) = tcx.trait_id_of_impl(impl_def_id) {
2755                 if let Some(trait_item) = tcx
2756                     .associated_items(trait_def_id)
2757                     .filter_by_name_unhygienic(impl_item.ident.name)
2758                     .find(move |trait_item| {
2759                         trait_item.kind == ty::AssocKind::Fn
2760                             && tcx.hygienic_eq(impl_item.ident, trait_item.ident, trait_def_id)
2761                     })
2762                 {
2763                     return tcx
2764                         .codegen_fn_attrs(trait_item.def_id)
2765                         .flags
2766                         .intersects(CodegenFnAttrFlags::TRACK_CALLER);
2767                 }
2768             }
2769         }
2770     }
2771
2772     false
2773 }
2774
2775 fn check_link_ordinal(tcx: TyCtxt<'_>, attr: &ast::Attribute) -> Option<usize> {
2776     use rustc_ast::{Lit, LitIntType, LitKind};
2777     let meta_item_list = attr.meta_item_list();
2778     let meta_item_list: Option<&[ast::NestedMetaItem]> = meta_item_list.as_ref().map(Vec::as_ref);
2779     let sole_meta_list = match meta_item_list {
2780         Some([item]) => item.literal(),
2781         _ => None,
2782     };
2783     if let Some(Lit { kind: LitKind::Int(ordinal, LitIntType::Unsuffixed), .. }) = sole_meta_list {
2784         if *ordinal <= usize::MAX as u128 {
2785             Some(*ordinal as usize)
2786         } else {
2787             let msg = format!("ordinal value in `link_ordinal` is too large: `{}`", &ordinal);
2788             tcx.sess
2789                 .struct_span_err(attr.span, &msg)
2790                 .note("the value may not exceed `usize::MAX`")
2791                 .emit();
2792             None
2793         }
2794     } else {
2795         tcx.sess
2796             .struct_span_err(attr.span, "illegal ordinal format in `link_ordinal`")
2797             .note("an unsuffixed integer value, e.g., `1`, is expected")
2798             .emit();
2799         None
2800     }
2801 }
2802
2803 fn check_link_name_xor_ordinal(
2804     tcx: TyCtxt<'_>,
2805     codegen_fn_attrs: &CodegenFnAttrs,
2806     inline_span: Option<Span>,
2807 ) {
2808     if codegen_fn_attrs.link_name.is_none() || codegen_fn_attrs.link_ordinal.is_none() {
2809         return;
2810     }
2811     let msg = "cannot use `#[link_name]` with `#[link_ordinal]`";
2812     if let Some(span) = inline_span {
2813         tcx.sess.span_err(span, msg);
2814     } else {
2815         tcx.sess.err(msg);
2816     }
2817 }
2818
2819 /// Checks the function annotated with `#[target_feature]` is not a safe
2820 /// trait method implementation, reporting an error if it is.
2821 fn check_target_feature_trait_unsafe(tcx: TyCtxt<'_>, id: LocalDefId, attr_span: Span) {
2822     let hir_id = tcx.hir().local_def_id_to_hir_id(id);
2823     let node = tcx.hir().get(hir_id);
2824     if let Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Fn(..), .. }) = node {
2825         let parent_id = tcx.hir().get_parent_item(hir_id);
2826         let parent_item = tcx.hir().expect_item(parent_id);
2827         if let hir::ItemKind::Impl { of_trait: Some(_), .. } = parent_item.kind {
2828             tcx.sess
2829                 .struct_span_err(
2830                     attr_span,
2831                     "`#[target_feature(..)]` cannot be applied to safe trait method",
2832                 )
2833                 .span_label(attr_span, "cannot be applied to safe trait method")
2834                 .span_label(tcx.def_span(id), "not an `unsafe` function")
2835                 .emit();
2836         }
2837     }
2838 }