diff --git a/compiler/rustc_error_messages/locales/en-US/metadata.ftl b/compiler/rustc_error_messages/locales/en-US/metadata.ftl index d27100c56af..08e553d9f15 100644 --- a/compiler/rustc_error_messages/locales/en-US/metadata.ftl +++ b/compiler/rustc_error_messages/locales/en-US/metadata.ftl @@ -165,6 +165,8 @@ metadata_failed_write_error = metadata_missing_native_library = could not find native static library `{$libname}`, perhaps an -L flag is missing? +metadata_only_provide_library_name = only provide the library name `{$suggested_name}`, not the full filename + metadata_failed_create_tempdir = couldn't create a temp dir: {$err} diff --git a/compiler/rustc_metadata/src/errors.rs b/compiler/rustc_metadata/src/errors.rs index 1cd550644bf..dbfa22aaff0 100644 --- a/compiler/rustc_metadata/src/errors.rs +++ b/compiler/rustc_metadata/src/errors.rs @@ -372,7 +372,41 @@ pub struct FailedWriteError { #[derive(Diagnostic)] #[diag(metadata::missing_native_library)] pub struct MissingNativeLibrary<'a> { - pub libname: &'a str, + libname: &'a str, + #[subdiagnostic] + suggest_name: Option>, +} + +impl<'a> MissingNativeLibrary<'a> { + pub fn new(libname: &'a str, verbatim: bool) -> Self { + // if it looks like the user has provided a complete filename rather just the bare lib name, + // then provide a note that they might want to try trimming the name + let suggested_name = if !verbatim { + if let Some(libname) = libname.strip_prefix("lib") && let Some(libname) = libname.strip_suffix(".a") { + // this is a unix style filename so trim prefix & suffix + Some(libname) + } else if let Some(libname) = libname.strip_suffix(".lib") { + // this is a Windows style filename so just trim the suffix + Some(libname) + } else { + None + } + } else { + None + }; + + Self { + libname, + suggest_name: suggested_name + .map(|suggested_name| SuggestLibraryName { suggested_name }), + } + } +} + +#[derive(Subdiagnostic)] +#[help(metadata::only_provide_library_name)] +pub struct SuggestLibraryName<'a> { + suggested_name: &'a str, } #[derive(Diagnostic)] diff --git a/compiler/rustc_metadata/src/native_libs.rs b/compiler/rustc_metadata/src/native_libs.rs index 9abb5c74895..676c67bad82 100644 --- a/compiler/rustc_metadata/src/native_libs.rs +++ b/compiler/rustc_metadata/src/native_libs.rs @@ -52,7 +52,7 @@ pub fn find_native_static_library( } } - sess.emit_fatal(MissingNativeLibrary { libname: name }); + sess.emit_fatal(MissingNativeLibrary::new(name, verbatim.unwrap_or(false))); } fn find_bundled_library( diff --git a/src/test/ui/native-library-link-flags/suggest-libname-only-1.rs b/src/test/ui/native-library-link-flags/suggest-libname-only-1.rs new file mode 100644 index 00000000000..abf988a7c1e --- /dev/null +++ b/src/test/ui/native-library-link-flags/suggest-libname-only-1.rs @@ -0,0 +1,9 @@ +// build-fail +// compile-flags: --crate-type rlib +// error-pattern: could not find native static library `libfoo.a` +// error-pattern: only provide the library name `foo`, not the full filename + +#[link(name = "libfoo.a", kind = "static")] +extern { } + +pub fn main() { } diff --git a/src/test/ui/native-library-link-flags/suggest-libname-only-1.stderr b/src/test/ui/native-library-link-flags/suggest-libname-only-1.stderr new file mode 100644 index 00000000000..64d0a9077ed --- /dev/null +++ b/src/test/ui/native-library-link-flags/suggest-libname-only-1.stderr @@ -0,0 +1,6 @@ +error: could not find native static library `libfoo.a`, perhaps an -L flag is missing? + | + = help: only provide the library name `foo`, not the full filename + +error: aborting due to previous error + diff --git a/src/test/ui/native-library-link-flags/suggest-libname-only-2.rs b/src/test/ui/native-library-link-flags/suggest-libname-only-2.rs new file mode 100644 index 00000000000..dfa70e56db7 --- /dev/null +++ b/src/test/ui/native-library-link-flags/suggest-libname-only-2.rs @@ -0,0 +1,9 @@ +// build-fail +// compile-flags: --crate-type rlib +// error-pattern: could not find native static library `bar.lib` +// error-pattern: only provide the library name `bar`, not the full filename + +#[link(name = "bar.lib", kind = "static")] +extern { } + +pub fn main() { } diff --git a/src/test/ui/native-library-link-flags/suggest-libname-only-2.stderr b/src/test/ui/native-library-link-flags/suggest-libname-only-2.stderr new file mode 100644 index 00000000000..e166af9ed8f --- /dev/null +++ b/src/test/ui/native-library-link-flags/suggest-libname-only-2.stderr @@ -0,0 +1,6 @@ +error: could not find native static library `bar.lib`, perhaps an -L flag is missing? + | + = help: only provide the library name `bar`, not the full filename + +error: aborting due to previous error +