Skip to content

Commit c127ed6

Browse files
committed
Force vec! to expressions only
1 parent f07dd6d commit c127ed6

8 files changed

+33
-37
lines changed

library/alloc/src/lib.rs

+8
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@
140140
#![feature(type_alias_impl_trait)]
141141
#![feature(associated_type_bounds)]
142142
#![feature(slice_group_by)]
143+
#![feature(decl_macro)]
143144
// Allow testing this library
144145

145146
#[cfg(test)]
@@ -193,4 +194,11 @@ mod std {
193194
#[unstable(feature = "liballoc_internals", issue = "none", reason = "implementation detail")]
194195
pub mod __export {
195196
pub use core::format_args;
197+
198+
/// Force AST node to an expression to improve diagnostics in pattern position.
199+
#[rustc_macro_transparency = "semitransparent"]
200+
#[unstable(feature = "liballoc_internals", issue = "none", reason = "implementation detail")]
201+
pub macro force_expr($e:expr) {
202+
$e
203+
}
196204
}

library/alloc/src/macros.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,16 @@
3737
#[cfg(not(test))]
3838
#[macro_export]
3939
#[stable(feature = "rust1", since = "1.0.0")]
40-
#[allow_internal_unstable(box_syntax)]
40+
#[allow_internal_unstable(box_syntax, liballoc_internals)]
4141
macro_rules! vec {
4242
() => (
43-
$crate::vec::Vec::new()
43+
$crate::__export::force_expr!($crate::vec::Vec::new())
4444
);
4545
($elem:expr; $n:expr) => (
46-
$crate::vec::from_elem($elem, $n)
46+
$crate::__export::force_expr!($crate::vec::from_elem($elem, $n))
4747
);
4848
($($x:expr),+ $(,)?) => (
49-
<[_]>::into_vec(box [$($x),+])
49+
$crate::__export::force_expr!(<[_]>::into_vec(box [$($x),+]))
5050
);
5151
}
5252

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// This is a regression test for #61933
2+
// Verify that the vec![] macro may not be used in patterns
3+
// and that the resulting diagnostic is actually helpful.
4+
5+
fn main() {
6+
match Some(vec![42]) {
7+
Some(vec![43]) => {} //~ ERROR arbitrary expressions aren't allowed in patterns
8+
_ => {}
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: arbitrary expressions aren't allowed in patterns
2+
--> $DIR/vec-macro-in-pattern.rs:7:14
3+
|
4+
LL | Some(vec![43]) => {}
5+
| ^^^^^^^^
6+
|
7+
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
8+
9+
error: aborting due to previous error
10+

src/test/ui/suggestions/vec-macro-in-pattern.fixed

-8
This file was deleted.

src/test/ui/suggestions/vec-macro-in-pattern.rs

-8
This file was deleted.

src/test/ui/suggestions/vec-macro-in-pattern.stderr

-16
This file was deleted.

src/test/ui/type/ascription/issue-47666.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: expected type, found reserved keyword `box`
1+
error: expected type, found `<[_]>::into_vec(box [0, 1])`
22
--> $DIR/issue-47666.rs:3:25
33
|
44
LL | let _ = Option:Some(vec![0, 1]);

0 commit comments

Comments
 (0)