diff --git a/codegen/build.rs b/codegen/build.rs index 3cdb104d..6996b306 100644 --- a/codegen/build.rs +++ b/codegen/build.rs @@ -8,7 +8,7 @@ use yansi::Color::{Red, Yellow, Blue, White}; use version_check::{supports_features, is_min_version, is_min_date}; // Specifies the minimum nightly version needed to compile Rocket's codegen. -const MIN_DATE: &'static str = "2018-04-03"; +const MIN_DATE: &'static str = "2018-04-06"; const MIN_VERSION: &'static str = "1.27.0-nightly"; fn main() { diff --git a/codegen/src/macros/mod.rs b/codegen/src/macros/mod.rs index 45b2aa8d..1e75544d 100644 --- a/codegen/src/macros/mod.rs +++ b/codegen/src/macros/mod.rs @@ -16,7 +16,7 @@ pub use self::uri::{uri, uri_internal}; pub fn prefix_path(prefix: &str, path: &mut Path) { let last = path.segments.len() - 1; let last_seg = &mut path.segments[last]; - last_seg.identifier = last_seg.identifier.prepend(prefix); + last_seg.ident = last_seg.ident.prepend(prefix); } #[inline] diff --git a/codegen/src/macros/uri.rs b/codegen/src/macros/uri.rs index 9c61fe6d..8b8f3f60 100644 --- a/codegen/src/macros/uri.rs +++ b/codegen/src/macros/uri.rs @@ -1,19 +1,19 @@ use std::fmt::Display; -use URI_INFO_MACRO_PREFIX; -use super::prefix_path; -use utils::{SpanExt, IdentExt, split_idents, ExprExt}; - -use parser::{UriParams, InternalUriParams, Validation}; - use syntax::codemap::Span; use syntax::ext::base::{DummyResult, ExtCtxt, MacEager, MacResult}; use syntax::tokenstream::{TokenStream, TokenTree}; use syntax::ast::{self, Ident}; +use syntax::symbol::Symbol; use syntax::parse::PResult; use syntax::ext::build::AstBuilder; use syntax::ptr::P; +use URI_INFO_MACRO_PREFIX; +use super::prefix_path; +use utils::{IdentExt, split_idents, ExprExt}; +use parser::{UriParams, InternalUriParams, Validation}; + pub fn uri( ecx: &mut ExtCtxt, sp: Span, @@ -125,14 +125,15 @@ pub fn uri_internal( // Now the user's parameters. // Building <$T as ::rocket::http::uri::FromUriParam<_>>::from_uri_param($e). - for (i, &(ident, ref ty)) in internal.fn_args.iter().enumerate() { + for (i, &(mut ident, ref ty)) in internal.fn_args.iter().enumerate() { let (span, mut expr) = (exprs[i].span, exprs[i].clone()); + ident.span = span; // path for call: >::from_uri_param let idents = split_idents("rocket::http::uri::FromUriParam"); let generics = vec![ecx.ty(span, ast::TyKind::Infer)]; let trait_path = ecx.path_all(span, true, idents, vec![], generics, vec![]); - let method = span.wrap(Ident::from_str("from_uri_param")); + let method = Ident::new(Symbol::intern("from_uri_param"), span); let (qself, path) = ecx.qpath(ty.clone(), trait_path, method); // replace &expr with [let tmp = expr; &tmp] so that borrows of @@ -142,7 +143,7 @@ pub fn uri_internal( if let ast::ExprKind::AddrOf(_, inner) = cloned_expr.node { // Only reassign temporary expressions, not locations. if !inner.is_location() { - let tmp_ident = ident.node.append("_tmp"); + let tmp_ident = ident.append("_tmp"); let tmp_stmt = ecx.stmt_let(span, false, tmp_ident, inner); argument_stmts.push(tmp_stmt); expr = ecx.expr_ident(span, tmp_ident); @@ -152,7 +153,7 @@ pub fn uri_internal( // generating: let $ident = path($expr); let path_expr = ecx.expr_qpath(span, qself, path); let call = ecx.expr_call(span, path_expr, vec![expr]); - let stmt = ecx.stmt_let(span, false, ident.node, call); + let stmt = ecx.stmt_let(span, false, ident, call); debug!("Emitting URI typecheck statement: {:?}", stmt); argument_stmts.push(stmt); @@ -163,8 +164,11 @@ pub fn uri_internal( format_assign_tokens.push(tokens); } - MacEager::expr(quote_expr!(ecx, { + let expr = quote_expr!(ecx, { $argument_stmts ::rocket::http::uri::Uri::from(format!($fmt_string, $format_assign_tokens)) - })) + }); + + debug!("Emitting URI expression: {:?}", expr); + MacEager::expr(expr) } diff --git a/codegen/src/parser/route.rs b/codegen/src/parser/route.rs index b8e35191..a4a0f662 100644 --- a/codegen/src/parser/route.rs +++ b/codegen/src/parser/route.rs @@ -5,7 +5,7 @@ use syntax::ast::*; use syntax::ext::base::{ExtCtxt, Annotatable}; use syntax::codemap::{Span, Spanned, dummy_spanned}; -use utils::{span, MetaItemExt, SpanExt, is_valid_ident}; +use utils::{MetaItemExt, SpanExt, span, is_valid_ident}; use super::Function; use super::keyvalue::KVSpanned; use super::uri::validate_uri; @@ -169,13 +169,13 @@ fn parse_method(ecx: &ExtCtxt, meta_item: &NestedMetaItem) -> Spanned { `HEAD`, `PATCH`, `OPTIONS`"; if let Some(word) = meta_item.word() { - if let Ok(method) = Method::from_str(&word.name().as_str()) { + if let Ok(method) = Method::from_str(&word.ident.name.as_str()) { if is_valid_method(method) { return span(method, word.span()); } } - let msg = format!("'{}' is not a valid method", word.name()); + let msg = format!("'{}' is not a valid method", word.ident); ecx.struct_span_err(word.span, &msg).help(valid_methods).emit(); return default_method; } diff --git a/codegen/src/parser/uri_macro.rs b/codegen/src/parser/uri_macro.rs index 6cc2decf..26820224 100644 --- a/codegen/src/parser/uri_macro.rs +++ b/codegen/src/parser/uri_macro.rs @@ -35,7 +35,7 @@ pub struct UriParams { #[derive(Debug)] pub struct InternalUriParams { pub uri: Spanned, - pub fn_args: Vec<(Spanned, P)>, + pub fn_args: Vec<(ast::Ident, P)>, pub uri_params: UriParams, } @@ -208,7 +208,7 @@ impl InternalUriParams { pub fn fn_args_str(&self) -> String { self.fn_args.iter() - .map(|&(ident, ref ty)| format!("{}: {}", ident.node, ty_to_string(&ty))) + .map(|&(ident, ref ty)| format!("{}: {}", ident.name, ty_to_string(&ty))) .collect::>() .join(", ") } @@ -225,7 +225,7 @@ impl InternalUriParams { Some(Spanned { node: Args::Unnamed(ref args), .. }) => unnamed(args), Some(Spanned { node: Args::Named(ref args), .. }) => { let mut params: IndexMap>> = self.fn_args.iter() - .map(|&(ident, _)| (ident.node.name, None)) + .map(|&(ident, _)| (ident.name, None)) .collect(); let (mut extra, mut dup) = (vec![], vec![]); diff --git a/codegen/src/utils/arg_ext.rs b/codegen/src/utils/arg_ext.rs index 6ff0fce1..0ae2c472 100644 --- a/codegen/src/utils/arg_ext.rs +++ b/codegen/src/utils/arg_ext.rs @@ -15,7 +15,7 @@ pub trait ArgExt { impl ArgExt for Arg { fn ident(&self) -> Option<&Ident> { match self.pat.node { - PatKind::Ident(_, ref ident, _) => Some(&ident.node), + PatKind::Ident(_, ref ident, _) => Some(&ident), _ => None, } } diff --git a/codegen/src/utils/ident_ext.rs b/codegen/src/utils/ident_ext.rs index 1e3b9f93..145030a8 100644 --- a/codegen/src/utils/ident_ext.rs +++ b/codegen/src/utils/ident_ext.rs @@ -1,5 +1,6 @@ use std::fmt::Display; use syntax::ast::Ident; +use syntax::symbol::Symbol; pub trait IdentExt { fn prepend(&self, other: T) -> Ident; @@ -9,11 +10,11 @@ pub trait IdentExt { impl IdentExt for Ident { fn prepend(&self, other: T) -> Ident { let new_ident = format!("{}{}", other, self.name); - Ident::from_str(new_ident.as_str()) + Ident::new(Symbol::intern(&new_ident), self.span) } fn append(&self, other: T) -> Ident { let new_ident = format!("{}{}", self.name, other); - Ident::from_str(new_ident.as_str()) + Ident::new(Symbol::intern(&new_ident), self.span) } } diff --git a/codegen/src/utils/meta_item_ext.rs b/codegen/src/utils/meta_item_ext.rs index 18b0596f..242141c5 100644 --- a/codegen/src/utils/meta_item_ext.rs +++ b/codegen/src/utils/meta_item_ext.rs @@ -10,7 +10,7 @@ pub trait MetaItemExt { impl MetaItemExt for NestedMetaItem { fn name_value(&self) -> Option<(&Symbol, &Lit)> { self.meta_item().and_then(|mi| match mi.node { - MetaItemKind::NameValue(ref l) => Some((&mi.name, l)), + MetaItemKind::NameValue(ref l) => Some((&mi.ident.name, l)), _ => None, }) } diff --git a/codegen/src/utils/parser_ext.rs b/codegen/src/utils/parser_ext.rs index 9a0ae1ae..657e7606 100644 --- a/codegen/src/utils/parser_ext.rs +++ b/codegen/src/utils/parser_ext.rs @@ -1,11 +1,8 @@ -use super::SpanExt; - use syntax::parse::parser::{PathStyle, Parser}; use syntax::parse::PResult; use syntax::ast::{self, Path, StrStyle, Ident}; use syntax::parse::token::Token::{Eof, Comma}; use syntax::parse::common::SeqSep; -use syntax::codemap::Spanned; use syntax::symbol::Symbol; pub trait ParserExt<'a> { @@ -16,7 +13,7 @@ pub trait ParserExt<'a> { fn parse_str_lit(&mut self) -> PResult<'a, (Symbol, StrStyle)>; // Like `parse_ident` but also looks for an `ident` in a `Pat`. - fn parse_ident_inc_pat(&mut self) -> PResult<'a, Spanned>; + fn parse_ident_inc_pat(&mut self) -> PResult<'a, Ident>; } impl<'a> ParserExt<'a> for Parser<'a> { @@ -43,9 +40,8 @@ impl<'a> ParserExt<'a> for Parser<'a> { }) } - fn parse_ident_inc_pat(&mut self) -> PResult<'a, Spanned> { + fn parse_ident_inc_pat(&mut self) -> PResult<'a, Ident> { self.parse_ident() - .map(|ident| self.prev_span.wrap(ident)) .or_else(|mut e| { let pat = self.parse_pat().map_err(|i| { e.cancel(); i })?; let ident = match pat.node { diff --git a/examples/session/src/main.rs b/examples/session/src/main.rs index a4525c67..b130fa4b 100644 --- a/examples/session/src/main.rs +++ b/examples/session/src/main.rs @@ -39,21 +39,21 @@ impl<'a, 'r> FromRequest<'a, 'r> for User { fn login(mut cookies: Cookies, login: Form) -> Flash { if login.get().username == "Sergio" && login.get().password == "password" { cookies.add_private(Cookie::new("user_id", 1.to_string())); - Flash::success(Redirect::to("/"), "Successfully logged in.") + Flash::success(Redirect::to(uri!(index)), "Successfully logged in.") } else { - Flash::error(Redirect::to("/login"), "Invalid username/password.") + Flash::error(Redirect::to(uri!(login_page)), "Invalid username/password.") } } #[post("/logout")] fn logout(mut cookies: Cookies) -> Flash { cookies.remove_private(Cookie::named("user_id")); - Flash::success(Redirect::to("/login"), "Successfully logged out.") + Flash::success(Redirect::to(uri!(login_page)), "Successfully logged out.") } #[get("/login")] fn login_user(_user: User) -> Redirect { - Redirect::to("/") + Redirect::to(uri!(index)) } #[get("/login", rank = 2)] @@ -75,7 +75,7 @@ fn user_index(user: User) -> Template { #[get("/", rank = 2)] fn index() -> Redirect { - Redirect::to("/login") + Redirect::to(uri!(login_page)) } fn rocket() -> rocket::Rocket { diff --git a/examples/tera_templates/src/main.rs b/examples/tera_templates/src/main.rs index 66961dc7..d48cf0a1 100644 --- a/examples/tera_templates/src/main.rs +++ b/examples/tera_templates/src/main.rs @@ -22,7 +22,7 @@ struct TemplateContext { #[get("/")] fn index() -> Redirect { - Redirect::to("/hello/Unknown") + Redirect::to(uri!(get: name = "Unknown")) } #[get("/hello/")]