From 02cff01e1721d92cfcaea5ad9d2efa51cd601abc Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Mon, 11 Sep 2017 00:43:37 -0700 Subject: [PATCH] Give a nice error when uri! macro is empty. --- codegen/src/macros/uri.rs | 6 +++--- codegen/src/parser/uri_macro.rs | 13 +++++++++++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/codegen/src/macros/uri.rs b/codegen/src/macros/uri.rs index cf3e797c..3816b951 100644 --- a/codegen/src/macros/uri.rs +++ b/codegen/src/macros/uri.rs @@ -25,14 +25,14 @@ pub fn uri( ) -> Box { // Generate the path to the internal macro. let mut parser = ecx.new_parser_from_tts(args); - let (_, mut macro_path) = try_parse!(sp, UriParams::parse_prelude(&mut parser)); - prefix_path(URI_INFO_MACRO_PREFIX, &mut macro_path); + let (_, mut path) = try_parse!(sp, UriParams::parse_prelude(ecx, &mut parser)); + prefix_path(URI_INFO_MACRO_PREFIX, &mut path); // It's incredibly important we use `sp` as the Span for the generated code // so that errors from the `internal` call show up on the user's code. let expr = parser.mk_mac_expr(sp, ast::Mac_ { - path: macro_path, + path: path, tts: args.to_vec().into_iter().collect::().into(), }, ::syntax::util::ThinVec::new(), diff --git a/codegen/src/parser/uri_macro.rs b/codegen/src/parser/uri_macro.rs index faa6bef8..dffdf8f0 100644 --- a/codegen/src/parser/uri_macro.rs +++ b/codegen/src/parser/uri_macro.rs @@ -65,8 +65,14 @@ impl Arg { impl UriParams { // Parses the mount point, if any, and route identifier. pub fn parse_prelude<'a>( + ecx: &'a ExtCtxt, parser: &mut Parser<'a> ) -> PResult<'a, (Option>, Path)> { + if parser.token == Token::Eof { + return Err(ecx.struct_span_err(ecx.call_site(), + "call to `uri!` cannot be empty")); + } + // Parse the mount point and suffixing ',', if any. let mount_point = match parser.parse_optional_str() { Some((symbol, _, _)) => { @@ -91,9 +97,12 @@ impl UriParams { } } - pub fn parse<'a>( ecx: &'a ExtCtxt, parser: &mut Parser<'a>,) -> PResult<'a, UriParams> { + pub fn parse<'a>( + ecx: &'a ExtCtxt, + parser: &mut Parser<'a> + ) -> PResult<'a, UriParams> { // Parse the mount point and suffixing ',', if any. - let (mount_point, route_path) = Self::parse_prelude(parser)?; + let (mount_point, route_path) = Self::parse_prelude(ecx, parser)?; // If there are no arguments, finish early. if !parser.eat(&Token::Colon) {