Skip to content

Commit 62f4ce9

Browse files
committed
Stop treating extern crate loading failures as fatal errors
1 parent 10b3a57 commit 62f4ce9

12 files changed

+124
-41
lines changed

compiler/rustc_metadata/src/creader.rs

+26-12
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use rustc_ast::{self as ast, *};
99
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1010
use rustc_data_structures::svh::Svh;
1111
use rustc_data_structures::sync::Lrc;
12+
use rustc_errors::FatalError;
1213
use rustc_expand::base::SyntaxExtension;
1314
use rustc_hir::def_id::{CrateNum, LocalDefId, StableCrateId, LOCAL_CRATE};
1415
use rustc_hir::definitions::Definitions;
@@ -507,18 +508,31 @@ impl<'a> CrateLoader<'a> {
507508
}))
508509
}
509510

510-
fn resolve_crate<'b>(
511+
fn resolve_crate_or_abort<'b>(
511512
&'b mut self,
512513
name: Symbol,
513514
span: Span,
514515
dep_kind: CrateDepKind,
515516
) -> CrateNum {
517+
self.resolve_crate(name, span, dep_kind).unwrap_or_else(|| FatalError.raise())
518+
}
519+
520+
fn resolve_crate<'b>(
521+
&'b mut self,
522+
name: Symbol,
523+
span: Span,
524+
dep_kind: CrateDepKind,
525+
) -> Option<CrateNum> {
516526
self.used_extern_options.insert(name);
517-
self.maybe_resolve_crate(name, dep_kind, None).unwrap_or_else(|err| {
518-
let missing_core =
519-
self.maybe_resolve_crate(sym::core, CrateDepKind::Explicit, None).is_err();
520-
err.report(&self.sess, span, missing_core)
521-
})
527+
self.maybe_resolve_crate(name, dep_kind, None).map_or_else(
528+
|err| {
529+
let missing_core =
530+
self.maybe_resolve_crate(sym::core, CrateDepKind::Explicit, None).is_err();
531+
err.report(&self.sess, span, missing_core);
532+
None
533+
},
534+
|cnum| Some(cnum),
535+
)
522536
}
523537

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

754-
let cnum = self.resolve_crate(name, DUMMY_SP, CrateDepKind::Implicit);
768+
let cnum = self.resolve_crate_or_abort(name, DUMMY_SP, CrateDepKind::Implicit);
755769
let data = self.cstore.get_crate_data(cnum);
756770

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

794-
let cnum = self.resolve_crate(name, DUMMY_SP, CrateDepKind::Implicit);
808+
let cnum = self.resolve_crate_or_abort(name, DUMMY_SP, CrateDepKind::Implicit);
795809
let data = self.cstore.get_crate_data(cnum);
796810

797811
// Sanity check the loaded crate to ensure it is indeed a profiler runtime
@@ -991,7 +1005,7 @@ impl<'a> CrateLoader<'a> {
9911005
item: &ast::Item,
9921006
definitions: &Definitions,
9931007
def_id: LocalDefId,
994-
) -> CrateNum {
1008+
) -> Option<CrateNum> {
9951009
match item.kind {
9961010
ast::ItemKind::ExternCrate(orig_name) => {
9971011
debug!(
@@ -1011,7 +1025,7 @@ impl<'a> CrateLoader<'a> {
10111025
CrateDepKind::Explicit
10121026
};
10131027

1014-
let cnum = self.resolve_crate(name, item.span, dep_kind);
1028+
let cnum = self.resolve_crate(name, item.span, dep_kind)?;
10151029

10161030
let path_len = definitions.def_path(def_id).data.len();
10171031
self.update_extern_crate(
@@ -1023,14 +1037,14 @@ impl<'a> CrateLoader<'a> {
10231037
dependency_of: LOCAL_CRATE,
10241038
},
10251039
);
1026-
cnum
1040+
Some(cnum)
10271041
}
10281042
_ => bug!(),
10291043
}
10301044
}
10311045

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

10351049
self.update_extern_crate(
10361050
cnum,

compiler/rustc_metadata/src/locator.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ use rustc_data_structures::memmap::Mmap;
220220
use rustc_data_structures::owning_ref::OwningRef;
221221
use rustc_data_structures::svh::Svh;
222222
use rustc_data_structures::sync::MetadataRef;
223-
use rustc_errors::struct_span_err;
223+
use rustc_errors::{struct_span_err, DiagnosticBuilder, FatalError};
224224
use rustc_session::config::{self, CrateType};
225225
use rustc_session::cstore::{CrateSource, MetadataLoader};
226226
use rustc_session::filesearch::{FileDoesntMatch, FileMatches, FileSearch};
@@ -814,11 +814,11 @@ pub fn find_plugin_registrar(
814814
span: Span,
815815
name: Symbol,
816816
) -> PathBuf {
817-
match find_plugin_registrar_impl(sess, metadata_loader, name) {
818-
Ok(res) => res,
817+
find_plugin_registrar_impl(sess, metadata_loader, name).unwrap_or_else(|err| {
819818
// `core` is always available if we got as far as loading plugins.
820-
Err(err) => err.report(sess, span, false),
821-
}
819+
err.report(sess, span, false);
820+
FatalError.raise()
821+
})
822822
}
823823

824824
fn find_plugin_registrar_impl<'a>(
@@ -931,8 +931,8 @@ impl fmt::Display for MetadataError<'_> {
931931
}
932932

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

1213-
err.emit();
1214-
sess.abort_if_errors();
1215-
unreachable!();
1214+
crate fn report(self, sess: &Session, span: Span, missing_core: bool) {
1215+
self.build_diag(sess, span, missing_core).emit();
12161216
}
12171217
}

