diff --git a/compiler/rustc_metadata/src/creader.rs b/compiler/rustc_metadata/src/creader.rs index 345ee185156ca..3c8ea1a9f43d4 100644 --- a/compiler/rustc_metadata/src/creader.rs +++ b/compiler/rustc_metadata/src/creader.rs @@ -77,6 +77,9 @@ pub struct CStore { unused_externs: Vec, used_extern_options: FxHashSet, + /// Whether there was a failure in resolving crate, + /// it's used to suppress some diagnostics that would otherwise too noisey. + has_crate_resolve_with_fail: bool, } impl std::fmt::Debug for CStore { @@ -328,6 +331,10 @@ impl CStore { self.has_alloc_error_handler } + pub fn had_extern_crate_load_failure(&self) -> bool { + self.has_crate_resolve_with_fail + } + pub fn report_unused_deps(&self, tcx: TyCtxt<'_>) { let json_unused_externs = tcx.sess.opts.json_unused_externs; @@ -514,6 +521,7 @@ impl CStore { resolved_externs: UnordMap::default(), unused_externs: Vec::new(), used_extern_options: Default::default(), + has_crate_resolve_with_fail: false, } } @@ -723,6 +731,13 @@ impl CStore { } Err(err) => { debug!("failed to resolve crate {} {:?}", name, dep_kind); + // crate maybe injrected with `standard_library_imports::inject`, their span is dummy. + // we ignore compiler-injected prelude/sysroot loads here so they don't suppress + // unrelated diagnostics, such as `unsupported targets for std library` etc, + // these maybe helpful for users to resolve crate loading failure. + if !tcx.sess.dcx().has_errors().is_some() && !span.is_dummy() { + self.has_crate_resolve_with_fail = true; + } let missing_core = self .maybe_resolve_crate( tcx, diff --git a/compiler/rustc_resolve/src/imports.rs b/compiler/rustc_resolve/src/imports.rs index 6507ee3477379..d51ce9fb7946b 100644 --- a/compiler/rustc_resolve/src/imports.rs +++ b/compiler/rustc_resolve/src/imports.rs @@ -674,6 +674,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { } } + if self.cstore().had_extern_crate_load_failure() { + self.tcx.sess.dcx().abort_if_errors(); + } + if !errors.is_empty() { self.throw_unresolved_import_error(errors, glob_error); return; diff --git a/tests/ui/crate-loading/missing-extern-crate-cascade-issue-154096.rs b/tests/ui/crate-loading/missing-extern-crate-cascade-issue-154096.rs new file mode 100644 index 0000000000000..5bbaa1980ee67 --- /dev/null +++ b/tests/ui/crate-loading/missing-extern-crate-cascade-issue-154096.rs @@ -0,0 +1,24 @@ +//@ edition: 2024 + +extern crate missing_154096; +//~^ ERROR can't find crate for `missing_154096` + +mod defs { + pub use missing_154096::Thing; +} + +pub use defs::Thing; + +mod first { + use crate::Thing; + + pub fn take(_: Thing) {} +} + +mod second { + use crate::Thing; + + pub fn take(_: Thing) {} +} + +fn main() {} diff --git a/tests/ui/crate-loading/missing-extern-crate-cascade-issue-154096.stderr b/tests/ui/crate-loading/missing-extern-crate-cascade-issue-154096.stderr new file mode 100644 index 0000000000000..4deac6dbcda39 --- /dev/null +++ b/tests/ui/crate-loading/missing-extern-crate-cascade-issue-154096.stderr @@ -0,0 +1,9 @@ +error[E0463]: can't find crate for `missing_154096` + --> $DIR/missing-extern-crate-cascade-issue-154096.rs:3:1 + | +LL | extern crate missing_154096; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't find crate + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0463`. diff --git a/tests/ui/crate-loading/multiple-candidates-cascade-issue-118130.rs b/tests/ui/crate-loading/multiple-candidates-cascade-issue-118130.rs new file mode 100644 index 0000000000000..1b3814a5e543e --- /dev/null +++ b/tests/ui/crate-loading/multiple-candidates-cascade-issue-118130.rs @@ -0,0 +1,20 @@ +//@ aux-build:crateresolve1-1.rs +//@ aux-build:crateresolve1-2.rs +//@ aux-build:crateresolve1-3.rs + +//@ normalize-stderr: "multiple-candidates-cascade-issue-118130\..+/auxiliary/" -> "multiple-candidates-cascade-issue-118130/auxiliary/" +//@ normalize-stderr: "\\\?\\" -> "" +//@ normalize-stderr: "(lib)?crateresolve1-([123])\.[a-z]+" -> "libcrateresolve1-$2.somelib" + +extern crate crateresolve1; +//~^ ERROR multiple candidates for `rlib` dependency `crateresolve1` found + +mod defs { + pub use crateresolve1::f; +} + +pub use defs::f; + +fn main() { + let _ = f(); +} diff --git a/tests/ui/crate-loading/multiple-candidates-cascade-issue-118130.stderr b/tests/ui/crate-loading/multiple-candidates-cascade-issue-118130.stderr new file mode 100644 index 0000000000000..1bd31b924f7f5 --- /dev/null +++ b/tests/ui/crate-loading/multiple-candidates-cascade-issue-118130.stderr @@ -0,0 +1,13 @@ +error[E0464]: multiple candidates for `rlib` dependency `crateresolve1` found + --> $DIR/multiple-candidates-cascade-issue-118130.rs:9:1 + | +LL | extern crate crateresolve1; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: candidate #1: $TEST_BUILD_DIR/auxiliary/libcrateresolve1-1.somelib + = note: candidate #2: $TEST_BUILD_DIR/auxiliary/libcrateresolve1-2.somelib + = note: candidate #3: $TEST_BUILD_DIR/auxiliary/libcrateresolve1-3.somelib + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0464`. diff --git a/tests/ui/rust-2018/uniform-paths/deadlock.rs b/tests/ui/rust-2018/uniform-paths/deadlock.rs index d2296c51bdd39..4fab9e9219fab 100644 --- a/tests/ui/rust-2018/uniform-paths/deadlock.rs +++ b/tests/ui/rust-2018/uniform-paths/deadlock.rs @@ -3,6 +3,5 @@ use bar::foo; //~ ERROR can't find crate for `bar` use foo::bar; -//~^^ ERROR unresolved imports `bar::foo`, `foo::bar` fn main() {} diff --git a/tests/ui/rust-2018/uniform-paths/deadlock.stderr b/tests/ui/rust-2018/uniform-paths/deadlock.stderr index c50bc16ac55f3..c3050d6749287 100644 --- a/tests/ui/rust-2018/uniform-paths/deadlock.stderr +++ b/tests/ui/rust-2018/uniform-paths/deadlock.stderr @@ -4,15 +4,6 @@ error[E0463]: can't find crate for `bar` LL | use bar::foo; | ^^^ can't find crate -error[E0432]: unresolved imports `bar::foo`, `foo::bar` - --> $DIR/deadlock.rs:4:5 - | -LL | use bar::foo; - | ^^^^^^^^ -LL | use foo::bar; - | ^^^^^^^^ - -error: aborting due to 2 previous errors +error: aborting due to 1 previous error -Some errors have detailed explanations: E0432, E0463. -For more information about an error, try `rustc --explain E0432`. +For more information about this error, try `rustc --explain E0463`.