Skip to content

Commit 9be2622

Browse files
committed
Fix ui test error_if_ineffective
`ty_conversions` only cover named generics, it doesn't cover `impl Trait`. I also changed the span for the error to `call_site`, otherwise it would show that the `compile_error!` comes from the comment, which is impossible. Signed-off-by: Jiahao XU <[email protected]>
1 parent c8e7324 commit 9be2622

File tree

3 files changed

+27
-21
lines changed

3 files changed

+27
-21
lines changed

gix-macros/src/momo.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@ pub(crate) fn inner(code: proc_macro2::TokenStream) -> proc_macro2::TokenStream
1111

1212
if let Item::Fn(item_fn) = fn_item {
1313
let ty_conversions = parse_generics(&item_fn.sig);
14-
// TODO: uncomment and see it fail in places where it should actually succeed.
15-
// if ty_conversions.is_empty() {
16-
// return Error::new(
17-
// item_fn.span(),
18-
// "Couldn't apply a single conversion - momo is ineffective here",
19-
// )
20-
// .to_compile_error();
21-
// }
22-
let (argtypes, argexprs, has_self) = convert(&item_fn.sig.inputs, &ty_conversions);
14+
let (has_conversion_in_effect, argtypes, argexprs, has_self) = convert(&item_fn.sig.inputs, &ty_conversions);
15+
if !has_conversion_in_effect {
16+
return Error::new(
17+
proc_macro2::Span::call_site(),
18+
"Couldn't apply a single conversion - momo is ineffective here",
19+
)
20+
.to_compile_error();
21+
}
2322

2423
let uses_self = has_self
2524
|| item_fn.sig.inputs.iter().any(has_self_type)
@@ -171,7 +170,8 @@ fn parse_generics(decl: &Signature) -> HashMap<Ident, Conversion<'_>> {
171170
fn convert<'a>(
172171
inputs: &'a Punctuated<FnArg, Token![,]>,
173172
ty_conversions: &HashMap<Ident, Conversion<'a>>,
174-
) -> (Punctuated<FnArg, Token![,]>, Punctuated<Expr, Token![,]>, bool) {
173+
) -> (bool, Punctuated<FnArg, Token![,]>, Punctuated<Expr, Token![,]>, bool) {
174+
let mut has_conversion_in_effect = false;
175175
let mut argtypes = Punctuated::new();
176176
let mut argexprs = Punctuated::new();
177177
let mut has_self = false;
@@ -210,6 +210,7 @@ fn convert<'a>(
210210
match *pat_type.ty {
211211
Type::ImplTrait(TypeImplTrait { ref bounds, .. }) => {
212212
if let Some(conv) = parse_bounds(bounds) {
213+
has_conversion_in_effect = true;
213214
argexprs.push(conv.conversion_expr(ident));
214215
if let Conversion::AsMut(_) = conv {
215216
pat_ident.mutability = Some(Default::default());
@@ -220,6 +221,7 @@ fn convert<'a>(
220221
}
221222
Type::Path(..) => {
222223
if let Some(conv) = parse_bounded_type(&pat_type.ty).and_then(|ident| ty_conversions.get(&ident)) {
224+
has_conversion_in_effect = true;
223225
argexprs.push(conv.conversion_expr(ident));
224226
if let Conversion::AsMut(_) = conv {
225227
pat_ident.mutability = Some(Default::default());
@@ -237,7 +239,7 @@ fn convert<'a>(
237239
argtypes.push(FnArg::Typed(pat_type));
238240
}
239241
});
240-
(argtypes, argexprs, has_self)
242+
(has_conversion_in_effect, argtypes, argexprs, has_self)
241243
}
242244

243245
fn contains_self_type_path(path: &Path) -> bool {

gix-macros/tests/momo/mod.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,6 @@ fn test_open_opts(directory: impl Into<std::path::PathBuf>, options: Options) ->
1515
test_open_opts_inner(directory, options)
1616
}
1717

18-
#[allow(dead_code)]
19-
#[momo]
20-
fn test_with_try(t: impl TryInto<String, Error = ()>, _options: Options) -> Result<(), ()> {
21-
let t = t.try_into()?;
22-
t.strip_prefix('1').ok_or(())?;
23-
Ok(())
24-
}
25-
2618
#[momo]
2719
fn test_fn<E>(
2820
a: impl Into<String>,
@@ -310,8 +302,7 @@ fn ux() {
310302
}
311303

312304
#[test]
313-
#[ignore = "needs work"]
314-
fn ux_todo() {
305+
fn ux_on_ineffective() {
315306
let t = trybuild::TestCases::new();
316307
t.compile_fail("tests/momo/ux/error_if_ineffective.rs");
317308
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error: Couldn't apply a single conversion - momo is ineffective here
2+
--> tests/momo/ux/error_if_ineffective.rs:2:1
3+
|
4+
2 | #[gix_macros::momo]
5+
| ^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: this error originates in the attribute macro `gix_macros::momo` (in Nightly builds, run with -Z macro-backtrace for more info)
8+
9+
error[E0601]: `main` function not found in crate `$CRATE`
10+
--> tests/momo/ux/error_if_ineffective.rs:3:13
11+
|
12+
3 | fn main() {}
13+
| ^ consider adding a `main` function to `$DIR/tests/momo/ux/error_if_ineffective.rs`

0 commit comments

Comments
 (0)