From dcd91d5ceb1990a48c985e01cbe6fca7e274f016 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Wed, 13 Nov 2019 12:05:37 +0100 Subject: [PATCH] parser: don't use `unreachable!()` in `fn unexpected`. --- src/librustc_parse/parser/mod.rs | 4 +++- .../parser/issue-66357-unexpected-unreachable.rs | 14 ++++++++++++++ .../issue-66357-unexpected-unreachable.stderr | 16 ++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 src/test/ui/parser/issue-66357-unexpected-unreachable.rs create mode 100644 src/test/ui/parser/issue-66357-unexpected-unreachable.stderr diff --git a/src/librustc_parse/parser/mod.rs b/src/librustc_parse/parser/mod.rs index a491d91e20f..d5d8604be85 100644 --- a/src/librustc_parse/parser/mod.rs +++ b/src/librustc_parse/parser/mod.rs @@ -443,7 +443,9 @@ pub(super) fn this_token_descr(&self) -> String { crate fn unexpected(&mut self) -> PResult<'a, T> { match self.expect_one_of(&[], &[]) { Err(e) => Err(e), - Ok(_) => unreachable!(), + // We can get `Ok(true)` from `recover_closing_delimiter` + // which is called in `expected_one_of_not_found`. + Ok(_) => FatalError.raise(), } } diff --git a/src/test/ui/parser/issue-66357-unexpected-unreachable.rs b/src/test/ui/parser/issue-66357-unexpected-unreachable.rs new file mode 100644 index 00000000000..1730adfa914 --- /dev/null +++ b/src/test/ui/parser/issue-66357-unexpected-unreachable.rs @@ -0,0 +1,14 @@ +// The problem in #66357 was that the call trace: +// +// - parse_fn_block_decl +// - expect_or +// - unexpected +// - expect_one_of +// - expected_one_of_not_found +// - recover_closing_delimiter +// +// ended up bubbling up `Ok(true)` to `unexpected` which then used `unreachable!()`. + +fn f() { |[](* } +//~^ ERROR expected one of `,` or `:`, found `(` +//~| ERROR expected one of `)`, `-`, `_`, `box`, `mut`, `ref`, `|`, identifier, or path, found `*` diff --git a/src/test/ui/parser/issue-66357-unexpected-unreachable.stderr b/src/test/ui/parser/issue-66357-unexpected-unreachable.stderr new file mode 100644 index 00000000000..00d84e2afe3 --- /dev/null +++ b/src/test/ui/parser/issue-66357-unexpected-unreachable.stderr @@ -0,0 +1,16 @@ +error: expected one of `,` or `:`, found `(` + --> $DIR/issue-66357-unexpected-unreachable.rs:12:13 + | +LL | fn f() { |[](* } + | ^ expected one of `,` or `:` + +error: expected one of `)`, `-`, `_`, `box`, `mut`, `ref`, `|`, identifier, or path, found `*` + --> $DIR/issue-66357-unexpected-unreachable.rs:12:14 + | +LL | fn f() { |[](* } + | -^ help: `)` may belong here + | | + | unclosed delimiter + +error: aborting due to 2 previous errors +