1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
use crate::check_attr::target_from_impl_item;
use crate::errors::{
DuplicateLangItem, IncorrectTarget, LangItemOnIncorrectTarget, UnknownLangItem,
};
use crate::weak_lang_items;
use rustc_hir as hir;
use rustc_hir::def::DefKind;
use rustc_hir::def_id::DefId;
use rustc_hir::lang_items::{extract, GenericRequirement};
use rustc_hir::{HirId, LangItem, LanguageItems, Target};
use rustc_middle::ty::TyCtxt;
use rustc_session::cstore::ExternCrate;
use rustc_span::{symbol::kw::Empty, Span};
use rustc_middle::ty::query::Providers;
pub(crate) enum Duplicate {
Plain,
Crate,
CrateDepends,
}
struct LanguageItemCollector<'tcx> {
items: LanguageItems,
tcx: TyCtxt<'tcx>,
}
impl<'tcx> LanguageItemCollector<'tcx> {
fn new(tcx: TyCtxt<'tcx>) -> LanguageItemCollector<'tcx> {
LanguageItemCollector { tcx, items: LanguageItems::new() }
}
fn check_for_lang(&mut self, actual_target: Target, hir_id: HirId) {
let attrs = self.tcx.hir().attrs(hir_id);
if let Some((name, span)) = extract(&attrs) {
match LangItem::from_name(name) {
Some(lang_item) if actual_target == lang_item.target() => {
self.collect_item_extended(lang_item, hir_id, span);
}
Some(lang_item) => {
self.tcx.sess.emit_err(LangItemOnIncorrectTarget {
span,
name,
expected_target: lang_item.target(),
actual_target,
});
}
_ => {
self.tcx.sess.emit_err(UnknownLangItem { span, name });
}
}
}
}
fn collect_item(&mut self, lang_item: LangItem, item_def_id: DefId) {
if let Some(original_def_id) = self.items.get(lang_item) {
if original_def_id != item_def_id {
let local_span = self.tcx.hir().span_if_local(item_def_id);
let lang_item_name = lang_item.name();
let crate_name = self.tcx.crate_name(item_def_id.krate);
let mut dependency_of = Empty;
let is_local = item_def_id.is_local();
let path = if is_local {
String::new()
} else {
self.tcx
.crate_extern_paths(item_def_id.krate)
.iter()
.map(|p| p.display().to_string())
.collect::<Vec<_>>()
.join(", ")
.into()
};
let first_defined_span = self.tcx.hir().span_if_local(original_def_id);
let mut orig_crate_name = Empty;
let mut orig_dependency_of = Empty;
let orig_is_local = original_def_id.is_local();
let orig_path = if orig_is_local {
String::new()
} else {
self.tcx
.crate_extern_paths(original_def_id.krate)
.iter()
.map(|p| p.display().to_string())
.collect::<Vec<_>>()
.join(", ")
.into()
};
if first_defined_span.is_none() {
orig_crate_name = self.tcx.crate_name(original_def_id.krate);
if let Some(ExternCrate { dependency_of: inner_dependency_of, .. }) =
self.tcx.extern_crate(original_def_id)
{
orig_dependency_of = self.tcx.crate_name(*inner_dependency_of);
}
}
let duplicate = if local_span.is_some() {
Duplicate::Plain
} else {
match self.tcx.extern_crate(item_def_id) {
Some(ExternCrate { dependency_of: inner_dependency_of, .. }) => {
dependency_of = self.tcx.crate_name(*inner_dependency_of);
Duplicate::CrateDepends
}
_ => Duplicate::Crate,
}
};
self.tcx.sess.emit_err(DuplicateLangItem {
local_span,
lang_item_name,
crate_name,
dependency_of,
is_local,
path,
first_defined_span,
orig_crate_name,
orig_dependency_of,
orig_is_local,
orig_path,
duplicate,
});
}
}
self.items.set(lang_item, item_def_id);
}
fn collect_item_extended(&mut self, lang_item: LangItem, hir_id: HirId, span: Span) {
let item_def_id = self.tcx.hir().local_def_id(hir_id).to_def_id();
let name = lang_item.name();
if let hir::Node::Item(hir::Item { kind, span: item_span, .. }) = self.tcx.hir().get(hir_id)
{
let (actual_num, generics_span) = match kind.generics() {
Some(generics) => (generics.params.len(), generics.span),
None => (0, *item_span),
};
let mut at_least = false;
let required = match lang_item.required_generics() {
GenericRequirement::Exact(num) if num != actual_num => Some(num),
GenericRequirement::Minimum(num) if actual_num < num => {
at_least = true;
Some(num)}
,
_ => None,
};
if let Some(num) = required {
self.tcx.sess.emit_err(IncorrectTarget {
span,
generics_span,
name: name.as_str(),
kind: kind.descr(),
num,
actual_num,
at_least,
});
return;
}
}
self.collect_item(lang_item, item_def_id);
}
}
fn get_lang_items(tcx: TyCtxt<'_>, (): ()) -> LanguageItems {
let mut collector = LanguageItemCollector::new(tcx);
for &cnum in tcx.crates(()).iter() {
for &(def_id, lang_item) in tcx.defined_lang_items(cnum).iter() {
collector.collect_item(lang_item, def_id);
}
}
let crate_items = tcx.hir_crate_items(());
for id in crate_items.items() {
collector.check_for_lang(Target::from_def_kind(tcx.def_kind(id.owner_id)), id.hir_id());
if matches!(tcx.def_kind(id.owner_id), DefKind::Enum) {
let item = tcx.hir().item(id);
if let hir::ItemKind::Enum(def, ..) = &item.kind {
for variant in def.variants {
collector.check_for_lang(Target::Variant, variant.hir_id);
}
}
}
}
for id in crate_items.trait_items() {
let item = tcx.hir().trait_item(id);
collector.check_for_lang(Target::from_trait_item(item), item.hir_id())
}
for id in crate_items.impl_items() {
let item = tcx.hir().impl_item(id);
collector.check_for_lang(target_from_impl_item(tcx, item), item.hir_id())
}
let LanguageItemCollector { mut items, .. } = collector;
weak_lang_items::check_crate(tcx, &mut items);
items
}
pub fn provide(providers: &mut Providers) {
providers.get_lang_items = get_lang_items;
}