Fix #107: support generic functions with reference type parameters in func!#120
Open
mazong1123 wants to merge 1 commit intomainfrom
Open
Fix #107: support generic functions with reference type parameters in func!#120mazong1123 wants to merge 1 commit intomainfrom
mazong1123 wants to merge 1 commit intomainfrom
Conversation
… func! Remove type coercion in func! Case 1 and Case 2. Instead of coercing the function expression to the user-specified fn pointer type (which fails when a generic type parameter is a reference due to Rust's higher-ranked lifetime mismatch), cast the function item directly to *const () and use type_name::<fn_type>() for the signature string. For generic functions like execvp<S: AsRef<CStr>>, users can now use turbofish to monomorphize: func!(execvp::<&CStr>, fn(&CStr, &[&CStr]) -> Result<...>) This is non-breaking: all existing tests pass unchanged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #107 by removing the type coercion in
func!macro's Case 1 and Case 2, which fails for generic functions whose type parameters are reference types.Problem
When mocking a generic function like
nix::unistd::execvp<S: AsRef<CStr>>, the user specifies:The explicit fn pointer type
fn(&CStr, &[&CStr])desugars tofor<'a, 'b, 'c> fn(&'a CStr, &'b [&'c CStr])(3 independent lifetimes). But the monomorphizedexecvp::<&CStr>hasfor<'a, 'b> fn(&'a CStr, &'b [&CStr])(2 lifetimes — the inner reference's lifetime is tied to the generic parameter). Rust's type system rejects this coercion.Fix
Replace the type coercion (
let fn_val: $fn_type = $f) with a direct cast to*const ()and usetype_name::<$fn_type>()for the signature string. This:For generic functions with reference type params, users provide turbofish:
Testing
test_will_execute_generic_func_with_ref_type_param_via_turbofish— reproduces the exact issue func! macro lifetime mismatch for generic functions #107 patternCloses #107