Skip to content

Commit 34c8a39

Browse files
authored
Rollup merge of rust-lang#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.
2 parents 6b3ede3 + 097b6d3 commit 34c8a39

File tree

7 files changed

+68
-2
lines changed

7 files changed

+68
-2
lines changed

compiler/rustc_error_messages/locales/en-US/metadata.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ metadata_failed_write_error =
165165
metadata_missing_native_library =
166166
could not find native static library `{$libname}`, perhaps an -L flag is missing?
167167
168+
metadata_only_provide_library_name = only provide the library name `{$suggested_name}`, not the full filename
169+
168170
metadata_failed_create_tempdir =
169171
couldn't create a temp dir: {$err}
170172

compiler/rustc_metadata/src/errors.rs

+35-1
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,41 @@ pub struct FailedWriteError {
372372
#[derive(Diagnostic)]
373373
#[diag(metadata::missing_native_library)]
374374
pub struct MissingNativeLibrary<'a> {
375-
pub libname: &'a str,
375+
libname: &'a str,
376+
#[subdiagnostic]
377+
suggest_name: Option<SuggestLibraryName<'a>>,
378+
}
379+
380+
impl<'a> MissingNativeLibrary<'a> {
381+
pub fn new(libname: &'a str, verbatim: bool) -> Self {
382+
// if it looks like the user has provided a complete filename rather just the bare lib name,
383+
// then provide a note that they might want to try trimming the name
384+
let suggested_name = if !verbatim {
385+
if let Some(libname) = libname.strip_prefix("lib") && let Some(libname) = libname.strip_suffix(".a") {
386+
// this is a unix style filename so trim prefix & suffix
387+
Some(libname)
388+
} else if let Some(libname) = libname.strip_suffix(".lib") {
389+
// this is a Windows style filename so just trim the suffix
390+
Some(libname)
391+
} else {
392+
None
393+
}
394+
} else {
395+
None
396+
};
397+
398+
Self {
399+
libname,
400+
suggest_name: suggested_name
401+
.map(|suggested_name| SuggestLibraryName { suggested_name }),
402+
}
403+
}
404+
}
405+
406+
#[derive(Subdiagnostic)]
407+
#[help(metadata::only_provide_library_name)]
408+
pub struct SuggestLibraryName<'a> {
409+
suggested_name: &'a str,
376410
}
377411

378412
#[derive(Diagnostic)]

compiler/rustc_metadata/src/native_libs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub fn find_native_static_library(
5252
}
5353
}
5454

55-
sess.emit_fatal(MissingNativeLibrary { libname: name });
55+
sess.emit_fatal(MissingNativeLibrary::new(name, verbatim.unwrap_or(false)));
5656
}
5757

5858
fn find_bundled_library(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// build-fail
2+
// compile-flags: --crate-type rlib
3+
// error-pattern: could not find native static library `libfoo.a`
4+
// error-pattern: only provide the library name `foo`, not the full filename
5+
6+
#[link(name = "libfoo.a", kind = "static")]
7+
extern { }
8+
9+
pub fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
error: could not find native static library `libfoo.a`, perhaps an -L flag is missing?
2+
|
3+
= help: only provide the library name `foo`, not the full filename
4+
5+
error: aborting due to previous error
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// build-fail
2+
// compile-flags: --crate-type rlib
3+
// error-pattern: could not find native static library `bar.lib`
4+
// error-pattern: only provide the library name `bar`, not the full filename
5+
6+
#[link(name = "bar.lib", kind = "static")]
7+
extern { }
8+
9+
pub fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
error: could not find native static library `bar.lib`, perhaps an -L flag is missing?
2+
|
3+
= help: only provide the library name `bar`, not the full filename
4+
5+
error: aborting due to previous error
6+

0 commit comments

Comments
 (0)