From 48e863ed69cb29a0b4ff5e20519d30227a1f8df5 Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Thu, 30 May 2019 15:37:10 +0200 Subject: [PATCH] Harden non-const items to not accept underscore names. --- .../ui/parser/underscore_item_not_const.rs | 30 ++++++ .../parser/underscore_item_not_const.stderr | 92 +++++++++++++++++++ src/test/ui/parser/underscore_static.rs | 3 - src/test/ui/parser/underscore_static.stderr | 8 -- 4 files changed, 122 insertions(+), 11 deletions(-) create mode 100644 src/test/ui/parser/underscore_item_not_const.rs create mode 100644 src/test/ui/parser/underscore_item_not_const.stderr delete mode 100644 src/test/ui/parser/underscore_static.rs delete mode 100644 src/test/ui/parser/underscore_static.stderr diff --git a/src/test/ui/parser/underscore_item_not_const.rs b/src/test/ui/parser/underscore_item_not_const.rs new file mode 100644 index 0000000000000..375bdc3a46369 --- /dev/null +++ b/src/test/ui/parser/underscore_item_not_const.rs @@ -0,0 +1,30 @@ +// Test that various non-const items and associated consts do not permit `_` as a name. + +// Associated `const`s: + +pub trait A { + const _: () = (); //~ ERROR expected identifier, found reserved identifier `_` +} +impl A for () { + const _: () = (); //~ ERROR expected identifier, found reserved identifier `_` +} +impl dyn A { + const _: () = (); //~ ERROR expected identifier, found reserved identifier `_` +} + +// Other kinds of items: + +static _: () = (); //~ ERROR expected identifier, found reserved identifier `_` +struct _(); //~ ERROR expected identifier, found reserved identifier `_` +enum _ {} //~ ERROR expected identifier, found reserved identifier `_` +fn _() {} //~ ERROR expected identifier, found reserved identifier `_` +mod _ {} //~ ERROR expected identifier, found reserved identifier `_` +type _ = (); //~ ERROR expected identifier, found reserved identifier `_` +use _; //~ ERROR expected identifier, found reserved identifier `_` +use _ as g; //~ ERROR expected identifier, found reserved identifier `_` +trait _ {} //~ ERROR expected identifier, found reserved identifier `_` +trait _ = Copy; //~ ERROR expected identifier, found reserved identifier `_` +macro_rules! _ { () => {} } //~ ERROR expected identifier, found reserved identifier `_` +union _ { f: u8 } //~ ERROR expected one of `!` or `::`, found `_` + +fn main() {} diff --git a/src/test/ui/parser/underscore_item_not_const.stderr b/src/test/ui/parser/underscore_item_not_const.stderr new file mode 100644 index 0000000000000..deb4a012e32ab --- /dev/null +++ b/src/test/ui/parser/underscore_item_not_const.stderr @@ -0,0 +1,92 @@ +error: expected identifier, found reserved identifier `_` + --> $DIR/underscore_item_not_const.rs:6:11 + | +LL | const _: () = (); + | ^ expected identifier, found reserved identifier + +error: expected identifier, found reserved identifier `_` + --> $DIR/underscore_item_not_const.rs:9:11 + | +LL | const _: () = (); + | ^ expected identifier, found reserved identifier + +error: expected identifier, found reserved identifier `_` + --> $DIR/underscore_item_not_const.rs:12:11 + | +LL | const _: () = (); + | ^ expected identifier, found reserved identifier + +error: expected identifier, found reserved identifier `_` + --> $DIR/underscore_item_not_const.rs:17:8 + | +LL | static _: () = (); + | ^ expected identifier, found reserved identifier + +error: expected identifier, found reserved identifier `_` + --> $DIR/underscore_item_not_const.rs:18:8 + | +LL | struct _(); + | ^ expected identifier, found reserved identifier + +error: expected identifier, found reserved identifier `_` + --> $DIR/underscore_item_not_const.rs:19:6 + | +LL | enum _ {} + | ^ expected identifier, found reserved identifier + +error: expected identifier, found reserved identifier `_` + --> $DIR/underscore_item_not_const.rs:20:4 + | +LL | fn _() {} + | ^ expected identifier, found reserved identifier + +error: expected identifier, found reserved identifier `_` + --> $DIR/underscore_item_not_const.rs:21:5 + | +LL | mod _ {} + | ^ expected identifier, found reserved identifier + +error: expected identifier, found reserved identifier `_` + --> $DIR/underscore_item_not_const.rs:22:6 + | +LL | type _ = (); + | ^ expected identifier, found reserved identifier + +error: expected identifier, found reserved identifier `_` + --> $DIR/underscore_item_not_const.rs:23:5 + | +LL | use _; + | ^ expected identifier, found reserved identifier + +error: expected identifier, found reserved identifier `_` + --> $DIR/underscore_item_not_const.rs:24:5 + | +LL | use _ as g; + | ^ expected identifier, found reserved identifier + +error: expected identifier, found reserved identifier `_` + --> $DIR/underscore_item_not_const.rs:25:7 + | +LL | trait _ {} + | ^ expected identifier, found reserved identifier + +error: expected identifier, found reserved identifier `_` + --> $DIR/underscore_item_not_const.rs:26:7 + | +LL | trait _ = Copy; + | ^ expected identifier, found reserved identifier + +error: expected identifier, found reserved identifier `_` + --> $DIR/underscore_item_not_const.rs:27:14 + | +LL | macro_rules! _ { () => {} } + | ^ expected identifier, found reserved identifier + +error: expected one of `!` or `::`, found `_` + --> $DIR/underscore_item_not_const.rs:28:7 + | +LL | union _ { f: u8 } + | ^ expected one of `!` or `::` here + +error: aborting due to 15 previous errors + diff --git a/src/test/ui/parser/underscore_static.rs b/src/test/ui/parser/underscore_static.rs deleted file mode 100644 index 21d6a1bc1b360..0000000000000 --- a/src/test/ui/parser/underscore_static.rs +++ /dev/null @@ -1,3 +0,0 @@ -static _: () = (); //~ ERROR expected identifier, found reserved identifier `_` - -fn main() {} diff --git a/src/test/ui/parser/underscore_static.stderr b/src/test/ui/parser/underscore_static.stderr deleted file mode 100644 index 4c41afdc3f09e..0000000000000 --- a/src/test/ui/parser/underscore_static.stderr +++ /dev/null @@ -1,8 +0,0 @@ -error: expected identifier, found reserved identifier `_` - --> $DIR/underscore_static.rs:1:8 - | -LL | static _: () = (); - | ^ expected identifier, found reserved identifier - -error: aborting due to previous error -