]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_hir_analysis/src/collect.rs
Rollup merge of #104359 - Nilstrieb:plus-one, r=fee1-dead
[rust.git] / compiler / rustc_hir_analysis / 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;
18 use crate::check::intrinsic::intrinsic_operation_unsafety;
19 use crate::errors;
20 use rustc_ast as ast;
21 use rustc_ast::{MetaItemKind, NestedMetaItem};
22 use rustc_attr::{list_contains_name, InlineAttr, InstructionSetAttr, OptimizeAttr};
23 use rustc_data_structures::captures::Captures;
24 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
25 use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, ErrorGuaranteed, StashKey};
26 use rustc_hir as hir;
27 use rustc_hir::def_id::{DefId, LocalDefId, LOCAL_CRATE};
28 use rustc_hir::intravisit::{self, Visitor};
29 use rustc_hir::weak_lang_items::WEAK_LANG_ITEMS;
30 use rustc_hir::{lang_items, GenericParamKind, LangItem, Node};
31 use rustc_middle::hir::nested_filter;
32 use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
33 use rustc_middle::mir::mono::Linkage;
34 use rustc_middle::ty::query::Providers;
35 use rustc_middle::ty::util::{Discr, IntTypeExt};
36 use rustc_middle::ty::ReprOptions;
37 use rustc_middle::ty::{self, AdtKind, Const, DefIdTree, IsSuggestable, Ty, TyCtxt};
38 use rustc_session::lint;
39 use rustc_session::parse::feature_err;
40 use rustc_span::symbol::{kw, sym, Ident, Symbol};
41 use rustc_span::Span;
42 use rustc_target::spec::{abi, SanitizerSet};
43 use rustc_trait_selection::traits::error_reporting::suggestions::NextTypeParamName;
44 use std::iter;
45
46 mod generics_of;
47 mod item_bounds;
48 mod lifetimes;
49 mod predicates_of;
50 mod type_of;
51
52 ///////////////////////////////////////////////////////////////////////////
53 // Main entry point
54
55 fn collect_mod_item_types(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
56     tcx.hir().visit_item_likes_in_module(module_def_id, &mut CollectItemTypesVisitor { tcx });
57 }
58
59 pub fn provide(providers: &mut Providers) {
60     lifetimes::provide(providers);
61     *providers = Providers {
62         opt_const_param_of: type_of::opt_const_param_of,
63         type_of: type_of::type_of,
64         item_bounds: item_bounds::item_bounds,
65         explicit_item_bounds: item_bounds::explicit_item_bounds,
66         generics_of: generics_of::generics_of,
67         predicates_of: predicates_of::predicates_of,
68         predicates_defined_on,
69         explicit_predicates_of: predicates_of::explicit_predicates_of,
70         super_predicates_of: predicates_of::super_predicates_of,
71         super_predicates_that_define_assoc_type:
72             predicates_of::super_predicates_that_define_assoc_type,
73         trait_explicit_predicates_and_bounds: predicates_of::trait_explicit_predicates_and_bounds,
74         type_param_predicates: predicates_of::type_param_predicates,
75         trait_def,
76         adt_def,
77         fn_sig,
78         impl_trait_ref,
79         impl_polarity,
80         is_foreign_item,
81         generator_kind,
82         codegen_fn_attrs,
83         asm_target_features,
84         collect_mod_item_types,
85         should_inherit_track_caller,
86         ..*providers
87     };
88 }
89
90 ///////////////////////////////////////////////////////////////////////////
91
92 /// Context specific to some particular item. This is what implements
93 /// [`AstConv`].
94 ///
95 /// # `ItemCtxt` vs `FnCtxt`
96 ///
97 /// `ItemCtxt` is primarily used to type-check item signatures and lower them
98 /// from HIR to their [`ty::Ty`] representation, which is exposed using [`AstConv`].
99 /// It's also used for the bodies of items like structs where the body (the fields)
100 /// are just signatures.
101 ///
102 /// This is in contrast to `FnCtxt`, which is used to type-check bodies of
103 /// functions, closures, and `const`s -- anywhere that expressions and statements show up.
104 ///
105 /// An important thing to note is that `ItemCtxt` does no inference -- it has no [`InferCtxt`] --
106 /// while `FnCtxt` does do inference.
107 ///
108 /// [`InferCtxt`]: rustc_infer::infer::InferCtxt
109 ///
110 /// # Trait predicates
111 ///
112 /// `ItemCtxt` has information about the predicates that are defined
113 /// on the trait. Unfortunately, this predicate information is
114 /// available in various different forms at various points in the
115 /// process. So we can't just store a pointer to e.g., the AST or the
116 /// parsed ty form, we have to be more flexible. To this end, the
117 /// `ItemCtxt` is parameterized by a `DefId` that it uses to satisfy
118 /// `get_type_parameter_bounds` requests, drawing the information from
119 /// the AST (`hir::Generics`), recursively.
120 pub struct ItemCtxt<'tcx> {
121     tcx: TyCtxt<'tcx>,
122     item_def_id: DefId,
123 }
124
125 ///////////////////////////////////////////////////////////////////////////
126
127 #[derive(Default)]
128 pub(crate) struct HirPlaceholderCollector(pub(crate) Vec<Span>);
129
130 impl<'v> Visitor<'v> for HirPlaceholderCollector {
131     fn visit_ty(&mut self, t: &'v hir::Ty<'v>) {
132         if let hir::TyKind::Infer = t.kind {
133             self.0.push(t.span);
134         }
135         intravisit::walk_ty(self, t)
136     }
137     fn visit_generic_arg(&mut self, generic_arg: &'v hir::GenericArg<'v>) {
138         match generic_arg {
139             hir::GenericArg::Infer(inf) => {
140                 self.0.push(inf.span);
141                 intravisit::walk_inf(self, inf);
142             }
143             hir::GenericArg::Type(t) => self.visit_ty(t),
144             _ => {}
145         }
146     }
147     fn visit_array_length(&mut self, length: &'v hir::ArrayLen) {
148         if let &hir::ArrayLen::Infer(_, span) = length {
149             self.0.push(span);
150         }
151         intravisit::walk_array_len(self, length)
152     }
153 }
154
155 struct CollectItemTypesVisitor<'tcx> {
156     tcx: TyCtxt<'tcx>,
157 }
158
159 /// If there are any placeholder types (`_`), emit an error explaining that this is not allowed
160 /// and suggest adding type parameters in the appropriate place, taking into consideration any and
161 /// all already existing generic type parameters to avoid suggesting a name that is already in use.
162 pub(crate) fn placeholder_type_error<'tcx>(
163     tcx: TyCtxt<'tcx>,
164     generics: Option<&hir::Generics<'_>>,
165     placeholder_types: Vec<Span>,
166     suggest: bool,
167     hir_ty: Option<&hir::Ty<'_>>,
168     kind: &'static str,
169 ) {
170     if placeholder_types.is_empty() {
171         return;
172     }
173
174     placeholder_type_error_diag(tcx, generics, placeholder_types, vec![], suggest, hir_ty, kind)
175         .emit();
176 }
177
178 pub(crate) fn placeholder_type_error_diag<'tcx>(
179     tcx: TyCtxt<'tcx>,
180     generics: Option<&hir::Generics<'_>>,
181     placeholder_types: Vec<Span>,
182     additional_spans: Vec<Span>,
183     suggest: bool,
184     hir_ty: Option<&hir::Ty<'_>>,
185     kind: &'static str,
186 ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
187     if placeholder_types.is_empty() {
188         return bad_placeholder(tcx, additional_spans, kind);
189     }
190
191     let params = generics.map(|g| g.params).unwrap_or_default();
192     let type_name = params.next_type_param_name(None);
193     let mut sugg: Vec<_> =
194         placeholder_types.iter().map(|sp| (*sp, (*type_name).to_string())).collect();
195
196     if let Some(generics) = generics {
197         if let Some(arg) = params.iter().find(|arg| {
198             matches!(arg.name, hir::ParamName::Plain(Ident { name: kw::Underscore, .. }))
199         }) {
200             // Account for `_` already present in cases like `struct S<_>(_);` and suggest
201             // `struct S<T>(T);` instead of `struct S<_, T>(T);`.
202             sugg.push((arg.span, (*type_name).to_string()));
203         } else if let Some(span) = generics.span_for_param_suggestion() {
204             // Account for bounds, we want `fn foo<T: E, K>(_: K)` not `fn foo<T, K: E>(_: K)`.
205             sugg.push((span, format!(", {}", type_name)));
206         } else {
207             sugg.push((generics.span, format!("<{}>", type_name)));
208         }
209     }
210
211     let mut err =
212         bad_placeholder(tcx, placeholder_types.into_iter().chain(additional_spans).collect(), kind);
213
214     // Suggest, but only if it is not a function in const or static
215     if suggest {
216         let mut is_fn = false;
217         let mut is_const_or_static = false;
218
219         if let Some(hir_ty) = hir_ty && let hir::TyKind::BareFn(_) = hir_ty.kind {
220             is_fn = true;
221
222             // Check if parent is const or static
223             let parent_id = tcx.hir().get_parent_node(hir_ty.hir_id);
224             let parent_node = tcx.hir().get(parent_id);
225
226             is_const_or_static = matches!(
227                 parent_node,
228                 Node::Item(&hir::Item {
229                     kind: hir::ItemKind::Const(..) | hir::ItemKind::Static(..),
230                     ..
231                 }) | Node::TraitItem(&hir::TraitItem {
232                     kind: hir::TraitItemKind::Const(..),
233                     ..
234                 }) | Node::ImplItem(&hir::ImplItem { kind: hir::ImplItemKind::Const(..), .. })
235             );
236         }
237
238         // if function is wrapped around a const or static,
239         // then don't show the suggestion
240         if !(is_fn && is_const_or_static) {
241             err.multipart_suggestion(
242                 "use type parameters instead",
243                 sugg,
244                 Applicability::HasPlaceholders,
245             );
246         }
247     }
248
249     err
250 }
251
252 fn reject_placeholder_type_signatures_in_item<'tcx>(
253     tcx: TyCtxt<'tcx>,
254     item: &'tcx hir::Item<'tcx>,
255 ) {
256     let (generics, suggest) = match &item.kind {
257         hir::ItemKind::Union(_, generics)
258         | hir::ItemKind::Enum(_, generics)
259         | hir::ItemKind::TraitAlias(generics, _)
260         | hir::ItemKind::Trait(_, _, generics, ..)
261         | hir::ItemKind::Impl(hir::Impl { generics, .. })
262         | hir::ItemKind::Struct(_, generics) => (generics, true),
263         hir::ItemKind::OpaqueTy(hir::OpaqueTy { generics, .. })
264         | hir::ItemKind::TyAlias(_, generics) => (generics, false),
265         // `static`, `fn` and `const` are handled elsewhere to suggest appropriate type.
266         _ => return,
267     };
268
269     let mut visitor = HirPlaceholderCollector::default();
270     visitor.visit_item(item);
271
272     placeholder_type_error(tcx, Some(generics), visitor.0, suggest, None, item.kind.descr());
273 }
274
275 impl<'tcx> Visitor<'tcx> for CollectItemTypesVisitor<'tcx> {
276     type NestedFilter = nested_filter::OnlyBodies;
277
278     fn nested_visit_map(&mut self) -> Self::Map {
279         self.tcx.hir()
280     }
281
282     fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
283         convert_item(self.tcx, item.item_id());
284         reject_placeholder_type_signatures_in_item(self.tcx, item);
285         intravisit::walk_item(self, item);
286     }
287
288     fn visit_generics(&mut self, generics: &'tcx hir::Generics<'tcx>) {
289         for param in generics.params {
290             match param.kind {
291                 hir::GenericParamKind::Lifetime { .. } => {}
292                 hir::GenericParamKind::Type { default: Some(_), .. } => {
293                     self.tcx.ensure().type_of(param.def_id);
294                 }
295                 hir::GenericParamKind::Type { .. } => {}
296                 hir::GenericParamKind::Const { default, .. } => {
297                     self.tcx.ensure().type_of(param.def_id);
298                     if let Some(default) = default {
299                         // need to store default and type of default
300                         self.tcx.ensure().type_of(default.def_id);
301                         self.tcx.ensure().const_param_default(param.def_id);
302                     }
303                 }
304             }
305         }
306         intravisit::walk_generics(self, generics);
307     }
308
309     fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
310         if let hir::ExprKind::Closure(closure) = expr.kind {
311             self.tcx.ensure().generics_of(closure.def_id);
312             self.tcx.ensure().codegen_fn_attrs(closure.def_id);
313             // We do not call `type_of` for closures here as that
314             // depends on typecheck and would therefore hide
315             // any further errors in case one typeck fails.
316         }
317         intravisit::walk_expr(self, expr);
318     }
319
320     fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>) {
321         convert_trait_item(self.tcx, trait_item.trait_item_id());
322         intravisit::walk_trait_item(self, trait_item);
323     }
324
325     fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) {
326         convert_impl_item(self.tcx, impl_item.impl_item_id());
327         intravisit::walk_impl_item(self, impl_item);
328     }
329 }
330
331 ///////////////////////////////////////////////////////////////////////////
332 // Utility types and common code for the above passes.
333
334 fn bad_placeholder<'tcx>(
335     tcx: TyCtxt<'tcx>,
336     mut spans: Vec<Span>,
337     kind: &'static str,
338 ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
339     let kind = if kind.ends_with('s') { format!("{}es", kind) } else { format!("{}s", kind) };
340
341     spans.sort();
342     let mut err = struct_span_err!(
343         tcx.sess,
344         spans.clone(),
345         E0121,
346         "the placeholder `_` is not allowed within types on item signatures for {}",
347         kind
348     );
349     for span in spans {
350         err.span_label(span, "not allowed in type signatures");
351     }
352     err
353 }
354
355 impl<'tcx> ItemCtxt<'tcx> {
356     pub fn new(tcx: TyCtxt<'tcx>, item_def_id: DefId) -> ItemCtxt<'tcx> {
357         ItemCtxt { tcx, item_def_id }
358     }
359
360     pub fn to_ty(&self, ast_ty: &hir::Ty<'_>) -> Ty<'tcx> {
361         <dyn AstConv<'_>>::ast_ty_to_ty(self, ast_ty)
362     }
363
364     pub fn hir_id(&self) -> hir::HirId {
365         self.tcx.hir().local_def_id_to_hir_id(self.item_def_id.expect_local())
366     }
367
368     pub fn node(&self) -> hir::Node<'tcx> {
369         self.tcx.hir().get(self.hir_id())
370     }
371 }
372
373 impl<'tcx> AstConv<'tcx> for ItemCtxt<'tcx> {
374     fn tcx(&self) -> TyCtxt<'tcx> {
375         self.tcx
376     }
377
378     fn item_def_id(&self) -> DefId {
379         self.item_def_id
380     }
381
382     fn get_type_parameter_bounds(
383         &self,
384         span: Span,
385         def_id: DefId,
386         assoc_name: Ident,
387     ) -> ty::GenericPredicates<'tcx> {
388         self.tcx.at(span).type_param_predicates((
389             self.item_def_id,
390             def_id.expect_local(),
391             assoc_name,
392         ))
393     }
394
395     fn re_infer(&self, _: Option<&ty::GenericParamDef>, _: Span) -> Option<ty::Region<'tcx>> {
396         None
397     }
398
399     fn allow_ty_infer(&self) -> bool {
400         false
401     }
402
403     fn ty_infer(&self, _: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx> {
404         self.tcx().ty_error_with_message(span, "bad placeholder type")
405     }
406
407     fn ct_infer(&self, ty: Ty<'tcx>, _: Option<&ty::GenericParamDef>, span: Span) -> Const<'tcx> {
408         let ty = self.tcx.fold_regions(ty, |r, _| match *r {
409             ty::ReErased => self.tcx.lifetimes.re_static,
410             _ => r,
411         });
412         self.tcx().const_error_with_message(ty, span, "bad placeholder constant")
413     }
414
415     fn projected_ty_from_poly_trait_ref(
416         &self,
417         span: Span,
418         item_def_id: DefId,
419         item_segment: &hir::PathSegment<'_>,
420         poly_trait_ref: ty::PolyTraitRef<'tcx>,
421     ) -> Ty<'tcx> {
422         if let Some(trait_ref) = poly_trait_ref.no_bound_vars() {
423             let item_substs = <dyn AstConv<'tcx>>::create_substs_for_associated_item(
424                 self,
425                 span,
426                 item_def_id,
427                 item_segment,
428                 trait_ref.substs,
429             );
430             self.tcx().mk_projection(item_def_id, item_substs)
431         } else {
432             // There are no late-bound regions; we can just ignore the binder.
433             let mut err = struct_span_err!(
434                 self.tcx().sess,
435                 span,
436                 E0212,
437                 "cannot use the associated type of a trait \
438                  with uninferred generic parameters"
439             );
440
441             match self.node() {
442                 hir::Node::Field(_) | hir::Node::Ctor(_) | hir::Node::Variant(_) => {
443                     let item = self
444                         .tcx
445                         .hir()
446                         .expect_item(self.tcx.hir().get_parent_item(self.hir_id()).def_id);
447                     match &item.kind {
448                         hir::ItemKind::Enum(_, generics)
449                         | hir::ItemKind::Struct(_, generics)
450                         | hir::ItemKind::Union(_, generics) => {
451                             let lt_name = get_new_lifetime_name(self.tcx, poly_trait_ref, generics);
452                             let (lt_sp, sugg) = match generics.params {
453                                 [] => (generics.span, format!("<{}>", lt_name)),
454                                 [bound, ..] => {
455                                     (bound.span.shrink_to_lo(), format!("{}, ", lt_name))
456                                 }
457                             };
458                             let suggestions = vec![
459                                 (lt_sp, sugg),
460                                 (
461                                     span.with_hi(item_segment.ident.span.lo()),
462                                     format!(
463                                         "{}::",
464                                         // Replace the existing lifetimes with a new named lifetime.
465                                         self.tcx.replace_late_bound_regions_uncached(
466                                             poly_trait_ref,
467                                             |_| {
468                                                 self.tcx.mk_region(ty::ReEarlyBound(
469                                                     ty::EarlyBoundRegion {
470                                                         def_id: item_def_id,
471                                                         index: 0,
472                                                         name: Symbol::intern(&lt_name),
473                                                     },
474                                                 ))
475                                             }
476                                         ),
477                                     ),
478                                 ),
479                             ];
480                             err.multipart_suggestion(
481                                 "use a fully qualified path with explicit lifetimes",
482                                 suggestions,
483                                 Applicability::MaybeIncorrect,
484                             );
485                         }
486                         _ => {}
487                     }
488                 }
489                 hir::Node::Item(hir::Item {
490                     kind:
491                         hir::ItemKind::Struct(..) | hir::ItemKind::Enum(..) | hir::ItemKind::Union(..),
492                     ..
493                 }) => {}
494                 hir::Node::Item(_)
495                 | hir::Node::ForeignItem(_)
496                 | hir::Node::TraitItem(_)
497                 | hir::Node::ImplItem(_) => {
498                     err.span_suggestion_verbose(
499                         span.with_hi(item_segment.ident.span.lo()),
500                         "use a fully qualified path with inferred lifetimes",
501                         format!(
502                             "{}::",
503                             // Erase named lt, we want `<A as B<'_>::C`, not `<A as B<'a>::C`.
504                             self.tcx.anonymize_late_bound_regions(poly_trait_ref).skip_binder(),
505                         ),
506                         Applicability::MaybeIncorrect,
507                     );
508                 }
509                 _ => {}
510             }
511             self.tcx().ty_error_with_guaranteed(err.emit())
512         }
513     }
514
515     fn normalize_ty(&self, _span: Span, ty: Ty<'tcx>) -> Ty<'tcx> {
516         // Types in item signatures are not normalized to avoid undue dependencies.
517         ty
518     }
519
520     fn set_tainted_by_errors(&self, _: ErrorGuaranteed) {
521         // There's no obvious place to track this, so just let it go.
522     }
523
524     fn record_ty(&self, _hir_id: hir::HirId, _ty: Ty<'tcx>, _span: Span) {
525         // There's no place to record types from signatures?
526     }
527 }
528
529 /// Synthesize a new lifetime name that doesn't clash with any of the lifetimes already present.
530 fn get_new_lifetime_name<'tcx>(
531     tcx: TyCtxt<'tcx>,
532     poly_trait_ref: ty::PolyTraitRef<'tcx>,
533     generics: &hir::Generics<'tcx>,
534 ) -> String {
535     let existing_lifetimes = tcx
536         .collect_referenced_late_bound_regions(&poly_trait_ref)
537         .into_iter()
538         .filter_map(|lt| {
539             if let ty::BoundRegionKind::BrNamed(_, name) = lt {
540                 Some(name.as_str().to_string())
541             } else {
542                 None
543             }
544         })
545         .chain(generics.params.iter().filter_map(|param| {
546             if let hir::GenericParamKind::Lifetime { .. } = &param.kind {
547                 Some(param.name.ident().as_str().to_string())
548             } else {
549                 None
550             }
551         }))
552         .collect::<FxHashSet<String>>();
553
554     let a_to_z_repeat_n = |n| {
555         (b'a'..=b'z').map(move |c| {
556             let mut s = '\''.to_string();
557             s.extend(std::iter::repeat(char::from(c)).take(n));
558             s
559         })
560     };
561
562     // If all single char lifetime names are present, we wrap around and double the chars.
563     (1..).flat_map(a_to_z_repeat_n).find(|lt| !existing_lifetimes.contains(lt.as_str())).unwrap()
564 }
565
566 fn convert_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) {
567     let it = tcx.hir().item(item_id);
568     debug!("convert: item {} with id {}", it.ident, it.hir_id());
569     let def_id = item_id.owner_id.def_id;
570
571     match it.kind {
572         // These don't define types.
573         hir::ItemKind::ExternCrate(_)
574         | hir::ItemKind::Use(..)
575         | hir::ItemKind::Macro(..)
576         | hir::ItemKind::Mod(_)
577         | hir::ItemKind::GlobalAsm(_) => {}
578         hir::ItemKind::ForeignMod { items, .. } => {
579             for item in items {
580                 let item = tcx.hir().foreign_item(item.id);
581                 tcx.ensure().generics_of(item.owner_id);
582                 tcx.ensure().type_of(item.owner_id);
583                 tcx.ensure().predicates_of(item.owner_id);
584                 match item.kind {
585                     hir::ForeignItemKind::Fn(..) => {
586                         tcx.ensure().codegen_fn_attrs(item.owner_id);
587                         tcx.ensure().fn_sig(item.owner_id)
588                     }
589                     hir::ForeignItemKind::Static(..) => {
590                         tcx.ensure().codegen_fn_attrs(item.owner_id);
591                         let mut visitor = HirPlaceholderCollector::default();
592                         visitor.visit_foreign_item(item);
593                         placeholder_type_error(
594                             tcx,
595                             None,
596                             visitor.0,
597                             false,
598                             None,
599                             "static variable",
600                         );
601                     }
602                     _ => (),
603                 }
604             }
605         }
606         hir::ItemKind::Enum(..) => {
607             tcx.ensure().generics_of(def_id);
608             tcx.ensure().type_of(def_id);
609             tcx.ensure().predicates_of(def_id);
610             convert_enum_variant_types(tcx, def_id.to_def_id());
611         }
612         hir::ItemKind::Impl { .. } => {
613             tcx.ensure().generics_of(def_id);
614             tcx.ensure().type_of(def_id);
615             tcx.ensure().impl_trait_ref(def_id);
616             tcx.ensure().predicates_of(def_id);
617         }
618         hir::ItemKind::Trait(..) => {
619             tcx.ensure().generics_of(def_id);
620             tcx.ensure().trait_def(def_id);
621             tcx.at(it.span).super_predicates_of(def_id);
622             tcx.ensure().predicates_of(def_id);
623         }
624         hir::ItemKind::TraitAlias(..) => {
625             tcx.ensure().generics_of(def_id);
626             tcx.at(it.span).super_predicates_of(def_id);
627             tcx.ensure().predicates_of(def_id);
628         }
629         hir::ItemKind::Struct(ref struct_def, _) | hir::ItemKind::Union(ref struct_def, _) => {
630             tcx.ensure().generics_of(def_id);
631             tcx.ensure().type_of(def_id);
632             tcx.ensure().predicates_of(def_id);
633
634             for f in struct_def.fields() {
635                 tcx.ensure().generics_of(f.def_id);
636                 tcx.ensure().type_of(f.def_id);
637                 tcx.ensure().predicates_of(f.def_id);
638             }
639
640             if let Some(ctor_def_id) = struct_def.ctor_def_id() {
641                 convert_variant_ctor(tcx, ctor_def_id);
642             }
643         }
644
645         // Don't call `type_of` on opaque types, since that depends on type
646         // checking function bodies. `check_item_type` ensures that it's called
647         // instead.
648         hir::ItemKind::OpaqueTy(..) => {
649             tcx.ensure().generics_of(def_id);
650             tcx.ensure().predicates_of(def_id);
651             tcx.ensure().explicit_item_bounds(def_id);
652             tcx.ensure().item_bounds(def_id);
653         }
654
655         hir::ItemKind::TyAlias(..) => {
656             tcx.ensure().generics_of(def_id);
657             tcx.ensure().type_of(def_id);
658             tcx.ensure().predicates_of(def_id);
659         }
660
661         hir::ItemKind::Static(ty, ..) | hir::ItemKind::Const(ty, ..) => {
662             tcx.ensure().generics_of(def_id);
663             tcx.ensure().type_of(def_id);
664             tcx.ensure().predicates_of(def_id);
665             if !is_suggestable_infer_ty(ty) {
666                 let mut visitor = HirPlaceholderCollector::default();
667                 visitor.visit_item(it);
668                 placeholder_type_error(tcx, None, visitor.0, false, None, it.kind.descr());
669             }
670         }
671
672         hir::ItemKind::Fn(..) => {
673             tcx.ensure().generics_of(def_id);
674             tcx.ensure().type_of(def_id);
675             tcx.ensure().predicates_of(def_id);
676             tcx.ensure().fn_sig(def_id);
677             tcx.ensure().codegen_fn_attrs(def_id);
678         }
679     }
680 }
681
682 fn convert_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::TraitItemId) {
683     let trait_item = tcx.hir().trait_item(trait_item_id);
684     let def_id = trait_item_id.owner_id;
685     tcx.ensure().generics_of(def_id);
686
687     match trait_item.kind {
688         hir::TraitItemKind::Fn(..) => {
689             tcx.ensure().codegen_fn_attrs(def_id);
690             tcx.ensure().type_of(def_id);
691             tcx.ensure().fn_sig(def_id);
692         }
693
694         hir::TraitItemKind::Const(.., Some(_)) => {
695             tcx.ensure().type_of(def_id);
696         }
697
698         hir::TraitItemKind::Const(hir_ty, _) => {
699             tcx.ensure().type_of(def_id);
700             // Account for `const C: _;`.
701             let mut visitor = HirPlaceholderCollector::default();
702             visitor.visit_trait_item(trait_item);
703             if !tcx.sess.diagnostic().has_stashed_diagnostic(hir_ty.span, StashKey::ItemNoType) {
704                 placeholder_type_error(tcx, None, visitor.0, false, None, "constant");
705             }
706         }
707
708         hir::TraitItemKind::Type(_, Some(_)) => {
709             tcx.ensure().item_bounds(def_id);
710             tcx.ensure().type_of(def_id);
711             // Account for `type T = _;`.
712             let mut visitor = HirPlaceholderCollector::default();
713             visitor.visit_trait_item(trait_item);
714             placeholder_type_error(tcx, None, visitor.0, false, None, "associated type");
715         }
716
717         hir::TraitItemKind::Type(_, None) => {
718             tcx.ensure().item_bounds(def_id);
719             // #74612: Visit and try to find bad placeholders
720             // even if there is no concrete type.
721             let mut visitor = HirPlaceholderCollector::default();
722             visitor.visit_trait_item(trait_item);
723
724             placeholder_type_error(tcx, None, visitor.0, false, None, "associated type");
725         }
726     };
727
728     tcx.ensure().predicates_of(def_id);
729 }
730
731 fn convert_impl_item(tcx: TyCtxt<'_>, impl_item_id: hir::ImplItemId) {
732     let def_id = impl_item_id.owner_id;
733     tcx.ensure().generics_of(def_id);
734     tcx.ensure().type_of(def_id);
735     tcx.ensure().predicates_of(def_id);
736     let impl_item = tcx.hir().impl_item(impl_item_id);
737     match impl_item.kind {
738         hir::ImplItemKind::Fn(..) => {
739             tcx.ensure().codegen_fn_attrs(def_id);
740             tcx.ensure().fn_sig(def_id);
741         }
742         hir::ImplItemKind::Type(_) => {
743             // Account for `type T = _;`
744             let mut visitor = HirPlaceholderCollector::default();
745             visitor.visit_impl_item(impl_item);
746
747             placeholder_type_error(tcx, None, visitor.0, false, None, "associated type");
748         }
749         hir::ImplItemKind::Const(..) => {}
750     }
751 }
752
753 fn convert_variant_ctor(tcx: TyCtxt<'_>, def_id: LocalDefId) {
754     tcx.ensure().generics_of(def_id);
755     tcx.ensure().type_of(def_id);
756     tcx.ensure().predicates_of(def_id);
757 }
758
759 fn convert_enum_variant_types(tcx: TyCtxt<'_>, def_id: DefId) {
760     let def = tcx.adt_def(def_id);
761     let repr_type = def.repr().discr_type();
762     let initial = repr_type.initial_discriminant(tcx);
763     let mut prev_discr = None::<Discr<'_>>;
764
765     // fill the discriminant values and field types
766     for variant in def.variants() {
767         let wrapped_discr = prev_discr.map_or(initial, |d| d.wrap_incr(tcx));
768         prev_discr = Some(
769             if let ty::VariantDiscr::Explicit(const_def_id) = variant.discr {
770                 def.eval_explicit_discr(tcx, const_def_id)
771             } else if let Some(discr) = repr_type.disr_incr(tcx, prev_discr) {
772                 Some(discr)
773             } else {
774                 let span = tcx.def_span(variant.def_id);
775                 struct_span_err!(tcx.sess, span, E0370, "enum discriminant overflowed")
776                     .span_label(span, format!("overflowed on value after {}", prev_discr.unwrap()))
777                     .note(&format!(
778                         "explicitly set `{} = {}` if that is desired outcome",
779                         tcx.item_name(variant.def_id),
780                         wrapped_discr
781                     ))
782                     .emit();
783                 None
784             }
785             .unwrap_or(wrapped_discr),
786         );
787
788         for f in &variant.fields {
789             tcx.ensure().generics_of(f.did);
790             tcx.ensure().type_of(f.did);
791             tcx.ensure().predicates_of(f.did);
792         }
793
794         // Convert the ctor, if any. This also registers the variant as
795         // an item.
796         if let Some(ctor_def_id) = variant.ctor_def_id() {
797             convert_variant_ctor(tcx, ctor_def_id.expect_local());
798         }
799     }
800 }
801
802 fn convert_variant(
803     tcx: TyCtxt<'_>,
804     variant_did: Option<LocalDefId>,
805     ident: Ident,
806     discr: ty::VariantDiscr,
807     def: &hir::VariantData<'_>,
808     adt_kind: ty::AdtKind,
809     parent_did: LocalDefId,
810 ) -> ty::VariantDef {
811     let mut seen_fields: FxHashMap<Ident, Span> = Default::default();
812     let fields = def
813         .fields()
814         .iter()
815         .map(|f| {
816             let dup_span = seen_fields.get(&f.ident.normalize_to_macros_2_0()).cloned();
817             if let Some(prev_span) = dup_span {
818                 tcx.sess.emit_err(errors::FieldAlreadyDeclared {
819                     field_name: f.ident,
820                     span: f.span,
821                     prev_span,
822                 });
823             } else {
824                 seen_fields.insert(f.ident.normalize_to_macros_2_0(), f.span);
825             }
826
827             ty::FieldDef {
828                 did: f.def_id.to_def_id(),
829                 name: f.ident.name,
830                 vis: tcx.visibility(f.def_id),
831             }
832         })
833         .collect();
834     let recovered = match def {
835         hir::VariantData::Struct(_, r) => *r,
836         _ => false,
837     };
838     ty::VariantDef::new(
839         ident.name,
840         variant_did.map(LocalDefId::to_def_id),
841         def.ctor().map(|(kind, _, def_id)| (kind, def_id.to_def_id())),
842         discr,
843         fields,
844         adt_kind,
845         parent_did.to_def_id(),
846         recovered,
847         adt_kind == AdtKind::Struct && tcx.has_attr(parent_did.to_def_id(), sym::non_exhaustive)
848             || variant_did.map_or(false, |variant_did| {
849                 tcx.has_attr(variant_did.to_def_id(), sym::non_exhaustive)
850             }),
851     )
852 }
853
854 fn adt_def<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> ty::AdtDef<'tcx> {
855     use rustc_hir::*;
856
857     let def_id = def_id.expect_local();
858     let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
859     let Node::Item(item) = tcx.hir().get(hir_id) else {
860         bug!();
861     };
862
863     let repr = ReprOptions::new(tcx, def_id.to_def_id());
864     let (kind, variants) = match item.kind {
865         ItemKind::Enum(ref def, _) => {
866             let mut distance_from_explicit = 0;
867             let variants = def
868                 .variants
869                 .iter()
870                 .map(|v| {
871                     let discr = if let Some(ref e) = v.disr_expr {
872                         distance_from_explicit = 0;
873                         ty::VariantDiscr::Explicit(e.def_id.to_def_id())
874                     } else {
875                         ty::VariantDiscr::Relative(distance_from_explicit)
876                     };
877                     distance_from_explicit += 1;
878
879                     convert_variant(
880                         tcx,
881                         Some(v.def_id),
882                         v.ident,
883                         discr,
884                         &v.data,
885                         AdtKind::Enum,
886                         def_id,
887                     )
888                 })
889                 .collect();
890
891             (AdtKind::Enum, variants)
892         }
893         ItemKind::Struct(ref def, _) | ItemKind::Union(ref def, _) => {
894             let adt_kind = match item.kind {
895                 ItemKind::Struct(..) => AdtKind::Struct,
896                 _ => AdtKind::Union,
897             };
898             let variants = std::iter::once(convert_variant(
899                 tcx,
900                 None,
901                 item.ident,
902                 ty::VariantDiscr::Relative(0),
903                 def,
904                 adt_kind,
905                 def_id,
906             ))
907             .collect();
908
909             (adt_kind, variants)
910         }
911         _ => bug!(),
912     };
913     tcx.alloc_adt_def(def_id.to_def_id(), kind, variants, repr)
914 }
915
916 fn trait_def(tcx: TyCtxt<'_>, def_id: DefId) -> ty::TraitDef {
917     let item = tcx.hir().expect_item(def_id.expect_local());
918
919     let (is_auto, unsafety, items) = match item.kind {
920         hir::ItemKind::Trait(is_auto, unsafety, .., items) => {
921             (is_auto == hir::IsAuto::Yes, unsafety, items)
922         }
923         hir::ItemKind::TraitAlias(..) => (false, hir::Unsafety::Normal, &[][..]),
924         _ => span_bug!(item.span, "trait_def_of_item invoked on non-trait"),
925     };
926
927     let paren_sugar = tcx.has_attr(def_id, sym::rustc_paren_sugar);
928     if paren_sugar && !tcx.features().unboxed_closures {
929         tcx.sess
930             .struct_span_err(
931                 item.span,
932                 "the `#[rustc_paren_sugar]` attribute is a temporary means of controlling \
933                  which traits can use parenthetical notation",
934             )
935             .help("add `#![feature(unboxed_closures)]` to the crate attributes to use it")
936             .emit();
937     }
938
939     let is_marker = tcx.has_attr(def_id, sym::marker);
940     let skip_array_during_method_dispatch =
941         tcx.has_attr(def_id, sym::rustc_skip_array_during_method_dispatch);
942     let spec_kind = if tcx.has_attr(def_id, sym::rustc_unsafe_specialization_marker) {
943         ty::trait_def::TraitSpecializationKind::Marker
944     } else if tcx.has_attr(def_id, sym::rustc_specialization_trait) {
945         ty::trait_def::TraitSpecializationKind::AlwaysApplicable
946     } else {
947         ty::trait_def::TraitSpecializationKind::None
948     };
949     let must_implement_one_of = tcx
950         .get_attr(def_id, sym::rustc_must_implement_one_of)
951         // Check that there are at least 2 arguments of `#[rustc_must_implement_one_of]`
952         // and that they are all identifiers
953         .and_then(|attr| match attr.meta_item_list() {
954             Some(items) if items.len() < 2 => {
955                 tcx.sess
956                     .struct_span_err(
957                         attr.span,
958                         "the `#[rustc_must_implement_one_of]` attribute must be \
959                         used with at least 2 args",
960                     )
961                     .emit();
962
963                 None
964             }
965             Some(items) => items
966                 .into_iter()
967                 .map(|item| item.ident().ok_or(item.span()))
968                 .collect::<Result<Box<[_]>, _>>()
969                 .map_err(|span| {
970                     tcx.sess
971                         .struct_span_err(span, "must be a name of an associated function")
972                         .emit();
973                 })
974                 .ok()
975                 .zip(Some(attr.span)),
976             // Error is reported by `rustc_attr!`
977             None => None,
978         })
979         // Check that all arguments of `#[rustc_must_implement_one_of]` reference
980         // functions in the trait with default implementations
981         .and_then(|(list, attr_span)| {
982             let errors = list.iter().filter_map(|ident| {
983                 let item = items.iter().find(|item| item.ident == *ident);
984
985                 match item {
986                     Some(item) if matches!(item.kind, hir::AssocItemKind::Fn { .. }) => {
987                         if !tcx.impl_defaultness(item.id.owner_id).has_value() {
988                             tcx.sess
989                                 .struct_span_err(
990                                     item.span,
991                                     "This function doesn't have a default implementation",
992                                 )
993                                 .span_note(attr_span, "required by this annotation")
994                                 .emit();
995
996                             return Some(());
997                         }
998
999                         return None;
1000                     }
1001                     Some(item) => {
1002                         tcx.sess
1003                             .struct_span_err(item.span, "Not a function")
1004                             .span_note(attr_span, "required by this annotation")
1005                             .note(
1006                                 "All `#[rustc_must_implement_one_of]` arguments \
1007                             must be associated function names",
1008                             )
1009                             .emit();
1010                     }
1011                     None => {
1012                         tcx.sess
1013                             .struct_span_err(ident.span, "Function not found in this trait")
1014                             .emit();
1015                     }
1016                 }
1017
1018                 Some(())
1019             });
1020
1021             (errors.count() == 0).then_some(list)
1022         })
1023         // Check for duplicates
1024         .and_then(|list| {
1025             let mut set: FxHashMap<Symbol, Span> = FxHashMap::default();
1026             let mut no_dups = true;
1027
1028             for ident in &*list {
1029                 if let Some(dup) = set.insert(ident.name, ident.span) {
1030                     tcx.sess
1031                         .struct_span_err(vec![dup, ident.span], "Functions names are duplicated")
1032                         .note(
1033                             "All `#[rustc_must_implement_one_of]` arguments \
1034                             must be unique",
1035                         )
1036                         .emit();
1037
1038                     no_dups = false;
1039                 }
1040             }
1041
1042             no_dups.then_some(list)
1043         });
1044
1045     ty::TraitDef::new(
1046         def_id,
1047         unsafety,
1048         paren_sugar,
1049         is_auto,
1050         is_marker,
1051         skip_array_during_method_dispatch,
1052         spec_kind,
1053         must_implement_one_of,
1054     )
1055 }
1056
1057 fn are_suggestable_generic_args(generic_args: &[hir::GenericArg<'_>]) -> bool {
1058     generic_args.iter().any(|arg| match arg {
1059         hir::GenericArg::Type(ty) => is_suggestable_infer_ty(ty),
1060         hir::GenericArg::Infer(_) => true,
1061         _ => false,
1062     })
1063 }
1064
1065 /// Whether `ty` is a type with `_` placeholders that can be inferred. Used in diagnostics only to
1066 /// use inference to provide suggestions for the appropriate type if possible.
1067 fn is_suggestable_infer_ty(ty: &hir::Ty<'_>) -> bool {
1068     debug!(?ty);
1069     use hir::TyKind::*;
1070     match &ty.kind {
1071         Infer => true,
1072         Slice(ty) => is_suggestable_infer_ty(ty),
1073         Array(ty, length) => {
1074             is_suggestable_infer_ty(ty) || matches!(length, hir::ArrayLen::Infer(_, _))
1075         }
1076         Tup(tys) => tys.iter().any(is_suggestable_infer_ty),
1077         Ptr(mut_ty) | Rptr(_, mut_ty) => is_suggestable_infer_ty(mut_ty.ty),
1078         OpaqueDef(_, generic_args, _) => are_suggestable_generic_args(generic_args),
1079         Path(hir::QPath::TypeRelative(ty, segment)) => {
1080             is_suggestable_infer_ty(ty) || are_suggestable_generic_args(segment.args().args)
1081         }
1082         Path(hir::QPath::Resolved(ty_opt, hir::Path { segments, .. })) => {
1083             ty_opt.map_or(false, is_suggestable_infer_ty)
1084                 || segments.iter().any(|segment| are_suggestable_generic_args(segment.args().args))
1085         }
1086         _ => false,
1087     }
1088 }
1089
1090 pub fn get_infer_ret_ty<'hir>(output: &'hir hir::FnRetTy<'hir>) -> Option<&'hir hir::Ty<'hir>> {
1091     if let hir::FnRetTy::Return(ty) = output {
1092         if is_suggestable_infer_ty(ty) {
1093             return Some(&*ty);
1094         }
1095     }
1096     None
1097 }
1098
1099 #[instrument(level = "debug", skip(tcx))]
1100 fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> {
1101     use rustc_hir::Node::*;
1102     use rustc_hir::*;
1103
1104     let def_id = def_id.expect_local();
1105     let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
1106
1107     let icx = ItemCtxt::new(tcx, def_id.to_def_id());
1108
1109     match tcx.hir().get(hir_id) {
1110         TraitItem(hir::TraitItem {
1111             kind: TraitItemKind::Fn(sig, TraitFn::Provided(_)),
1112             generics,
1113             ..
1114         })
1115         | Item(hir::Item { kind: ItemKind::Fn(sig, generics, _), .. }) => {
1116             infer_return_ty_for_fn_sig(tcx, sig, generics, def_id, &icx)
1117         }
1118
1119         ImplItem(hir::ImplItem { kind: ImplItemKind::Fn(sig, _), generics, .. }) => {
1120             // Do not try to infer the return type for a impl method coming from a trait
1121             if let Item(hir::Item { kind: ItemKind::Impl(i), .. }) =
1122                 tcx.hir().get(tcx.hir().get_parent_node(hir_id))
1123                 && i.of_trait.is_some()
1124             {
1125                 <dyn AstConv<'_>>::ty_of_fn(
1126                     &icx,
1127                     hir_id,
1128                     sig.header.unsafety,
1129                     sig.header.abi,
1130                     sig.decl,
1131                     Some(generics),
1132                     None,
1133                 )
1134             } else {
1135                 infer_return_ty_for_fn_sig(tcx, sig, generics, def_id, &icx)
1136             }
1137         }
1138
1139         TraitItem(hir::TraitItem {
1140             kind: TraitItemKind::Fn(FnSig { header, decl, span: _ }, _),
1141             generics,
1142             ..
1143         }) => <dyn AstConv<'_>>::ty_of_fn(
1144             &icx,
1145             hir_id,
1146             header.unsafety,
1147             header.abi,
1148             decl,
1149             Some(generics),
1150             None,
1151         ),
1152
1153         ForeignItem(&hir::ForeignItem { kind: ForeignItemKind::Fn(fn_decl, _, _), .. }) => {
1154             let abi = tcx.hir().get_foreign_abi(hir_id);
1155             compute_sig_of_foreign_fn_decl(tcx, def_id.to_def_id(), fn_decl, abi)
1156         }
1157
1158         Ctor(data) | Variant(hir::Variant { data, .. }) if data.ctor().is_some() => {
1159             let ty = tcx.type_of(tcx.hir().get_parent_item(hir_id));
1160             let inputs = data.fields().iter().map(|f| tcx.type_of(f.def_id));
1161             ty::Binder::dummy(tcx.mk_fn_sig(
1162                 inputs,
1163                 ty,
1164                 false,
1165                 hir::Unsafety::Normal,
1166                 abi::Abi::Rust,
1167             ))
1168         }
1169
1170         Expr(&hir::Expr { kind: hir::ExprKind::Closure { .. }, .. }) => {
1171             // Closure signatures are not like other function
1172             // signatures and cannot be accessed through `fn_sig`. For
1173             // example, a closure signature excludes the `self`
1174             // argument. In any case they are embedded within the
1175             // closure type as part of the `ClosureSubsts`.
1176             //
1177             // To get the signature of a closure, you should use the
1178             // `sig` method on the `ClosureSubsts`:
1179             //
1180             //    substs.as_closure().sig(def_id, tcx)
1181             bug!(
1182                 "to get the signature of a closure, use `substs.as_closure().sig()` not `fn_sig()`",
1183             );
1184         }
1185
1186         x => {
1187             bug!("unexpected sort of node in fn_sig(): {:?}", x);
1188         }
1189     }
1190 }
1191
1192 fn infer_return_ty_for_fn_sig<'tcx>(
1193     tcx: TyCtxt<'tcx>,
1194     sig: &hir::FnSig<'_>,
1195     generics: &hir::Generics<'_>,
1196     def_id: LocalDefId,
1197     icx: &ItemCtxt<'tcx>,
1198 ) -> ty::PolyFnSig<'tcx> {
1199     let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
1200
1201     match get_infer_ret_ty(&sig.decl.output) {
1202         Some(ty) => {
1203             let fn_sig = tcx.typeck(def_id).liberated_fn_sigs()[hir_id];
1204             // Typeck doesn't expect erased regions to be returned from `type_of`.
1205             let fn_sig = tcx.fold_regions(fn_sig, |r, _| match *r {
1206                 ty::ReErased => tcx.lifetimes.re_static,
1207                 _ => r,
1208             });
1209             let fn_sig = ty::Binder::dummy(fn_sig);
1210
1211             let mut visitor = HirPlaceholderCollector::default();
1212             visitor.visit_ty(ty);
1213             let mut diag = bad_placeholder(tcx, visitor.0, "return type");
1214             let ret_ty = fn_sig.skip_binder().output();
1215             if ret_ty.is_suggestable(tcx, false) {
1216                 diag.span_suggestion(
1217                     ty.span,
1218                     "replace with the correct return type",
1219                     ret_ty,
1220                     Applicability::MachineApplicable,
1221                 );
1222             } else if matches!(ret_ty.kind(), ty::FnDef(..)) {
1223                 let fn_sig = ret_ty.fn_sig(tcx);
1224                 if fn_sig
1225                     .skip_binder()
1226                     .inputs_and_output
1227                     .iter()
1228                     .all(|t| t.is_suggestable(tcx, false))
1229                 {
1230                     diag.span_suggestion(
1231                         ty.span,
1232                         "replace with the correct return type",
1233                         fn_sig,
1234                         Applicability::MachineApplicable,
1235                     );
1236                 }
1237             } else if ret_ty.is_closure() {
1238                 // We're dealing with a closure, so we should suggest using `impl Fn` or trait bounds
1239                 // to prevent the user from getting a papercut while trying to use the unique closure
1240                 // syntax (e.g. `[closure@src/lib.rs:2:5: 2:9]`).
1241                 diag.help("consider using an `Fn`, `FnMut`, or `FnOnce` trait bound");
1242                 diag.note("for more information on `Fn` traits and closure types, see https://doc.rust-lang.org/book/ch13-01-closures.html");
1243             }
1244             diag.emit();
1245
1246             fn_sig
1247         }
1248         None => <dyn AstConv<'_>>::ty_of_fn(
1249             icx,
1250             hir_id,
1251             sig.header.unsafety,
1252             sig.header.abi,
1253             sig.decl,
1254             Some(generics),
1255             None,
1256         ),
1257     }
1258 }
1259
1260 fn impl_trait_ref(tcx: TyCtxt<'_>, def_id: DefId) -> Option<ty::TraitRef<'_>> {
1261     let icx = ItemCtxt::new(tcx, def_id);
1262     let item = tcx.hir().expect_item(def_id.expect_local());
1263     match item.kind {
1264         hir::ItemKind::Impl(ref impl_) => impl_.of_trait.as_ref().map(|ast_trait_ref| {
1265             let selfty = tcx.type_of(def_id);
1266             <dyn AstConv<'_>>::instantiate_mono_trait_ref(
1267                 &icx,
1268                 ast_trait_ref,
1269                 selfty,
1270                 check_impl_constness(tcx, impl_.constness, ast_trait_ref),
1271             )
1272         }),
1273         _ => bug!(),
1274     }
1275 }
1276
1277 fn check_impl_constness(
1278     tcx: TyCtxt<'_>,
1279     constness: hir::Constness,
1280     ast_trait_ref: &hir::TraitRef<'_>,
1281 ) -> ty::BoundConstness {
1282     match constness {
1283         hir::Constness::Const => {
1284             if let Some(trait_def_id) = ast_trait_ref.trait_def_id() && !tcx.has_attr(trait_def_id, sym::const_trait) {
1285                 let trait_name = tcx.item_name(trait_def_id).to_string();
1286                 tcx.sess.emit_err(errors::ConstImplForNonConstTrait {
1287                     trait_ref_span: ast_trait_ref.path.span,
1288                     trait_name,
1289                     local_trait_span: trait_def_id.as_local().map(|_| tcx.def_span(trait_def_id).shrink_to_lo()),
1290                     marking: (),
1291                     adding: (),
1292                 });
1293                 ty::BoundConstness::NotConst
1294             } else {
1295                 ty::BoundConstness::ConstIfConst
1296             }
1297         },
1298         hir::Constness::NotConst => ty::BoundConstness::NotConst,
1299     }
1300 }
1301
1302 fn impl_polarity(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ImplPolarity {
1303     let is_rustc_reservation = tcx.has_attr(def_id, sym::rustc_reservation_impl);
1304     let item = tcx.hir().expect_item(def_id.expect_local());
1305     match &item.kind {
1306         hir::ItemKind::Impl(hir::Impl {
1307             polarity: hir::ImplPolarity::Negative(span),
1308             of_trait,
1309             ..
1310         }) => {
1311             if is_rustc_reservation {
1312                 let span = span.to(of_trait.as_ref().map_or(*span, |t| t.path.span));
1313                 tcx.sess.span_err(span, "reservation impls can't be negative");
1314             }
1315             ty::ImplPolarity::Negative
1316         }
1317         hir::ItemKind::Impl(hir::Impl {
1318             polarity: hir::ImplPolarity::Positive,
1319             of_trait: None,
1320             ..
1321         }) => {
1322             if is_rustc_reservation {
1323                 tcx.sess.span_err(item.span, "reservation impls can't be inherent");
1324             }
1325             ty::ImplPolarity::Positive
1326         }
1327         hir::ItemKind::Impl(hir::Impl {
1328             polarity: hir::ImplPolarity::Positive,
1329             of_trait: Some(_),
1330             ..
1331         }) => {
1332             if is_rustc_reservation {
1333                 ty::ImplPolarity::Reservation
1334             } else {
1335                 ty::ImplPolarity::Positive
1336             }
1337         }
1338         item => bug!("impl_polarity: {:?} not an impl", item),
1339     }
1340 }
1341
1342 /// Returns the early-bound lifetimes declared in this generics
1343 /// listing. For anything other than fns/methods, this is just all
1344 /// the lifetimes that are declared. For fns or methods, we have to
1345 /// screen out those that do not appear in any where-clauses etc using
1346 /// `resolve_lifetime::early_bound_lifetimes`.
1347 fn early_bound_lifetimes_from_generics<'a, 'tcx: 'a>(
1348     tcx: TyCtxt<'tcx>,
1349     generics: &'a hir::Generics<'a>,
1350 ) -> impl Iterator<Item = &'a hir::GenericParam<'a>> + Captures<'tcx> {
1351     generics.params.iter().filter(move |param| match param.kind {
1352         GenericParamKind::Lifetime { .. } => !tcx.is_late_bound(param.hir_id),
1353         _ => false,
1354     })
1355 }
1356
1357 /// Returns a list of type predicates for the definition with ID `def_id`, including inferred
1358 /// lifetime constraints. This includes all predicates returned by `explicit_predicates_of`, plus
1359 /// inferred constraints concerning which regions outlive other regions.
1360 #[instrument(level = "debug", skip(tcx))]
1361 fn predicates_defined_on(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicates<'_> {
1362     let mut result = tcx.explicit_predicates_of(def_id);
1363     debug!("predicates_defined_on: explicit_predicates_of({:?}) = {:?}", def_id, result,);
1364     let inferred_outlives = tcx.inferred_outlives_of(def_id);
1365     if !inferred_outlives.is_empty() {
1366         debug!(
1367             "predicates_defined_on: inferred_outlives_of({:?}) = {:?}",
1368             def_id, inferred_outlives,
1369         );
1370         if result.predicates.is_empty() {
1371             result.predicates = inferred_outlives;
1372         } else {
1373             result.predicates = tcx
1374                 .arena
1375                 .alloc_from_iter(result.predicates.iter().chain(inferred_outlives).copied());
1376         }
1377     }
1378
1379     debug!("predicates_defined_on({:?}) = {:?}", def_id, result);
1380     result
1381 }
1382
1383 fn compute_sig_of_foreign_fn_decl<'tcx>(
1384     tcx: TyCtxt<'tcx>,
1385     def_id: DefId,
1386     decl: &'tcx hir::FnDecl<'tcx>,
1387     abi: abi::Abi,
1388 ) -> ty::PolyFnSig<'tcx> {
1389     let unsafety = if abi == abi::Abi::RustIntrinsic {
1390         intrinsic_operation_unsafety(tcx, def_id)
1391     } else {
1392         hir::Unsafety::Unsafe
1393     };
1394     let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
1395     let fty = <dyn AstConv<'_>>::ty_of_fn(
1396         &ItemCtxt::new(tcx, def_id),
1397         hir_id,
1398         unsafety,
1399         abi,
1400         decl,
1401         None,
1402         None,
1403     );
1404
1405     // Feature gate SIMD types in FFI, since I am not sure that the
1406     // ABIs are handled at all correctly. -huonw
1407     if abi != abi::Abi::RustIntrinsic
1408         && abi != abi::Abi::PlatformIntrinsic
1409         && !tcx.features().simd_ffi
1410     {
1411         let check = |ast_ty: &hir::Ty<'_>, ty: Ty<'_>| {
1412             if ty.is_simd() {
1413                 let snip = tcx
1414                     .sess
1415                     .source_map()
1416                     .span_to_snippet(ast_ty.span)
1417                     .map_or_else(|_| String::new(), |s| format!(" `{}`", s));
1418                 tcx.sess
1419                     .struct_span_err(
1420                         ast_ty.span,
1421                         &format!(
1422                             "use of SIMD type{} in FFI is highly experimental and \
1423                              may result in invalid code",
1424                             snip
1425                         ),
1426                     )
1427                     .help("add `#![feature(simd_ffi)]` to the crate attributes to enable")
1428                     .emit();
1429             }
1430         };
1431         for (input, ty) in iter::zip(decl.inputs, fty.inputs().skip_binder()) {
1432             check(input, *ty)
1433         }
1434         if let hir::FnRetTy::Return(ref ty) = decl.output {
1435             check(ty, fty.output().skip_binder())
1436         }
1437     }
1438
1439     fty
1440 }
1441
1442 fn is_foreign_item(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
1443     match tcx.hir().get_if_local(def_id) {
1444         Some(Node::ForeignItem(..)) => true,
1445         Some(_) => false,
1446         _ => bug!("is_foreign_item applied to non-local def-id {:?}", def_id),
1447     }
1448 }
1449
1450 fn generator_kind(tcx: TyCtxt<'_>, def_id: DefId) -> Option<hir::GeneratorKind> {
1451     match tcx.hir().get_if_local(def_id) {
1452         Some(Node::Expr(&rustc_hir::Expr {
1453             kind: rustc_hir::ExprKind::Closure(&rustc_hir::Closure { body, .. }),
1454             ..
1455         })) => tcx.hir().body(body).generator_kind(),
1456         Some(_) => None,
1457         _ => bug!("generator_kind applied to non-local def-id {:?}", def_id),
1458     }
1459 }
1460
1461 fn from_target_feature(
1462     tcx: TyCtxt<'_>,
1463     attr: &ast::Attribute,
1464     supported_target_features: &FxHashMap<String, Option<Symbol>>,
1465     target_features: &mut Vec<Symbol>,
1466 ) {
1467     let Some(list) = attr.meta_item_list() else { return };
1468     let bad_item = |span| {
1469         let msg = "malformed `target_feature` attribute input";
1470         let code = "enable = \"..\"";
1471         tcx.sess
1472             .struct_span_err(span, msg)
1473             .span_suggestion(span, "must be of the form", code, Applicability::HasPlaceholders)
1474             .emit();
1475     };
1476     let rust_features = tcx.features();
1477     for item in list {
1478         // Only `enable = ...` is accepted in the meta-item list.
1479         if !item.has_name(sym::enable) {
1480             bad_item(item.span());
1481             continue;
1482         }
1483
1484         // Must be of the form `enable = "..."` (a string).
1485         let Some(value) = item.value_str() else {
1486             bad_item(item.span());
1487             continue;
1488         };
1489
1490         // We allow comma separation to enable multiple features.
1491         target_features.extend(value.as_str().split(',').filter_map(|feature| {
1492             let Some(feature_gate) = supported_target_features.get(feature) else {
1493                 let msg =
1494                     format!("the feature named `{}` is not valid for this target", feature);
1495                 let mut err = tcx.sess.struct_span_err(item.span(), &msg);
1496                 err.span_label(
1497                     item.span(),
1498                     format!("`{}` is not valid for this target", feature),
1499                 );
1500                 if let Some(stripped) = feature.strip_prefix('+') {
1501                     let valid = supported_target_features.contains_key(stripped);
1502                     if valid {
1503                         err.help("consider removing the leading `+` in the feature name");
1504                     }
1505                 }
1506                 err.emit();
1507                 return None;
1508             };
1509
1510             // Only allow features whose feature gates have been enabled.
1511             let allowed = match feature_gate.as_ref().copied() {
1512                 Some(sym::arm_target_feature) => rust_features.arm_target_feature,
1513                 Some(sym::hexagon_target_feature) => rust_features.hexagon_target_feature,
1514                 Some(sym::powerpc_target_feature) => rust_features.powerpc_target_feature,
1515                 Some(sym::mips_target_feature) => rust_features.mips_target_feature,
1516                 Some(sym::riscv_target_feature) => rust_features.riscv_target_feature,
1517                 Some(sym::avx512_target_feature) => rust_features.avx512_target_feature,
1518                 Some(sym::sse4a_target_feature) => rust_features.sse4a_target_feature,
1519                 Some(sym::tbm_target_feature) => rust_features.tbm_target_feature,
1520                 Some(sym::wasm_target_feature) => rust_features.wasm_target_feature,
1521                 Some(sym::cmpxchg16b_target_feature) => rust_features.cmpxchg16b_target_feature,
1522                 Some(sym::movbe_target_feature) => rust_features.movbe_target_feature,
1523                 Some(sym::rtm_target_feature) => rust_features.rtm_target_feature,
1524                 Some(sym::f16c_target_feature) => rust_features.f16c_target_feature,
1525                 Some(sym::ermsb_target_feature) => rust_features.ermsb_target_feature,
1526                 Some(sym::bpf_target_feature) => rust_features.bpf_target_feature,
1527                 Some(sym::aarch64_ver_target_feature) => rust_features.aarch64_ver_target_feature,
1528                 Some(name) => bug!("unknown target feature gate {}", name),
1529                 None => true,
1530             };
1531             if !allowed {
1532                 feature_err(
1533                     &tcx.sess.parse_sess,
1534                     feature_gate.unwrap(),
1535                     item.span(),
1536                     &format!("the target feature `{}` is currently unstable", feature),
1537                 )
1538                 .emit();
1539             }
1540             Some(Symbol::intern(feature))
1541         }));
1542     }
1543 }
1544
1545 fn linkage_by_name(tcx: TyCtxt<'_>, def_id: LocalDefId, name: &str) -> Linkage {
1546     use rustc_middle::mir::mono::Linkage::*;
1547
1548     // Use the names from src/llvm/docs/LangRef.rst here. Most types are only
1549     // applicable to variable declarations and may not really make sense for
1550     // Rust code in the first place but allow them anyway and trust that the
1551     // user knows what they're doing. Who knows, unanticipated use cases may pop
1552     // up in the future.
1553     //
1554     // ghost, dllimport, dllexport and linkonce_odr_autohide are not supported
1555     // and don't have to be, LLVM treats them as no-ops.
1556     match name {
1557         "appending" => Appending,
1558         "available_externally" => AvailableExternally,
1559         "common" => Common,
1560         "extern_weak" => ExternalWeak,
1561         "external" => External,
1562         "internal" => Internal,
1563         "linkonce" => LinkOnceAny,
1564         "linkonce_odr" => LinkOnceODR,
1565         "private" => Private,
1566         "weak" => WeakAny,
1567         "weak_odr" => WeakODR,
1568         _ => tcx.sess.span_fatal(tcx.def_span(def_id), "invalid linkage specified"),
1569     }
1570 }
1571
1572 fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: DefId) -> CodegenFnAttrs {
1573     if cfg!(debug_assertions) {
1574         let def_kind = tcx.def_kind(did);
1575         assert!(
1576             def_kind.has_codegen_attrs(),
1577             "unexpected `def_kind` in `codegen_fn_attrs`: {def_kind:?}",
1578         );
1579     }
1580
1581     let did = did.expect_local();
1582     let attrs = tcx.hir().attrs(tcx.hir().local_def_id_to_hir_id(did));
1583     let mut codegen_fn_attrs = CodegenFnAttrs::new();
1584     if tcx.should_inherit_track_caller(did) {
1585         codegen_fn_attrs.flags |= CodegenFnAttrFlags::TRACK_CALLER;
1586     }
1587
1588     let supported_target_features = tcx.supported_target_features(LOCAL_CRATE);
1589
1590     let mut inline_span = None;
1591     let mut link_ordinal_span = None;
1592     let mut no_sanitize_span = None;
1593     for attr in attrs.iter() {
1594         if attr.has_name(sym::cold) {
1595             codegen_fn_attrs.flags |= CodegenFnAttrFlags::COLD;
1596         } else if attr.has_name(sym::rustc_allocator) {
1597             codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR;
1598         } else if attr.has_name(sym::ffi_returns_twice) {
1599             if tcx.is_foreign_item(did) {
1600                 codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_RETURNS_TWICE;
1601             } else {
1602                 // `#[ffi_returns_twice]` is only allowed `extern fn`s.
1603                 struct_span_err!(
1604                     tcx.sess,
1605                     attr.span,
1606                     E0724,
1607                     "`#[ffi_returns_twice]` may only be used on foreign functions"
1608                 )
1609                 .emit();
1610             }
1611         } else if attr.has_name(sym::ffi_pure) {
1612             if tcx.is_foreign_item(did) {
1613                 if attrs.iter().any(|a| a.has_name(sym::ffi_const)) {
1614                     // `#[ffi_const]` functions cannot be `#[ffi_pure]`
1615                     struct_span_err!(
1616                         tcx.sess,
1617                         attr.span,
1618                         E0757,
1619                         "`#[ffi_const]` function cannot be `#[ffi_pure]`"
1620                     )
1621                     .emit();
1622                 } else {
1623                     codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_PURE;
1624                 }
1625             } else {
1626                 // `#[ffi_pure]` is only allowed on foreign functions
1627                 struct_span_err!(
1628                     tcx.sess,
1629                     attr.span,
1630                     E0755,
1631                     "`#[ffi_pure]` may only be used on foreign functions"
1632                 )
1633                 .emit();
1634             }
1635         } else if attr.has_name(sym::ffi_const) {
1636             if tcx.is_foreign_item(did) {
1637                 codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_CONST;
1638             } else {
1639                 // `#[ffi_const]` is only allowed on foreign functions
1640                 struct_span_err!(
1641                     tcx.sess,
1642                     attr.span,
1643                     E0756,
1644                     "`#[ffi_const]` may only be used on foreign functions"
1645                 )
1646                 .emit();
1647             }
1648         } else if attr.has_name(sym::rustc_nounwind) {
1649             codegen_fn_attrs.flags |= CodegenFnAttrFlags::NEVER_UNWIND;
1650         } else if attr.has_name(sym::rustc_reallocator) {
1651             codegen_fn_attrs.flags |= CodegenFnAttrFlags::REALLOCATOR;
1652         } else if attr.has_name(sym::rustc_deallocator) {
1653             codegen_fn_attrs.flags |= CodegenFnAttrFlags::DEALLOCATOR;
1654         } else if attr.has_name(sym::rustc_allocator_zeroed) {
1655             codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR_ZEROED;
1656         } else if attr.has_name(sym::naked) {
1657             codegen_fn_attrs.flags |= CodegenFnAttrFlags::NAKED;
1658         } else if attr.has_name(sym::no_mangle) {
1659             codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_MANGLE;
1660         } else if attr.has_name(sym::no_coverage) {
1661             codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_COVERAGE;
1662         } else if attr.has_name(sym::rustc_std_internal_symbol) {
1663             codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL;
1664         } else if attr.has_name(sym::used) {
1665             let inner = attr.meta_item_list();
1666             match inner.as_deref() {
1667                 Some([item]) if item.has_name(sym::linker) => {
1668                     if !tcx.features().used_with_arg {
1669                         feature_err(
1670                             &tcx.sess.parse_sess,
1671                             sym::used_with_arg,
1672                             attr.span,
1673                             "`#[used(linker)]` is currently unstable",
1674                         )
1675                         .emit();
1676                     }
1677                     codegen_fn_attrs.flags |= CodegenFnAttrFlags::USED_LINKER;
1678                 }
1679                 Some([item]) if item.has_name(sym::compiler) => {
1680                     if !tcx.features().used_with_arg {
1681                         feature_err(
1682                             &tcx.sess.parse_sess,
1683                             sym::used_with_arg,
1684                             attr.span,
1685                             "`#[used(compiler)]` is currently unstable",
1686                         )
1687                         .emit();
1688                     }
1689                     codegen_fn_attrs.flags |= CodegenFnAttrFlags::USED;
1690                 }
1691                 Some(_) => {
1692                     tcx.sess.emit_err(errors::ExpectedUsedSymbol { span: attr.span });
1693                 }
1694                 None => {
1695                     // Unfortunately, unconditionally using `llvm.used` causes
1696                     // issues in handling `.init_array` with the gold linker,
1697                     // but using `llvm.compiler.used` caused a nontrival amount
1698                     // of unintentional ecosystem breakage -- particularly on
1699                     // Mach-O targets.
1700                     //
1701                     // As a result, we emit `llvm.compiler.used` only on ELF
1702                     // targets. This is somewhat ad-hoc, but actually follows
1703                     // our pre-LLVM 13 behavior (prior to the ecosystem
1704                     // breakage), and seems to match `clang`'s behavior as well
1705                     // (both before and after LLVM 13), possibly because they
1706                     // have similar compatibility concerns to us. See
1707                     // https://github.com/rust-lang/rust/issues/47384#issuecomment-1019080146
1708                     // and following comments for some discussion of this, as
1709                     // well as the comments in `rustc_codegen_llvm` where these
1710                     // flags are handled.
1711                     //
1712                     // Anyway, to be clear: this is still up in the air
1713                     // somewhat, and is subject to change in the future (which
1714                     // is a good thing, because this would ideally be a bit
1715                     // more firmed up).
1716                     let is_like_elf = !(tcx.sess.target.is_like_osx
1717                         || tcx.sess.target.is_like_windows
1718                         || tcx.sess.target.is_like_wasm);
1719                     codegen_fn_attrs.flags |= if is_like_elf {
1720                         CodegenFnAttrFlags::USED
1721                     } else {
1722                         CodegenFnAttrFlags::USED_LINKER
1723                     };
1724                 }
1725             }
1726         } else if attr.has_name(sym::cmse_nonsecure_entry) {
1727             if !matches!(tcx.fn_sig(did).abi(), abi::Abi::C { .. }) {
1728                 struct_span_err!(
1729                     tcx.sess,
1730                     attr.span,
1731                     E0776,
1732                     "`#[cmse_nonsecure_entry]` requires C ABI"
1733                 )
1734                 .emit();
1735             }
1736             if !tcx.sess.target.llvm_target.contains("thumbv8m") {
1737                 struct_span_err!(tcx.sess, attr.span, E0775, "`#[cmse_nonsecure_entry]` is only valid for targets with the TrustZone-M extension")
1738                     .emit();
1739             }
1740             codegen_fn_attrs.flags |= CodegenFnAttrFlags::CMSE_NONSECURE_ENTRY;
1741         } else if attr.has_name(sym::thread_local) {
1742             codegen_fn_attrs.flags |= CodegenFnAttrFlags::THREAD_LOCAL;
1743         } else if attr.has_name(sym::track_caller) {
1744             if !tcx.is_closure(did.to_def_id()) && tcx.fn_sig(did).abi() != abi::Abi::Rust {
1745                 struct_span_err!(tcx.sess, attr.span, E0737, "`#[track_caller]` requires Rust ABI")
1746                     .emit();
1747             }
1748             if tcx.is_closure(did.to_def_id()) && !tcx.features().closure_track_caller {
1749                 feature_err(
1750                     &tcx.sess.parse_sess,
1751                     sym::closure_track_caller,
1752                     attr.span,
1753                     "`#[track_caller]` on closures is currently unstable",
1754                 )
1755                 .emit();
1756             }
1757             codegen_fn_attrs.flags |= CodegenFnAttrFlags::TRACK_CALLER;
1758         } else if attr.has_name(sym::export_name) {
1759             if let Some(s) = attr.value_str() {
1760                 if s.as_str().contains('\0') {
1761                     // `#[export_name = ...]` will be converted to a null-terminated string,
1762                     // so it may not contain any null characters.
1763                     struct_span_err!(
1764                         tcx.sess,
1765                         attr.span,
1766                         E0648,
1767                         "`export_name` may not contain null characters"
1768                     )
1769                     .emit();
1770                 }
1771                 codegen_fn_attrs.export_name = Some(s);
1772             }
1773         } else if attr.has_name(sym::target_feature) {
1774             if !tcx.is_closure(did.to_def_id())
1775                 && tcx.fn_sig(did).unsafety() == hir::Unsafety::Normal
1776             {
1777                 if tcx.sess.target.is_like_wasm || tcx.sess.opts.actually_rustdoc {
1778                     // The `#[target_feature]` attribute is allowed on
1779                     // WebAssembly targets on all functions, including safe
1780                     // ones. Other targets require that `#[target_feature]` is
1781                     // only applied to unsafe functions (pending the
1782                     // `target_feature_11` feature) because on most targets
1783                     // execution of instructions that are not supported is
1784                     // considered undefined behavior. For WebAssembly which is a
1785                     // 100% safe target at execution time it's not possible to
1786                     // execute undefined instructions, and even if a future
1787                     // feature was added in some form for this it would be a
1788                     // deterministic trap. There is no undefined behavior when
1789                     // executing WebAssembly so `#[target_feature]` is allowed
1790                     // on safe functions (but again, only for WebAssembly)
1791                     //
1792                     // Note that this is also allowed if `actually_rustdoc` so
1793                     // if a target is documenting some wasm-specific code then
1794                     // it's not spuriously denied.
1795                 } else if !tcx.features().target_feature_11 {
1796                     let mut err = feature_err(
1797                         &tcx.sess.parse_sess,
1798                         sym::target_feature_11,
1799                         attr.span,
1800                         "`#[target_feature(..)]` can only be applied to `unsafe` functions",
1801                     );
1802                     err.span_label(tcx.def_span(did), "not an `unsafe` function");
1803                     err.emit();
1804                 } else {
1805                     check_target_feature_trait_unsafe(tcx, did, attr.span);
1806                 }
1807             }
1808             from_target_feature(
1809                 tcx,
1810                 attr,
1811                 supported_target_features,
1812                 &mut codegen_fn_attrs.target_features,
1813             );
1814         } else if attr.has_name(sym::linkage) {
1815             if let Some(val) = attr.value_str() {
1816                 codegen_fn_attrs.linkage = Some(linkage_by_name(tcx, did, val.as_str()));
1817             }
1818         } else if attr.has_name(sym::link_section) {
1819             if let Some(val) = attr.value_str() {
1820                 if val.as_str().bytes().any(|b| b == 0) {
1821                     let msg = format!(
1822                         "illegal null byte in link_section \
1823                          value: `{}`",
1824                         &val
1825                     );
1826                     tcx.sess.span_err(attr.span, &msg);
1827                 } else {
1828                     codegen_fn_attrs.link_section = Some(val);
1829                 }
1830             }
1831         } else if attr.has_name(sym::link_name) {
1832             codegen_fn_attrs.link_name = attr.value_str();
1833         } else if attr.has_name(sym::link_ordinal) {
1834             link_ordinal_span = Some(attr.span);
1835             if let ordinal @ Some(_) = check_link_ordinal(tcx, attr) {
1836                 codegen_fn_attrs.link_ordinal = ordinal;
1837             }
1838         } else if attr.has_name(sym::no_sanitize) {
1839             no_sanitize_span = Some(attr.span);
1840             if let Some(list) = attr.meta_item_list() {
1841                 for item in list.iter() {
1842                     if item.has_name(sym::address) {
1843                         codegen_fn_attrs.no_sanitize |= SanitizerSet::ADDRESS;
1844                     } else if item.has_name(sym::cfi) {
1845                         codegen_fn_attrs.no_sanitize |= SanitizerSet::CFI;
1846                     } else if item.has_name(sym::memory) {
1847                         codegen_fn_attrs.no_sanitize |= SanitizerSet::MEMORY;
1848                     } else if item.has_name(sym::memtag) {
1849                         codegen_fn_attrs.no_sanitize |= SanitizerSet::MEMTAG;
1850                     } else if item.has_name(sym::shadow_call_stack) {
1851                         codegen_fn_attrs.no_sanitize |= SanitizerSet::SHADOWCALLSTACK;
1852                     } else if item.has_name(sym::thread) {
1853                         codegen_fn_attrs.no_sanitize |= SanitizerSet::THREAD;
1854                     } else if item.has_name(sym::hwaddress) {
1855                         codegen_fn_attrs.no_sanitize |= SanitizerSet::HWADDRESS;
1856                     } else {
1857                         tcx.sess
1858                             .struct_span_err(item.span(), "invalid argument for `no_sanitize`")
1859                             .note("expected one of: `address`, `cfi`, `hwaddress`, `memory`, `memtag`, `shadow-call-stack`, or `thread`")
1860                             .emit();
1861                     }
1862                 }
1863             }
1864         } else if attr.has_name(sym::instruction_set) {
1865             codegen_fn_attrs.instruction_set = match attr.meta_kind() {
1866                 Some(MetaItemKind::List(ref items)) => match items.as_slice() {
1867                     [NestedMetaItem::MetaItem(set)] => {
1868                         let segments =
1869                             set.path.segments.iter().map(|x| x.ident.name).collect::<Vec<_>>();
1870                         match segments.as_slice() {
1871                             [sym::arm, sym::a32] | [sym::arm, sym::t32] => {
1872                                 if !tcx.sess.target.has_thumb_interworking {
1873                                     struct_span_err!(
1874                                         tcx.sess.diagnostic(),
1875                                         attr.span,
1876                                         E0779,
1877                                         "target does not support `#[instruction_set]`"
1878                                     )
1879                                     .emit();
1880                                     None
1881                                 } else if segments[1] == sym::a32 {
1882                                     Some(InstructionSetAttr::ArmA32)
1883                                 } else if segments[1] == sym::t32 {
1884                                     Some(InstructionSetAttr::ArmT32)
1885                                 } else {
1886                                     unreachable!()
1887                                 }
1888                             }
1889                             _ => {
1890                                 struct_span_err!(
1891                                     tcx.sess.diagnostic(),
1892                                     attr.span,
1893                                     E0779,
1894                                     "invalid instruction set specified",
1895                                 )
1896                                 .emit();
1897                                 None
1898                             }
1899                         }
1900                     }
1901                     [] => {
1902                         struct_span_err!(
1903                             tcx.sess.diagnostic(),
1904                             attr.span,
1905                             E0778,
1906                             "`#[instruction_set]` requires an argument"
1907                         )
1908                         .emit();
1909                         None
1910                     }
1911                     _ => {
1912                         struct_span_err!(
1913                             tcx.sess.diagnostic(),
1914                             attr.span,
1915                             E0779,
1916                             "cannot specify more than one instruction set"
1917                         )
1918                         .emit();
1919                         None
1920                     }
1921                 },
1922                 _ => {
1923                     struct_span_err!(
1924                         tcx.sess.diagnostic(),
1925                         attr.span,
1926                         E0778,
1927                         "must specify an instruction set"
1928                     )
1929                     .emit();
1930                     None
1931                 }
1932             };
1933         } else if attr.has_name(sym::repr) {
1934             codegen_fn_attrs.alignment = match attr.meta_item_list() {
1935                 Some(items) => match items.as_slice() {
1936                     [item] => match item.name_value_literal() {
1937                         Some((sym::align, literal)) => {
1938                             let alignment = rustc_attr::parse_alignment(&literal.kind);
1939
1940                             match alignment {
1941                                 Ok(align) => Some(align),
1942                                 Err(msg) => {
1943                                     struct_span_err!(
1944                                         tcx.sess.diagnostic(),
1945                                         attr.span,
1946                                         E0589,
1947                                         "invalid `repr(align)` attribute: {}",
1948                                         msg
1949                                     )
1950                                     .emit();
1951
1952                                     None
1953                                 }
1954                             }
1955                         }
1956                         _ => None,
1957                     },
1958                     [] => None,
1959                     _ => None,
1960                 },
1961                 None => None,
1962             };
1963         }
1964     }
1965
1966     codegen_fn_attrs.inline = attrs.iter().fold(InlineAttr::None, |ia, attr| {
1967         if !attr.has_name(sym::inline) {
1968             return ia;
1969         }
1970         match attr.meta_kind() {
1971             Some(MetaItemKind::Word) => InlineAttr::Hint,
1972             Some(MetaItemKind::List(ref items)) => {
1973                 inline_span = Some(attr.span);
1974                 if items.len() != 1 {
1975                     struct_span_err!(
1976                         tcx.sess.diagnostic(),
1977                         attr.span,
1978                         E0534,
1979                         "expected one argument"
1980                     )
1981                     .emit();
1982                     InlineAttr::None
1983                 } else if list_contains_name(&items, sym::always) {
1984                     InlineAttr::Always
1985                 } else if list_contains_name(&items, sym::never) {
1986                     InlineAttr::Never
1987                 } else {
1988                     struct_span_err!(
1989                         tcx.sess.diagnostic(),
1990                         items[0].span(),
1991                         E0535,
1992                         "invalid argument"
1993                     )
1994                     .help("valid inline arguments are `always` and `never`")
1995                     .emit();
1996
1997                     InlineAttr::None
1998                 }
1999             }
2000             Some(MetaItemKind::NameValue(_)) => ia,
2001             None => ia,
2002         }
2003     });
2004
2005     codegen_fn_attrs.optimize = attrs.iter().fold(OptimizeAttr::None, |ia, attr| {
2006         if !attr.has_name(sym::optimize) {
2007             return ia;
2008         }
2009         let err = |sp, s| struct_span_err!(tcx.sess.diagnostic(), sp, E0722, "{}", s).emit();
2010         match attr.meta_kind() {
2011             Some(MetaItemKind::Word) => {
2012                 err(attr.span, "expected one argument");
2013                 ia
2014             }
2015             Some(MetaItemKind::List(ref items)) => {
2016                 inline_span = Some(attr.span);
2017                 if items.len() != 1 {
2018                     err(attr.span, "expected one argument");
2019                     OptimizeAttr::None
2020                 } else if list_contains_name(&items, sym::size) {
2021                     OptimizeAttr::Size
2022                 } else if list_contains_name(&items, sym::speed) {
2023                     OptimizeAttr::Speed
2024                 } else {
2025                     err(items[0].span(), "invalid argument");
2026                     OptimizeAttr::None
2027                 }
2028             }
2029             Some(MetaItemKind::NameValue(_)) => ia,
2030             None => ia,
2031         }
2032     });
2033
2034     // #73631: closures inherit `#[target_feature]` annotations
2035     if tcx.features().target_feature_11 && tcx.is_closure(did.to_def_id()) {
2036         let owner_id = tcx.parent(did.to_def_id());
2037         if tcx.def_kind(owner_id).has_codegen_attrs() {
2038             codegen_fn_attrs
2039                 .target_features
2040                 .extend(tcx.codegen_fn_attrs(owner_id).target_features.iter().copied());
2041         }
2042     }
2043
2044     // If a function uses #[target_feature] it can't be inlined into general
2045     // purpose functions as they wouldn't have the right target features
2046     // enabled. For that reason we also forbid #[inline(always)] as it can't be
2047     // respected.
2048     if !codegen_fn_attrs.target_features.is_empty() {
2049         if codegen_fn_attrs.inline == InlineAttr::Always {
2050             if let Some(span) = inline_span {
2051                 tcx.sess.span_err(
2052                     span,
2053                     "cannot use `#[inline(always)]` with \
2054                      `#[target_feature]`",
2055                 );
2056             }
2057         }
2058     }
2059
2060     if !codegen_fn_attrs.no_sanitize.is_empty() {
2061         if codegen_fn_attrs.inline == InlineAttr::Always {
2062             if let (Some(no_sanitize_span), Some(inline_span)) = (no_sanitize_span, inline_span) {
2063                 let hir_id = tcx.hir().local_def_id_to_hir_id(did);
2064                 tcx.struct_span_lint_hir(
2065                     lint::builtin::INLINE_NO_SANITIZE,
2066                     hir_id,
2067                     no_sanitize_span,
2068                     "`no_sanitize` will have no effect after inlining",
2069                     |lint| lint.span_note(inline_span, "inlining requested here"),
2070                 )
2071             }
2072         }
2073     }
2074
2075     // Weak lang items have the same semantics as "std internal" symbols in the
2076     // sense that they're preserved through all our LTO passes and only
2077     // strippable by the linker.
2078     //
2079     // Additionally weak lang items have predetermined symbol names.
2080     if WEAK_LANG_ITEMS.iter().any(|&l| tcx.lang_items().get(l) == Some(did.to_def_id())) {
2081         codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL;
2082     }
2083     if let Some((name, _)) = lang_items::extract(attrs)
2084         && let Some(lang_item) = LangItem::from_name(name)
2085         && let Some(link_name) = lang_item.link_name()
2086     {
2087         codegen_fn_attrs.export_name = Some(link_name);
2088         codegen_fn_attrs.link_name = Some(link_name);
2089     }
2090     check_link_name_xor_ordinal(tcx, &codegen_fn_attrs, link_ordinal_span);
2091
2092     // Internal symbols to the standard library all have no_mangle semantics in
2093     // that they have defined symbol names present in the function name. This
2094     // also applies to weak symbols where they all have known symbol names.
2095     if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL) {
2096         codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_MANGLE;
2097     }
2098
2099     // Any linkage to LLVM intrinsics for now forcibly marks them all as never
2100     // unwinds since LLVM sometimes can't handle codegen which `invoke`s
2101     // intrinsic functions.
2102     if let Some(name) = &codegen_fn_attrs.link_name {
2103         if name.as_str().starts_with("llvm.") {
2104             codegen_fn_attrs.flags |= CodegenFnAttrFlags::NEVER_UNWIND;
2105         }
2106     }
2107
2108     codegen_fn_attrs
2109 }
2110
2111 /// Computes the set of target features used in a function for the purposes of
2112 /// inline assembly.
2113 fn asm_target_features<'tcx>(tcx: TyCtxt<'tcx>, did: DefId) -> &'tcx FxHashSet<Symbol> {
2114     let mut target_features = tcx.sess.unstable_target_features.clone();
2115     if tcx.def_kind(did).has_codegen_attrs() {
2116         let attrs = tcx.codegen_fn_attrs(did);
2117         target_features.extend(&attrs.target_features);
2118         match attrs.instruction_set {
2119             None => {}
2120             Some(InstructionSetAttr::ArmA32) => {
2121                 target_features.remove(&sym::thumb_mode);
2122             }
2123             Some(InstructionSetAttr::ArmT32) => {
2124                 target_features.insert(sym::thumb_mode);
2125             }
2126         }
2127     }
2128
2129     tcx.arena.alloc(target_features)
2130 }
2131
2132 /// Checks if the provided DefId is a method in a trait impl for a trait which has track_caller
2133 /// applied to the method prototype.
2134 fn should_inherit_track_caller(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
2135     if let Some(impl_item) = tcx.opt_associated_item(def_id)
2136         && let ty::AssocItemContainer::ImplContainer = impl_item.container
2137         && let Some(trait_item) = impl_item.trait_item_def_id
2138     {
2139         return tcx
2140             .codegen_fn_attrs(trait_item)
2141             .flags
2142             .intersects(CodegenFnAttrFlags::TRACK_CALLER);
2143     }
2144
2145     false
2146 }
2147
2148 fn check_link_ordinal(tcx: TyCtxt<'_>, attr: &ast::Attribute) -> Option<u16> {
2149     use rustc_ast::{Lit, LitIntType, LitKind};
2150     if !tcx.features().raw_dylib && tcx.sess.target.arch == "x86" {
2151         feature_err(
2152             &tcx.sess.parse_sess,
2153             sym::raw_dylib,
2154             attr.span,
2155             "`#[link_ordinal]` is unstable on x86",
2156         )
2157         .emit();
2158     }
2159     let meta_item_list = attr.meta_item_list();
2160     let meta_item_list: Option<&[ast::NestedMetaItem]> = meta_item_list.as_ref().map(Vec::as_ref);
2161     let sole_meta_list = match meta_item_list {
2162         Some([item]) => item.literal(),
2163         Some(_) => {
2164             tcx.sess
2165                 .struct_span_err(attr.span, "incorrect number of arguments to `#[link_ordinal]`")
2166                 .note("the attribute requires exactly one argument")
2167                 .emit();
2168             return None;
2169         }
2170         _ => None,
2171     };
2172     if let Some(Lit { kind: LitKind::Int(ordinal, LitIntType::Unsuffixed), .. }) = sole_meta_list {
2173         // According to the table at https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#import-header,
2174         // the ordinal must fit into 16 bits.  Similarly, the Ordinal field in COFFShortExport (defined
2175         // in llvm/include/llvm/Object/COFFImportFile.h), which we use to communicate import information
2176         // to LLVM for `#[link(kind = "raw-dylib"_])`, is also defined to be uint16_t.
2177         //
2178         // FIXME: should we allow an ordinal of 0?  The MSVC toolchain has inconsistent support for this:
2179         // both LINK.EXE and LIB.EXE signal errors and abort when given a .DEF file that specifies
2180         // a zero ordinal.  However, llvm-dlltool is perfectly happy to generate an import library
2181         // for such a .DEF file, and MSVC's LINK.EXE is also perfectly happy to consume an import
2182         // library produced by LLVM with an ordinal of 0, and it generates an .EXE.  (I don't know yet
2183         // if the resulting EXE runs, as I haven't yet built the necessary DLL -- see earlier comment
2184         // about LINK.EXE failing.)
2185         if *ordinal <= u16::MAX as u128 {
2186             Some(*ordinal as u16)
2187         } else {
2188             let msg = format!("ordinal value in `link_ordinal` is too large: `{}`", &ordinal);
2189             tcx.sess
2190                 .struct_span_err(attr.span, &msg)
2191                 .note("the value may not exceed `u16::MAX`")
2192                 .emit();
2193             None
2194         }
2195     } else {
2196         tcx.sess
2197             .struct_span_err(attr.span, "illegal ordinal format in `link_ordinal`")
2198             .note("an unsuffixed integer value, e.g., `1`, is expected")
2199             .emit();
2200         None
2201     }
2202 }
2203
2204 fn check_link_name_xor_ordinal(
2205     tcx: TyCtxt<'_>,
2206     codegen_fn_attrs: &CodegenFnAttrs,
2207     inline_span: Option<Span>,
2208 ) {
2209     if codegen_fn_attrs.link_name.is_none() || codegen_fn_attrs.link_ordinal.is_none() {
2210         return;
2211     }
2212     let msg = "cannot use `#[link_name]` with `#[link_ordinal]`";
2213     if let Some(span) = inline_span {
2214         tcx.sess.span_err(span, msg);
2215     } else {
2216         tcx.sess.err(msg);
2217     }
2218 }
2219
2220 /// Checks the function annotated with `#[target_feature]` is not a safe
2221 /// trait method implementation, reporting an error if it is.
2222 fn check_target_feature_trait_unsafe(tcx: TyCtxt<'_>, id: LocalDefId, attr_span: Span) {
2223     let hir_id = tcx.hir().local_def_id_to_hir_id(id);
2224     let node = tcx.hir().get(hir_id);
2225     if let Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Fn(..), .. }) = node {
2226         let parent_id = tcx.hir().get_parent_item(hir_id);
2227         let parent_item = tcx.hir().expect_item(parent_id.def_id);
2228         if let hir::ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }) = parent_item.kind {
2229             tcx.sess
2230                 .struct_span_err(
2231                     attr_span,
2232                     "`#[target_feature(..)]` cannot be applied to safe trait method",
2233                 )
2234                 .span_label(attr_span, "cannot be applied to safe trait method")
2235                 .span_label(tcx.def_span(id), "not an `unsafe` function")
2236                 .emit();
2237         }
2238     }
2239 }