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