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