Skip to content

Commit

Permalink
Rollup merge of #81080 - bugadani:vec-diag, r=oli-obk,m-ou-se
Browse files Browse the repository at this point in the history
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
  • Loading branch information
m-ou-se authored Jan 17, 2021
2 parents 92dbfb5 + c127ed6 commit 19370a4
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 66 deletions.
30 changes: 1 addition & 29 deletions compiler/rustc_expand/src/mbe/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,36 +56,11 @@ crate fn annotate_err_with_kind(
};
}

/// Instead of e.g. `vec![a, b, c]` in a pattern context, suggest `[a, b, c]`.
fn suggest_slice_pat(e: &mut DiagnosticBuilder<'_>, site_span: Span, parser: &Parser<'_>) {
let mut suggestion = None;
if let Ok(code) = parser.sess.source_map().span_to_snippet(site_span) {
if let Some(bang) = code.find('!') {
suggestion = Some(code[bang + 1..].to_string());
}
}
if let Some(suggestion) = suggestion {
e.span_suggestion(
site_span,
"use a slice pattern here instead",
suggestion,
Applicability::MachineApplicable,
);
} else {
e.span_label(site_span, "use a slice pattern here instead");
}
e.help(
"for more information, see https://doc.rust-lang.org/edition-guide/\
rust-2018/slice-patterns.html",
);
}

fn emit_frag_parse_err(
mut e: DiagnosticBuilder<'_>,
parser: &Parser<'_>,
orig_parser: &mut Parser<'_>,
site_span: Span,
macro_ident: Ident,
arm_span: Span,
kind: AstFragmentKind,
) {
Expand Down Expand Up @@ -113,9 +88,6 @@ fn emit_frag_parse_err(
e.span_label(site_span, "in this macro invocation");
}
match kind {
AstFragmentKind::Pat if macro_ident.name == sym::vec => {
suggest_slice_pat(&mut e, site_span, parser);
}
// Try a statement if an expression is wanted but failed and suggest adding `;` to call.
AstFragmentKind::Expr => match parse_ast_fragment(orig_parser, AstFragmentKind::Stmts) {
Err(mut err) => err.cancel(),
Expand Down Expand Up @@ -143,7 +115,7 @@ impl<'a> ParserAnyMacro<'a> {
let fragment = match parse_ast_fragment(parser, kind) {
Ok(f) => f,
Err(err) => {
emit_frag_parse_err(err, parser, snapshot, site_span, macro_ident, arm_span, kind);
emit_frag_parse_err(err, parser, snapshot, site_span, arm_span, kind);
return kind.dummy(site_span);
}
};
Expand Down
8 changes: 8 additions & 0 deletions library/alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
#![feature(type_alias_impl_trait)]
#![feature(associated_type_bounds)]
#![feature(slice_group_by)]
#![feature(decl_macro)]
// Allow testing this library

#[cfg(test)]
Expand Down Expand Up @@ -193,4 +194,11 @@ mod std {
#[unstable(feature = "liballoc_internals", issue = "none", reason = "implementation detail")]
pub mod __export {
pub use core::format_args;

/// Force AST node to an expression to improve diagnostics in pattern position.
#[rustc_macro_transparency = "semitransparent"]
#[unstable(feature = "liballoc_internals", issue = "none", reason = "implementation detail")]
pub macro force_expr($e:expr) {
$e
}
}
8 changes: 4 additions & 4 deletions library/alloc/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@
#[cfg(not(test))]
#[macro_export]
#[stable(feature = "rust1", since = "1.0.0")]
#[allow_internal_unstable(box_syntax)]
#[allow_internal_unstable(box_syntax, liballoc_internals)]
macro_rules! vec {
() => (
$crate::vec::Vec::new()
$crate::__export::force_expr!($crate::vec::Vec::new())
);
($elem:expr; $n:expr) => (
$crate::vec::from_elem($elem, $n)
$crate::__export::force_expr!($crate::vec::from_elem($elem, $n))
);
($($x:expr),+ $(,)?) => (
<[_]>::into_vec(box [$($x),+])
$crate::__export::force_expr!(<[_]>::into_vec(box [$($x),+]))
);
}

Expand Down
10 changes: 10 additions & 0 deletions src/test/ui/macros/vec-macro-in-pattern.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// This is a regression test for #61933
// Verify that the vec![] macro may not be used in patterns
// and that the resulting diagnostic is actually helpful.

fn main() {
match Some(vec![42]) {
Some(vec![43]) => {} //~ ERROR arbitrary expressions aren't allowed in patterns
_ => {}
}
}
10 changes: 10 additions & 0 deletions src/test/ui/macros/vec-macro-in-pattern.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: arbitrary expressions aren't allowed in patterns
--> $DIR/vec-macro-in-pattern.rs:7:14
|
LL | Some(vec![43]) => {}
| ^^^^^^^^
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error

8 changes: 0 additions & 8 deletions src/test/ui/suggestions/vec-macro-in-pattern.fixed

This file was deleted.

8 changes: 0 additions & 8 deletions src/test/ui/suggestions/vec-macro-in-pattern.rs

This file was deleted.

16 changes: 0 additions & 16 deletions src/test/ui/suggestions/vec-macro-in-pattern.stderr

This file was deleted.

2 changes: 1 addition & 1 deletion src/test/ui/type/ascription/issue-47666.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: expected type, found reserved keyword `box`
error: expected type, found `<[_]>::into_vec(box [0, 1])`
--> $DIR/issue-47666.rs:3:25
|
LL | let _ = Option:Some(vec![0, 1]);
Expand Down

0 comments on commit 19370a4

Please sign in to comment.