Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add suggestion to the "missing native library" error #103000

Merged
merged 1 commit into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/rustc_error_messages/locales/en-US/metadata.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down
36 changes: 35 additions & 1 deletion compiler/rustc_metadata/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_metadata/src/native_libs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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() { }
Original file line number Diff line number Diff line change
@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a shame we don't have span information here to point out the link attr or ideally the "libfoo.a".


error: aborting due to previous error

Original file line number Diff line number Diff line change
@@ -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() { }
Original file line number Diff line number Diff line change
@@ -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