Skip to content

Commit

Permalink
Stop treating extern crate loading failures as fatal errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mjptree committed Nov 29, 2021
1 parent f67e73a commit 29b4b6c
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 41 deletions.
38 changes: 26 additions & 12 deletions compiler/rustc_metadata/src/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use rustc_ast::{self as ast, *};
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_data_structures::svh::Svh;
use rustc_data_structures::sync::Lrc;
use rustc_errors::FatalError;
use rustc_expand::base::SyntaxExtension;
use rustc_hir::def_id::{CrateNum, LocalDefId, StableCrateId, LOCAL_CRATE};
use rustc_hir::definitions::Definitions;
Expand Down Expand Up @@ -507,18 +508,31 @@ impl<'a> CrateLoader<'a> {
}))
}

fn resolve_crate<'b>(
fn resolve_crate_or_abort<'b>(
&'b mut self,
name: Symbol,
span: Span,
dep_kind: CrateDepKind,
) -> CrateNum {
self.resolve_crate(name, span, dep_kind).unwrap_or_else(|| FatalError.raise())
}

fn resolve_crate<'b>(
&'b mut self,
name: Symbol,
span: Span,
dep_kind: CrateDepKind,
) -> Option<CrateNum> {
self.used_extern_options.insert(name);
self.maybe_resolve_crate(name, dep_kind, None).unwrap_or_else(|err| {
let missing_core =
self.maybe_resolve_crate(sym::core, CrateDepKind::Explicit, None).is_err();
err.report(&self.sess, span, missing_core)
})
self.maybe_resolve_crate(name, dep_kind, None).map_or_else(
|err| {
let missing_core =
self.maybe_resolve_crate(sym::core, CrateDepKind::Explicit, None).is_err();
err.report(&self.sess, span, missing_core);
None
},
|cnum| Some(cnum),
)
}

fn maybe_resolve_crate<'b>(
Expand Down Expand Up @@ -751,7 +765,7 @@ impl<'a> CrateLoader<'a> {
};
info!("panic runtime not found -- loading {}", name);

let cnum = self.resolve_crate(name, DUMMY_SP, CrateDepKind::Implicit);
let cnum = self.resolve_crate_or_abort(name, DUMMY_SP, CrateDepKind::Implicit);
let data = self.cstore.get_crate_data(cnum);

// Sanity check the loaded crate to ensure it is indeed a panic runtime
Expand Down Expand Up @@ -791,7 +805,7 @@ impl<'a> CrateLoader<'a> {
);
}

let cnum = self.resolve_crate(name, DUMMY_SP, CrateDepKind::Implicit);
let cnum = self.resolve_crate_or_abort(name, DUMMY_SP, CrateDepKind::Implicit);
let data = self.cstore.get_crate_data(cnum);

