Skip to content

Commit

Permalink
Better error message when mocking structs with elided lifetimes (#622)
Browse files Browse the repository at this point in the history
If a struct has an elided lifetime in its impl block, then the generics
list from the "impl" won't match the generics list from the struct.
Mockall needs more work to handle that, and I'm not ready to invest that
time right now.
  • Loading branch information
asomers authored Nov 17, 2024
1 parent a96a23e commit 44d319b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
9 changes: 9 additions & 0 deletions mockall_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1556,6 +1556,15 @@ mod automock {
assert_not_contains(&output, quote!(#[must_use] struct MockFoo));
}

#[test]
#[should_panic(expected = "automock does not currently support structs with elided lifetimes")]
fn elided_lifetimes() {
let code = "impl X<'_> {}";
let ts = proc_macro2::TokenStream::from_str(code).unwrap();
let attrs_ts = proc_macro2::TokenStream::from_str("").unwrap();
do_automock(attrs_ts, ts).to_string();
}

#[test]
#[should_panic(expected = "can only mock inline modules")]
fn external_module() {
Expand Down
11 changes: 11 additions & 0 deletions mockall_derive/src/mockable_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,17 @@ impl From<ItemImpl> for MockableStruct {
let name = match &*item_impl.self_ty {
Type::Path(type_path) => {
let n = find_ident_from_path(&type_path.path).0;
let self_generics = &type_path.path.segments.last().unwrap().arguments;
if let PathArguments::AngleBracketed(abga) = &self_generics {
if item_impl.generics.params.len() != abga.args.len() {
// If a struct's impl block has elided lifetimes, then
// they won't show up in the impl block's generics
// list. automock can't currently handle that.
// https://github.com/asomers/mockall/issues/610
compile_error(item_impl.span(),
"automock does not currently support structs with elided lifetimes");
}
}
gen_mock_ident(&n)
},
x => {
Expand Down

0 comments on commit 44d319b

Please sign in to comment.