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
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
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.
17 use crate::astconv::AstConv;
18 use crate::check::intrinsic::intrinsic_operation_unsafety;
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};
27 use rustc_hir::def_id::{DefId, LocalDefId, LOCAL_CRATE};
28 use rustc_hir::intravisit::{self, Visitor};
29 use rustc_hir::weak_lang_items::WEAK_LANG_ITEMS;
30 use rustc_hir::{lang_items, GenericParamKind, LangItem, Node};
31 use rustc_middle::hir::nested_filter;
32 use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
33 use rustc_middle::mir::mono::Linkage;
34 use rustc_middle::ty::query::Providers;
35 use rustc_middle::ty::util::{Discr, IntTypeExt};
36 use rustc_middle::ty::ReprOptions;
37 use rustc_middle::ty::{self, AdtKind, Const, DefIdTree, IsSuggestable, Ty, TyCtxt};
38 use rustc_session::lint;
39 use rustc_session::parse::feature_err;
40 use rustc_span::symbol::{kw, sym, Ident, Symbol};
42 use rustc_target::spec::{abi, SanitizerSet};
43 use rustc_trait_selection::traits::error_reporting::suggestions::NextTypeParamName;
52 ///////////////////////////////////////////////////////////////////////////
55 fn collect_mod_item_types(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
56 tcx.hir().visit_item_likes_in_module(module_def_id, &mut CollectItemTypesVisitor { tcx });
59 pub fn provide(providers: &mut Providers) {
60 lifetimes::provide(providers);
61 *providers = Providers {
62 opt_const_param_of: type_of::opt_const_param_of,
63 type_of: type_of::type_of,
64 item_bounds: item_bounds::item_bounds,
65 explicit_item_bounds: item_bounds::explicit_item_bounds,
66 generics_of: generics_of::generics_of,
67 predicates_of: predicates_of::predicates_of,
68 predicates_defined_on,
69 explicit_predicates_of: predicates_of::explicit_predicates_of,
70 super_predicates_of: predicates_of::super_predicates_of,
71 super_predicates_that_define_assoc_type:
72 predicates_of::super_predicates_that_define_assoc_type,
73 trait_explicit_predicates_and_bounds: predicates_of::trait_explicit_predicates_and_bounds,
74 type_param_predicates: predicates_of::type_param_predicates,
84 collect_mod_item_types,
85 should_inherit_track_caller,
90 ///////////////////////////////////////////////////////////////////////////
92 /// Context specific to some particular item. This is what implements
95 /// # `ItemCtxt` vs `FnCtxt`
97 /// `ItemCtxt` is primarily used to type-check item signatures and lower them
98 /// from HIR to their [`ty::Ty`] representation, which is exposed using [`AstConv`].
99 /// It's also used for the bodies of items like structs where the body (the fields)
100 /// are just signatures.
102 /// This is in contrast to `FnCtxt`, which is used to type-check bodies of
103 /// functions, closures, and `const`s -- anywhere that expressions and statements show up.
105 /// An important thing to note is that `ItemCtxt` does no inference -- it has no [`InferCtxt`] --
106 /// while `FnCtxt` does do inference.
108 /// [`InferCtxt`]: rustc_infer::infer::InferCtxt
110 /// # Trait predicates
112 /// `ItemCtxt` has information about the predicates that are defined
113 /// on the trait. Unfortunately, this predicate information is
114 /// available in various different forms at various points in the
115 /// process. So we can't just store a pointer to e.g., the AST or the
116 /// parsed ty form, we have to be more flexible. To this end, the
117 /// `ItemCtxt` is parameterized by a `DefId` that it uses to satisfy
118 /// `get_type_parameter_bounds` requests, drawing the information from
119 /// the AST (`hir::Generics`), recursively.
120 pub struct ItemCtxt<'tcx> {
125 ///////////////////////////////////////////////////////////////////////////
128 pub(crate) struct HirPlaceholderCollector(pub(crate) Vec<Span>);
130 impl<'v> Visitor<'v> for HirPlaceholderCollector {
131 fn visit_ty(&mut self, t: &'v hir::Ty<'v>) {
132 if let hir::TyKind::Infer = t.kind {
135 intravisit::walk_ty(self, t)
137 fn visit_generic_arg(&mut self, generic_arg: &'v hir::GenericArg<'v>) {
139 hir::GenericArg::Infer(inf) => {
140 self.0.push(inf.span);
141 intravisit::walk_inf(self, inf);
143 hir::GenericArg::Type(t) => self.visit_ty(t),
147 fn visit_array_length(&mut self, length: &'v hir::ArrayLen) {
148 if let &hir::ArrayLen::Infer(_, span) = length {
151 intravisit::walk_array_len(self, length)
155 struct CollectItemTypesVisitor<'tcx> {
159 /// If there are any placeholder types (`_`), emit an error explaining that this is not allowed
160 /// and suggest adding type parameters in the appropriate place, taking into consideration any and
161 /// all already existing generic type parameters to avoid suggesting a name that is already in use.
162 pub(crate) fn placeholder_type_error<'tcx>(
164 generics: Option<&hir::Generics<'_>>,
165 placeholder_types: Vec<Span>,
167 hir_ty: Option<&hir::Ty<'_>>,
170 if placeholder_types.is_empty() {
174 placeholder_type_error_diag(tcx, generics, placeholder_types, vec![], suggest, hir_ty, kind)
178 pub(crate) fn placeholder_type_error_diag<'tcx>(
180 generics: Option<&hir::Generics<'_>>,
181 placeholder_types: Vec<Span>,
182 additional_spans: Vec<Span>,
184 hir_ty: Option<&hir::Ty<'_>>,
186 ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
187 if placeholder_types.is_empty() {
188 return bad_placeholder(tcx, additional_spans, kind);
191 let params = generics.map(|g| g.params).unwrap_or_default();
192 let type_name = params.next_type_param_name(None);
193 let mut sugg: Vec<_> =
194 placeholder_types.iter().map(|sp| (*sp, (*type_name).to_string())).collect();
196 if let Some(generics) = generics {
197 if let Some(arg) = params.iter().find(|arg| {
198 matches!(arg.name, hir::ParamName::Plain(Ident { name: kw::Underscore, .. }))
200 // Account for `_` already present in cases like `struct S<_>(_);` and suggest
201 // `struct S<T>(T);` instead of `struct S<_, T>(T);`.
202 sugg.push((arg.span, (*type_name).to_string()));
203 } else if let Some(span) = generics.span_for_param_suggestion() {
204 // Account for bounds, we want `fn foo<T: E, K>(_: K)` not `fn foo<T, K: E>(_: K)`.
205 sugg.push((span, format!(", {}", type_name)));
207 sugg.push((generics.span, format!("<{}>", type_name)));
212 bad_placeholder(tcx, placeholder_types.into_iter().chain(additional_spans).collect(), kind);
214 // Suggest, but only if it is not a function in const or static
216 let mut is_fn = false;
217 let mut is_const_or_static = false;
219 if let Some(hir_ty) = hir_ty && let hir::TyKind::BareFn(_) = hir_ty.kind {
222 // Check if parent is const or static
223 let parent_id = tcx.hir().get_parent_node(hir_ty.hir_id);
224 let parent_node = tcx.hir().get(parent_id);
226 is_const_or_static = matches!(
228 Node::Item(&hir::Item {
229 kind: hir::ItemKind::Const(..) | hir::ItemKind::Static(..),
231 }) | Node::TraitItem(&hir::TraitItem {
232 kind: hir::TraitItemKind::Const(..),
234 }) | Node::ImplItem(&hir::ImplItem { kind: hir::ImplItemKind::Const(..), .. })
238 // if function is wrapped around a const or static,
239 // then don't show the suggestion
240 if !(is_fn && is_const_or_static) {
241 err.multipart_suggestion(
242 "use type parameters instead",
244 Applicability::HasPlaceholders,
252 fn reject_placeholder_type_signatures_in_item<'tcx>(
254 item: &'tcx hir::Item<'tcx>,
256 let (generics, suggest) = match &item.kind {
257 hir::ItemKind::Union(_, generics)
258 | hir::ItemKind::Enum(_, generics)
259 | hir::ItemKind::TraitAlias(generics, _)
260 | hir::ItemKind::Trait(_, _, generics, ..)
261 | hir::ItemKind::Impl(hir::Impl { generics, .. })
262 | hir::ItemKind::Struct(_, generics) => (generics, true),
263 hir::ItemKind::OpaqueTy(hir::OpaqueTy { generics, .. })
264 | hir::ItemKind::TyAlias(_, generics) => (generics, false),
265 // `static`, `fn` and `const` are handled elsewhere to suggest appropriate type.
269 let mut visitor = HirPlaceholderCollector::default();
270 visitor.visit_item(item);
272 placeholder_type_error(tcx, Some(generics), visitor.0, suggest, None, item.kind.descr());
275 impl<'tcx> Visitor<'tcx> for CollectItemTypesVisitor<'tcx> {
276 type NestedFilter = nested_filter::OnlyBodies;
278 fn nested_visit_map(&mut self) -> Self::Map {
282 fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
283 convert_item(self.tcx, item.item_id());
284 reject_placeholder_type_signatures_in_item(self.tcx, item);
285 intravisit::walk_item(self, item);
288 fn visit_generics(&mut self, generics: &'tcx hir::Generics<'tcx>) {
289 for param in generics.params {
291 hir::GenericParamKind::Lifetime { .. } => {}
292 hir::GenericParamKind::Type { default: Some(_), .. } => {
293 self.tcx.ensure().type_of(param.def_id);
295 hir::GenericParamKind::Type { .. } => {}
296 hir::GenericParamKind::Const { default, .. } => {
297 self.tcx.ensure().type_of(param.def_id);
298 if let Some(default) = default {
299 // need to store default and type of default
300 self.tcx.ensure().type_of(default.def_id);
301 self.tcx.ensure().const_param_default(param.def_id);
306 intravisit::walk_generics(self, generics);
309 fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
310 if let hir::ExprKind::Closure(closure) = expr.kind {
311 self.tcx.ensure().generics_of(closure.def_id);
312 self.tcx.ensure().codegen_fn_attrs(closure.def_id);
313 // We do not call `type_of` for closures here as that
314 // depends on typecheck and would therefore hide
315 // any further errors in case one typeck fails.
317 intravisit::walk_expr(self, expr);
320 fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>) {
321 convert_trait_item(self.tcx, trait_item.trait_item_id());
322 intravisit::walk_trait_item(self, trait_item);
325 fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) {
326 convert_impl_item(self.tcx, impl_item.impl_item_id());
327 intravisit::walk_impl_item(self, impl_item);
331 ///////////////////////////////////////////////////////////////////////////
332 // Utility types and common code for the above passes.
334 fn bad_placeholder<'tcx>(
336 mut spans: Vec<Span>,
338 ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
339 let kind = if kind.ends_with('s') { format!("{}es", kind) } else { format!("{}s", kind) };
342 let mut err = struct_span_err!(
346 "the placeholder `_` is not allowed within types on item signatures for {}",
350 err.span_label(span, "not allowed in type signatures");
355 impl<'tcx> ItemCtxt<'tcx> {
356 pub fn new(tcx: TyCtxt<'tcx>, item_def_id: DefId) -> ItemCtxt<'tcx> {
357 ItemCtxt { tcx, item_def_id }
360 pub fn to_ty(&self, ast_ty: &hir::Ty<'_>) -> Ty<'tcx> {
361 <dyn AstConv<'_>>::ast_ty_to_ty(self, ast_ty)
364 pub fn hir_id(&self) -> hir::HirId {
365 self.tcx.hir().local_def_id_to_hir_id(self.item_def_id.expect_local())
368 pub fn node(&self) -> hir::Node<'tcx> {
369 self.tcx.hir().get(self.hir_id())
373 impl<'tcx> AstConv<'tcx> for ItemCtxt<'tcx> {
374 fn tcx(&self) -> TyCtxt<'tcx> {
378 fn item_def_id(&self) -> DefId {
382 fn get_type_parameter_bounds(
387 ) -> ty::GenericPredicates<'tcx> {
388 self.tcx.at(span).type_param_predicates((
390 def_id.expect_local(),
395 fn re_infer(&self, _: Option<&ty::GenericParamDef>, _: Span) -> Option<ty::Region<'tcx>> {
399 fn allow_ty_infer(&self) -> bool {
403 fn ty_infer(&self, _: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx> {
404 self.tcx().ty_error_with_message(span, "bad placeholder type")
407 fn ct_infer(&self, ty: Ty<'tcx>, _: Option<&ty::GenericParamDef>, span: Span) -> Const<'tcx> {
408 let ty = self.tcx.fold_regions(ty, |r, _| match *r {
409 ty::ReErased => self.tcx.lifetimes.re_static,
412 self.tcx().const_error_with_message(ty, span, "bad placeholder constant")
415 fn projected_ty_from_poly_trait_ref(
419 item_segment: &hir::PathSegment<'_>,
420 poly_trait_ref: ty::PolyTraitRef<'tcx>,
422 if let Some(trait_ref) = poly_trait_ref.no_bound_vars() {
423 let item_substs = <dyn AstConv<'tcx>>::create_substs_for_associated_item(
430 self.tcx().mk_projection(item_def_id, item_substs)
432 // There are no late-bound regions; we can just ignore the binder.
433 let mut err = struct_span_err!(
437 "cannot use the associated type of a trait \
438 with uninferred generic parameters"
442 hir::Node::Field(_) | hir::Node::Ctor(_) | hir::Node::Variant(_) => {
446 .expect_item(self.tcx.hir().get_parent_item(self.hir_id()).def_id);
448 hir::ItemKind::Enum(_, generics)
449 | hir::ItemKind::Struct(_, generics)
450 | hir::ItemKind::Union(_, generics) => {
451 let lt_name = get_new_lifetime_name(self.tcx, poly_trait_ref, generics);
452 let (lt_sp, sugg) = match generics.params {
453 [] => (generics.span, format!("<{}>", lt_name)),
455 (bound.span.shrink_to_lo(), format!("{}, ", lt_name))
458 let suggestions = vec![
461 span.with_hi(item_segment.ident.span.lo()),
464 // Replace the existing lifetimes with a new named lifetime.
465 self.tcx.replace_late_bound_regions_uncached(
468 self.tcx.mk_region(ty::ReEarlyBound(
469 ty::EarlyBoundRegion {
472 name: Symbol::intern(<_name),
480 err.multipart_suggestion(
481 "use a fully qualified path with explicit lifetimes",
483 Applicability::MaybeIncorrect,
489 hir::Node::Item(hir::Item {
491 hir::ItemKind::Struct(..) | hir::ItemKind::Enum(..) | hir::ItemKind::Union(..),
495 | hir::Node::ForeignItem(_)
496 | hir::Node::TraitItem(_)
497 | hir::Node::ImplItem(_) => {
498 err.span_suggestion_verbose(
499 span.with_hi(item_segment.ident.span.lo()),
500 "use a fully qualified path with inferred lifetimes",
503 // Erase named lt, we want `<A as B<'_>::C`, not `<A as B<'a>::C`.
504 self.tcx.anonymize_late_bound_regions(poly_trait_ref).skip_binder(),
506 Applicability::MaybeIncorrect,
511 self.tcx().ty_error_with_guaranteed(err.emit())
515 fn normalize_ty(&self, _span: Span, ty: Ty<'tcx>) -> Ty<'tcx> {
516 // Types in item signatures are not normalized to avoid undue dependencies.
520 fn set_tainted_by_errors(&self, _: ErrorGuaranteed) {
521 // There's no obvious place to track this, so just let it go.
524 fn record_ty(&self, _hir_id: hir::HirId, _ty: Ty<'tcx>, _span: Span) {
525 // There's no place to record types from signatures?
529 /// Synthesize a new lifetime name that doesn't clash with any of the lifetimes already present.
530 fn get_new_lifetime_name<'tcx>(
532 poly_trait_ref: ty::PolyTraitRef<'tcx>,
533 generics: &hir::Generics<'tcx>,
535 let existing_lifetimes = tcx
536 .collect_referenced_late_bound_regions(&poly_trait_ref)
539 if let ty::BoundRegionKind::BrNamed(_, name) = lt {
540 Some(name.as_str().to_string())
545 .chain(generics.params.iter().filter_map(|param| {
546 if let hir::GenericParamKind::Lifetime { .. } = ¶m.kind {
547 Some(param.name.ident().as_str().to_string())
552 .collect::<FxHashSet<String>>();
554 let a_to_z_repeat_n = |n| {
555 (b'a'..=b'z').map(move |c| {
556 let mut s = '\''.to_string();
557 s.extend(std::iter::repeat(char::from(c)).take(n));
562 // If all single char lifetime names are present, we wrap around and double the chars.
563 (1..).flat_map(a_to_z_repeat_n).find(|lt| !existing_lifetimes.contains(lt.as_str())).unwrap()
566 fn convert_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) {
567 let it = tcx.hir().item(item_id);
568 debug!("convert: item {} with id {}", it.ident, it.hir_id());
569 let def_id = item_id.owner_id.def_id;
572 // These don't define types.
573 hir::ItemKind::ExternCrate(_)
574 | hir::ItemKind::Use(..)
575 | hir::ItemKind::Macro(..)
576 | hir::ItemKind::Mod(_)
577 | hir::ItemKind::GlobalAsm(_) => {}
578 hir::ItemKind::ForeignMod { items, .. } => {
580 let item = tcx.hir().foreign_item(item.id);
581 tcx.ensure().generics_of(item.owner_id);
582 tcx.ensure().type_of(item.owner_id);
583 tcx.ensure().predicates_of(item.owner_id);
585 hir::ForeignItemKind::Fn(..) => {
586 tcx.ensure().codegen_fn_attrs(item.owner_id);
587 tcx.ensure().fn_sig(item.owner_id)
589 hir::ForeignItemKind::Static(..) => {
590 tcx.ensure().codegen_fn_attrs(item.owner_id);
591 let mut visitor = HirPlaceholderCollector::default();
592 visitor.visit_foreign_item(item);
593 placeholder_type_error(
606 hir::ItemKind::Enum(..) => {
607 tcx.ensure().generics_of(def_id);
608 tcx.ensure().type_of(def_id);
609 tcx.ensure().predicates_of(def_id);
610 convert_enum_variant_types(tcx, def_id.to_def_id());
612 hir::ItemKind::Impl { .. } => {
613 tcx.ensure().generics_of(def_id);
614 tcx.ensure().type_of(def_id);
615 tcx.ensure().impl_trait_ref(def_id);
616 tcx.ensure().predicates_of(def_id);
618 hir::ItemKind::Trait(..) => {
619 tcx.ensure().generics_of(def_id);
620 tcx.ensure().trait_def(def_id);
621 tcx.at(it.span).super_predicates_of(def_id);
622 tcx.ensure().predicates_of(def_id);
624 hir::ItemKind::TraitAlias(..) => {
625 tcx.ensure().generics_of(def_id);
626 tcx.at(it.span).super_predicates_of(def_id);
627 tcx.ensure().predicates_of(def_id);
629 hir::ItemKind::Struct(ref struct_def, _) | hir::ItemKind::Union(ref struct_def, _) => {
630 tcx.ensure().generics_of(def_id);
631 tcx.ensure().type_of(def_id);
632 tcx.ensure().predicates_of(def_id);
634 for f in struct_def.fields() {
635 tcx.ensure().generics_of(f.def_id);
636 tcx.ensure().type_of(f.def_id);
637 tcx.ensure().predicates_of(f.def_id);
640 if let Some(ctor_def_id) = struct_def.ctor_def_id() {
641 convert_variant_ctor(tcx, ctor_def_id);
645 // Don't call `type_of` on opaque types, since that depends on type
646 // checking function bodies. `check_item_type` ensures that it's called
648 hir::ItemKind::OpaqueTy(..) => {
649 tcx.ensure().generics_of(def_id);
650 tcx.ensure().predicates_of(def_id);
651 tcx.ensure().explicit_item_bounds(def_id);
652 tcx.ensure().item_bounds(def_id);
655 hir::ItemKind::TyAlias(..) => {
656 tcx.ensure().generics_of(def_id);
657 tcx.ensure().type_of(def_id);
658 tcx.ensure().predicates_of(def_id);
661 hir::ItemKind::Static(ty, ..) | hir::ItemKind::Const(ty, ..) => {
662 tcx.ensure().generics_of(def_id);
663 tcx.ensure().type_of(def_id);
664 tcx.ensure().predicates_of(def_id);
665 if !is_suggestable_infer_ty(ty) {
666 let mut visitor = HirPlaceholderCollector::default();
667 visitor.visit_item(it);
668 placeholder_type_error(tcx, None, visitor.0, false, None, it.kind.descr());
672 hir::ItemKind::Fn(..) => {
673 tcx.ensure().generics_of(def_id);
674 tcx.ensure().type_of(def_id);
675 tcx.ensure().predicates_of(def_id);
676 tcx.ensure().fn_sig(def_id);
677 tcx.ensure().codegen_fn_attrs(def_id);
682 fn convert_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::TraitItemId) {
683 let trait_item = tcx.hir().trait_item(trait_item_id);
684 let def_id = trait_item_id.owner_id;
685 tcx.ensure().generics_of(def_id);
687 match trait_item.kind {
688 hir::TraitItemKind::Fn(..) => {
689 tcx.ensure().codegen_fn_attrs(def_id);
690 tcx.ensure().type_of(def_id);
691 tcx.ensure().fn_sig(def_id);
694 hir::TraitItemKind::Const(.., Some(_)) => {
695 tcx.ensure().type_of(def_id);
698 hir::TraitItemKind::Const(hir_ty, _) => {
699 tcx.ensure().type_of(def_id);
700 // Account for `const C: _;`.
701 let mut visitor = HirPlaceholderCollector::default();
702 visitor.visit_trait_item(trait_item);
703 if !tcx.sess.diagnostic().has_stashed_diagnostic(hir_ty.span, StashKey::ItemNoType) {
704 placeholder_type_error(tcx, None, visitor.0, false, None, "constant");
708 hir::TraitItemKind::Type(_, Some(_)) => {
709 tcx.ensure().item_bounds(def_id);
710 tcx.ensure().type_of(def_id);
711 // Account for `type T = _;`.
712 let mut visitor = HirPlaceholderCollector::default();
713 visitor.visit_trait_item(trait_item);
714 placeholder_type_error(tcx, None, visitor.0, false, None, "associated type");
717 hir::TraitItemKind::Type(_, None) => {
718 tcx.ensure().item_bounds(def_id);
719 // #74612: Visit and try to find bad placeholders
720 // even if there is no concrete type.
721 let mut visitor = HirPlaceholderCollector::default();
722 visitor.visit_trait_item(trait_item);
724 placeholder_type_error(tcx, None, visitor.0, false, None, "associated type");
728 tcx.ensure().predicates_of(def_id);
731 fn convert_impl_item(tcx: TyCtxt<'_>, impl_item_id: hir::ImplItemId) {
732 let def_id = impl_item_id.owner_id;
733 tcx.ensure().generics_of(def_id);
734 tcx.ensure().type_of(def_id);
735 tcx.ensure().predicates_of(def_id);
736 let impl_item = tcx.hir().impl_item(impl_item_id);
737 match impl_item.kind {
738 hir::ImplItemKind::Fn(..) => {
739 tcx.ensure().codegen_fn_attrs(def_id);
740 tcx.ensure().fn_sig(def_id);
742 hir::ImplItemKind::Type(_) => {
743 // Account for `type T = _;`
744 let mut visitor = HirPlaceholderCollector::default();
745 visitor.visit_impl_item(impl_item);
747 placeholder_type_error(tcx, None, visitor.0, false, None, "associated type");
749 hir::ImplItemKind::Const(..) => {}
753 fn convert_variant_ctor(tcx: TyCtxt<'_>, def_id: LocalDefId) {
754 tcx.ensure().generics_of(def_id);
755 tcx.ensure().type_of(def_id);
756 tcx.ensure().predicates_of(def_id);
759 fn convert_enum_variant_types(tcx: TyCtxt<'_>, def_id: DefId) {
760 let def = tcx.adt_def(def_id);
761 let repr_type = def.repr().discr_type();
762 let initial = repr_type.initial_discriminant(tcx);
763 let mut prev_discr = None::<Discr<'_>>;
765 // fill the discriminant values and field types
766 for variant in def.variants() {
767 let wrapped_discr = prev_discr.map_or(initial, |d| d.wrap_incr(tcx));
769 if let ty::VariantDiscr::Explicit(const_def_id) = variant.discr {
770 def.eval_explicit_discr(tcx, const_def_id)
771 } else if let Some(discr) = repr_type.disr_incr(tcx, prev_discr) {
774 let span = tcx.def_span(variant.def_id);
775 struct_span_err!(tcx.sess, span, E0370, "enum discriminant overflowed")
776 .span_label(span, format!("overflowed on value after {}", prev_discr.unwrap()))
778 "explicitly set `{} = {}` if that is desired outcome",
779 tcx.item_name(variant.def_id),
785 .unwrap_or(wrapped_discr),
788 for f in &variant.fields {
789 tcx.ensure().generics_of(f.did);
790 tcx.ensure().type_of(f.did);
791 tcx.ensure().predicates_of(f.did);
794 // Convert the ctor, if any. This also registers the variant as
796 if let Some(ctor_def_id) = variant.ctor_def_id() {
797 convert_variant_ctor(tcx, ctor_def_id.expect_local());
804 variant_did: Option<LocalDefId>,
806 discr: ty::VariantDiscr,
807 def: &hir::VariantData<'_>,
808 adt_kind: ty::AdtKind,
809 parent_did: LocalDefId,
810 ) -> ty::VariantDef {
811 let mut seen_fields: FxHashMap<Ident, Span> = Default::default();
816 let dup_span = seen_fields.get(&f.ident.normalize_to_macros_2_0()).cloned();
817 if let Some(prev_span) = dup_span {
818 tcx.sess.emit_err(errors::FieldAlreadyDeclared {
824 seen_fields.insert(f.ident.normalize_to_macros_2_0(), f.span);
828 did: f.def_id.to_def_id(),
830 vis: tcx.visibility(f.def_id),
834 let recovered = match def {
835 hir::VariantData::Struct(_, r) => *r,
840 variant_did.map(LocalDefId::to_def_id),
841 def.ctor().map(|(kind, _, def_id)| (kind, def_id.to_def_id())),
845 parent_did.to_def_id(),
847 adt_kind == AdtKind::Struct && tcx.has_attr(parent_did.to_def_id(), sym::non_exhaustive)
848 || variant_did.map_or(false, |variant_did| {
849 tcx.has_attr(variant_did.to_def_id(), sym::non_exhaustive)
854 fn adt_def<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> ty::AdtDef<'tcx> {
857 let def_id = def_id.expect_local();
858 let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
859 let Node::Item(item) = tcx.hir().get(hir_id) else {
863 let repr = ReprOptions::new(tcx, def_id.to_def_id());
864 let (kind, variants) = match item.kind {
865 ItemKind::Enum(ref def, _) => {
866 let mut distance_from_explicit = 0;
871 let discr = if let Some(ref e) = v.disr_expr {
872 distance_from_explicit = 0;
873 ty::VariantDiscr::Explicit(e.def_id.to_def_id())
875 ty::VariantDiscr::Relative(distance_from_explicit)
877 distance_from_explicit += 1;
891 (AdtKind::Enum, variants)
893 ItemKind::Struct(ref def, _) | ItemKind::Union(ref def, _) => {
894 let adt_kind = match item.kind {
895 ItemKind::Struct(..) => AdtKind::Struct,
898 let variants = std::iter::once(convert_variant(
902 ty::VariantDiscr::Relative(0),
913 tcx.alloc_adt_def(def_id.to_def_id(), kind, variants, repr)
916 fn trait_def(tcx: TyCtxt<'_>, def_id: DefId) -> ty::TraitDef {
917 let item = tcx.hir().expect_item(def_id.expect_local());
919 let (is_auto, unsafety, items) = match item.kind {
920 hir::ItemKind::Trait(is_auto, unsafety, .., items) => {
921 (is_auto == hir::IsAuto::Yes, unsafety, items)
923 hir::ItemKind::TraitAlias(..) => (false, hir::Unsafety::Normal, &[][..]),
924 _ => span_bug!(item.span, "trait_def_of_item invoked on non-trait"),
927 let paren_sugar = tcx.has_attr(def_id, sym::rustc_paren_sugar);
928 if paren_sugar && !tcx.features().unboxed_closures {
932 "the `#[rustc_paren_sugar]` attribute is a temporary means of controlling \
933 which traits can use parenthetical notation",
935 .help("add `#![feature(unboxed_closures)]` to the crate attributes to use it")
939 let is_marker = tcx.has_attr(def_id, sym::marker);
940 let skip_array_during_method_dispatch =
941 tcx.has_attr(def_id, sym::rustc_skip_array_during_method_dispatch);
942 let spec_kind = if tcx.has_attr(def_id, sym::rustc_unsafe_specialization_marker) {
943 ty::trait_def::TraitSpecializationKind::Marker
944 } else if tcx.has_attr(def_id, sym::rustc_specialization_trait) {
945 ty::trait_def::TraitSpecializationKind::AlwaysApplicable
947 ty::trait_def::TraitSpecializationKind::None
949 let must_implement_one_of = tcx
950 .get_attr(def_id, sym::rustc_must_implement_one_of)
951 // Check that there are at least 2 arguments of `#[rustc_must_implement_one_of]`
952 // and that they are all identifiers
953 .and_then(|attr| match attr.meta_item_list() {
954 Some(items) if items.len() < 2 => {
958 "the `#[rustc_must_implement_one_of]` attribute must be \
959 used with at least 2 args",
967 .map(|item| item.ident().ok_or(item.span()))
968 .collect::<Result<Box<[_]>, _>>()
971 .struct_span_err(span, "must be a name of an associated function")
975 .zip(Some(attr.span)),
976 // Error is reported by `rustc_attr!`
979 // Check that all arguments of `#[rustc_must_implement_one_of]` reference
980 // functions in the trait with default implementations
981 .and_then(|(list, attr_span)| {
982 let errors = list.iter().filter_map(|ident| {
983 let item = items.iter().find(|item| item.ident == *ident);
986 Some(item) if matches!(item.kind, hir::AssocItemKind::Fn { .. }) => {
987 if !tcx.impl_defaultness(item.id.owner_id).has_value() {
991 "This function doesn't have a default implementation",
993 .span_note(attr_span, "required by this annotation")
1003 .struct_span_err(item.span, "Not a function")
1004 .span_note(attr_span, "required by this annotation")
1006 "All `#[rustc_must_implement_one_of]` arguments \
1007 must be associated function names",
1013 .struct_span_err(ident.span, "Function not found in this trait")
1021 (errors.count() == 0).then_some(list)
1023 // Check for duplicates
1025 let mut set: FxHashMap<Symbol, Span> = FxHashMap::default();
1026 let mut no_dups = true;
1028 for ident in &*list {
1029 if let Some(dup) = set.insert(ident.name, ident.span) {
1031 .struct_span_err(vec![dup, ident.span], "Functions names are duplicated")
1033 "All `#[rustc_must_implement_one_of]` arguments \
1042 no_dups.then_some(list)
1051 skip_array_during_method_dispatch,
1053 must_implement_one_of,
1057 fn are_suggestable_generic_args(generic_args: &[hir::GenericArg<'_>]) -> bool {
1058 generic_args.iter().any(|arg| match arg {
1059 hir::GenericArg::Type(ty) => is_suggestable_infer_ty(ty),
1060 hir::GenericArg::Infer(_) => true,
1065 /// Whether `ty` is a type with `_` placeholders that can be inferred. Used in diagnostics only to
1066 /// use inference to provide suggestions for the appropriate type if possible.
1067 fn is_suggestable_infer_ty(ty: &hir::Ty<'_>) -> bool {
1072 Slice(ty) => is_suggestable_infer_ty(ty),
1073 Array(ty, length) => {
1074 is_suggestable_infer_ty(ty) || matches!(length, hir::ArrayLen::Infer(_, _))
1076 Tup(tys) => tys.iter().any(is_suggestable_infer_ty),
1077 Ptr(mut_ty) | Rptr(_, mut_ty) => is_suggestable_infer_ty(mut_ty.ty),
1078 OpaqueDef(_, generic_args, _) => are_suggestable_generic_args(generic_args),
1079 Path(hir::QPath::TypeRelative(ty, segment)) => {
1080 is_suggestable_infer_ty(ty) || are_suggestable_generic_args(segment.args().args)
1082 Path(hir::QPath::Resolved(ty_opt, hir::Path { segments, .. })) => {
1083 ty_opt.map_or(false, is_suggestable_infer_ty)
1084 || segments.iter().any(|segment| are_suggestable_generic_args(segment.args().args))
1090 pub fn get_infer_ret_ty<'hir>(output: &'hir hir::FnRetTy<'hir>) -> Option<&'hir hir::Ty<'hir>> {
1091 if let hir::FnRetTy::Return(ty) = output {
1092 if is_suggestable_infer_ty(ty) {
1099 #[instrument(level = "debug", skip(tcx))]
1100 fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> {
1101 use rustc_hir::Node::*;
1104 let def_id = def_id.expect_local();
1105 let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
1107 let icx = ItemCtxt::new(tcx, def_id.to_def_id());
1109 match tcx.hir().get(hir_id) {
1110 TraitItem(hir::TraitItem {
1111 kind: TraitItemKind::Fn(sig, TraitFn::Provided(_)),
1115 | Item(hir::Item { kind: ItemKind::Fn(sig, generics, _), .. }) => {
1116 infer_return_ty_for_fn_sig(tcx, sig, generics, def_id, &icx)
1119 ImplItem(hir::ImplItem { kind: ImplItemKind::Fn(sig, _), generics, .. }) => {
1120 // Do not try to infer the return type for a impl method coming from a trait
1121 if let Item(hir::Item { kind: ItemKind::Impl(i), .. }) =
1122 tcx.hir().get(tcx.hir().get_parent_node(hir_id))
1123 && i.of_trait.is_some()
1125 <dyn AstConv<'_>>::ty_of_fn(
1128 sig.header.unsafety,
1135 infer_return_ty_for_fn_sig(tcx, sig, generics, def_id, &icx)
1139 TraitItem(hir::TraitItem {
1140 kind: TraitItemKind::Fn(FnSig { header, decl, span: _ }, _),
1143 }) => <dyn AstConv<'_>>::ty_of_fn(
1153 ForeignItem(&hir::ForeignItem { kind: ForeignItemKind::Fn(fn_decl, _, _), .. }) => {
1154 let abi = tcx.hir().get_foreign_abi(hir_id);
1155 compute_sig_of_foreign_fn_decl(tcx, def_id.to_def_id(), fn_decl, abi)
1158 Ctor(data) | Variant(hir::Variant { data, .. }) if data.ctor().is_some() => {
1159 let ty = tcx.type_of(tcx.hir().get_parent_item(hir_id));
1160 let inputs = data.fields().iter().map(|f| tcx.type_of(f.def_id));
1161 ty::Binder::dummy(tcx.mk_fn_sig(
1165 hir::Unsafety::Normal,
1170 Expr(&hir::Expr { kind: hir::ExprKind::Closure { .. }, .. }) => {
1171 // Closure signatures are not like other function
1172 // signatures and cannot be accessed through `fn_sig`. For
1173 // example, a closure signature excludes the `self`
1174 // argument. In any case they are embedded within the
1175 // closure type as part of the `ClosureSubsts`.
1177 // To get the signature of a closure, you should use the
1178 // `sig` method on the `ClosureSubsts`:
1180 // substs.as_closure().sig(def_id, tcx)
1182 "to get the signature of a closure, use `substs.as_closure().sig()` not `fn_sig()`",
1187 bug!("unexpected sort of node in fn_sig(): {:?}", x);
1192 fn infer_return_ty_for_fn_sig<'tcx>(
1194 sig: &hir::FnSig<'_>,
1195 generics: &hir::Generics<'_>,
1197 icx: &ItemCtxt<'tcx>,
1198 ) -> ty::PolyFnSig<'tcx> {
1199 let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
1201 match get_infer_ret_ty(&sig.decl.output) {
1203 let fn_sig = tcx.typeck(def_id).liberated_fn_sigs()[hir_id];
1204 // Typeck doesn't expect erased regions to be returned from `type_of`.
1205 let fn_sig = tcx.fold_regions(fn_sig, |r, _| match *r {
1206 ty::ReErased => tcx.lifetimes.re_static,
1209 let fn_sig = ty::Binder::dummy(fn_sig);
1211 let mut visitor = HirPlaceholderCollector::default();
1212 visitor.visit_ty(ty);
1213 let mut diag = bad_placeholder(tcx, visitor.0, "return type");
1214 let ret_ty = fn_sig.skip_binder().output();
1215 if ret_ty.is_suggestable(tcx, false) {
1216 diag.span_suggestion(
1218 "replace with the correct return type",
1220 Applicability::MachineApplicable,
1222 } else if matches!(ret_ty.kind(), ty::FnDef(..)) {
1223 let fn_sig = ret_ty.fn_sig(tcx);
1228 .all(|t| t.is_suggestable(tcx, false))
1230 diag.span_suggestion(
1232 "replace with the correct return type",
1234 Applicability::MachineApplicable,
1237 } else if ret_ty.is_closure() {
1238 // We're dealing with a closure, so we should suggest using `impl Fn` or trait bounds
1239 // to prevent the user from getting a papercut while trying to use the unique closure
1240 // syntax (e.g. `[closure@src/lib.rs:2:5: 2:9]`).
1241 diag.help("consider using an `Fn`, `FnMut`, or `FnOnce` trait bound");
1242 diag.note("for more information on `Fn` traits and closure types, see https://doc.rust-lang.org/book/ch13-01-closures.html");
1248 None => <dyn AstConv<'_>>::ty_of_fn(
1251 sig.header.unsafety,
1260 fn impl_trait_ref(tcx: TyCtxt<'_>, def_id: DefId) -> Option<ty::TraitRef<'_>> {
1261 let icx = ItemCtxt::new(tcx, def_id);
1262 let item = tcx.hir().expect_item(def_id.expect_local());
1264 hir::ItemKind::Impl(ref impl_) => impl_.of_trait.as_ref().map(|ast_trait_ref| {
1265 let selfty = tcx.type_of(def_id);
1266 <dyn AstConv<'_>>::instantiate_mono_trait_ref(
1270 check_impl_constness(tcx, impl_.constness, ast_trait_ref),
1277 fn check_impl_constness(
1279 constness: hir::Constness,
1280 ast_trait_ref: &hir::TraitRef<'_>,
1281 ) -> ty::BoundConstness {
1283 hir::Constness::Const => {
1284 if let Some(trait_def_id) = ast_trait_ref.trait_def_id() && !tcx.has_attr(trait_def_id, sym::const_trait) {
1285 let trait_name = tcx.item_name(trait_def_id).to_string();
1286 tcx.sess.emit_err(errors::ConstImplForNonConstTrait {
1287 trait_ref_span: ast_trait_ref.path.span,
1289 local_trait_span: trait_def_id.as_local().map(|_| tcx.def_span(trait_def_id).shrink_to_lo()),
1293 ty::BoundConstness::NotConst
1295 ty::BoundConstness::ConstIfConst
1298 hir::Constness::NotConst => ty::BoundConstness::NotConst,
1302 fn impl_polarity(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ImplPolarity {
1303 let is_rustc_reservation = tcx.has_attr(def_id, sym::rustc_reservation_impl);
1304 let item = tcx.hir().expect_item(def_id.expect_local());
1306 hir::ItemKind::Impl(hir::Impl {
1307 polarity: hir::ImplPolarity::Negative(span),
1311 if is_rustc_reservation {
1312 let span = span.to(of_trait.as_ref().map_or(*span, |t| t.path.span));
1313 tcx.sess.span_err(span, "reservation impls can't be negative");
1315 ty::ImplPolarity::Negative
1317 hir::ItemKind::Impl(hir::Impl {
1318 polarity: hir::ImplPolarity::Positive,
1322 if is_rustc_reservation {
1323 tcx.sess.span_err(item.span, "reservation impls can't be inherent");
1325 ty::ImplPolarity::Positive
1327 hir::ItemKind::Impl(hir::Impl {
1328 polarity: hir::ImplPolarity::Positive,
1332 if is_rustc_reservation {
1333 ty::ImplPolarity::Reservation
1335 ty::ImplPolarity::Positive
1338 item => bug!("impl_polarity: {:?} not an impl", item),
1342 /// Returns the early-bound lifetimes declared in this generics
1343 /// listing. For anything other than fns/methods, this is just all
1344 /// the lifetimes that are declared. For fns or methods, we have to
1345 /// screen out those that do not appear in any where-clauses etc using
1346 /// `resolve_lifetime::early_bound_lifetimes`.
1347 fn early_bound_lifetimes_from_generics<'a, 'tcx: 'a>(
1349 generics: &'a hir::Generics<'a>,
1350 ) -> impl Iterator<Item = &'a hir::GenericParam<'a>> + Captures<'tcx> {
1351 generics.params.iter().filter(move |param| match param.kind {
1352 GenericParamKind::Lifetime { .. } => !tcx.is_late_bound(param.hir_id),
1357 /// Returns a list of type predicates for the definition with ID `def_id`, including inferred
1358 /// lifetime constraints. This includes all predicates returned by `explicit_predicates_of`, plus
1359 /// inferred constraints concerning which regions outlive other regions.
1360 #[instrument(level = "debug", skip(tcx))]
1361 fn predicates_defined_on(tcx: TyCtxt<'_>, def_id: DefId) -> ty::GenericPredicates<'_> {
1362 let mut result = tcx.explicit_predicates_of(def_id);
1363 debug!("predicates_defined_on: explicit_predicates_of({:?}) = {:?}", def_id, result,);
1364 let inferred_outlives = tcx.inferred_outlives_of(def_id);
1365 if !inferred_outlives.is_empty() {
1367 "predicates_defined_on: inferred_outlives_of({:?}) = {:?}",
1368 def_id, inferred_outlives,
1370 if result.predicates.is_empty() {
1371 result.predicates = inferred_outlives;
1373 result.predicates = tcx
1375 .alloc_from_iter(result.predicates.iter().chain(inferred_outlives).copied());
1379 debug!("predicates_defined_on({:?}) = {:?}", def_id, result);
1383 fn compute_sig_of_foreign_fn_decl<'tcx>(
1386 decl: &'tcx hir::FnDecl<'tcx>,
1388 ) -> ty::PolyFnSig<'tcx> {
1389 let unsafety = if abi == abi::Abi::RustIntrinsic {
1390 intrinsic_operation_unsafety(tcx, def_id)
1392 hir::Unsafety::Unsafe
1394 let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
1395 let fty = <dyn AstConv<'_>>::ty_of_fn(
1396 &ItemCtxt::new(tcx, def_id),
1405 // Feature gate SIMD types in FFI, since I am not sure that the
1406 // ABIs are handled at all correctly. -huonw
1407 if abi != abi::Abi::RustIntrinsic
1408 && abi != abi::Abi::PlatformIntrinsic
1409 && !tcx.features().simd_ffi
1411 let check = |ast_ty: &hir::Ty<'_>, ty: Ty<'_>| {
1416 .span_to_snippet(ast_ty.span)
1417 .map_or_else(|_| String::new(), |s| format!(" `{}`", s));
1422 "use of SIMD type{} in FFI is highly experimental and \
1423 may result in invalid code",
1427 .help("add `#![feature(simd_ffi)]` to the crate attributes to enable")
1431 for (input, ty) in iter::zip(decl.inputs, fty.inputs().skip_binder()) {
1434 if let hir::FnRetTy::Return(ref ty) = decl.output {
1435 check(ty, fty.output().skip_binder())
1442 fn is_foreign_item(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
1443 match tcx.hir().get_if_local(def_id) {
1444 Some(Node::ForeignItem(..)) => true,
1446 _ => bug!("is_foreign_item applied to non-local def-id {:?}", def_id),
1450 fn generator_kind(tcx: TyCtxt<'_>, def_id: DefId) -> Option<hir::GeneratorKind> {
1451 match tcx.hir().get_if_local(def_id) {
1452 Some(Node::Expr(&rustc_hir::Expr {
1453 kind: rustc_hir::ExprKind::Closure(&rustc_hir::Closure { body, .. }),
1455 })) => tcx.hir().body(body).generator_kind(),
1457 _ => bug!("generator_kind applied to non-local def-id {:?}", def_id),
1461 fn from_target_feature(
1463 attr: &ast::Attribute,
1464 supported_target_features: &FxHashMap<String, Option<Symbol>>,
1465 target_features: &mut Vec<Symbol>,
1467 let Some(list) = attr.meta_item_list() else { return };
1468 let bad_item = |span| {
1469 let msg = "malformed `target_feature` attribute input";
1470 let code = "enable = \"..\"";
1472 .struct_span_err(span, msg)
1473 .span_suggestion(span, "must be of the form", code, Applicability::HasPlaceholders)
1476 let rust_features = tcx.features();
1478 // Only `enable = ...` is accepted in the meta-item list.
1479 if !item.has_name(sym::enable) {
1480 bad_item(item.span());
1484 // Must be of the form `enable = "..."` (a string).
1485 let Some(value) = item.value_str() else {
1486 bad_item(item.span());
1490 // We allow comma separation to enable multiple features.
1491 target_features.extend(value.as_str().split(',').filter_map(|feature| {
1492 let Some(feature_gate) = supported_target_features.get(feature) else {
1494 format!("the feature named `{}` is not valid for this target", feature);
1495 let mut err = tcx.sess.struct_span_err(item.span(), &msg);
1498 format!("`{}` is not valid for this target", feature),
1500 if let Some(stripped) = feature.strip_prefix('+') {
1501 let valid = supported_target_features.contains_key(stripped);
1503 err.help("consider removing the leading `+` in the feature name");
1510 // Only allow features whose feature gates have been enabled.
1511 let allowed = match feature_gate.as_ref().copied() {
1512 Some(sym::arm_target_feature) => rust_features.arm_target_feature,
1513 Some(sym::hexagon_target_feature) => rust_features.hexagon_target_feature,
1514 Some(sym::powerpc_target_feature) => rust_features.powerpc_target_feature,
1515 Some(sym::mips_target_feature) => rust_features.mips_target_feature,
1516 Some(sym::riscv_target_feature) => rust_features.riscv_target_feature,
1517 Some(sym::avx512_target_feature) => rust_features.avx512_target_feature,
1518 Some(sym::sse4a_target_feature) => rust_features.sse4a_target_feature,
1519 Some(sym::tbm_target_feature) => rust_features.tbm_target_feature,
1520 Some(sym::wasm_target_feature) => rust_features.wasm_target_feature,
1521 Some(sym::cmpxchg16b_target_feature) => rust_features.cmpxchg16b_target_feature,
1522 Some(sym::movbe_target_feature) => rust_features.movbe_target_feature,
1523 Some(sym::rtm_target_feature) => rust_features.rtm_target_feature,
1524 Some(sym::f16c_target_feature) => rust_features.f16c_target_feature,
1525 Some(sym::ermsb_target_feature) => rust_features.ermsb_target_feature,
1526 Some(sym::bpf_target_feature) => rust_features.bpf_target_feature,
1527 Some(sym::aarch64_ver_target_feature) => rust_features.aarch64_ver_target_feature,
1528 Some(name) => bug!("unknown target feature gate {}", name),
1533 &tcx.sess.parse_sess,
1534 feature_gate.unwrap(),
1536 &format!("the target feature `{}` is currently unstable", feature),
1540 Some(Symbol::intern(feature))
1545 fn linkage_by_name(tcx: TyCtxt<'_>, def_id: LocalDefId, name: &str) -> Linkage {
1546 use rustc_middle::mir::mono::Linkage::*;
1548 // Use the names from src/llvm/docs/LangRef.rst here. Most types are only
1549 // applicable to variable declarations and may not really make sense for
1550 // Rust code in the first place but allow them anyway and trust that the
1551 // user knows what they're doing. Who knows, unanticipated use cases may pop
1552 // up in the future.
1554 // ghost, dllimport, dllexport and linkonce_odr_autohide are not supported
1555 // and don't have to be, LLVM treats them as no-ops.
1557 "appending" => Appending,
1558 "available_externally" => AvailableExternally,
1560 "extern_weak" => ExternalWeak,
1561 "external" => External,
1562 "internal" => Internal,
1563 "linkonce" => LinkOnceAny,
1564 "linkonce_odr" => LinkOnceODR,
1565 "private" => Private,
1567 "weak_odr" => WeakODR,
1568 _ => tcx.sess.span_fatal(tcx.def_span(def_id), "invalid linkage specified"),
1572 fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: DefId) -> CodegenFnAttrs {
1573 if cfg!(debug_assertions) {
1574 let def_kind = tcx.def_kind(did);
1576 def_kind.has_codegen_attrs(),
1577 "unexpected `def_kind` in `codegen_fn_attrs`: {def_kind:?}",
1581 let did = did.expect_local();
1582 let attrs = tcx.hir().attrs(tcx.hir().local_def_id_to_hir_id(did));
1583 let mut codegen_fn_attrs = CodegenFnAttrs::new();
1584 if tcx.should_inherit_track_caller(did) {
1585 codegen_fn_attrs.flags |= CodegenFnAttrFlags::TRACK_CALLER;
1588 let supported_target_features = tcx.supported_target_features(LOCAL_CRATE);
1590 let mut inline_span = None;
1591 let mut link_ordinal_span = None;
1592 let mut no_sanitize_span = None;
1593 for attr in attrs.iter() {
1594 if attr.has_name(sym::cold) {
1595 codegen_fn_attrs.flags |= CodegenFnAttrFlags::COLD;
1596 } else if attr.has_name(sym::rustc_allocator) {
1597 codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR;
1598 } else if attr.has_name(sym::ffi_returns_twice) {
1599 if tcx.is_foreign_item(did) {
1600 codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_RETURNS_TWICE;
1602 // `#[ffi_returns_twice]` is only allowed `extern fn`s.
1607 "`#[ffi_returns_twice]` may only be used on foreign functions"
1611 } else if attr.has_name(sym::ffi_pure) {
1612 if tcx.is_foreign_item(did) {
1613 if attrs.iter().any(|a| a.has_name(sym::ffi_const)) {
1614 // `#[ffi_const]` functions cannot be `#[ffi_pure]`
1619 "`#[ffi_const]` function cannot be `#[ffi_pure]`"
1623 codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_PURE;
1626 // `#[ffi_pure]` is only allowed on foreign functions
1631 "`#[ffi_pure]` may only be used on foreign functions"
1635 } else if attr.has_name(sym::ffi_const) {
1636 if tcx.is_foreign_item(did) {
1637 codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_CONST;
1639 // `#[ffi_const]` is only allowed on foreign functions
1644 "`#[ffi_const]` may only be used on foreign functions"
1648 } else if attr.has_name(sym::rustc_nounwind) {
1649 codegen_fn_attrs.flags |= CodegenFnAttrFlags::NEVER_UNWIND;
1650 } else if attr.has_name(sym::rustc_reallocator) {
1651 codegen_fn_attrs.flags |= CodegenFnAttrFlags::REALLOCATOR;
1652 } else if attr.has_name(sym::rustc_deallocator) {
1653 codegen_fn_attrs.flags |= CodegenFnAttrFlags::DEALLOCATOR;
1654 } else if attr.has_name(sym::rustc_allocator_zeroed) {
1655 codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR_ZEROED;
1656 } else if attr.has_name(sym::naked) {
1657 codegen_fn_attrs.flags |= CodegenFnAttrFlags::NAKED;
1658 } else if attr.has_name(sym::no_mangle) {
1659 codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_MANGLE;
1660 } else if attr.has_name(sym::no_coverage) {
1661 codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_COVERAGE;
1662 } else if attr.has_name(sym::rustc_std_internal_symbol) {
1663 codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL;
1664 } else if attr.has_name(sym::used) {
1665 let inner = attr.meta_item_list();
1666 match inner.as_deref() {
1667 Some([item]) if item.has_name(sym::linker) => {
1668 if !tcx.features().used_with_arg {
1670 &tcx.sess.parse_sess,
1673 "`#[used(linker)]` is currently unstable",
1677 codegen_fn_attrs.flags |= CodegenFnAttrFlags::USED_LINKER;
1679 Some([item]) if item.has_name(sym::compiler) => {
1680 if !tcx.features().used_with_arg {
1682 &tcx.sess.parse_sess,
1685 "`#[used(compiler)]` is currently unstable",
1689 codegen_fn_attrs.flags |= CodegenFnAttrFlags::USED;
1692 tcx.sess.emit_err(errors::ExpectedUsedSymbol { span: attr.span });
1695 // Unfortunately, unconditionally using `llvm.used` causes
1696 // issues in handling `.init_array` with the gold linker,
1697 // but using `llvm.compiler.used` caused a nontrival amount
1698 // of unintentional ecosystem breakage -- particularly on
1701 // As a result, we emit `llvm.compiler.used` only on ELF
1702 // targets. This is somewhat ad-hoc, but actually follows
1703 // our pre-LLVM 13 behavior (prior to the ecosystem
1704 // breakage), and seems to match `clang`'s behavior as well
1705 // (both before and after LLVM 13), possibly because they
1706 // have similar compatibility concerns to us. See
1707 // https://github.com/rust-lang/rust/issues/47384#issuecomment-1019080146
1708 // and following comments for some discussion of this, as
1709 // well as the comments in `rustc_codegen_llvm` where these
1710 // flags are handled.
1712 // Anyway, to be clear: this is still up in the air
1713 // somewhat, and is subject to change in the future (which
1714 // is a good thing, because this would ideally be a bit
1716 let is_like_elf = !(tcx.sess.target.is_like_osx
1717 || tcx.sess.target.is_like_windows
1718 || tcx.sess.target.is_like_wasm);
1719 codegen_fn_attrs.flags |= if is_like_elf {
1720 CodegenFnAttrFlags::USED
1722 CodegenFnAttrFlags::USED_LINKER
1726 } else if attr.has_name(sym::cmse_nonsecure_entry) {
1727 if !matches!(tcx.fn_sig(did).abi(), abi::Abi::C { .. }) {
1732 "`#[cmse_nonsecure_entry]` requires C ABI"
1736 if !tcx.sess.target.llvm_target.contains("thumbv8m") {
1737 struct_span_err!(tcx.sess, attr.span, E0775, "`#[cmse_nonsecure_entry]` is only valid for targets with the TrustZone-M extension")
1740 codegen_fn_attrs.flags |= CodegenFnAttrFlags::CMSE_NONSECURE_ENTRY;
1741 } else if attr.has_name(sym::thread_local) {
1742 codegen_fn_attrs.flags |= CodegenFnAttrFlags::THREAD_LOCAL;
1743 } else if attr.has_name(sym::track_caller) {
1744 if !tcx.is_closure(did.to_def_id()) && tcx.fn_sig(did).abi() != abi::Abi::Rust {
1745 struct_span_err!(tcx.sess, attr.span, E0737, "`#[track_caller]` requires Rust ABI")
1748 if tcx.is_closure(did.to_def_id()) && !tcx.features().closure_track_caller {
1750 &tcx.sess.parse_sess,
1751 sym::closure_track_caller,
1753 "`#[track_caller]` on closures is currently unstable",
1757 codegen_fn_attrs.flags |= CodegenFnAttrFlags::TRACK_CALLER;
1758 } else if attr.has_name(sym::export_name) {
1759 if let Some(s) = attr.value_str() {
1760 if s.as_str().contains('\0') {
1761 // `#[export_name = ...]` will be converted to a null-terminated string,
1762 // so it may not contain any null characters.
1767 "`export_name` may not contain null characters"
1771 codegen_fn_attrs.export_name = Some(s);
1773 } else if attr.has_name(sym::target_feature) {
1774 if !tcx.is_closure(did.to_def_id())
1775 && tcx.fn_sig(did).unsafety() == hir::Unsafety::Normal
1777 if tcx.sess.target.is_like_wasm || tcx.sess.opts.actually_rustdoc {
1778 // The `#[target_feature]` attribute is allowed on
1779 // WebAssembly targets on all functions, including safe
1780 // ones. Other targets require that `#[target_feature]` is
1781 // only applied to unsafe functions (pending the
1782 // `target_feature_11` feature) because on most targets
1783 // execution of instructions that are not supported is
1784 // considered undefined behavior. For WebAssembly which is a
1785 // 100% safe target at execution time it's not possible to
1786 // execute undefined instructions, and even if a future
1787 // feature was added in some form for this it would be a
1788 // deterministic trap. There is no undefined behavior when
1789 // executing WebAssembly so `#[target_feature]` is allowed
1790 // on safe functions (but again, only for WebAssembly)
1792 // Note that this is also allowed if `actually_rustdoc` so
1793 // if a target is documenting some wasm-specific code then
1794 // it's not spuriously denied.
1795 } else if !tcx.features().target_feature_11 {
1796 let mut err = feature_err(
1797 &tcx.sess.parse_sess,
1798 sym::target_feature_11,
1800 "`#[target_feature(..)]` can only be applied to `unsafe` functions",
1802 err.span_label(tcx.def_span(did), "not an `unsafe` function");
1805 check_target_feature_trait_unsafe(tcx, did, attr.span);
1808 from_target_feature(
1811 supported_target_features,
1812 &mut codegen_fn_attrs.target_features,
1814 } else if attr.has_name(sym::linkage) {
1815 if let Some(val) = attr.value_str() {
1816 codegen_fn_attrs.linkage = Some(linkage_by_name(tcx, did, val.as_str()));
1818 } else if attr.has_name(sym::link_section) {
1819 if let Some(val) = attr.value_str() {
1820 if val.as_str().bytes().any(|b| b == 0) {
1822 "illegal null byte in link_section \
1826 tcx.sess.span_err(attr.span, &msg);
1828 codegen_fn_attrs.link_section = Some(val);
1831 } else if attr.has_name(sym::link_name) {
1832 codegen_fn_attrs.link_name = attr.value_str();
1833 } else if attr.has_name(sym::link_ordinal) {
1834 link_ordinal_span = Some(attr.span);
1835 if let ordinal @ Some(_) = check_link_ordinal(tcx, attr) {
1836 codegen_fn_attrs.link_ordinal = ordinal;
1838 } else if attr.has_name(sym::no_sanitize) {
1839 no_sanitize_span = Some(attr.span);
1840 if let Some(list) = attr.meta_item_list() {
1841 for item in list.iter() {
1842 if item.has_name(sym::address) {
1843 codegen_fn_attrs.no_sanitize |= SanitizerSet::ADDRESS;
1844 } else if item.has_name(sym::cfi) {
1845 codegen_fn_attrs.no_sanitize |= SanitizerSet::CFI;
1846 } else if item.has_name(sym::memory) {
1847 codegen_fn_attrs.no_sanitize |= SanitizerSet::MEMORY;
1848 } else if item.has_name(sym::memtag) {
1849 codegen_fn_attrs.no_sanitize |= SanitizerSet::MEMTAG;
1850 } else if item.has_name(sym::shadow_call_stack) {
1851 codegen_fn_attrs.no_sanitize |= SanitizerSet::SHADOWCALLSTACK;
1852 } else if item.has_name(sym::thread) {
1853 codegen_fn_attrs.no_sanitize |= SanitizerSet::THREAD;
1854 } else if item.has_name(sym::hwaddress) {
1855 codegen_fn_attrs.no_sanitize |= SanitizerSet::HWADDRESS;
1858 .struct_span_err(item.span(), "invalid argument for `no_sanitize`")
1859 .note("expected one of: `address`, `cfi`, `hwaddress`, `memory`, `memtag`, `shadow-call-stack`, or `thread`")
1864 } else if attr.has_name(sym::instruction_set) {
1865 codegen_fn_attrs.instruction_set = match attr.meta_kind() {
1866 Some(MetaItemKind::List(ref items)) => match items.as_slice() {
1867 [NestedMetaItem::MetaItem(set)] => {
1869 set.path.segments.iter().map(|x| x.ident.name).collect::<Vec<_>>();
1870 match segments.as_slice() {
1871 [sym::arm, sym::a32] | [sym::arm, sym::t32] => {
1872 if !tcx.sess.target.has_thumb_interworking {
1874 tcx.sess.diagnostic(),
1877 "target does not support `#[instruction_set]`"
1881 } else if segments[1] == sym::a32 {
1882 Some(InstructionSetAttr::ArmA32)
1883 } else if segments[1] == sym::t32 {
1884 Some(InstructionSetAttr::ArmT32)
1891 tcx.sess.diagnostic(),
1894 "invalid instruction set specified",
1903 tcx.sess.diagnostic(),
1906 "`#[instruction_set]` requires an argument"
1913 tcx.sess.diagnostic(),
1916 "cannot specify more than one instruction set"
1924 tcx.sess.diagnostic(),
1927 "must specify an instruction set"
1933 } else if attr.has_name(sym::repr) {
1934 codegen_fn_attrs.alignment = match attr.meta_item_list() {
1935 Some(items) => match items.as_slice() {
1936 [item] => match item.name_value_literal() {
1937 Some((sym::align, literal)) => {
1938 let alignment = rustc_attr::parse_alignment(&literal.kind);
1941 Ok(align) => Some(align),
1944 tcx.sess.diagnostic(),
1947 "invalid `repr(align)` attribute: {}",
1966 codegen_fn_attrs.inline = attrs.iter().fold(InlineAttr::None, |ia, attr| {
1967 if !attr.has_name(sym::inline) {
1970 match attr.meta_kind() {
1971 Some(MetaItemKind::Word) => InlineAttr::Hint,
1972 Some(MetaItemKind::List(ref items)) => {
1973 inline_span = Some(attr.span);
1974 if items.len() != 1 {
1976 tcx.sess.diagnostic(),
1979 "expected one argument"
1983 } else if list_contains_name(&items, sym::always) {
1985 } else if list_contains_name(&items, sym::never) {
1989 tcx.sess.diagnostic(),
1994 .help("valid inline arguments are `always` and `never`")
2000 Some(MetaItemKind::NameValue(_)) => ia,
2005 codegen_fn_attrs.optimize = attrs.iter().fold(OptimizeAttr::None, |ia, attr| {
2006 if !attr.has_name(sym::optimize) {
2009 let err = |sp, s| struct_span_err!(tcx.sess.diagnostic(), sp, E0722, "{}", s).emit();
2010 match attr.meta_kind() {
2011 Some(MetaItemKind::Word) => {
2012 err(attr.span, "expected one argument");
2015 Some(MetaItemKind::List(ref items)) => {
2016 inline_span = Some(attr.span);
2017 if items.len() != 1 {
2018 err(attr.span, "expected one argument");
2020 } else if list_contains_name(&items, sym::size) {
2022 } else if list_contains_name(&items, sym::speed) {
2025 err(items[0].span(), "invalid argument");
2029 Some(MetaItemKind::NameValue(_)) => ia,
2034 // #73631: closures inherit `#[target_feature]` annotations
2035 if tcx.features().target_feature_11 && tcx.is_closure(did.to_def_id()) {
2036 let owner_id = tcx.parent(did.to_def_id());
2037 if tcx.def_kind(owner_id).has_codegen_attrs() {
2040 .extend(tcx.codegen_fn_attrs(owner_id).target_features.iter().copied());
2044 // If a function uses #[target_feature] it can't be inlined into general
2045 // purpose functions as they wouldn't have the right target features
2046 // enabled. For that reason we also forbid #[inline(always)] as it can't be
2048 if !codegen_fn_attrs.target_features.is_empty() {
2049 if codegen_fn_attrs.inline == InlineAttr::Always {
2050 if let Some(span) = inline_span {
2053 "cannot use `#[inline(always)]` with \
2054 `#[target_feature]`",
2060 if !codegen_fn_attrs.no_sanitize.is_empty() {
2061 if codegen_fn_attrs.inline == InlineAttr::Always {
2062 if let (Some(no_sanitize_span), Some(inline_span)) = (no_sanitize_span, inline_span) {
2063 let hir_id = tcx.hir().local_def_id_to_hir_id(did);
2064 tcx.struct_span_lint_hir(
2065 lint::builtin::INLINE_NO_SANITIZE,
2068 "`no_sanitize` will have no effect after inlining",
2069 |lint| lint.span_note(inline_span, "inlining requested here"),
2075 // Weak lang items have the same semantics as "std internal" symbols in the
2076 // sense that they're preserved through all our LTO passes and only
2077 // strippable by the linker.
2079 // Additionally weak lang items have predetermined symbol names.
2080 if WEAK_LANG_ITEMS.iter().any(|&l| tcx.lang_items().get(l) == Some(did.to_def_id())) {
2081 codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL;
2083 if let Some((name, _)) = lang_items::extract(attrs)
2084 && let Some(lang_item) = LangItem::from_name(name)
2085 && let Some(link_name) = lang_item.link_name()
2087 codegen_fn_attrs.export_name = Some(link_name);
2088 codegen_fn_attrs.link_name = Some(link_name);
2090 check_link_name_xor_ordinal(tcx, &codegen_fn_attrs, link_ordinal_span);
2092 // Internal symbols to the standard library all have no_mangle semantics in
2093 // that they have defined symbol names present in the function name. This
2094 // also applies to weak symbols where they all have known symbol names.
2095 if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL) {
2096 codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_MANGLE;
2099 // Any linkage to LLVM intrinsics for now forcibly marks them all as never
2100 // unwinds since LLVM sometimes can't handle codegen which `invoke`s
2101 // intrinsic functions.
2102 if let Some(name) = &codegen_fn_attrs.link_name {
2103 if name.as_str().starts_with("llvm.") {
2104 codegen_fn_attrs.flags |= CodegenFnAttrFlags::NEVER_UNWIND;
2111 /// Computes the set of target features used in a function for the purposes of
2112 /// inline assembly.
2113 fn asm_target_features<'tcx>(tcx: TyCtxt<'tcx>, did: DefId) -> &'tcx FxHashSet<Symbol> {
2114 let mut target_features = tcx.sess.unstable_target_features.clone();
2115 if tcx.def_kind(did).has_codegen_attrs() {
2116 let attrs = tcx.codegen_fn_attrs(did);
2117 target_features.extend(&attrs.target_features);
2118 match attrs.instruction_set {
2120 Some(InstructionSetAttr::ArmA32) => {
2121 target_features.remove(&sym::thumb_mode);
2123 Some(InstructionSetAttr::ArmT32) => {
2124 target_features.insert(sym::thumb_mode);
2129 tcx.arena.alloc(target_features)
2132 /// Checks if the provided DefId is a method in a trait impl for a trait which has track_caller
2133 /// applied to the method prototype.
2134 fn should_inherit_track_caller(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
2135 if let Some(impl_item) = tcx.opt_associated_item(def_id)
2136 && let ty::AssocItemContainer::ImplContainer = impl_item.container
2137 && let Some(trait_item) = impl_item.trait_item_def_id
2140 .codegen_fn_attrs(trait_item)
2142 .intersects(CodegenFnAttrFlags::TRACK_CALLER);
2148 fn check_link_ordinal(tcx: TyCtxt<'_>, attr: &ast::Attribute) -> Option<u16> {
2149 use rustc_ast::{Lit, LitIntType, LitKind};
2150 if !tcx.features().raw_dylib && tcx.sess.target.arch == "x86" {
2152 &tcx.sess.parse_sess,
2155 "`#[link_ordinal]` is unstable on x86",
2159 let meta_item_list = attr.meta_item_list();
2160 let meta_item_list: Option<&[ast::NestedMetaItem]> = meta_item_list.as_ref().map(Vec::as_ref);
2161 let sole_meta_list = match meta_item_list {
2162 Some([item]) => item.literal(),
2165 .struct_span_err(attr.span, "incorrect number of arguments to `#[link_ordinal]`")
2166 .note("the attribute requires exactly one argument")
2172 if let Some(Lit { kind: LitKind::Int(ordinal, LitIntType::Unsuffixed), .. }) = sole_meta_list {
2173 // According to the table at https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#import-header,
2174 // the ordinal must fit into 16 bits. Similarly, the Ordinal field in COFFShortExport (defined
2175 // in llvm/include/llvm/Object/COFFImportFile.h), which we use to communicate import information
2176 // to LLVM for `#[link(kind = "raw-dylib"_])`, is also defined to be uint16_t.
2178 // FIXME: should we allow an ordinal of 0? The MSVC toolchain has inconsistent support for this:
2179 // both LINK.EXE and LIB.EXE signal errors and abort when given a .DEF file that specifies
2180 // a zero ordinal. However, llvm-dlltool is perfectly happy to generate an import library
2181 // for such a .DEF file, and MSVC's LINK.EXE is also perfectly happy to consume an import
2182 // library produced by LLVM with an ordinal of 0, and it generates an .EXE. (I don't know yet
2183 // if the resulting EXE runs, as I haven't yet built the necessary DLL -- see earlier comment
2184 // about LINK.EXE failing.)
2185 if *ordinal <= u16::MAX as u128 {
2186 Some(*ordinal as u16)
2188 let msg = format!("ordinal value in `link_ordinal` is too large: `{}`", &ordinal);
2190 .struct_span_err(attr.span, &msg)
2191 .note("the value may not exceed `u16::MAX`")
2197 .struct_span_err(attr.span, "illegal ordinal format in `link_ordinal`")
2198 .note("an unsuffixed integer value, e.g., `1`, is expected")
2204 fn check_link_name_xor_ordinal(
2206 codegen_fn_attrs: &CodegenFnAttrs,
2207 inline_span: Option<Span>,
2209 if codegen_fn_attrs.link_name.is_none() || codegen_fn_attrs.link_ordinal.is_none() {
2212 let msg = "cannot use `#[link_name]` with `#[link_ordinal]`";
2213 if let Some(span) = inline_span {
2214 tcx.sess.span_err(span, msg);
2220 /// Checks the function annotated with `#[target_feature]` is not a safe
2221 /// trait method implementation, reporting an error if it is.
2222 fn check_target_feature_trait_unsafe(tcx: TyCtxt<'_>, id: LocalDefId, attr_span: Span) {
2223 let hir_id = tcx.hir().local_def_id_to_hir_id(id);
2224 let node = tcx.hir().get(hir_id);
2225 if let Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Fn(..), .. }) = node {
2226 let parent_id = tcx.hir().get_parent_item(hir_id);
2227 let parent_item = tcx.hir().expect_item(parent_id.def_id);
2228 if let hir::ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }) = parent_item.kind {
2232 "`#[target_feature(..)]` cannot be applied to safe trait method",
2234 .span_label(attr_span, "cannot be applied to safe trait method")
2235 .span_label(tcx.def_span(id), "not an `unsafe` function")