// Sanity check the loaded crate to ensure it is indeed a profiler runtime
Expand Down Expand Up @@ -991,7 +1005,7 @@ impl<'a> CrateLoader<'a> {
item: &ast::Item,
definitions: &Definitions,
def_id: LocalDefId,
) -> CrateNum {
) -> Option<CrateNum> {
match item.kind {
ast::ItemKind::ExternCrate(orig_name) => {
debug!(
Expand All @@ -1011,7 +1025,7 @@ impl<'a> CrateLoader<'a> {
CrateDepKind::Explicit
};

let cnum = self.resolve_crate(name, item.span, dep_kind);
let cnum = self.resolve_crate(name, item.span, dep_kind)?;

let path_len = definitions.def_path(def_id).data.len();
self.update_extern_crate(
Expand All @@ -1023,14 +1037,14 @@ impl<'a> CrateLoader<'a> {
dependency_of: LOCAL_CRATE,
},
);
cnum
Some(cnum)
}
_ => bug!(),
}
}

pub fn process_path_extern(&mut self, name: Symbol, span: Span) -> CrateNum {
let cnum = self.resolve_crate(name, span, CrateDepKind::Explicit);
let cnum = self.resolve_crate_or_abort(name, span, CrateDepKind::Explicit);

self.update_extern_crate(
cnum,
Expand Down
22 changes: 11 additions & 11 deletions compiler/rustc_metadata/src/locator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ use rustc_data_structures::memmap::Mmap;
use rustc_data_structures::owning_ref::OwningRef;
use rustc_data_structures::svh::Svh;
use rustc_data_structures::sync::MetadataRef;
use rustc_errors::struct_span_err;
use rustc_errors::{struct_span_err, DiagnosticBuilder, FatalError};
use rustc_session::config::{self, CrateType};
use rustc_session::cstore::{CrateSource, MetadataLoader};
use rustc_session::filesearch::{FileDoesntMatch, FileMatches, FileSearch};
Expand Down Expand Up @@ -814,11 +814,11 @@ pub fn find_plugin_registrar(
span: Span,
name: Symbol,
) -> PathBuf {
match find_plugin_registrar_impl(sess, metadata_loader, name) {
Ok(res) => res,
find_plugin_registrar_impl(sess, metadata_loader, name).unwrap_or_else(|err| {
// `core` is always available if we got as far as loading plugins.
Err(err) => err.report(sess, span, false),
}
err.report(sess, span, false);
FatalError.raise()
})
}

fn find_plugin_registrar_impl<'a>(
Expand Down Expand Up @@ -931,8 +931,8 @@ impl fmt::Display for MetadataError<'_> {
}

impl CrateError {
crate fn report(self, sess: &Session, span: Span, missing_core: bool) -> ! {
let mut err = match self {
fn build_diag(self, sess: &Session, span: Span, missing_core: bool) -> DiagnosticBuilder<'_> {
match self {
CrateError::NonAsciiName(crate_name) => sess.struct_span_err(
span,
&format!("cannot load a crate with a non-ascii name `{}`", crate_name),
Expand Down Expand Up @@ -1208,10 +1208,10 @@ impl CrateError {
"plugin `{}` only found in rlib format, but must be available in dylib format",
crate_name,
),
};
}
}

err.emit();
sess.abort_if_errors();
unreachable!();
crate fn report(self, sess: &Session, span: Span, missing_core: bool) {
self.build_diag(sess, span, missing_core).emit();
}
}
41 changes: 31 additions & 10 deletions compiler/rustc_resolve/src/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,11 +685,8 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
ItemKind::ExternCrate(orig_name) => {
self.build_reduced_graph_for_extern_crate(
orig_name,
ident,
item,
local_def_id,
sp,
expansion,
vis,
parent,
);
Expand Down Expand Up @@ -833,14 +830,16 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
fn build_reduced_graph_for_extern_crate(
&mut self,
orig_name: Option<Symbol>,
ident: Ident,
item: &Item,
local_def_id: LocalDefId,
sp: Span,
expansion: LocalExpnId,
vis: ty::Visibility,
parent: Module<'a>,
) {
let ident = item.ident;
let sp = item.span;
let parent_scope = self.parent_scope;
let expansion = parent_scope.expansion;

let module = if orig_name.is_none() && ident.name == kw::SelfLower {
self.r
.session
Expand All @@ -856,10 +855,32 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
} else if orig_name == Some(kw::SelfLower) {
self.r.graph_root
} else {
let crate_id =
self.r.crate_loader.process_extern_crate(item, &self.r.definitions, local_def_id);
self.r.extern_crate_map.insert(local_def_id, crate_id);
self.r.expect_module(crate_id.as_def_id())
match self.r.crate_loader.process_extern_crate(item, &self.r.definitions, local_def_id)
{
Some(crate_id) => {
self.r.extern_crate_map.insert(local_def_id, crate_id);
self.r.expect_module(crate_id.as_def_id())
}
_ => {
let dummy_import = self.r.arenas.alloc_import(Import {
kind: ImportKind::ExternCrate { source: orig_name, target: ident },
root_id: item.id,
id: item.id,
parent_scope: self.parent_scope,
imported_module: Cell::new(None),
has_attributes: !item.attrs.is_empty(),
use_span_with_attributes: item.span_with_attributes(),
use_span: item.span,
root_span: item.span,
span: item.span,
module_path: Vec::new(),
vis: Cell::new(vis),
used: Cell::new(true),
});
self.r.import_dummy_binding(dummy_import);
return;
}
}
};
let used = self.process_macro_use_imports(item, module);
let binding =
Expand Down
6 changes: 4 additions & 2 deletions compiler/rustc_resolve/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,8 +600,10 @@ impl<'a> Resolver<'a> {

// Define a "dummy" resolution containing a Res::Err as a placeholder for a
// failed resolution
fn import_dummy_binding(&mut self, import: &'a Import<'a>) {
if let ImportKind::Single { target, .. } = import.kind {
crate fn import_dummy_binding(&mut self, import: &'a Import<'a>) {
if let ImportKind::Single { target, .. } | ImportKind::ExternCrate { target, .. } =
import.kind
{
let dummy_binding = self.dummy_binding;
let dummy_binding = self.import(dummy_binding, import);
self.per_ns(|this, ns| {
Expand Down
1 change: 1 addition & 0 deletions src/test/ui/crate-loading/missing-std.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// compile-flags: --target x86_64-unknown-uefi
// needs-llvm-components: x86
// rustc-env:CARGO=/usr/bin/cargo
#![feature(no_core)]
#![no_core]
extern crate core;
//~^ ERROR can't find crate for `core`
Expand Down
6 changes: 4 additions & 2 deletions src/test/ui/crate-loading/missing-std.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error[E0463]: can't find crate for `core`
--> $DIR/missing-std.rs:5:1
--> $DIR/missing-std.rs:6:1
|
LL | extern crate core;
| ^^^^^^^^^^^^^^^^^^ can't find crate
Expand All @@ -8,6 +8,8 @@ LL | extern crate core;
= help: consider downloading the target with `rustup target add x86_64-unknown-uefi`
= help: consider building the standard library from source with `cargo build -Zbuild-std`

error: aborting due to previous error
error: requires `sized` lang_item

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0463`.
6 changes: 5 additions & 1 deletion src/test/ui/extern-flag/empty-extern-arg.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
error: extern location for std does not exist:

error: aborting due to previous error
error: language item required, but not found: `eh_personality`

error: `#[panic_handler]` function required, but not found

error: aborting due to 3 previous errors

8 changes: 8 additions & 0 deletions src/test/ui/extern/extern-crate-multiple-missing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// If multiple `extern crate` resolutions fail each of them should produce an error
extern crate bar; //~ ERROR can't find crate for `bar`
extern crate foo; //~ ERROR can't find crate for `foo`

fn main() {
foo::something();
bar::something();
}
15 changes: 15 additions & 0 deletions src/test/ui/extern/extern-crate-multiple-missing.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error[E0463]: can't find crate for `bar`
--> $DIR/extern-crate-multiple-missing.rs:2:1
|
LL | extern crate bar;
| ^^^^^^^^^^^^^^^^^ can't find crate

error[E0463]: can't find crate for `foo`
--> $DIR/extern-crate-multiple-missing.rs:3:1
|
LL | extern crate foo;
| ^^^^^^^^^^^^^^^^^ can't find crate

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0463`.
4 changes: 3 additions & 1 deletion src/test/ui/issues/issue-37131.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ error[E0463]: can't find crate for `std`
= help: consider downloading the target with `rustup target add thumbv6m-none-eabi`
= help: consider building the standard library from source with `cargo build -Zbuild-std`

error: aborting due to previous error
error: requires `sized` lang_item

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0463`.
6 changes: 5 additions & 1 deletion src/test/ui/issues/issue-49851/compiler-builtins-error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//~ ERROR 1:1: 1:1: can't find crate for `core` [E0463]
//~ ERROR can't find crate for `core`
//~^ ERROR can't find crate for `compiler_builtins`

// compile-flags: --target thumbv7em-none-eabihf
// needs-llvm-components: arm
Expand All @@ -7,3 +8,6 @@
#![no_std]

extern crate cortex_m;
//~^ ERROR can't find crate for `cortex_m`

fn main() {}
12 changes: 11 additions & 1 deletion src/test/ui/issues/issue-49851/compiler-builtins-error.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ error[E0463]: can't find crate for `core`
= help: consider downloading the target with `rustup target add thumbv7em-none-eabihf`
= help: consider building the standard library from source with `cargo build -Zbuild-std`

error: aborting due to previous error
error[E0463]: can't find crate for `compiler_builtins`

error[E0463]: can't find crate for `cortex_m`
--> $DIR/compiler-builtins-error.rs:10:1
|
LL | extern crate cortex_m;
| ^^^^^^^^^^^^^^^^^^^^^^ can't find crate

error: requires `sized` lang_item

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0463`.

0 comments on commit 29b4b6c

Please sign in to comment.