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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
//! Detecting lang items.
//!
//! Language items are items that represent concepts intrinsic to the language
//! itself. Examples are:
//!
//! * Traits that specify "kinds"; e.g., `Sync`, `Send`.
//! * Traits that represent operators; e.g., `Add`, `Sub`, `Index`.
//! * Functions called by the compiler itself.
use alloc::vec::Vec;
use alloc::string::String;
use alloc::string::ToString;
use crate::rustc_ast as ast;
use crate::rustc_ast::visit;
use crate::rustc_crate_store::ExternCrate;
use crate::rustc_hir::Target;
use crate::rustc_hir::attrs::lang_items::{GenericRequirement, LangItem, LanguageItems};
use crate::rustc_hir::def_id::{DefId, LocalDefId};
use crate::rustc_middle::query::Providers;
use crate::rustc_middle::ty::{ResolverAstLowering, TyCtxt};
use crate::rustc_span::{Span, Symbol, sym};
use crate::rustc_passes::diagnostics::{DuplicateLangItem, IncorrectCrateType, IncorrectTarget};
use crate::rustc_passes::weak_lang_items;
pub(crate) enum Duplicate {
Plain,
Crate,
CrateDepends,
}
struct LanguageItemCollector<'ast, 'tcx> {
items: LanguageItems,
tcx: TyCtxt<'tcx>,
resolver: &'ast ResolverAstLowering<'tcx>,
parent_item: Option<&'ast ast::Item>,
}
impl<'ast, 'tcx> LanguageItemCollector<'ast, 'tcx> {
fn new(
tcx: TyCtxt<'tcx>,
resolver: &'ast ResolverAstLowering<'tcx>,
) -> LanguageItemCollector<'ast, 'tcx> {
LanguageItemCollector { tcx, resolver, items: LanguageItems::new(), parent_item: None }
}
fn check_for_lang(
&mut self,
actual_target: Target,
def_id: LocalDefId,
attrs: &'ast [ast::Attribute],
item_span: Span,
generics: Option<&'ast ast::Generics>,
) {
if let Some((name, attr_span)) = extract_ast(attrs) {
match LangItem::from_name(name) {
// Known lang item
Some(lang_item) => {
if actual_target != lang_item.target() {
// `#[panic_handler]` is turned into `#[lang = "panic_impl"]`, but in contrast
// to the actual lang item attr, is applied to `Fn` instead of `ForeignFn`.
if !(lang_item.is_weak()
&& actual_target == Target::Fn
&& lang_item.target() == Target::ForeignFn
&& matches!(lang_item, LangItem::PanicImpl))
{
self.tcx
.dcx()
.delayed_bug(format!("lang item target is checked in attribute parser: {:?} has {} but expected {}", def_id, actual_target, lang_item.target()));
return;
}
}
// Weak lang items are handled separately
if lang_item.is_weak() && actual_target == Target::ForeignFn {
self.items.missing.push(lang_item);
} else {
// Weak only lang items are always handled here
self.collect_item_extended(
lang_item,
def_id,
item_span,
attr_span,
generics,
actual_target,
);
}
}
// Unknown lang item.
_ => {
self.tcx.dcx().delayed_bug("unknown lang item");
}
}
}
}
fn collect_item(&mut self, lang_item: LangItem, item_def_id: DefId, item_span: Option<Span>) {
// Check for duplicates.
if let Some(original_def_id) = self.items.get(lang_item)
&& original_def_id != 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 = None;
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(", ")
};
let mut orig_crate_name = None;
let mut orig_dependency_of = None;
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(", ")
};
if !original_def_id.is_local() {
orig_crate_name = Some(self.tcx.crate_name(original_def_id.krate));
if let Some(ExternCrate { dependency_of: inner_dependency_of, .. }) =
self.tcx.extern_crate(original_def_id.krate)
{
orig_dependency_of = Some(self.tcx.crate_name(*inner_dependency_of));
}
}
let duplicate = if item_span.is_some() {
Duplicate::Plain
} else {
match self.tcx.extern_crate(item_def_id.krate) {
Some(ExternCrate { dependency_of: inner_dependency_of, .. }) => {
dependency_of = Some(self.tcx.crate_name(*inner_dependency_of));
Duplicate::CrateDepends
}
_ => Duplicate::Crate,
}
};
// When there's a duplicate lang item, something went very wrong and there's no value
// in recovering or doing anything. Give the user the one message to let them debug the
// mess they created and then wish them farewell.
self.tcx.dcx().emit_fatal(DuplicateLangItem {
local_span: item_span,
lang_item_name,
crate_name,
dependency_of,
is_local,
path,
first_defined_span: original_def_id.as_local().map(|did| self.tcx.source_span(did)),
orig_crate_name,
orig_dependency_of,
orig_is_local,
orig_path,
duplicate,
});
} else {
// Matched.
self.items.set(lang_item, item_def_id);
}
}
// Like collect_item() above, but also checks whether the lang item is declared
// with the right number of generic arguments.
fn collect_item_extended(
&mut self,
lang_item: LangItem,
item_def_id: LocalDefId,
item_span: Span,
attr_span: Span,
generics: Option<&'ast ast::Generics>,
target: Target,
) {
let name = lang_item.name();
if let Some(generics) = generics {
// Now check whether the lang_item has the expected number of generic
// arguments. Generally speaking, binary and indexing operations have
// one (for the RHS/index), unary operations have none, the closure
// traits have one for the argument list, coroutines have one for the
// resume argument, and ordering/equality relations have one for the RHS
// Some other types like Box and various unsizing-related traits
// have minimum requirements.
// FIXME: This still doesn't count, e.g., elided lifetimes and APITs.
let mut actual_num = generics.params.len();
if target.is_associated_item() {
actual_num += self
.parent_item
.unwrap()
.opt_generics()
.map_or(0, |generics| generics.params.len());
}
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)
}
// If the number matches, or there is no requirement, handle it normally
_ => None,
};
if let Some(num) = required {
// We are issuing E0718 "incorrect target" here, because while the
// item kind of the target is correct, the target is still wrong
// because of the wrong number of generic arguments.
self.tcx.dcx().emit_err(IncorrectTarget {
span: attr_span,
generics_span: generics.span,
name: name.as_str(),
kind: target.name(),
num,
actual_num,
at_least,
});
// return early to not collect the lang item
return;
}
}
if self.tcx.crate_types().contains(&crate::rustc_structures::CrateType::Sdylib) {
self.tcx.dcx().emit_err(IncorrectCrateType { span: attr_span });
}
self.collect_item(lang_item, item_def_id.to_def_id(), Some(item_span));
}
}
/// Traverses and collects all the lang items in all crates.
fn get_lang_items(tcx: TyCtxt<'_>, (): ()) -> LanguageItems {
let (resolver, krate) = tcx.resolver_for_lowering();
let resolver = &*resolver.borrow();
let krate = &*krate.borrow();
// Initialize the collector.
let mut collector = LanguageItemCollector::new(tcx, resolver);
// Collect lang items in other crates.
for &cnum in tcx.used_crates(()).iter() {
for &(def_id, lang_item) in tcx.defined_lang_items(cnum).iter() {
collector.collect_item(lang_item, def_id, None);
}
}
// Collect lang items local to this crate.
visit::Visitor::visit_crate(&mut collector, krate);
// Find all required but not-yet-defined lang items.
weak_lang_items::check_crate(tcx, &mut collector.items);
// Return all the lang items that were found.
collector.items
}
impl<'ast, 'tcx> visit::Visitor<'ast> for LanguageItemCollector<'ast, 'tcx> {
type Result = ();
fn visit_item(&mut self, i: &'ast ast::Item) {
let target = Target::from_ast_item(i);
self.check_for_lang(
target,
self.resolver.owners[&i.id].def_id,
&i.attrs,
i.span,
i.opt_generics(),
);
let parent_item = self.parent_item.replace(i);
visit::walk_item(self, i);
self.parent_item = parent_item;
}
fn visit_foreign_item(&mut self, i: &'ast ast::ForeignItem) {
self.check_for_lang(
Target::from_foreign_item_kind(&i.kind),
self.resolver.owners[&i.id].def_id,
&i.attrs,
i.span,
None,
);
}
fn visit_variant(&mut self, variant: &'ast ast::Variant) {
self.check_for_lang(
Target::Variant,
self.resolver.owners[&self.parent_item.unwrap().id].node_id_to_def_id[&variant.id],
&variant.attrs,
variant.span,
None,
);
}
fn visit_assoc_item(&mut self, i: &'ast ast::AssocItem, ctxt: visit::AssocCtxt) {
let target = Target::from_assoc_item_kind(&i.kind, ctxt);
let generics = i.opt_generics();
self.check_for_lang(target, self.resolver.owners[&i.id].def_id, &i.attrs, i.span, generics);
visit::walk_assoc_item(self, i, ctxt);
}
}
/// Extracts the first `lang = "$name"` out of a list of attributes.
/// The `#[panic_handler]` attribute is also extracted out when found.
///
/// This function is used for `ast::Attribute`, for `hir::Attribute` use the `find_attr!` macro with `AttributeKind::Lang`
pub(crate) fn extract_ast(attrs: &[crate::rustc_ast::ast::Attribute]) -> Option<(Symbol, Span)> {
attrs.iter().find_map(|attr| {
Some(match attr {
_ if attr.has_name(sym::lang) => (attr.value_str()?, attr.span()),
_ if attr.has_name(sym::panic_handler) => (sym::panic_impl, attr.span()),
_ => return None,
})
})
}
pub(crate) fn provide(providers: &mut Providers) {
providers.get_lang_items = get_lang_items;
}