about summary refs log tree commit diff
path: root/compiler/rustc_metadata/src/errors.rs
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2022-10-14 00:45:17 +0200
committerGitHub <noreply@github.com>2022-10-14 00:45:17 +0200
commitc7f048ea00c35dc6d30ba8e63ace96bbc65cc153 (patch)
tree05d11b6549672b176fd0bafbc79b808ec50c7445 /compiler/rustc_metadata/src/errors.rs
parent6b3ede3f7bc502eba7bbd202b4b9312d812adcd7 (diff)
parent097b6d3bafb1d61bfe8fd3f5e9d9acfd9ee2c702 (diff)
downloadrust-c7f048ea00c35dc6d30ba8e63ace96bbc65cc153.tar.gz
rust-c7f048ea00c35dc6d30ba8e63ace96bbc65cc153.zip
Rollup merge of #103000 - wesleywiser:suggest_libname, r=compiler-errors
Add suggestion to the "missing native library" error

If we fail to locate a native library that we are linking with, it could be the case the user entered a complete file name like `foo.lib` or `libfoo.a` when we expect them to simply provide `foo`.

In this situation, we now detect that case and suggest the user only provide the library name itself.
Diffstat (limited to 'compiler/rustc_metadata/src/errors.rs')
-rw-r--r--compiler/rustc_metadata/src/errors.rs36
1 files changed, 35 insertions, 1 deletions
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<SuggestLibraryName<'a>>,
+}
+
+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)]