Give a nice error when uri! macro is empty.

This commit is contained in:
Sergio Benitez 2017-09-11 00:43:37 -07:00
parent c4524400b2
commit 02cff01e17
2 changed files with 14 additions and 5 deletions

View File

@ -25,14 +25,14 @@ pub fn uri(
) -> Box<MacResult + 'static> { ) -> Box<MacResult + 'static> {
// Generate the path to the internal macro. // Generate the path to the internal macro.
let mut parser = ecx.new_parser_from_tts(args); let mut parser = ecx.new_parser_from_tts(args);
let (_, mut macro_path) = try_parse!(sp, UriParams::parse_prelude(&mut parser)); let (_, mut path) = try_parse!(sp, UriParams::parse_prelude(ecx, &mut parser));
prefix_path(URI_INFO_MACRO_PREFIX, &mut macro_path); prefix_path(URI_INFO_MACRO_PREFIX, &mut path);
// It's incredibly important we use `sp` as the Span for the generated code // 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. // so that errors from the `internal` call show up on the user's code.
let expr = parser.mk_mac_expr(sp, let expr = parser.mk_mac_expr(sp,
ast::Mac_ { ast::Mac_ {
path: macro_path, path: path,
tts: args.to_vec().into_iter().collect::<TokenStream>().into(), tts: args.to_vec().into_iter().collect::<TokenStream>().into(),
}, },
::syntax::util::ThinVec::new(), ::syntax::util::ThinVec::new(),

View File

@ -65,8 +65,14 @@ impl Arg {
impl UriParams { impl UriParams {
// Parses the mount point, if any, and route identifier. // Parses the mount point, if any, and route identifier.
pub fn parse_prelude<'a>( pub fn parse_prelude<'a>(
ecx: &'a ExtCtxt,
parser: &mut Parser<'a> parser: &mut Parser<'a>
) -> PResult<'a, (Option<Spanned<InternedString>>, Path)> { ) -> PResult<'a, (Option<Spanned<InternedString>>, 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. // Parse the mount point and suffixing ',', if any.
let mount_point = match parser.parse_optional_str() { let mount_point = match parser.parse_optional_str() {
Some((symbol, _, _)) => { 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. // 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 there are no arguments, finish early.
if !parser.eat(&Token::Colon) { if !parser.eat(&Token::Colon) {