1
Fork 0

Rollup merge of #91045 - mjptree:issue-90702-fix, r=petrochenkov

Issue 90702 fix: Stop treating some crate loading failures as fatal errors

Surface mulitple `extern crate` resolution errors at a time.

This is achieved by creating a dummy crate, instead of aborting directly after the resolution error. The `ExternCrateError` has been added to allow propagating the resolution error from `rustc_metadata` crate to the `rustc_resolve` with a minimal public surface. The `import_extern_crate` function is a block that was factored out from `build_reduced_graph_for_item` for better organization. The only added functionality made to it where the added error handling in the `process_extern_crate` call. The remaining bits in this function are the same as before.

Resolves #90702

r? `@petrochenkov`
This commit is contained in:
Matthias Krüger 2021-12-02 15:52:01 +01:00 committed by GitHub
commit 87ca333210
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 210 additions and 106 deletions

View file

@ -512,13 +512,17 @@ impl<'a> CrateLoader<'a> {
name: Symbol, name: Symbol,
span: Span, span: Span,
dep_kind: CrateDepKind, dep_kind: CrateDepKind,
) -> CrateNum { ) -> Option<CrateNum> {
self.used_extern_options.insert(name); self.used_extern_options.insert(name);
self.maybe_resolve_crate(name, dep_kind, None).unwrap_or_else(|err| { match self.maybe_resolve_crate(name, dep_kind, None) {
Ok(cnum) => Some(cnum),
Err(err) => {
let missing_core = let missing_core =
self.maybe_resolve_crate(sym::core, CrateDepKind::Explicit, None).is_err(); self.maybe_resolve_crate(sym::core, CrateDepKind::Explicit, None).is_err();
err.report(&self.sess, span, missing_core) err.report(&self.sess, span, missing_core);
}) None
}
}
} }
fn maybe_resolve_crate<'b>( fn maybe_resolve_crate<'b>(
@ -751,7 +755,7 @@ impl<'a> CrateLoader<'a> {
}; };
info!("panic runtime not found -- loading {}", name); info!("panic runtime not found -- loading {}", name);
let cnum = self.resolve_crate(name, DUMMY_SP, CrateDepKind::Implicit); let Some(cnum) = self.resolve_crate(name, DUMMY_SP, CrateDepKind::Implicit) else { return; };
let data = self.cstore.get_crate_data(cnum); let data = self.cstore.get_crate_data(cnum);
// Sanity check the loaded crate to ensure it is indeed a panic runtime // Sanity check the loaded crate to ensure it is indeed a panic runtime
@ -791,7 +795,7 @@ impl<'a> CrateLoader<'a> {
); );
} }
let cnum = self.resolve_crate(name, DUMMY_SP, CrateDepKind::Implicit); let Some(cnum) = self.resolve_crate(name, DUMMY_SP, CrateDepKind::Implicit) else { return; };
let data = self.cstore.get_crate_data(cnum); let data = self.cstore.get_crate_data(cnum);
// Sanity check the loaded crate to ensure it is indeed a profiler runtime // Sanity check the loaded crate to ensure it is indeed a profiler runtime
@ -991,7 +995,7 @@ impl<'a> CrateLoader<'a> {
item: &ast::Item, item: &ast::Item,
definitions: &Definitions, definitions: &Definitions,
def_id: LocalDefId, def_id: LocalDefId,
) -> CrateNum { ) -> Option<CrateNum> {
match item.kind { match item.kind {
ast::ItemKind::ExternCrate(orig_name) => { ast::ItemKind::ExternCrate(orig_name) => {
debug!( debug!(
@ -1011,7 +1015,7 @@ impl<'a> CrateLoader<'a> {
CrateDepKind::Explicit 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(); let path_len = definitions.def_path(def_id).data.len();
self.update_extern_crate( self.update_extern_crate(
@ -1023,14 +1027,14 @@ impl<'a> CrateLoader<'a> {
dependency_of: LOCAL_CRATE, dependency_of: LOCAL_CRATE,
}, },
); );
cnum Some(cnum)
} }
_ => bug!(), _ => bug!(),
} }
} }
pub fn process_path_extern(&mut self, name: Symbol, span: Span) -> CrateNum { pub fn process_path_extern(&mut self, name: Symbol, span: Span) -> Option<CrateNum> {
let cnum = self.resolve_crate(name, span, CrateDepKind::Explicit); let cnum = self.resolve_crate(name, span, CrateDepKind::Explicit)?;
self.update_extern_crate( self.update_extern_crate(
cnum, cnum,
@ -1043,7 +1047,7 @@ impl<'a> CrateLoader<'a> {
}, },
); );
cnum Some(cnum)
} }
pub fn maybe_process_path_extern(&mut self, name: Symbol) -> Option<CrateNum> { pub fn maybe_process_path_extern(&mut self, name: Symbol) -> Option<CrateNum> {

View file

@ -220,7 +220,7 @@ use rustc_data_structures::memmap::Mmap;
use rustc_data_structures::owning_ref::OwningRef; use rustc_data_structures::owning_ref::OwningRef;
use rustc_data_structures::svh::Svh; use rustc_data_structures::svh::Svh;
use rustc_data_structures::sync::MetadataRef; use rustc_data_structures::sync::MetadataRef;
use rustc_errors::struct_span_err; use rustc_errors::{struct_span_err, FatalError};
use rustc_session::config::{self, CrateType}; use rustc_session::config::{self, CrateType};
use rustc_session::cstore::{CrateSource, MetadataLoader}; use rustc_session::cstore::{CrateSource, MetadataLoader};
use rustc_session::filesearch::{FileDoesntMatch, FileMatches, FileSearch}; use rustc_session::filesearch::{FileDoesntMatch, FileMatches, FileSearch};
@ -814,11 +814,11 @@ pub fn find_plugin_registrar(
span: Span, span: Span,
name: Symbol, name: Symbol,
) -> PathBuf { ) -> PathBuf {
match find_plugin_registrar_impl(sess, metadata_loader, name) { find_plugin_registrar_impl(sess, metadata_loader, name).unwrap_or_else(|err| {
Ok(res) => res,
// `core` is always available if we got as far as loading plugins. // `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>( fn find_plugin_registrar_impl<'a>(
@ -931,8 +931,8 @@ impl fmt::Display for MetadataError<'_> {
} }
impl CrateError { impl CrateError {
crate fn report(self, sess: &Session, span: Span, missing_core: bool) -> ! { crate fn report(self, sess: &Session, span: Span, missing_core: bool) {
let mut err = match self { let mut diag = match self {
CrateError::NonAsciiName(crate_name) => sess.struct_span_err( CrateError::NonAsciiName(crate_name) => sess.struct_span_err(
span, span,
&format!("cannot load a crate with a non-ascii name `{}`", crate_name), &format!("cannot load a crate with a non-ascii name `{}`", crate_name),
@ -1210,8 +1210,6 @@ impl CrateError {
), ),
}; };
err.emit(); diag.emit();
sess.abort_if_errors();
unreachable!();
} }
} }

View file

@ -678,75 +678,13 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
} }
ItemKind::ExternCrate(orig_name) => { ItemKind::ExternCrate(orig_name) => {
let module = if orig_name.is_none() && ident.name == kw::SelfLower { self.build_reduced_graph_for_extern_crate(
self.r orig_name,
.session
.struct_span_err(item.span, "`extern crate self;` requires renaming")
.span_suggestion(
item.span,
"try",
"extern crate self as name;".into(),
Applicability::HasPlaceholders,
)
.emit();
return;
} else if orig_name == Some(kw::SelfLower) {
self.r.graph_root
} else {
let crate_id = self.r.crate_loader.process_extern_crate(
item, item,
&self.r.definitions,
local_def_id, local_def_id,
vis,
parent,
); );
self.r.extern_crate_map.insert(local_def_id, crate_id);
self.r.expect_module(crate_id.as_def_id())
};
let used = self.process_macro_use_imports(item, module);
let binding =
(module, ty::Visibility::Public, sp, expansion).to_name_binding(self.r.arenas);
let 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(Some(ModuleOrUniformRoot::Module(module))),
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(used),
});
self.r.potentially_unused_imports.push(import);
let imported_binding = self.r.import(binding, import);
if ptr::eq(parent, self.r.graph_root) {
if let Some(entry) = self.r.extern_prelude.get(&ident.normalize_to_macros_2_0())
{
if expansion != LocalExpnId::ROOT
&& orig_name.is_some()
&& entry.extern_crate_item.is_none()
{
let msg = "macro-expanded `extern crate` items cannot \
shadow names passed with `--extern`";
self.r.session.span_err(item.span, msg);
}
}
let entry =
self.r.extern_prelude.entry(ident.normalize_to_macros_2_0()).or_insert(
ExternPreludeEntry {
extern_crate_item: None,
introduced_by_item: true,
},
);
entry.extern_crate_item = Some(imported_binding);
if orig_name.is_some() {
entry.introduced_by_item = true;
}
}
self.r.define(parent, ident, TypeNS, imported_binding);
} }
ItemKind::Mod(..) => { ItemKind::Mod(..) => {
@ -884,6 +822,87 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
} }
} }
fn build_reduced_graph_for_extern_crate(
&mut self,
orig_name: Option<Symbol>,
item: &Item,
local_def_id: LocalDefId,
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 (used, module, binding) = if orig_name.is_none() && ident.name == kw::SelfLower {
self.r
.session
.struct_span_err(item.span, "`extern crate self;` requires renaming")
.span_suggestion(
item.span,
"rename the `self` crate to be able to import it",
"extern crate self as name;".into(),
Applicability::HasPlaceholders,
)
.emit();
return;
} else if orig_name == Some(kw::SelfLower) {
Some(self.r.graph_root)
} else {
self.r.crate_loader.process_extern_crate(item, &self.r.definitions, local_def_id).map(
|crate_id| {
self.r.extern_crate_map.insert(local_def_id, crate_id);
self.r.expect_module(crate_id.as_def_id())
},
)
}
.map(|module| {
let used = self.process_macro_use_imports(item, module);
let binding =
(module, ty::Visibility::Public, sp, expansion).to_name_binding(self.r.arenas);
(used, Some(ModuleOrUniformRoot::Module(module)), binding)
})
.unwrap_or((true, None, self.r.dummy_binding));
let 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(module),
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(used),
});
self.r.potentially_unused_imports.push(import);
let imported_binding = self.r.import(binding, import);
if ptr::eq(parent, self.r.graph_root) {
if let Some(entry) = self.r.extern_prelude.get(&ident.normalize_to_macros_2_0()) {
if expansion != LocalExpnId::ROOT
&& orig_name.is_some()
&& entry.extern_crate_item.is_none()
{
let msg = "macro-expanded `extern crate` items cannot \
shadow names passed with `--extern`";
self.r.session.span_err(item.span, msg);
}
}
let entry = self.r.extern_prelude.entry(ident.normalize_to_macros_2_0()).or_insert(
ExternPreludeEntry { extern_crate_item: None, introduced_by_item: true },
);
entry.extern_crate_item = Some(imported_binding);
if orig_name.is_some() {
entry.introduced_by_item = true;
}
}
self.r.define(parent, ident, TypeNS, imported_binding);
}
/// Constructs the reduced graph for one foreign item. /// Constructs the reduced graph for one foreign item.
fn build_reduced_graph_for_foreign_item(&mut self, item: &ForeignItem) { fn build_reduced_graph_for_foreign_item(&mut self, item: &ForeignItem) {
let local_def_id = self.r.local_def_id(item.id); let local_def_id = self.r.local_def_id(item.id);

View file

@ -3285,7 +3285,9 @@ impl<'a> Resolver<'a> {
Some(binding) Some(binding)
} else { } else {
let crate_id = if !speculative { let crate_id = if !speculative {
self.crate_loader.process_path_extern(ident.name, ident.span) let Some(crate_id) =
self.crate_loader.process_path_extern(ident.name, ident.span) else { return Some(self.dummy_binding); };
crate_id
} else { } else {
self.crate_loader.maybe_process_path_extern(ident.name)? self.crate_loader.maybe_process_path_extern(ident.name)?
}; };

View file

@ -6,3 +6,5 @@
#![no_std] #![no_std]
use ::foo; //~ ERROR invalid metadata files for crate `foo` use ::foo; //~ ERROR invalid metadata files for crate `foo`
//~| NOTE failed to mmap file //~| NOTE failed to mmap file
//~^^ ERROR invalid metadata files for crate `foo`
//~| NOTE failed to mmap file

View file

@ -6,6 +6,14 @@ LL | use ::foo;
| |
= note: failed to mmap file 'auxiliary/libfoo.rlib' = note: failed to mmap file 'auxiliary/libfoo.rlib'
error: aborting due to previous error error[E0786]: found invalid metadata files for crate `foo`
--> $DIR/invalid-rlib.rs:7:7
|
LL | use ::foo;
| ^^^
|
= note: failed to mmap file 'auxiliary/libfoo.rlib'
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0786`. For more information about this error, try `rustc --explain E0786`.

View file

@ -1,6 +1,7 @@
// compile-flags: --target x86_64-unknown-uefi // compile-flags: --target x86_64-unknown-uefi
// needs-llvm-components: x86 // needs-llvm-components: x86
// rustc-env:CARGO=/usr/bin/cargo // rustc-env:CARGO=/usr/bin/cargo
#![feature(no_core)]
#![no_core] #![no_core]
extern crate core; extern crate core;
//~^ ERROR can't find crate for `core` //~^ ERROR can't find crate for `core`

View file

@ -1,5 +1,5 @@
error[E0463]: can't find crate for `core` error[E0463]: can't find crate for `core`
--> $DIR/missing-std.rs:5:1 --> $DIR/missing-std.rs:6:1
| |
LL | extern crate core; LL | extern crate core;
| ^^^^^^^^^^^^^^^^^^ can't find crate | ^^^^^^^^^^^^^^^^^^ can't find crate
@ -8,6 +8,8 @@ LL | extern crate core;
= help: consider downloading the target with `rustup target add x86_64-unknown-uefi` = 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` = 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`. For more information about this error, try `rustc --explain E0463`.

View file

@ -1,4 +1,6 @@
// compile-flags: --extern std= // compile-flags: --extern std=
// error-pattern: extern location for std does not exist // error-pattern: extern location for std does not exist
// needs-unwind since it affects the error output
// ignore-emscripten compiled with panic=abort, personality not required
fn main() {} fn main() {}

View file

@ -1,4 +1,8 @@
error: extern location for std does not exist: 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

View file

@ -0,0 +1,10 @@
// 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() {
// If the crate name introduced by `extern crate` failed to resolve then subsequent
// derived paths do not emit additional errors
foo::something();
bar::something();
}

View file

@ -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`.

View file

@ -2,7 +2,12 @@ error: `extern crate self;` requires renaming
--> $DIR/extern-crate-self-fail.rs:1:1 --> $DIR/extern-crate-self-fail.rs:1:1
| |
LL | extern crate self; LL | extern crate self;
| ^^^^^^^^^^^^^^^^^^ help: try: `extern crate self as name;` | ^^^^^^^^^^^^^^^^^^
|
help: rename the `self` crate to be able to import it
|
LL | extern crate self as name;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~
error: `#[macro_use]` is not supported on `extern crate self` error: `#[macro_use]` is not supported on `extern crate self`
--> $DIR/extern-crate-self-fail.rs:3:1 --> $DIR/extern-crate-self-fail.rs:3:1

View file

@ -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 downloading the target with `rustup target add thumbv6m-none-eabi`
= help: consider building the standard library from source with `cargo build -Zbuild-std` = 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`. For more information about this error, try `rustc --explain E0463`.

View file

@ -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 // compile-flags: --target thumbv7em-none-eabihf
// needs-llvm-components: arm // needs-llvm-components: arm
@ -7,3 +8,6 @@
#![no_std] #![no_std]
extern crate cortex_m; extern crate cortex_m;
//~^ ERROR can't find crate for `cortex_m`
fn main() {}

View file

@ -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 downloading the target with `rustup target add thumbv7em-none-eabihf`
= help: consider building the standard library from source with `cargo build -Zbuild-std` = 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`. For more information about this error, try `rustc --explain E0463`.

View file

@ -1,7 +1,8 @@
// edition:2018 // edition:2018
// compile-flags:--extern foo --extern bar // compile-flags:--extern foo --extern bar
use bar::foo; //~ ERROR can't find crate for `bar`
use foo::bar; //~ ERROR can't find crate for `foo` use foo::bar; //~ ERROR can't find crate for `foo`
use bar::foo; //~^^ ERROR unresolved imports `bar::foo`, `foo::bar`
fn main() {} fn main() {}

View file

@ -1,9 +1,24 @@
error[E0463]: can't find crate for `foo` error[E0463]: can't find crate for `bar`
--> $DIR/deadlock.rs:4:5 --> $DIR/deadlock.rs:4:5
| |
LL | use bar::foo;
| ^^^ can't find crate
error[E0463]: can't find crate for `foo`
--> $DIR/deadlock.rs:5:5
|
LL | use foo::bar; LL | use foo::bar;
| ^^^ can't find crate | ^^^ can't find crate
error: aborting due to previous error error[E0432]: unresolved imports `bar::foo`, `foo::bar`
--> $DIR/deadlock.rs:4:5
|
LL | use bar::foo;
| ^^^^^^^^
LL | use foo::bar;
| ^^^^^^^^
For more information about this error, try `rustc --explain E0463`. error: aborting due to 3 previous errors
Some errors have detailed explanations: E0432, E0463.
For more information about an error, try `rustc --explain E0432`.