Factor out some shared code.
`global_allocator_spans` and `alloc_error_handler_span` are identical except for `name`.
This commit is contained in:
parent
0b45675cfc
commit
59307fd9fd
1 changed files with 14 additions and 32 deletions
|
@ -1032,14 +1032,19 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
|
|||
}
|
||||
|
||||
fn inject_allocator_crate(&mut self, krate: &ast::Crate) {
|
||||
self.cstore.has_global_allocator = match &*global_allocator_spans(krate) {
|
||||
self.cstore.has_global_allocator =
|
||||
match &*fn_spans(krate, Symbol::intern(&global_fn_name(sym::alloc))) {
|
||||
[span1, span2, ..] => {
|
||||
self.dcx().emit_err(errors::NoMultipleGlobalAlloc { span2: *span2, span1: *span1 });
|
||||
self.dcx()
|
||||
.emit_err(errors::NoMultipleGlobalAlloc { span2: *span2, span1: *span1 });
|
||||
true
|
||||
}
|
||||
spans => !spans.is_empty(),
|
||||
};
|
||||
self.cstore.has_alloc_error_handler = match &*alloc_error_handler_spans(krate) {
|
||||
self.cstore.has_alloc_error_handler = match &*fn_spans(
|
||||
krate,
|
||||
Symbol::intern(alloc_error_handler_name(AllocatorKind::Global)),
|
||||
) {
|
||||
[span1, span2, ..] => {
|
||||
self.dcx()
|
||||
.emit_err(errors::NoMultipleAllocErrorHandler { span2: *span2, span1: *span1 });
|
||||
|
@ -1368,7 +1373,7 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
|
|||
}
|
||||
}
|
||||
|
||||
fn global_allocator_spans(krate: &ast::Crate) -> Vec<Span> {
|
||||
fn fn_spans(krate: &ast::Crate, name: Symbol) -> Vec<Span> {
|
||||
struct Finder {
|
||||
name: Symbol,
|
||||
spans: Vec<Span>,
|
||||
|
@ -1384,29 +1389,6 @@ fn global_allocator_spans(krate: &ast::Crate) -> Vec<Span> {
|
|||
}
|
||||
}
|
||||
|
||||
let name = Symbol::intern(&global_fn_name(sym::alloc));
|
||||
let mut f = Finder { name, spans: Vec::new() };
|
||||
visit::walk_crate(&mut f, krate);
|
||||
f.spans
|
||||
}
|
||||
|
||||
fn alloc_error_handler_spans(krate: &ast::Crate) -> Vec<Span> {
|
||||
struct Finder {
|
||||
name: Symbol,
|
||||
spans: Vec<Span>,
|
||||
}
|
||||
impl<'ast> visit::Visitor<'ast> for Finder {
|
||||
fn visit_item(&mut self, item: &'ast ast::Item) {
|
||||
if item.ident.name == self.name
|
||||
&& attr::contains_name(&item.attrs, sym::rustc_std_internal_symbol)
|
||||
{
|
||||
self.spans.push(item.span);
|
||||
}
|
||||
visit::walk_item(self, item)
|
||||
}
|
||||
}
|
||||
|
||||
let name = Symbol::intern(alloc_error_handler_name(AllocatorKind::Global));
|
||||
let mut f = Finder { name, spans: Vec::new() };
|
||||
visit::walk_crate(&mut f, krate);
|
||||
f.spans
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue