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,7 +427,8 @@ pub(crate) fn with_methods(input: TokenStream) -> TokenStream {
ast.attrs = others.into_iter().map(Result::unwrap_err).collect();
let methods = match methods
let methods =
match methods
.into_iter()
.map(Result::unwrap)
.map(|(method, attr)| {
@ -439,8 +440,15 @@ pub(crate) fn with_methods(input: TokenStream) -> TokenStream {
}
})
})
.collect()
.collect::<Result<Vec<_>, _>>()
{
Ok(methods) if methods.is_empty() => return input_and_compile_error(
input,
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),
};

View File

@ -13,6 +13,7 @@ fn compile_macros() {
t.compile_fail("tests/trybuild/route-malformed-path-fail.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");

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}`