1 //! Detecting language items.
3 //! Language items are items that represent concepts intrinsic to the language
4 //! itself. Examples are:
6 //! * Traits that specify "kinds"; e.g., `Sync`, `Send`.
7 //! * Traits that represent operators; e.g., `Add`, `Sub`, `Index`.
8 //! * Functions called by the compiler itself.
10 use crate::check_attr::target_from_impl_item;
11 use crate::weak_lang_items;
13 use rustc_middle::middle::cstore::ExternCrate;
14 use rustc_middle::ty::TyCtxt;
16 use rustc_errors::struct_span_err;
18 use rustc_hir::def_id::{DefId, LOCAL_CRATE};
19 use rustc_hir::itemlikevisit::ItemLikeVisitor;
20 use rustc_hir::lang_items::{extract, ITEM_REFS};
21 use rustc_hir::{HirId, LangItem, LanguageItems, Target};
23 use rustc_middle::ty::query::Providers;
25 struct LanguageItemCollector<'tcx> {
30 impl ItemLikeVisitor<'v> for LanguageItemCollector<'tcx> {
31 fn visit_item(&mut self, item: &hir::Item<'_>) {
32 self.check_for_lang(Target::from_item(item), item.hir_id());
34 if let hir::ItemKind::Enum(def, ..) = &item.kind {
35 for variant in def.variants {
36 self.check_for_lang(Target::Variant, variant.id);
41 fn visit_trait_item(&mut self, trait_item: &hir::TraitItem<'_>) {
42 self.check_for_lang(Target::from_trait_item(trait_item), trait_item.hir_id())
45 fn visit_impl_item(&mut self, impl_item: &hir::ImplItem<'_>) {
46 self.check_for_lang(target_from_impl_item(self.tcx, impl_item), impl_item.hir_id())
49 fn visit_foreign_item(&mut self, _: &hir::ForeignItem<'_>) {}
52 impl LanguageItemCollector<'tcx> {
53 fn new(tcx: TyCtxt<'tcx>) -> LanguageItemCollector<'tcx> {
54 LanguageItemCollector { tcx, items: LanguageItems::new() }
57 fn check_for_lang(&mut self, actual_target: Target, hir_id: HirId) {
58 let attrs = self.tcx.hir().attrs(hir_id);
59 let check_name = |attr, sym| self.tcx.sess.check_name(attr, sym);
60 if let Some((value, span)) = extract(check_name, &attrs) {
61 match ITEM_REFS.get(&value).cloned() {
62 // Known lang item with attribute on correct target.
63 Some((item_index, expected_target)) if actual_target == expected_target => {
64 let def_id = self.tcx.hir().local_def_id(hir_id);
65 self.collect_item(item_index, def_id.to_def_id());
67 // Known lang item with attribute on incorrect target.
68 Some((_, expected_target)) => {
73 "`{}` language item must be applied to a {}",
80 "attribute should be applied to a {}, not a {}",
81 expected_target, actual_target,
92 "definition of an unknown language item: `{}`",
95 .span_label(span, format!("definition of unknown language item `{}`", value))
102 fn collect_item(&mut self, item_index: usize, item_def_id: DefId) {
103 // Check for duplicates.
104 if let Some(original_def_id) = self.items.items[item_index] {
105 if original_def_id != item_def_id {
106 let lang_item = LangItem::from_u32(item_index as u32).unwrap();
107 let name = lang_item.name();
108 let mut err = match self.tcx.hir().span_if_local(item_def_id) {
109 Some(span) => struct_span_err!(
113 "found duplicate lang item `{}`",
116 None => match self.tcx.extern_crate(item_def_id) {
117 Some(ExternCrate { dependency_of, .. }) => {
118 self.tcx.sess.struct_err(&format!(
119 "duplicate lang item in crate `{}` (which `{}` depends on): `{}`.",
120 self.tcx.crate_name(item_def_id.krate),
121 self.tcx.crate_name(*dependency_of),
125 _ => self.tcx.sess.struct_err(&format!(
126 "duplicate lang item in crate `{}`: `{}`.",
127 self.tcx.crate_name(item_def_id.krate),
132 if let Some(span) = self.tcx.hir().span_if_local(original_def_id) {
133 err.span_note(span, "the lang item is first defined here");
135 match self.tcx.extern_crate(original_def_id) {
136 Some(ExternCrate { dependency_of, .. }) => {
138 "the lang item is first defined in crate `{}` (which `{}` depends on)",
139 self.tcx.crate_name(original_def_id.krate),
140 self.tcx.crate_name(*dependency_of)
145 "the lang item is first defined in crate `{}`.",
146 self.tcx.crate_name(original_def_id.krate)
150 let mut note_def = |which, def_id: DefId| {
151 let crate_name = self.tcx.crate_name(def_id.krate);
152 let note = if def_id.is_local() {
153 format!("{} definition in the local crate (`{}`)", which, crate_name)
155 let paths: Vec<_> = self
157 .crate_extern_paths(def_id.krate)
159 .map(|p| p.display().to_string())
162 "{} definition in `{}` loaded from {}",
170 note_def("first", original_def_id);
171 note_def("second", item_def_id);
178 self.items.items[item_index] = Some(item_def_id);
179 if let Some(group) = LangItem::from_u32(item_index as u32).unwrap().group() {
180 self.items.groups[group as usize].push(item_def_id);
185 /// Traverses and collects all the lang items in all crates.
186 fn collect(tcx: TyCtxt<'_>) -> LanguageItems {
187 // Initialize the collector.
188 let mut collector = LanguageItemCollector::new(tcx);
190 // Collect lang items in other crates.
191 for &cnum in tcx.crates().iter() {
192 for &(def_id, item_index) in tcx.defined_lang_items(cnum).iter() {
193 collector.collect_item(item_index, def_id);
197 // Collect lang items in this crate.
198 tcx.hir().krate().visit_all_item_likes(&mut collector);
200 // Extract out the found lang items.
201 let LanguageItemCollector { mut items, .. } = collector;
203 // Find all required but not-yet-defined lang items.
204 weak_lang_items::check_crate(tcx, &mut items);
209 pub fn provide(providers: &mut Providers) {
210 providers.get_lang_items = |tcx, id| {
211 assert_eq!(id, LOCAL_CRATE);