compiler/rustc_resolve/src/build_reduced_graph.rs

+31-10
Original file line numberDiff line numberDiff line change
@@ -685,11 +685,8 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
685685
ItemKind::ExternCrate(orig_name) => {
686686
self.build_reduced_graph_for_extern_crate(
687687
orig_name,
688-
ident,
689688
item,
690689
local_def_id,
691-
sp,
692-
expansion,
693690
vis,
694691
parent,
695692
);
@@ -833,14 +830,16 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
833830
fn build_reduced_graph_for_extern_crate(
834831
&mut self,
835832
orig_name: Option<Symbol>,
836-
ident: Ident,
837833
item: &Item,
838834
local_def_id: LocalDefId,
839-
sp: Span,
840-
expansion: LocalExpnId,
841835
vis: ty::Visibility,
842836
parent: Module<'a>,
843837
) {
838+
let ident = item.ident;
839+
let sp = item.span;
840+
let parent_scope = self.parent_scope;
841+
let expansion = parent_scope.expansion;
842+
844843
let module = if orig_name.is_none() && ident.name == kw::SelfLower {
845844
self.r
846845
.session
@@ -856,10 +855,32 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
856855
} else if orig_name == Some(kw::SelfLower) {
857856
self.r.graph_root
858857
} else {
859-
let crate_id =
860-
self.r.crate_loader.process_extern_crate(item, &self.r.definitions, local_def_id);
861-
self.r.extern_crate_map.insert(local_def_id, crate_id);
862-
self.r.expect_module(crate_id.as_def_id())
858+
match self.r.crate_loader.process_extern_crate(item, &self.r.definitions, local_def_id)
859+
{
860+
Some(crate_id) => {
861+
self.r.extern_crate_map.insert(local_def_id, crate_id);
862+
self.r.expect_module(crate_id.as_def_id())
863+
}
864+
_ => {
865+
let dummy_import = self.r.arenas.alloc_import(Import {
866+
kind: ImportKind::ExternCrate { source: orig_name, target: ident },
867+
root_id: item.id,
868+
id: item.id,
869+
parent_scope: self.parent_scope,
870+
imported_module: Cell::new(None),
871+
has_attributes: !item.attrs.is_empty(),
872+
use_span_with_attributes: item.span_with_attributes(),
873+
use_span: item.span,
874+
root_span: item.span,
875+
span: item.span,
876+
module_path: Vec::new(),
877+
vis: Cell::new(vis),
878+
used: Cell::new(true),
879+
});
880+
self.r.import_dummy_binding(dummy_import);
881+
return;
882+
}
883+
}
863884
};
864885
let used = self.process_macro_use_imports(item, module);
865886
let binding =

compiler/rustc_resolve/src/imports.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -600,8 +600,10 @@ impl<'a> Resolver<'a> {
600600

601601
// Define a "dummy" resolution containing a Res::Err as a placeholder for a
602602
// failed resolution
603-
fn import_dummy_binding(&mut self, import: &'a Import<'a>) {
604-
if let ImportKind::Single { target, .. } = import.kind {
603+
crate fn import_dummy_binding(&mut self, import: &'a Import<'a>) {
604+
if let ImportKind::Single { target, .. } | ImportKind::ExternCrate { target, .. } =
605+
import.kind
606+
{
605607
let dummy_binding = self.dummy_binding;
606608
let dummy_binding = self.import(dummy_binding, import);
607609
self.per_ns(|this, ns| {

src/test/ui/crate-loading/missing-std.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// compile-flags: --target x86_64-unknown-uefi
22
// needs-llvm-components: x86
33
// rustc-env:CARGO=/usr/bin/cargo
4+
#![feature(no_core)]
45
#![no_core]
56
extern crate core;
67
//~^ ERROR can't find crate for `core`
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0463]: can't find crate for `core`
2-
--> $DIR/missing-std.rs:5:1
2+
--> $DIR/missing-std.rs:6:1
33
|
44
LL | extern crate core;
55
| ^^^^^^^^^^^^^^^^^^ can't find crate
@@ -8,6 +8,8 @@ LL | extern crate core;
88
= help: consider downloading the target with `rustup target add x86_64-unknown-uefi`
99
= help: consider building the standard library from source with `cargo build -Zbuild-std`
1010

11-
error: aborting due to previous error
11+
error: requires `sized` lang_item
12+
13+
error: aborting due to 2 previous errors
1214

1315
For more information about this error, try `rustc --explain E0463`.
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
error: extern location for std does not exist:
22

3-
error: aborting due to previous error
3+
error: language item required, but not found: `eh_personality`
4+
5+
error: `#[panic_handler]` function required, but not found
6+
7+
error: aborting due to 3 previous errors
48

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// If multiple `extern crate` resolutions fail each of them should produce an error
2+
extern crate bar; //~ ERROR can't find crate for `bar`
3+
extern crate foo; //~ ERROR can't find crate for `foo`
4+
5+
fn main() {
6+
foo::something();
7+
bar::something();
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0463]: can't find crate for `bar`
2+
--> $DIR/extern-crate-multiple-missing.rs:2:1
3+
|
4+
LL | extern crate bar;
5+
| ^^^^^^^^^^^^^^^^^ can't find crate
6+
7+
error[E0463]: can't find crate for `foo`
8+
--> $DIR/extern-crate-multiple-missing.rs:3:1
9+
|
10+
LL | extern crate foo;
11+
| ^^^^^^^^^^^^^^^^^ can't find crate
12+
13+
error: aborting due to 2 previous errors
14+
15+
For more information about this error, try `rustc --explain E0463`.

src/test/ui/issues/issue-37131.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ error[E0463]: can't find crate for `std`
44
= help: consider downloading the target with `rustup target add thumbv6m-none-eabi`
55
= help: consider building the standard library from source with `cargo build -Zbuild-std`
66

7-
error: aborting due to previous error
7+
error: requires `sized` lang_item
8+
9+
error: aborting due to 2 previous errors
810

911
For more information about this error, try `rustc --explain E0463`.
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
//~ ERROR 1:1: 1:1: can't find crate for `core` [E0463]
1+
//~ ERROR can't find crate for `core`
2+
//~^ ERROR can't find crate for `compiler_builtins`
23

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

910
extern crate cortex_m;
11+
//~^ ERROR can't find crate for `cortex_m`
12+
13+
fn main() {}

src/test/ui/issues/issue-49851/compiler-builtins-error.stderr

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ error[E0463]: can't find crate for `core`
44
= help: consider downloading the target with `rustup target add thumbv7em-none-eabihf`
55
= help: consider building the standard library from source with `cargo build -Zbuild-std`
66

7-
error: aborting due to previous error
7+
error[E0463]: can't find crate for `compiler_builtins`
8+
9+
error[E0463]: can't find crate for `cortex_m`
10+
--> $DIR/compiler-builtins-error.rs:10:1
11+
|
12+
LL | extern crate cortex_m;
13+
| ^^^^^^^^^^^^^^^^^^^^^^ can't find crate
14+
15+
error: requires `sized` lang_item
16+
17+
error: aborting due to 4 previous errors
818

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

0 commit comments

Comments
 (0)