error on missing methods

This commit is contained in:
Roland Fredenhagen 2022-05-03 19:40:53 +02:00
parent f73c5406cb
commit cd2599eee8
No known key found for this signature in database
GPG Key ID: 094AF99241035EB6
4 changed files with 51 additions and 16 deletions

View File

@ -427,23 +427,31 @@ pub(crate) fn with_methods(input: TokenStream) -> TokenStream {
ast.attrs = others.into_iter().map(Result::unwrap_err).collect(); ast.attrs = others.into_iter().map(Result::unwrap_err).collect();
let methods = match methods let methods =
.into_iter() match methods
.map(Result::unwrap) .into_iter()
.map(|(method, attr)| { .map(Result::unwrap)
attr.parse_meta().and_then(|args| { .map(|(method, attr)| {
if let Meta::List(args) = args { attr.parse_meta().and_then(|args| {
Args::new(args.nested.into_iter().collect(), Some(method)) if let Meta::List(args) = args {
} else { Args::new(args.nested.into_iter().collect(), Some(method))
Err(syn::Error::new_spanned(attr, "Invalid input for macro")) } else {
} Err(syn::Error::new_spanned(attr, "Invalid input for macro"))
}
})
}) })
}) .collect::<Result<Vec<_>, _>>()
.collect() {
{ Ok(methods) if methods.is_empty() => return input_and_compile_error(
Ok(methods) => methods, input,
Err(err) => return input_and_compile_error(input, err), syn::Error::new(
}; Span::call_site(),
"The #[routes] macro requires at least one `#[<method>(..)]` attribute.",
),
),
Ok(methods) => methods,
Err(err) => return input_and_compile_error(input, err),
};
match Route::multiple(methods, ast) { match Route::multiple(methods, ast) {
Ok(route) => route.into_token_stream().into(), Ok(route) => route.into_token_stream().into(),

View File

@ -13,6 +13,7 @@ fn compile_macros() {
t.compile_fail("tests/trybuild/route-malformed-path-fail.rs"); t.compile_fail("tests/trybuild/route-malformed-path-fail.rs");
t.pass("tests/trybuild/routes-ok.rs"); t.pass("tests/trybuild/routes-ok.rs");
t.compile_fail("tests/trybuild/routes-missing-method-fail.rs");
t.pass("tests/trybuild/docstring-ok.rs"); t.pass("tests/trybuild/docstring-ok.rs");

View File

@ -0,0 +1,13 @@
use actix_web_codegen::*;
#[routes]
async fn index() -> String {
"Hello World!".to_owned()
}
#[actix_web::main]
async fn main() {
use actix_web::App;
let srv = actix_test::start(|| App::new().service(index));
}

View File

@ -0,0 +1,13 @@
error: The #[routes] macro requires at least one `#[<method>(..)]` attribute.
--> tests/trybuild/routes-missing-method-fail.rs:3:1
|
3 | #[routes]
| ^^^^^^^^^
|
= note: this error originates in the attribute macro `routes` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: the trait bound `fn() -> impl std::future::Future {index}: HttpServiceFactory` is not satisfied
--> tests/trybuild/routes-missing-method-fail.rs:12:55
|
12 | let srv = actix_test::start(|| App::new().service(index));
| ^^^^^ the trait `HttpServiceFactory` is not implemented for `fn() -> impl std::future::Future {index}`