Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Rocket is web framework for Rust (nightly) with a focus on ease-of-use,
expressibility, and speed. Here's an example of a complete Rocket application:

```rust
#![feature(proc_macro_hygiene, decl_macro)]
#![feature(proc_macro_hygiene)]

#[macro_use] extern crate rocket;

Expand Down
8 changes: 4 additions & 4 deletions contrib/lib/src/databases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
//! In your application's source code, one-time:
//!
//! ```rust
//! #![feature(proc_macro_hygiene, decl_macro)]
//! #![feature(proc_macro_hygiene)]
//!
//! #[macro_use] extern crate rocket;
//! #[macro_use] extern crate rocket_contrib;
Expand All @@ -73,7 +73,7 @@
//! Whenever a connection to the database is needed:
//!
//! ```rust
//! # #![feature(proc_macro_hygiene, decl_macro)]
//! # #![feature(proc_macro_hygiene)]
//! #
//! # #[macro_use] extern crate rocket;
//! # #[macro_use] extern crate rocket_contrib;
Expand Down Expand Up @@ -289,7 +289,7 @@
//! connection to a given database:
//!
//! ```rust
//! # #![feature(proc_macro_hygiene, decl_macro)]
//! # #![feature(proc_macro_hygiene)]
//! #
//! # #[macro_use] extern crate rocket;
//! # #[macro_use] extern crate rocket_contrib;
Expand All @@ -311,7 +311,7 @@
//! connection type:
//!
//! ```rust
//! # #![feature(proc_macro_hygiene, decl_macro)]
//! # #![feature(proc_macro_hygiene)]
//! #
//! # #[macro_use] extern crate rocket;
//! # #[macro_use] extern crate rocket_contrib;
Expand Down
8 changes: 4 additions & 4 deletions contrib/lib/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub use serde_json::{json_internal, json_internal_vec};
/// or from [`serde`]. The data is parsed from the HTTP request body.
///
/// ```rust
/// # #![feature(proc_macro_hygiene, decl_macro)]
/// # #![feature(proc_macro_hygiene)]
/// # #[macro_use] extern crate rocket;
/// # extern crate rocket_contrib;
/// # type User = usize;
Expand All @@ -65,7 +65,7 @@ pub use serde_json::{json_internal, json_internal_vec};
/// set to `application/json` automatically.
///
/// ```rust
/// # #![feature(proc_macro_hygiene, decl_macro)]
/// # #![feature(proc_macro_hygiene)]
/// # #[macro_use] extern crate rocket;
/// # extern crate rocket_contrib;
/// # type User = usize;
Expand Down Expand Up @@ -210,7 +210,7 @@ impl<T> DerefMut for Json<T> {
/// fashion during request handling. This looks something like:
///
/// ```rust
/// # #![feature(proc_macro_hygiene, decl_macro)]
/// # #![feature(proc_macro_hygiene)]
/// # #[macro_use] extern crate rocket;
/// # #[macro_use] extern crate rocket_contrib;
/// use rocket_contrib::json::JsonValue;
Expand Down Expand Up @@ -305,7 +305,7 @@ impl<'a> Responder<'a> for JsonValue {
/// value created with this macro can be returned from a handler as follows:
///
/// ```rust
/// # #![feature(proc_macro_hygiene, decl_macro)]
/// # #![feature(proc_macro_hygiene)]
/// # #[macro_use] extern crate rocket;
/// # #[macro_use] extern crate rocket_contrib;
/// use rocket_contrib::json::JsonValue;
Expand Down
4 changes: 2 additions & 2 deletions contrib/lib/src/msgpack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub use rmp_serde::decode::Error;
/// request body.
///
/// ```rust
/// # #![feature(proc_macro_hygiene, decl_macro)]
/// # #![feature(proc_macro_hygiene)]
/// # #[macro_use] extern crate rocket;
/// # extern crate rocket_contrib;
/// # type User = usize;
Expand All @@ -64,7 +64,7 @@ pub use rmp_serde::decode::Error;
/// response is set to `application/msgpack` automatically.
///
/// ```rust
/// # #![feature(proc_macro_hygiene, decl_macro)]
/// # #![feature(proc_macro_hygiene)]
/// # #[macro_use] extern crate rocket;
/// # extern crate rocket_contrib;
/// # type User = usize;
Expand Down
6 changes: 3 additions & 3 deletions contrib/lib/src/templates/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::templates::ContextManager;
/// used as a request guard in any request handler.
///
/// ```rust
/// # #![feature(proc_macro_hygiene, decl_macro)]
/// # #![feature(proc_macro_hygiene)]
/// # #[macro_use] extern crate rocket;
/// # #[macro_use] extern crate rocket_contrib;
/// use rocket_contrib::templates::{Template, Metadata};
Expand Down Expand Up @@ -46,7 +46,7 @@ impl Metadata<'_> {
/// # Example
///
/// ```rust
/// # #![feature(proc_macro_hygiene, decl_macro)]
/// # #![feature(proc_macro_hygiene)]
/// # #[macro_use] extern crate rocket;
/// # extern crate rocket_contrib;
/// #
Expand All @@ -67,7 +67,7 @@ impl Metadata<'_> {
/// # Example
///
/// ```rust
/// # #![feature(proc_macro_hygiene, decl_macro)]
/// # #![feature(proc_macro_hygiene)]
/// # #[macro_use] extern crate rocket;
/// # extern crate rocket_contrib;
/// #
Expand Down
4 changes: 2 additions & 2 deletions contrib/lib/src/templates/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
//! of the template file minus the last two extensions, from a handler.
//!
//! ```rust
//! # #![feature(proc_macro_hygiene, decl_macro)]
//! # #![feature(proc_macro_hygiene)]
//! # #[macro_use] extern crate rocket;
//! # #[macro_use] extern crate rocket_contrib;
//! # fn context() { }
Expand Down Expand Up @@ -179,7 +179,7 @@ const DEFAULT_TEMPLATE_DIR: &str = "templates";
/// returned from a request handler directly:
///
/// ```rust
/// # #![feature(proc_macro_hygiene, decl_macro)]
/// # #![feature(proc_macro_hygiene)]
/// # #[macro_use] extern crate rocket;
/// # #[macro_use] extern crate rocket_contrib;
/// # fn context() { }
Expand Down
4 changes: 2 additions & 2 deletions contrib/lib/src/uuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub use self::uuid_crate::parser::ParseError;
/// You can use the `Uuid` type directly as a target of a dynamic parameter:
///
/// ```rust
/// # #![feature(proc_macro_hygiene, decl_macro)]
/// # #![feature(proc_macro_hygiene)]
/// # #[macro_use] extern crate rocket;
/// # #[macro_use] extern crate rocket_contrib;
/// use rocket_contrib::uuid::Uuid;
Expand All @@ -56,7 +56,7 @@ pub use self::uuid_crate::parser::ParseError;
/// You can also use the `Uuid` as a form value, including in query strings:
///
/// ```rust
/// # #![feature(proc_macro_hygiene, decl_macro)]
/// # #![feature(proc_macro_hygiene)]
/// # #[macro_use] extern crate rocket;
/// # #[macro_use] extern crate rocket_contrib;
/// use rocket_contrib::uuid::Uuid;
Expand Down
2 changes: 1 addition & 1 deletion contrib/lib/tests/compress_responder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(decl_macro, proc_macro_hygiene)]
#![feature(proc_macro_hygiene)]

#[macro_use]
#[cfg(all(feature = "brotli_compression", feature = "gzip_compression"))]
Expand Down
2 changes: 1 addition & 1 deletion contrib/lib/tests/compression_fairing.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(decl_macro, proc_macro_hygiene)]
#![feature(proc_macro_hygiene)]

