Skip to content

Commit 19370a4

Browse files
authored
Rollup merge of #81080 - bugadani:vec-diag, r=oli-obk,m-ou-se
Force vec![] to expression position only r? `@oli-obk` I went with the lazy way of only changing what broke. I moved the test to ui/macros because the diagnostics no longer give suggestions. Closes #61933
2 parents 92dbfb5 + c127ed6 commit 19370a4

File tree

9 files changed

+34
-66
lines changed

9 files changed

+34
-66
lines changed

compiler/rustc_expand/src/mbe/macro_rules.rs

+1-29
Original file line numberDiff line numberDiff line change
@@ -56,36 +56,11 @@ crate fn annotate_err_with_kind(
5656
};
5757
}
5858

59-
/// Instead of e.g. `vec![a, b, c]` in a pattern context, suggest `[a, b, c]`.
60-
fn suggest_slice_pat(e: &mut DiagnosticBuilder<'_>, site_span: Span, parser: &Parser<'_>) {
61-
let mut suggestion = None;
62-
if let Ok(code) = parser.sess.source_map().span_to_snippet(site_span) {
63-
if let Some(bang) = code.find('!') {
64-
suggestion = Some(code[bang + 1..].to_string());
65-
}
66-
}
67-
if let Some(suggestion) = suggestion {
68-
e.span_suggestion(
69-
site_span,
70-
"use a slice pattern here instead",
71-
suggestion,
72-
Applicability::MachineApplicable,
73-
);
74-
} else {
75-
e.span_label(site_span, "use a slice pattern here instead");
76-
}
77-
e.help(
78-
"for more information, see https://doc.rust-lang.org/edition-guide/\
79-
rust-2018/slice-patterns.html",
80-
);
81-
}
82-
8359
fn emit_frag_parse_err(
8460
mut e: DiagnosticBuilder<'_>,
8561
parser: &Parser<'_>,
8662
orig_parser: &mut Parser<'_>,
8763
site_span: Span,
88-
macro_ident: Ident,
8964
arm_span: Span,
9065
kind: AstFragmentKind,
9166
) {
@@ -113,9 +88,6 @@ fn emit_frag_parse_err(
11388
e.span_label(site_span, "in this macro invocation");
11489
}
11590
match kind {
116-
AstFragmentKind::Pat if macro_ident.name == sym::vec => {
117-
suggest_slice_pat(&mut e, site_span, parser);
118-
}
11991
// Try a statement if an expression is wanted but failed and suggest adding `;` to call.
12092
AstFragmentKind::Expr => match parse_ast_fragment(orig_parser, AstFragmentKind::Stmts) {
12193
Err(mut err) => err.cancel(),
@@ -143,7 +115,7 @@ impl<'a> ParserAnyMacro<'a> {
143115
let fragment = match parse_ast_fragment(parser, kind) {
144116
Ok(f) => f,
145117
Err(err) => {
146-
emit_frag_parse_err(err, parser, snapshot, site_span, macro_ident, arm_span, kind);
118+
emit_frag_parse_err(err, parser, snapshot, site_span, arm_span, kind);
147119
return kind.dummy(site_span);
148120
}
149121
};

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)