summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmilio Cobos Álvarez <emilio@crisal.io>2021-04-03 12:30:43 +0200
committerEmilio Cobos Álvarez <emilio@crisal.io>2021-04-03 13:05:04 +0200
commitb21086cc3ba5aaeea53082e0618733a1b354daab (patch)
treeb110e0b04c1023032077003fc45c50a94d1a48bf
parent2605da86c8cd7930f0c0d5f1025e33855a582bc0 (diff)
codegen: Don't use a `sym` temporary in dynamic library code.
Fixes #2014.
-rw-r--r--src/codegen/dyngen.rs14
-rw-r--r--tests/expectations/tests/dynamic_loading_required.rs10
-rw-r--r--tests/expectations/tests/dynamic_loading_simple.rs13
-rw-r--r--tests/expectations/tests/dynamic_loading_template.rs10
-rw-r--r--tests/expectations/tests/dynamic_loading_with_allowlist.rs10
-rw-r--r--tests/expectations/tests/dynamic_loading_with_blocklist.rs10
-rw-r--r--tests/expectations/tests/dynamic_loading_with_class.rs10
7 files changed, 38 insertions, 39 deletions
diff --git a/src/codegen/dyngen.rs b/src/codegen/dyngen.rs
index 5bb8747c..71c4dab1 100644
--- a/src/codegen/dyngen.rs
+++ b/src/codegen/dyngen.rs
@@ -140,15 +140,13 @@ impl DynamicItems {
// N.B: If the signature was required, it won't be wrapped in a Result<...>
// and we can simply call it directly.
- let call_body = if is_required {
- quote! {
- self.#ident(#( #args_identifiers ),*)
- }
+ let fn_ = if is_required {
+ quote! { self.#ident }
} else {
- quote! {
- let sym = self.#ident.as_ref().expect("Expected function, got error.");
- (sym)(#( #args_identifiers ),*)
- }
+ quote! { self.#ident.as_ref().expect("Expected function, got error.") }
+ };
+ let call_body = quote! {
+ (#fn_)(#( #args_identifiers ),*)
};
// We can't implement variadic functions from C easily, so we allow to
diff --git a/tests/expectations/tests/dynamic_loading_required.rs b/tests/expectations/tests/dynamic_loading_required.rs
index 3600eace..e46ffd49 100644
--- a/tests/expectations/tests/dynamic_loading_required.rs
+++ b/tests/expectations/tests/dynamic_loading_required.rs
@@ -25,7 +25,9 @@ impl TestLib {
let library = ::libloading::Library::new(path)?;
Self::from_library(library)
}
- pub unsafe fn from_library<L>(library: L) -> Result<Self, ::libloading::Error>
+ pub unsafe fn from_library<L>(
+ library: L,
+ ) -> Result<Self, ::libloading::Error>
where
L: Into<::libloading::Library>,
{
@@ -45,15 +47,15 @@ impl TestLib {
x: ::std::os::raw::c_int,
y: ::std::os::raw::c_int,
) -> ::std::os::raw::c_int {
- self.foo(x, y)
+ (self.foo)(x, y)
}
pub unsafe fn bar(
&self,
x: *mut ::std::os::raw::c_void,
) -> ::std::os::raw::c_int {
- self.bar(x)
+ (self.bar)(x)
}
pub unsafe fn baz(&self) -> ::std::os::raw::c_int {
- self.baz()
+ (self.baz)()
}
}
diff --git a/tests/expectations/tests/dynamic_loading_simple.rs b/tests/expectations/tests/dynamic_loading_simple.rs
index 3d2e3d2c..cae5bd69 100644
--- a/tests/expectations/tests/dynamic_loading_simple.rs
+++ b/tests/expectations/tests/dynamic_loading_simple.rs
@@ -34,7 +34,9 @@ impl TestLib {
let library = ::libloading::Library::new(path)?;
Self::from_library(library)
}
- pub unsafe fn from_library<L>(library: L) -> Result<Self, ::libloading::Error>
+ pub unsafe fn from_library<L>(
+ library: L,
+ ) -> Result<Self, ::libloading::Error>
where
L: Into<::libloading::Library>,
{
@@ -54,18 +56,15 @@ impl TestLib {
x: ::std::os::raw::c_int,
y: ::std::os::raw::c_int,
) -> ::std::os::raw::c_int {
- let sym = self.foo.as_ref().expect("Expected function, got error.");
- (sym)(x, y)
+ (self.foo.as_ref().expect("Expected function, got error."))(x, y)
}
pub unsafe fn bar(
&self,
x: *mut ::std::os::raw::c_void,
) -> ::std::os::raw::c_int {
- let sym = self.bar.as_ref().expect("Expected function, got error.");
- (sym)(x)
+ (self.bar.as_ref().expect("Expected function, got error."))(x)
}
pub unsafe fn baz(&self) -> ::std::os::raw::c_int {
- let sym = self.baz.as_ref().expect("Expected function, got error.");
- (sym)()
+ (self.baz.as_ref().expect("Expected function, got error."))()
}
}
diff --git a/tests/expectations/tests/dynamic_loading_template.rs b/tests/expectations/tests/dynamic_loading_template.rs
index de231434..06e67ed7 100644
--- a/tests/expectations/tests/dynamic_loading_template.rs
+++ b/tests/expectations/tests/dynamic_loading_template.rs
@@ -22,7 +22,9 @@ impl TestLib {
let library = ::libloading::Library::new(path)?;
Self::from_library(library)
}
- pub unsafe fn from_library<L>(library: L) -> Result<Self, ::libloading::Error>
+ pub unsafe fn from_library<L>(
+ library: L,
+ ) -> Result<Self, ::libloading::Error>
where
L: Into<::libloading::Library>,
{
@@ -39,11 +41,9 @@ impl TestLib {
&self,
x: ::std::os::raw::c_int,
) -> ::std::os::raw::c_int {
- let sym = self.foo.as_ref().expect("Expected function, got error.");
- (sym)(x)
+ (self.foo.as_ref().expect("Expected function, got error."))(x)
}
pub unsafe fn foo1(&self, x: f32) -> f32 {
- let sym = self.foo1.as_ref().expect("Expected function, got error.");
- (sym)(x)
+ (self.foo1.as_ref().expect("Expected function, got error."))(x)
}
}
diff --git a/tests/expectations/tests/dynamic_loading_with_allowlist.rs b/tests/expectations/tests/dynamic_loading_with_allowlist.rs
index 83e4a541..97bb67ab 100644
--- a/tests/expectations/tests/dynamic_loading_with_allowlist.rs
+++ b/tests/expectations/tests/dynamic_loading_with_allowlist.rs
@@ -36,7 +36,9 @@ impl TestLib {
let library = ::libloading::Library::new(path)?;
Self::from_library(library)
}
- pub unsafe fn from_library<L>(library: L) -> Result<Self, ::libloading::Error>
+ pub unsafe fn from_library<L>(
+ library: L,
+ ) -> Result<Self, ::libloading::Error>
where
L: Into<::libloading::Library>,
{
@@ -55,14 +57,12 @@ impl TestLib {
&self,
x: *mut ::std::os::raw::c_void,
) -> ::std::os::raw::c_int {
- let sym = self.foo.as_ref().expect("Expected function, got error.");
- (sym)(x)
+ (self.foo.as_ref().expect("Expected function, got error."))(x)
}
pub unsafe fn baz(
&self,
x: *mut ::std::os::raw::c_void,
) -> ::std::os::raw::c_int {
- let sym = self.baz.as_ref().expect("Expected function, got error.");
- (sym)(x)
+ (self.baz.as_ref().expect("Expected function, got error."))(x)
}
}
diff --git a/tests/expectations/tests/dynamic_loading_with_blocklist.rs b/tests/expectations/tests/dynamic_loading_with_blocklist.rs
index 598780b2..b06a6cf8 100644
--- a/tests/expectations/tests/dynamic_loading_with_blocklist.rs
+++ b/tests/expectations/tests/dynamic_loading_with_blocklist.rs
@@ -80,7 +80,9 @@ impl TestLib {
let library = ::libloading::Library::new(path)?;
Self::from_library(library)
}
- pub unsafe fn from_library<L>(library: L) -> Result<Self, ::libloading::Error>
+ pub unsafe fn from_library<L>(
+ library: L,
+ ) -> Result<Self, ::libloading::Error>
where
L: Into<::libloading::Library>,
{
@@ -97,14 +99,12 @@ impl TestLib {
&self,
x: *mut ::std::os::raw::c_void,
) -> ::std::os::raw::c_int {
- let sym = self.foo.as_ref().expect("Expected function, got error.");
- (sym)(x)
+ (self.foo.as_ref().expect("Expected function, got error."))(x)
}
pub unsafe fn bar(
&self,
x: *mut ::std::os::raw::c_void,
) -> ::std::os::raw::c_int {
- let sym = self.bar.as_ref().expect("Expected function, got error.");
- (sym)(x)
+ (self.bar.as_ref().expect("Expected function, got error."))(x)
}
}
diff --git a/tests/expectations/tests/dynamic_loading_with_class.rs b/tests/expectations/tests/dynamic_loading_with_class.rs
index ba0defaa..8a66dc3f 100644
--- a/tests/expectations/tests/dynamic_loading_with_class.rs
+++ b/tests/expectations/tests/dynamic_loading_with_class.rs
@@ -75,7 +75,9 @@ impl TestLib {
let library = ::libloading::Library::new(path)?;
Self::from_library(library)
}
- pub unsafe fn from_library<L>(library: L) -> Result<Self, ::libloading::Error>
+ pub unsafe fn from_library<L>(
+ library: L,
+ ) -> Result<Self, ::libloading::Error>
where
L: Into<::libloading::Library>,
{
@@ -92,11 +94,9 @@ impl TestLib {
&self,
x: *mut ::std::os::raw::c_void,
) -> ::std::os::raw::c_int {
- let sym = self.foo.as_ref().expect("Expected function, got error.");
- (sym)(x)
+ (self.foo.as_ref().expect("Expected function, got error."))(x)
}
pub unsafe fn bar(&self) -> () {
- let sym = self.bar.as_ref().expect("Expected function, got error.");
- (sym)()
+ (self.bar.as_ref().expect("Expected function, got error."))()
}
}