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

Prevent coercion before distributed slice typecheck #83

Merged
merged 1 commit into from
Mar 6, 2024
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: 1 addition & 1 deletion impl/src/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ fn do_expand(path: Path, pos: Option<usize>, input: Element) -> TokenStream {
#vis static #ident : #ty = {
#[allow(clippy::no_effect_underscore_binding)]
unsafe fn __typecheck(_: #linkme_path::__private::Void) {
let #new = #linkme_path::__private::value::<#ty>;
let #new = || (|| &#ident) as fn() -> _;
unsafe {
#linkme_path::DistributedSlice::private_typecheck(#path, #uninit);
}
Expand Down
4 changes: 2 additions & 2 deletions src/distributed_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ impl<T> DistributedSlice<[T]> {

#[doc(hidden)]
#[inline]
pub unsafe fn private_typecheck(self, element: T) {
mem::forget(element);
pub unsafe fn private_typecheck(self, get: fn() -> &'static T) {
let _ = get;
}
}

Expand Down
11 changes: 11 additions & 0 deletions tests/ui/attempted_coercion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#![cfg_attr(feature = "used_linker", feature(used_with_arg))]

use linkme::distributed_slice;

#[distributed_slice]
pub static SLICE: [&'static str];

#[distributed_slice(SLICE)]
static ELEMENT: &&str = &"uhoh";

fn main() {}
15 changes: 15 additions & 0 deletions tests/ui/attempted_coercion.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error[E0308]: mismatched types
--> tests/ui/attempted_coercion.rs:9:17
|
8 | #[distributed_slice(SLICE)]
| --------------------------- arguments to this function are incorrect
9 | static ELEMENT: &&str = &"uhoh";
| ^^^^^ expected `str`, found `&str`
|
= note: expected fn pointer `fn() -> &'static &'static _`
found fn pointer `fn() -> &&&_`
note: method defined here
--> src/distributed_slice.rs
|
| pub unsafe fn private_typecheck(self, get: fn() -> &'static T) {
| ^^^^^^^^^^^^^^^^^
12 changes: 6 additions & 6 deletions tests/ui/mismatched_types.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ error[E0308]: mismatched types
11 | static BENCH_WTF: usize = 999;
| ^^^^^ expected fn pointer, found `usize`
|
= note: expected fn pointer `for<'a> fn(&'a mut Bencher)`
found type `usize`
= note: expected fn pointer `fn() -> &'static for<'a> fn(&'a mut Bencher)`
found fn pointer `fn() -> &usize`
note: method defined here
--> src/distributed_slice.rs
|
| pub unsafe fn private_typecheck(self, element: T) {
| pub unsafe fn private_typecheck(self, get: fn() -> &'static T) {
| ^^^^^^^^^^^^^^^^^

error[E0308]: mismatched types
Expand All @@ -22,10 +22,10 @@ error[E0308]: mismatched types
14 | fn wrong_bench_fn<'a>(_: &'a mut ()) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Bencher`, found `()`
|
= note: expected fn pointer `for<'a> fn(&'a mut Bencher)`
found fn pointer `for<'a> fn(&'a mut ())`
= note: expected fn pointer `fn() -> &'static for<'a> fn(&'a mut Bencher)`
found fn pointer `fn() -> &for<'a> fn(&'a mut ())`
note: method defined here
--> src/distributed_slice.rs
|
| pub unsafe fn private_typecheck(self, element: T) {
| pub unsafe fn private_typecheck(self, get: fn() -> &'static T) {
| ^^^^^^^^^^^^^^^^^
Loading