Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Construct rocket_param_ idents from the function arguments #778

Closed
wants to merge 1 commit into from
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
8 changes: 5 additions & 3 deletions codegen/src/decorators/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,18 @@ impl RouteGenerateExt for RouteParams {
let mut declared_set = HashSet::new();
for (i, param) in self.path_params(ecx).enumerate() {
declared_set.insert(param.ident().name);
let ty = match self.annotated_fn.find_input(&param.ident().name) {
Some(arg) => strip_ty_lifetimes(arg.ty.clone()),
let arg = match self.annotated_fn.find_input(&param.ident().name) {
Some(arg) => arg,
None => {
self.missing_declared_err(ecx, param.inner());
continue;
}
};

let ty = strip_ty_lifetimes(arg.ty.clone());
let ident = arg.ident().expect("function argument ident").prepend(PARAM_PREFIX);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it correct to expect this? I didn't look too thoroughly, but I think only PatKind::Ident is valid for FromParam guards, in practice.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes; the find_input() method only returns Some if ident() here would return Some. Since we bailed on the None case above, this is safe.


// Note: the `None` case shouldn't happen if a route is matched.
let ident = param.ident().prepend(PARAM_PREFIX);
let expr = match param {
Param::Single(_) => quote_expr!(ecx, match __req.get_param_str($i) {
Some(s) => <$ty as ::rocket::request::FromParam>::from_param(s),
Expand Down
22 changes: 22 additions & 0 deletions codegen/tests/run-pass/route_ident_hygiene.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#![feature(plugin)]
#![plugin(rocket_codegen)]

extern crate rocket;

#[get("/easy/<id>")]
fn easy(id: i32) -> String {
format!("id: {}", id)
}

macro_rules! make_handler {
() => {
#[get("/hard/<id>")]
fn hard(id: i32) -> String {
format!("id: {}", id)
}
}
}

make_handler!();

fn main() { }