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::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};
43 use rustc_target::spec::{abi, SanitizerSet};
44 use rustc_trait_selection::traits::error_reporting::suggestions::NextTypeParamName;
53 ///////////////////////////////////////////////////////////////////////////
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 });
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,
85 collect_mod_item_types,
86 should_inherit_track_caller,
91 ///////////////////////////////////////////////////////////////////////////
93 /// Context specific to some particular item. This is what implements
96 /// # `ItemCtxt` vs `FnCtxt`
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.
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.
106 /// An important thing to note is that `ItemCtxt` does no inference -- it has no [`InferCtxt`] --
107 /// while `FnCtxt` does do inference.
109 /// [`FnCtxt`]: crate::check::FnCtxt
110 /// [`InferCtxt`]: rustc_infer::infer::InferCtxt
112 /// # Trait predicates
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> {
127 ///////////////////////////////////////////////////////////////////////////
130 pub(crate) struct HirPlaceholderCollector(pub(crate) Vec<Span>);
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 {
137 intravisit::walk_ty(self, t)
139 fn visit_generic_arg(&mut self, generic_arg: &'v hir::GenericArg<'v>) {
141 hir::GenericArg::Infer(inf) => {
142 self.0.push(inf.span);
143 intravisit::walk_inf(self, inf);
145 hir::GenericArg::Type(t) => self.visit_ty(t),
149 fn visit_array_length(&mut self, length: &'v hir::ArrayLen) {
150 if let &hir::ArrayLen::Infer(_, span) = length {
153 intravisit::walk_array_len(self, length)
157 struct CollectItemTypesVisitor<'tcx> {
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>(
166 generics: Option<&hir::Generics<'_>>,
167 placeholder_types: Vec<Span>,
169 hir_ty: Option<&hir::Ty<'_>>,
172 if placeholder_types.is_empty() {
176 placeholder_type_error_diag(tcx, generics, placeholder_types, vec![], suggest, hir_ty, kind)
180 pub(crate) fn placeholder_type_error_diag<'tcx>(
182 generics: Option<&hir::Generics<'_>>,
183 placeholder_types: Vec<Span>,
184 additional_spans: Vec<Span>,
186 hir_ty: Option<&hir::Ty<'_>>,
188 ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
189 if placeholder_types.is_empty() {
190 return bad_placeholder(tcx, additional_spans, kind);
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();
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, .. }))
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)));
209 sugg.push((generics.span, format!("<{}>", type_name)));
214 bad_placeholder(tcx, placeholder_types.into_iter().chain(additional_spans).collect(), kind);
216 // Suggest, but only if it is not a function in const or static
218 let mut is_fn = false;
219 let mut is_const_or_static = false;
221 if let Some(hir_ty) = hir_ty && let hir::TyKind::BareFn(_) = hir_ty.kind {
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);
228 is_const_or_static = matches!(
230 Node::Item(&hir::Item {
231 kind: hir::ItemKind::Const(..) | hir::ItemKind::Static(..),
233 }) | Node::TraitItem(&hir::TraitItem {
234 kind: hir::TraitItemKind::Const(..),
236 }) | Node::ImplItem(&hir::ImplItem { kind: hir::ImplItemKind::Const(..), .. })
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",
246 Applicability::HasPlaceholders,
254 fn reject_placeholder_type_signatures_in_item<'tcx>(
256 item: &'tcx hir::Item<'tcx>,
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.
271 let mut visitor = HirPlaceholderCollector::default();
272 visitor.visit_item(item);
274 placeholder_type_error(tcx, Some(generics), visitor.0, suggest, None, item.kind.descr());
277 impl<'tcx> Visitor<'tcx> for CollectItemTypesVisitor<'tcx> {
278 type NestedFilter = nested_filter::OnlyBodies;
280 fn nested_visit_map(&mut self) -> Self::Map {
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);
290 fn visit_generics(&mut self, generics: &'tcx hir::Generics<'tcx>) {
291 for param in generics.params {
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);
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);
311 intravisit::walk_generics(self, generics);
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.
322 intravisit::walk_expr(self, expr);
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);
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);
336 ///////////////////////////////////////////////////////////////////////////
337 // Utility types and common code for the above passes.
339 fn bad_placeholder<'tcx>(
341 mut spans: Vec<Span>,
343 ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
344 let kind = if kind.ends_with('s') { format!("{}es", kind) } else { format!("{}s", kind) };
347 let mut err = struct_span_err!(
351 "the placeholder `_` is not allowed within types on item signatures for {}",
355 err.span_label(span, "not allowed in type signatures");
360 impl<'tcx> ItemCtxt<'tcx> {
361 pub fn new(tcx: TyCtxt<'tcx>, item_def_id: DefId) -> ItemCtxt<'tcx> {
362 ItemCtxt { tcx, item_def_id }
365 pub fn to_ty(&self, ast_ty: &hir::Ty<'_>) -> Ty<'tcx> {
366 <dyn AstConv<'_>>::ast_ty_to_ty(self, ast_ty)
369 pub fn hir_id(&self) -> hir::HirId {
370 self.tcx.hir().local_def_id_to_hir_id(self.item_def_id.expect_local())
373 pub fn node(&self) -> hir::Node<'tcx> {
374 self.tcx.hir().get(self.hir_id())
378 impl<'tcx> AstConv<'tcx> for ItemCtxt<'tcx> {
379 fn tcx(&self) -> TyCtxt<'tcx> {
383 fn item_def_id(&self) -> Option<DefId> {
384 Some(self.item_def_id)
387 fn get_type_parameter_bounds(
392 ) -> ty::GenericPredicates<'tcx> {
393 self.tcx.at(span).type_param_predicates((
395 def_id.expect_local(),
400 fn re_infer(&self, _: Option<&ty::GenericParamDef>, _: Span) -> Option<ty::Region<'tcx>> {
404 fn allow_ty_infer(&self) -> bool {
408 fn ty_infer(&self, _: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx> {
409 self.tcx().ty_error_with_message(span, "bad placeholder type")
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,
417 self.tcx().const_error_with_message(ty, span, "bad placeholder constant")
420 fn projected_ty_from_poly_trait_ref(
424 item_segment: &hir::PathSegment<'_>,
425 poly_trait_ref: ty::PolyTraitRef<'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(
435 self.tcx().mk_projection(item_def_id, item_substs)
437 // There are no late-bound regions; we can just ignore the binder.
438 let mut err = struct_span_err!(
442 "cannot use the associated type of a trait \
443 with uninferred generic parameters"
447 hir::Node::Field(_) | hir::Node::Ctor(_) | hir::Node::Variant(_) => {
451 .expect_item(self.tcx.hir().get_parent_item(self.hir_id()).def_id);
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)),
460 (bound.span.shrink_to_lo(), format!("{}, ", lt_name))
463 let suggestions = vec![
466 span.with_hi(item_segment.ident.span.lo()),
469 // Replace the existing lifetimes with a new named lifetime.
470 self.tcx.replace_late_bound_regions_uncached(
473 self.tcx.mk_region(ty::ReEarlyBound(
474 ty::EarlyBoundRegion {
477 name: Symbol::intern(<_name),
485 err.multipart_suggestion(
486 "use a fully qualified path with explicit lifetimes",
488 Applicability::MaybeIncorrect,
494 hir::Node::Item(hir::Item {
496 hir::ItemKind::Struct(..) | hir::ItemKind::Enum(..) | hir::ItemKind::Union(..),
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",
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(),
511 Applicability::MaybeIncorrect,
517 self.tcx().ty_error()
521 fn normalize_ty(&self, _span: Span, ty: Ty<'tcx>) -> Ty<'tcx> {
522 // Types in item signatures are not normalized to avoid undue dependencies.
526 fn set_tainted_by_errors(&self) {
527 // There's no obvious place to track this, so just let it go.
530 fn record_ty(&self, _hir_id: hir::HirId, _ty: Ty<'tcx>, _span: Span) {
531 // There's no place to record types from signatures?
535 /// Synthesize a new lifetime name that doesn't clash with any of the lifetimes already present.
536 fn get_new_lifetime_name<'tcx>(
538 poly_trait_ref: ty::PolyTraitRef<'tcx>,
539 generics: &hir::Generics<'tcx>,
541 let existing_lifetimes = tcx
542 .collect_referenced_late_bound_regions(&poly_trait_ref)
545 if let ty::BoundRegionKind::BrNamed(_, name) = lt {
546 Some(name.as_str().to_string())
551 .chain(generics.params.iter().filter_map(|param| {
552 if let hir::GenericParamKind::Lifetime { .. } = ¶m.kind {
553 Some(param.name.ident().as_str().to_string())
558 .collect::<FxHashSet<String>>();
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));
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()
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;
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, .. } => {
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);
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(
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);
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);
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);
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);
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);
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);
643 if let Some(ctor_hir_id) = struct_def.ctor_hir_id() {
644 convert_variant_ctor(tcx, ctor_hir_id);
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(..),
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
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);
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);
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());
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);
690 match trait_item.kind {
691 hir::TraitItemKind::Fn(..) => {
692 tcx.ensure().type_of(def_id);
693 tcx.ensure().fn_sig(def_id);
696 hir::TraitItemKind::Const(.., Some(_)) => {
697 tcx.ensure().type_of(def_id);
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");
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");
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);
726 placeholder_type_error(tcx, None, visitor.0, false, None, "associated type");
730 tcx.ensure().predicates_of(def_id);
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);
743 hir::ImplItemKind::Type(_) => {
744 // Account for `type T = _;`
745 let mut visitor = HirPlaceholderCollector::default();
746 visitor.visit_impl_item(impl_item);
748 placeholder_type_error(tcx, None, visitor.0, false, None, "associated type");
750 hir::ImplItemKind::Const(..) => {}
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);
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<'_>>;
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));
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) {
777 struct_span_err!(tcx.sess, variant.span, E0370, "enum discriminant overflowed")
780 format!("overflowed on value after {}", prev_discr.unwrap()),
783 "explicitly set `{} = {}` if that is desired outcome",
784 variant.ident, wrapped_discr
789 .unwrap_or(wrapped_discr),
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);
799 // Convert the ctor, if any. This also registers the variant as
801 if let Some(ctor_hir_id) = variant.data.ctor_hir_id() {
802 convert_variant_ctor(tcx, ctor_hir_id);
809 variant_did: Option<LocalDefId>,
810 ctor_did: Option<LocalDefId>,
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();
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 {
831 seen_fields.insert(f.ident.normalize_to_macros_2_0(), f.span);
834 ty::FieldDef { did: fid.to_def_id(), name: f.ident.name, vis: tcx.visibility(fid) }
837 let recovered = match def {
838 hir::VariantData::Struct(_, r) => *r,
843 variant_did.map(LocalDefId::to_def_id),
844 ctor_did.map(LocalDefId::to_def_id),
847 CtorKind::from_hir(def),
849 parent_did.to_def_id(),
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)
858 fn adt_def<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> ty::AdtDef<'tcx> {
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 {
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;
875 let variant_did = Some(tcx.hir().local_def_id(v.id));
877 v.data.ctor_hir_id().map(|hir_id| tcx.hir().local_def_id(hir_id));
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())
883 ty::VariantDiscr::Relative(distance_from_explicit)
885 distance_from_explicit += 1;
900 (AdtKind::Enum, variants)
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));
906 let variants = std::iter::once(convert_variant(
911 ty::VariantDiscr::Relative(0),
918 (AdtKind::Struct, variants)
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));
924 let variants = std::iter::once(convert_variant(
929 ty::VariantDiscr::Relative(0),
936 (AdtKind::Union, variants)
940 tcx.alloc_adt_def(def_id.to_def_id(), kind, variants, repr)
943 fn trait_def(tcx: TyCtxt<'_>, def_id: DefId) -> ty::TraitDef {
944 let item = tcx.hir().expect_item(def_id.expect_local());
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)
950 hir::ItemKind::TraitAlias(..) => (false, hir::Unsafety::Normal, &[][..]),
951 _ => span_bug!(item.span, "trait_def_of_item invoked on non-trait"),
954 let paren_sugar = tcx.has_attr(def_id, sym::rustc_paren_sugar);
955 if paren_sugar && !tcx.features().unboxed_closures {
959 "the `#[rustc_paren_sugar]` attribute is a temporary means of controlling \
960 which traits can use parenthetical notation",
962 .help("add `#![feature(unboxed_closures)]` to the crate attributes to use it")
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
974 ty::trait_def::TraitSpecializationKind::None
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 => {
985 "the `#[rustc_must_implement_one_of]` attribute must be \
986 used with at least 2 args",
994 .map(|item| item.ident().ok_or(item.span()))
995 .collect::<Result<Box<[_]>, _>>()
998 .struct_span_err(span, "must be a name of an associated function")
1002 .zip(Some(attr.span)),
1003 // Error is reported by `rustc_attr!`
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);
1013 Some(item) if matches!(item.kind, hir::AssocItemKind::Fn { .. }) => {
1014 if !tcx.impl_defaultness(item.id.def_id).has_value() {
1018 "This function doesn't have a default implementation",
1020 .span_note(attr_span, "required by this annotation")
1030 .struct_span_err(item.span, "Not a function")
1031 .span_note(attr_span, "required by this annotation")
1033 "All `#[rustc_must_implement_one_of]` arguments \
1034 must be associated function names",
1040 .struct_span_err(ident.span, "Function not found in this trait")
1048 (errors.count() == 0).then_some(list)
1050 // Check for duplicates
1052 let mut set: FxHashMap<Symbol, Span> = FxHashMap::default();
1053 let mut no_dups = true;
1055 for ident in &*list {
1056 if let Some(dup) = set.insert(ident.name, ident.span) {
1058 .struct_span_err(vec![dup, ident.span], "Functions names are duplicated")
1060 "All `#[rustc_must_implement_one_of]` arguments \
1069 no_dups.then_some(list)
1078 skip_array_during_method_dispatch,
1080 must_implement_one_of,
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,
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 {
1099 Slice(ty) => is_suggestable_infer_ty(ty),
1100 Array(ty, length) => {
1101 is_suggestable_infer_ty(ty) || matches!(length, hir::ArrayLen::Infer(_, _))
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)
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))
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) {
1126 #[instrument(level = "debug", skip(tcx))]
1127 fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> {
1128 use rustc_hir::Node::*;
1131 let def_id = def_id.expect_local();
1132 let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
1134 let icx = ItemCtxt::new(tcx, def_id.to_def_id());
1136 match tcx.hir().get(hir_id) {
1137 TraitItem(hir::TraitItem {
1138 kind: TraitItemKind::Fn(sig, TraitFn::Provided(_)),
1142 | Item(hir::Item { kind: ItemKind::Fn(sig, generics, _), .. }) => {
1143 infer_return_ty_for_fn_sig(tcx, sig, generics, def_id, &icx)
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()
1152 <dyn AstConv<'_>>::ty_of_fn(
1155 sig.header.unsafety,
1162 infer_return_ty_for_fn_sig(tcx, sig, generics, def_id, &icx)
1166 TraitItem(hir::TraitItem {
1167 kind: TraitItemKind::Fn(FnSig { header, decl, span: _ }, _),
1170 }) => <dyn AstConv<'_>>::ty_of_fn(
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)
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));
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(
1193 hir::Unsafety::Normal,
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`.
1205 // To get the signature of a closure, you should use the
1206 // `sig` method on the `ClosureSubsts`:
1208 // substs.as_closure().sig(def_id, tcx)
1210 "to get the signature of a closure, use `substs.as_closure().sig()` not `fn_sig()`",
1215 bug!("unexpected sort of node in fn_sig(): {:?}", x);
1220 fn infer_return_ty_for_fn_sig<'tcx>(
1222 sig: &hir::FnSig<'_>,
1223 generics: &hir::Generics<'_>,
1225 icx: &ItemCtxt<'tcx>,
1226 ) -> ty::PolyFnSig<'tcx> {
1227 let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
1229 match get_infer_ret_ty(&sig.decl.output) {
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,
1237 let fn_sig = ty::Binder::dummy(fn_sig);
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(
1246 "replace with the correct return type",
1248 Applicability::MachineApplicable,
1250 } else if matches!(ret_ty.kind(), ty::FnDef(..)) {
1251 let fn_sig = ret_ty.fn_sig(tcx);
1256 .all(|t| t.is_suggestable(tcx, false))
1258 diag.span_suggestion(
1260 "replace with the correct return type",
1262 Applicability::MachineApplicable,
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");
1276 None => <dyn AstConv<'_>>::ty_of_fn(
1279 sig.header.unsafety,
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)
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());
1303 hir::ItemKind::Impl(hir::Impl {
1304 polarity: hir::ImplPolarity::Negative(span),
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");
1312 ty::ImplPolarity::Negative
1314 hir::ItemKind::Impl(hir::Impl {
1315 polarity: hir::ImplPolarity::Positive,
1319 if is_rustc_reservation {
1320 tcx.sess.span_err(item.span, "reservation impls can't be inherent");
1322 ty::ImplPolarity::Positive
1324 hir::ItemKind::Impl(hir::Impl {
1325 polarity: hir::ImplPolarity::Positive,
1329 if is_rustc_reservation {
1330 ty::ImplPolarity::Reservation
1332 ty::ImplPolarity::Positive
1335 item => bug!("impl_polarity: {:?} not an impl", item),
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>(
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),
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() {
1364 "predicates_defined_on: inferred_outlives_of({:?}) = {:?}",
1365 def_id, inferred_outlives,
1367 if result.predicates.is_empty() {
1368 result.predicates = inferred_outlives;
1370 result.predicates = tcx
1372 .alloc_from_iter(result.predicates.iter().chain(inferred_outlives).copied());
1376 debug!("predicates_defined_on({:?}) = {:?}", def_id, result);
1380 fn compute_sig_of_foreign_fn_decl<'tcx>(
1383 decl: &'tcx hir::FnDecl<'tcx>,
1385 ) -> ty::PolyFnSig<'tcx> {
1386 let unsafety = if abi == abi::Abi::RustIntrinsic {
1387 intrinsic_operation_unsafety(tcx, def_id)
1389 hir::Unsafety::Unsafe
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),
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
1408 let check = |ast_ty: &hir::Ty<'_>, ty: Ty<'_>| {
1413 .span_to_snippet(ast_ty.span)
1414 .map_or_else(|_| String::new(), |s| format!(" `{}`", s));
1419 "use of SIMD type{} in FFI is highly experimental and \
1420 may result in invalid code",
1424 .help("add `#![feature(simd_ffi)]` to the crate attributes to enable")
1428 for (input, ty) in iter::zip(decl.inputs, fty.inputs().skip_binder()) {
1431 if let hir::FnRetTy::Return(ref ty) = decl.output {
1432 check(ty, fty.output().skip_binder())
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,
1443 _ => bug!("is_foreign_item applied to non-local def-id {:?}", def_id),
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, .. }),
1452 })) => tcx.hir().body(body).generator_kind(),
1454 _ => bug!("generator_kind applied to non-local def-id {:?}", def_id),
1458 fn from_target_feature(
1460 attr: &ast::Attribute,
1461 supported_target_features: &FxHashMap<String, Option<Symbol>>,
1462 target_features: &mut Vec<Symbol>,
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 = \"..\"";
1469 .struct_span_err(span, msg)
1470 .span_suggestion(span, "must be of the form", code, Applicability::HasPlaceholders)
1473 let rust_features = tcx.features();
1475 // Only `enable = ...` is accepted in the meta-item list.
1476 if !item.has_name(sym::enable) {
1477 bad_item(item.span());
1481 // Must be of the form `enable = "..."` (a string).
1482 let Some(value) = item.value_str() else {
1483 bad_item(item.span());
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 {
1491 format!("the feature named `{}` is not valid for this target", feature);
1492 let mut err = tcx.sess.struct_span_err(item.span(), &msg);
1495 format!("`{}` is not valid for this target", feature),
1497 if let Some(stripped) = feature.strip_prefix('+') {
1498 let valid = supported_target_features.contains_key(stripped);
1500 err.help("consider removing the leading `+` in the feature name");
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),
1530 &tcx.sess.parse_sess,
1531 feature_gate.unwrap(),
1533 &format!("the target feature `{}` is currently unstable", feature),
1537 Some(Symbol::intern(feature))
1542 fn linkage_by_name(tcx: TyCtxt<'_>, def_id: LocalDefId, name: &str) -> Linkage {
1543 use rustc_middle::mir::mono::Linkage::*;
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.
1551 // ghost, dllimport, dllexport and linkonce_odr_autohide are not supported
1552 // and don't have to be, LLVM treats them as no-ops.
1554 "appending" => Appending,
1555 "available_externally" => AvailableExternally,
1557 "extern_weak" => ExternalWeak,
1558 "external" => External,
1559 "internal" => Internal,
1560 "linkonce" => LinkOnceAny,
1561 "linkonce_odr" => LinkOnceODR,
1562 "private" => Private,
1564 "weak_odr" => WeakODR,
1565 _ => tcx.sess.span_fatal(tcx.def_span(def_id), "invalid linkage specified"),
1569 fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: DefId) -> CodegenFnAttrs {
1570 if cfg!(debug_assertions) {
1571 let def_kind = tcx.def_kind(did);
1573 def_kind.has_codegen_attrs(),
1574 "unexpected `def_kind` in `codegen_fn_attrs`: {def_kind:?}",
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;
1585 let supported_target_features = tcx.supported_target_features(LOCAL_CRATE);
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;
1599 // `#[ffi_returns_twice]` is only allowed `extern fn`s.
1604 "`#[ffi_returns_twice]` may only be used on foreign functions"
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]`
1616 "`#[ffi_const]` function cannot be `#[ffi_pure]`"
1620 codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_PURE;
1623 // `#[ffi_pure]` is only allowed on foreign functions
1628 "`#[ffi_pure]` may only be used on foreign functions"
1632 } else if attr.has_name(sym::ffi_const) {
1633 if tcx.is_foreign_item(did) {
1634 codegen_fn_attrs.flags |= CodegenFnAttrFlags::FFI_CONST;
1636 // `#[ffi_const]` is only allowed on foreign functions
1641 "`#[ffi_const]` may only be used on foreign functions"
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 {
1667 &tcx.sess.parse_sess,
1670 "`#[used(linker)]` is currently unstable",
1674 codegen_fn_attrs.flags |= CodegenFnAttrFlags::USED_LINKER;
1676 Some([item]) if item.has_name(sym::compiler) => {
1677 if !tcx.features().used_with_arg {
1679 &tcx.sess.parse_sess,
1682 "`#[used(compiler)]` is currently unstable",
1686 codegen_fn_attrs.flags |= CodegenFnAttrFlags::USED;
1689 tcx.sess.emit_err(errors::ExpectedUsedSymbol { span: attr.span });
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
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.
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
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
1719 CodegenFnAttrFlags::USED_LINKER
1723 } else if attr.has_name(sym::cmse_nonsecure_entry) {
1724 if !matches!(tcx.fn_sig(did).abi(), abi::Abi::C { .. }) {
1729 "`#[cmse_nonsecure_entry]` requires C ABI"
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")
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")
1745 if tcx.is_closure(did.to_def_id()) && !tcx.features().closure_track_caller {
1747 &tcx.sess.parse_sess,
1748 sym::closure_track_caller,
1750 "`#[track_caller]` on closures is currently unstable",
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.
1764 "`export_name` may not contain null characters"
1768 codegen_fn_attrs.export_name = Some(s);
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
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)
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,
1797 "`#[target_feature(..)]` can only be applied to `unsafe` functions",
1799 err.span_label(tcx.def_span(did), "not an `unsafe` function");
1802 check_target_feature_trait_unsafe(tcx, did, attr.span);
1805 from_target_feature(
1808 supported_target_features,
1809 &mut codegen_fn_attrs.target_features,
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()));
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) {
1819 "illegal null byte in link_section \
1823 tcx.sess.span_err(attr.span, &msg);
1825 codegen_fn_attrs.link_section = Some(val);
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;
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;
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`")
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)] => {
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 {
1871 tcx.sess.diagnostic(),
1874 "target does not support `#[instruction_set]`"
1878 } else if segments[1] == sym::a32 {
1879 Some(InstructionSetAttr::ArmA32)
1880 } else if segments[1] == sym::t32 {
1881 Some(InstructionSetAttr::ArmT32)
1888 tcx.sess.diagnostic(),
1891 "invalid instruction set specified",
1900 tcx.sess.diagnostic(),
1903 "`#[instruction_set]` requires an argument"
1910 tcx.sess.diagnostic(),
1913 "cannot specify more than one instruction set"
1921 tcx.sess.diagnostic(),
1924 "must specify an instruction set"
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);
1938 Ok(align) => Some(align),
1941 tcx.sess.diagnostic(),
1944 "invalid `repr(align)` attribute: {}",
1963 codegen_fn_attrs.inline = attrs.iter().fold(InlineAttr::None, |ia, attr| {
1964 if !attr.has_name(sym::inline) {
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 {
1973 tcx.sess.diagnostic(),
1976 "expected one argument"
1980 } else if list_contains_name(&items, sym::always) {
1982 } else if list_contains_name(&items, sym::never) {
1986 tcx.sess.diagnostic(),
1991 .help("valid inline arguments are `always` and `never`")
1997 Some(MetaItemKind::NameValue(_)) => ia,
2002 codegen_fn_attrs.optimize = attrs.iter().fold(OptimizeAttr::None, |ia, attr| {
2003 if !attr.has_name(sym::optimize) {
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");
2012 Some(MetaItemKind::List(ref items)) => {
2013 inline_span = Some(attr.span);
2014 if items.len() != 1 {
2015 err(attr.span, "expected one argument");
2017 } else if list_contains_name(&items, sym::size) {
2019 } else if list_contains_name(&items, sym::speed) {
2022 err(items[0].span(), "invalid argument");
2026 Some(MetaItemKind::NameValue(_)) => ia,
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() {
2037 .extend(tcx.codegen_fn_attrs(owner_id).target_features.iter().copied());
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
2045 if !codegen_fn_attrs.target_features.is_empty() {
2046 if codegen_fn_attrs.inline == InlineAttr::Always {
2047 if let Some(span) = inline_span {
2050 "cannot use `#[inline(always)]` with \
2051 `#[target_feature]`",
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,
2065 "`no_sanitize` will have no effect after inlining",
2066 |lint| lint.span_note(inline_span, "inlining requested here"),
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.
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;
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);
2084 check_link_name_xor_ordinal(tcx, &codegen_fn_attrs, link_ordinal_span);
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;
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;
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 {
2114 Some(InstructionSetAttr::ArmA32) => {
2115 target_features.remove(&sym::thumb_mode);
2117 Some(InstructionSetAttr::ArmT32) => {
2118 target_features.insert(sym::thumb_mode);
2123 tcx.arena.alloc(target_features)
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
2134 .codegen_fn_attrs(trait_item)
2136 .intersects(CodegenFnAttrFlags::TRACK_CALLER);
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" {
2146 &tcx.sess.parse_sess,
2149 "`#[link_ordinal]` is unstable on x86",
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(),
2159 .struct_span_err(attr.span, "incorrect number of arguments to `#[link_ordinal]`")
2160 .note("the attribute requires exactly one argument")
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.
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)
2182 let msg = format!("ordinal value in `link_ordinal` is too large: `{}`", &ordinal);
2184 .struct_span_err(attr.span, &msg)
2185 .note("the value may not exceed `u16::MAX`")
2191 .struct_span_err(attr.span, "illegal ordinal format in `link_ordinal`")
2192 .note("an unsuffixed integer value, e.g., `1`, is expected")
2198 fn check_link_name_xor_ordinal(
2200 codegen_fn_attrs: &CodegenFnAttrs,
2201 inline_span: Option<Span>,
2203 if codegen_fn_attrs.link_name.is_none() || codegen_fn_attrs.link_ordinal.is_none() {
2206 let msg = "cannot use `#[link_name]` with `#[link_ordinal]`";
2207 if let Some(span) = inline_span {
2208 tcx.sess.span_err(span, msg);
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 {
2226 "`#[target_feature(..)]` cannot be applied to safe trait method",
2228 .span_label(attr_span, "cannot be applied to safe trait method")
2229 .span_label(tcx.def_span(id), "not an `unsafe` function")