From 25d55b4b0fbae0848ed42e7b2c5642a1c38b0b1f Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Tue, 25 Oct 2016 16:42:10 +0200 Subject: [PATCH] Disallow use of data parameter with non-payload methods. --- codegen/src/parser/route.rs | 10 ++++++++++ codegen/tests/compile-fail/data-without-post.rs | 12 ++++++++++++ codegen/tests/compile-fail/ignored_params.rs | 4 ++-- lib/src/response/responder.rs | 2 +- 4 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 codegen/tests/compile-fail/data-without-post.rs diff --git a/codegen/src/parser/route.rs b/codegen/src/parser/route.rs index 9289774a..5bc404b4 100644 --- a/codegen/src/parser/route.rs +++ b/codegen/src/parser/route.rs @@ -104,6 +104,16 @@ impl RouteParams { } } + // Sanity check: `data` should only be used with payload methods. + if let Some(ref data_param) = data { + if !method.node.supports_payload() { + ecx.struct_span_err(data_param.span, "`data` route parameters \ + can only be used with payload supporting methods") + .note(&format!("'{}' does not support payloads", method.node)) + .emit(); + } + } + RouteParams { method: method, path: path, diff --git a/codegen/tests/compile-fail/data-without-post.rs b/codegen/tests/compile-fail/data-without-post.rs new file mode 100644 index 00000000..44ea2b04 --- /dev/null +++ b/codegen/tests/compile-fail/data-without-post.rs @@ -0,0 +1,12 @@ +#![feature(plugin)] +#![plugin(rocket_codegen)] + +extern crate rocket; + +use rocket::Data; + +#[get("/", data = "")] +//~^ ERROR payload supporting methods +fn get(something: Data) -> &'static str { "hi" } + +fn main() { } diff --git a/codegen/tests/compile-fail/ignored_params.rs b/codegen/tests/compile-fail/ignored_params.rs index 8902c897..0aa5b9e3 100644 --- a/codegen/tests/compile-fail/ignored_params.rs +++ b/codegen/tests/compile-fail/ignored_params.rs @@ -7,7 +7,7 @@ fn get(other: &str) -> &'static str { "hi" } //~ ERROR isn't in the function #[get("/a?")] //~ ERROR 'r' is declared fn get1() -> &'static str { "hi" } //~ ERROR isn't in the function -#[get("/a", data = "")] //~ ERROR 'test' is declared -fn get2() -> &'static str { "hi" } //~ ERROR isn't in the function +#[post("/a", data = "")] //~ ERROR 'test' is declared +fn post() -> &'static str { "hi" } //~ ERROR isn't in the function fn main() { } diff --git a/lib/src/response/responder.rs b/lib/src/response/responder.rs index 03dfe8f0..27ece089 100644 --- a/lib/src/response/responder.rs +++ b/lib/src/response/responder.rs @@ -7,9 +7,9 @@ use http::hyper::{header, FreshHyperResponse, StatusCode}; use outcome::{self, IntoOutcome}; use outcome::Outcome::*; +/// Type alias for the `Outcome` of a `Responder`. pub type Outcome<'a> = outcome::Outcome<(), (), (StatusCode, FreshHyperResponse<'a>)>; - impl<'a, T, E> IntoOutcome<(), (), (StatusCode, FreshHyperResponse<'a>)> for Result { fn into_outcome(self) -> Outcome<'a> { match self {