From 7a281718f0d32ea678f031252c0e130035fc2a80 Mon Sep 17 00:00:00 2001 From: Kang Seonghoon Date: Thu, 3 Apr 2014 16:40:56 +0900 Subject: [PATCH] std: make vec!() macro handle a trailing comma Fixes #12910. --- src/libstd/macros.rs | 3 ++- .../compile-fail/vec-macro-with-comma-only.rs | 13 +++++++++++++ .../run-pass/vec-macro-with-trailing-comma.rs | 15 +++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 src/test/compile-fail/vec-macro-with-comma-only.rs create mode 100644 src/test/run-pass/vec-macro-with-trailing-comma.rs diff --git a/src/libstd/macros.rs b/src/libstd/macros.rs index 1a35252f8ca..fbb48f2ebcb 100644 --- a/src/libstd/macros.rs +++ b/src/libstd/macros.rs @@ -273,7 +273,8 @@ macro_rules! vec( let mut _temp = ::std::vec::Vec::new(); $(_temp.push($e);)* _temp - }) + }); + ($($e:expr),+,) => (vec!($($e),+)) ) diff --git a/src/test/compile-fail/vec-macro-with-comma-only.rs b/src/test/compile-fail/vec-macro-with-comma-only.rs new file mode 100644 index 00000000000..8c8e789cd96 --- /dev/null +++ b/src/test/compile-fail/vec-macro-with-comma-only.rs @@ -0,0 +1,13 @@ +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +pub fn main() { + vec!(,); //~ ERROR unexpected token +} diff --git a/src/test/run-pass/vec-macro-with-trailing-comma.rs b/src/test/run-pass/vec-macro-with-trailing-comma.rs new file mode 100644 index 00000000000..07033d60497 --- /dev/null +++ b/src/test/run-pass/vec-macro-with-trailing-comma.rs @@ -0,0 +1,15 @@ +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + + +pub fn main() { + assert_eq!(vec!(1), vec!(1,)); + assert_eq!(vec!(1, 2, 3), vec!(1, 2, 3,)); +}