#[macro_use]
#[cfg(all(feature = "brotli_compression", feature = "gzip_compression"))]
Expand Down
2 changes: 1 addition & 1 deletion contrib/lib/tests/helmet.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(proc_macro_hygiene, decl_macro)]
#![feature(proc_macro_hygiene)]

#[macro_use]
#[cfg(feature = "helmet")]
Expand Down
2 changes: 1 addition & 1 deletion contrib/lib/tests/static_files.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(proc_macro_hygiene, decl_macro)]
#![feature(proc_macro_hygiene)]

#[cfg(feature = "serve")]
mod static_tests {
Expand Down
2 changes: 1 addition & 1 deletion contrib/lib/tests/templates.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(proc_macro_hygiene, decl_macro)]
#![feature(proc_macro_hygiene)]

#[cfg(feature = "templates")]
#[macro_use] extern crate rocket;
Expand Down
31 changes: 25 additions & 6 deletions core/codegen/src/attribute/route.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

use proc_macro::{TokenStream, Span};
use crate::proc_macro2::TokenStream as TokenStream2;
use devise::{syn, Spanned, SpanWrapped, Result, FromMeta, ext::TypeExt};
Expand Down Expand Up @@ -325,15 +328,31 @@ fn generate_internal_uri_macro(route: &Route) -> TokenStream2 {
.map(|name| route.inputs.iter().find(|(ident, ..)| ident == name).unwrap())
.map(|(ident, _, ty)| quote!(#ident: #ty));

let generated_macro_name = route.function.ident.prepend(URI_MACRO_PREFIX);
let mut hasher = DefaultHasher::new();
let route_span = route.function.span();
route_span.source_file().path().hash(&mut hasher);
let line_column = route_span.start();
line_column.line.hash(&mut hasher);
line_column.column.hash(&mut hasher);

let mut generated_macro_name = route.function.ident.prepend(URI_MACRO_PREFIX);
generated_macro_name.set_span(Span::call_site().into());
let inner_generated_macro_name = generated_macro_name.append(&hasher.finish().to_string());
let route_uri = route.attribute.path.origin.0.to_string();

quote! {
pub macro #generated_macro_name($($token:tt)*) {{
extern crate std;
extern crate rocket;
rocket::rocket_internal_uri!(#route_uri, (#(#dynamic_args),*), $($token)*)
}}
#[doc(hidden)]
#[macro_export]
macro_rules! #inner_generated_macro_name {
($($token:tt)*) => {{
extern crate std;
extern crate rocket;
rocket::rocket_internal_uri!(#route_uri, (#(#dynamic_args),*), $($token)*)
}};
}

#[doc(hidden)]
pub use #inner_generated_macro_name as #generated_macro_name;
}
}

Expand Down
24 changes: 12 additions & 12 deletions core/codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
//! crate root:
//!
//! ```rust
//! #![feature(proc_macro_hygiene, decl_macro)]
//! #![feature(proc_macro_hygiene)]
//!
//! #[macro_use] extern crate rocket;
//! # #[get("/")] fn hello() { }
Expand All @@ -40,7 +40,7 @@
//! Or, alternatively, selectively import from the top-level scope:
//!
//! ```rust
//! #![feature(proc_macro_hygiene, decl_macro)]
//! #![feature(proc_macro_hygiene)]
//! # extern crate rocket;
//!
//! use rocket::{get, routes};
Expand Down Expand Up @@ -131,7 +131,7 @@ macro_rules! route_attribute {
/// functions:
///
/// ```rust
/// # #![feature(proc_macro_hygiene, decl_macro)]
/// # #![feature(proc_macro_hygiene)]
/// # #[macro_use] extern crate rocket;
/// #
/// #[get("/")]
Expand All @@ -154,7 +154,7 @@ macro_rules! route_attribute {
/// explicitly specified:
///
/// ```rust
/// # #![feature(proc_macro_hygiene, decl_macro)]
/// # #![feature(proc_macro_hygiene)]
/// # #[macro_use] extern crate rocket;
/// #
/// #[route(GET, path = "/")]
Expand Down Expand Up @@ -215,7 +215,7 @@ macro_rules! route_attribute {
/// the arguments `foo`, `baz`, `msg`, `rest`, and `form`:
///
/// ```rust
/// # #![feature(proc_macro_hygiene, decl_macro)]
/// # #![feature(proc_macro_hygiene)]
/// # #[macro_use] extern crate rocket;
/// # use rocket::request::Form;
/// # use std::path::PathBuf;
Expand Down Expand Up @@ -327,7 +327,7 @@ route_attribute!(options => Method::Options);
/// This attribute can only be applied to free functions:
///
/// ```rust
/// # #![feature(proc_macro_hygiene, decl_macro)]
/// # #![feature(proc_macro_hygiene)]
/// # #[macro_use] extern crate rocket;
/// #
/// use rocket::Request;
Expand Down Expand Up @@ -739,7 +739,7 @@ pub fn derive_uri_display_path(input: TokenStream) -> TokenStream {
/// corresponding [`Route`] structures. For example, given the following routes:
///
/// ```rust
/// # #![feature(proc_macro_hygiene, decl_macro)]
/// # #![feature(proc_macro_hygiene)]
/// # #[macro_use] extern crate rocket;
/// #
/// #[get("/")]
Expand All @@ -754,7 +754,7 @@ pub fn derive_uri_display_path(input: TokenStream) -> TokenStream {
/// The `routes!` macro can be used as:
///
/// ```rust
/// # #![feature(proc_macro_hygiene, decl_macro)]
/// # #![feature(proc_macro_hygiene)]
/// # #[macro_use] extern crate rocket;
/// #
/// # use rocket::http::Method;
Expand Down Expand Up @@ -798,7 +798,7 @@ pub fn routes(input: TokenStream) -> TokenStream {
/// catchers:
///
/// ```rust
/// # #![feature(proc_macro_hygiene, decl_macro)]
/// # #![feature(proc_macro_hygiene)]
/// # #[macro_use] extern crate rocket;
/// #
/// #[catch(404)]
Expand All @@ -813,7 +813,7 @@ pub fn routes(input: TokenStream) -> TokenStream {
/// The `catchers!` macro can be used as:
///
/// ```rust
/// # #![feature(proc_macro_hygiene, decl_macro)]
/// # #![feature(proc_macro_hygiene)]
/// # #[macro_use] extern crate rocket;
/// #
/// # #[catch(404)] fn not_found() { /* .. */ }
Expand Down Expand Up @@ -855,7 +855,7 @@ pub fn catchers(input: TokenStream) -> TokenStream {
/// For example, for the following route:
///
/// ```rust
/// # #![feature(proc_macro_hygiene, decl_macro)]
/// # #![feature(proc_macro_hygiene)]
/// # #[macro_use] extern crate rocket;
/// #
/// #[get("/person/<name>?<age>")]
Expand All @@ -869,7 +869,7 @@ pub fn catchers(input: TokenStream) -> TokenStream {
/// A URI can be created as follows:
///
/// ```rust
/// # #![feature(proc_macro_hygiene, decl_macro)]
/// # #![feature(proc_macro_hygiene)]
/// # #[macro_use] extern crate rocket;
/// #
/// # #[get("/person/<name>?<age>")]
Expand Down
5 changes: 5 additions & 0 deletions core/codegen/src/syn_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ pub fn syn_to_diag(error: syn::parse::Error) -> Diagnostic {

pub trait IdentExt {
fn prepend(&self, string: &str) -> syn::Ident;
fn append(&self, string: &str) -> syn::Ident;
}

impl IdentExt for syn::Ident {
fn prepend(&self, string: &str) -> syn::Ident {
syn::Ident::new(&format!("{}{}", string, self), self.span())
}

fn append(&self, string: &str) -> syn::Ident {
syn::Ident::new(&format!("{}{}", self, string), self.span())
}
}

pub trait ReturnTypeExt {
Expand Down
2 changes: 1 addition & 1 deletion core/codegen/tests/expansion.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(proc_macro_hygiene, decl_macro)]
#![feature(proc_macro_hygiene)]

#[macro_use] extern crate rocket;

Expand Down
2 changes: 1 addition & 1 deletion core/codegen/tests/responder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(proc_macro_hygiene, decl_macro)]
#![feature(proc_macro_hygiene)]

use rocket::local::Client;
use rocket::response::Responder;
Expand Down
2 changes: 1 addition & 1 deletion core/codegen/tests/route-data.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(proc_macro_hygiene, decl_macro)]
#![feature(proc_macro_hygiene)]

#[macro_use] extern crate rocket;

Expand Down
2 changes: 1 addition & 1 deletion core/codegen/tests/route-format.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(proc_macro_hygiene, decl_macro)]
#![feature(proc_macro_hygiene)]

#[macro_use] extern crate rocket;

Expand Down
2 changes: 1 addition & 1 deletion core/codegen/tests/route-ranking.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(proc_macro_hygiene, decl_macro)]
#![feature(proc_macro_hygiene)]

#[macro_use] extern crate rocket;

Expand Down
2 changes: 1 addition & 1 deletion core/codegen/tests/route.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(proc_macro_hygiene, decl_macro)]
#![feature(proc_macro_hygiene)]

// Rocket sometimes generates mangled identifiers that activate the
// non_snake_case lint. We deny the lint in this test to ensure that
Expand Down
Loading