From 9d1211e37f65204307ed23debc0b2012e15993a3 Mon Sep 17 00:00:00 2001 From: rhysd Date: Fri, 15 Jul 2022 17:26:01 +0900 Subject: [PATCH 1/2] use `#[non_exhaustive]` attribute instead of `__Nonexhaustive` enum variant hack --- regex-syntax/src/ast/mod.rs | 9 +-------- regex-syntax/src/error.rs | 10 +--------- regex-syntax/src/hir/mod.rs | 9 +-------- src/error.rs | 13 +------------ 4 files changed, 4 insertions(+), 37 deletions(-) diff --git a/regex-syntax/src/ast/mod.rs b/regex-syntax/src/ast/mod.rs index 387ea3a69..2276ec5a9 100644 --- a/regex-syntax/src/ast/mod.rs +++ b/regex-syntax/src/ast/mod.rs @@ -66,6 +66,7 @@ impl Error { /// The type of an error that occurred while building an AST. #[derive(Clone, Debug, Eq, PartialEq)] +#[non_exhaustive] pub enum ErrorKind { /// The capturing group limit was exceeded. /// @@ -169,13 +170,6 @@ pub enum ErrorKind { /// `(? unreachable!(), } } } diff --git a/regex-syntax/src/error.rs b/regex-syntax/src/error.rs index 1230d2fc5..2e10dec89 100644 --- a/regex-syntax/src/error.rs +++ b/regex-syntax/src/error.rs @@ -11,6 +11,7 @@ pub type Result = result::Result; /// This error type encompasses any error that can be returned by this crate. #[derive(Clone, Debug, Eq, PartialEq)] +#[non_exhaustive] pub enum Error { /// An error that occurred while translating concrete syntax into abstract /// syntax (AST). @@ -18,13 +19,6 @@ pub enum Error { /// An error that occurred while translating abstract syntax into a high /// level intermediate representation (HIR). Translate(hir::Error), - /// Hints that destructuring should not be exhaustive. - /// - /// This enum may grow additional variants, so this makes sure clients - /// don't count on exhaustive matching. (Otherwise, adding a new variant - /// could break existing code.) - #[doc(hidden)] - __Nonexhaustive, } impl From for Error { @@ -46,7 +40,6 @@ impl error::Error for Error { match *self { Error::Parse(ref x) => x.description(), Error::Translate(ref x) => x.description(), - _ => unreachable!(), } } } @@ -56,7 +49,6 @@ impl fmt::Display for Error { match *self { Error::Parse(ref x) => x.fmt(f), Error::Translate(ref x) => x.fmt(f), - _ => unreachable!(), } } } diff --git a/regex-syntax/src/hir/mod.rs b/regex-syntax/src/hir/mod.rs index 1096e9f05..09d416560 100644 --- a/regex-syntax/src/hir/mod.rs +++ b/regex-syntax/src/hir/mod.rs @@ -54,6 +54,7 @@ impl Error { /// The type of an error that occurred while building an `Hir`. #[derive(Clone, Debug, Eq, PartialEq)] +#[non_exhaustive] pub enum ErrorKind { /// This error occurs when a Unicode feature is used when Unicode /// support is disabled. For example `(?-u:\pL)` would trigger this error. @@ -81,13 +82,6 @@ pub enum ErrorKind { /// Note that this restriction in the translator may be removed in the /// future. EmptyClassNotAllowed, - /// Hints that destructuring should not be exhaustive. - /// - /// This enum may grow additional variants, so this makes sure clients - /// don't count on exhaustive matching. (Otherwise, adding a new variant - /// could break existing code.) - #[doc(hidden)] - __Nonexhaustive, } impl ErrorKind { @@ -109,7 +103,6 @@ impl ErrorKind { (make sure the unicode-case feature is enabled)" } EmptyClassNotAllowed => "empty character classes are not allowed", - __Nonexhaustive => unreachable!(), } } } diff --git a/src/error.rs b/src/error.rs index 3e0ec7521..189f4ca10 100644 --- a/src/error.rs +++ b/src/error.rs @@ -3,19 +3,13 @@ use std::iter::repeat; /// An error that occurred during parsing or compiling a regular expression. #[derive(Clone, PartialEq)] +#[non_exhaustive] pub enum Error { /// A syntax error. Syntax(String), /// The compiled program exceeded the set size limit. /// The argument is the size limit imposed. CompiledTooBig(usize), - /// Hints that destructuring should not be exhaustive. - /// - /// This enum may grow additional variants, so this makes sure clients - /// don't count on exhaustive matching. (Otherwise, adding a new variant - /// could break existing code.) - #[doc(hidden)] - __Nonexhaustive, } impl ::std::error::Error for Error { @@ -25,7 +19,6 @@ impl ::std::error::Error for Error { match *self { Error::Syntax(ref err) => err, Error::CompiledTooBig(_) => "compiled program too big", - Error::__Nonexhaustive => unreachable!(), } } } @@ -39,7 +32,6 @@ impl fmt::Display for Error { "Compiled regex exceeds size limit of {} bytes.", limit ), - Error::__Nonexhaustive => unreachable!(), } } } @@ -63,9 +55,6 @@ impl fmt::Debug for Error { Error::CompiledTooBig(limit) => { f.debug_tuple("CompiledTooBig").field(&limit).finish() } - Error::__Nonexhaustive => { - f.debug_tuple("__Nonexhaustive").finish() - } } } } From 31d78a64aa815e1f32ca07b998398b6012285751 Mon Sep 17 00:00:00 2001 From: rhysd Date: Fri, 15 Jul 2022 17:30:29 +0900 Subject: [PATCH 2/2] fix `RepetitionCountDecimalEmpty` is not covered in `ast::Error::description` --- regex-syntax/src/ast/mod.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/regex-syntax/src/ast/mod.rs b/regex-syntax/src/ast/mod.rs index 2276ec5a9..de346d836 100644 --- a/regex-syntax/src/ast/mod.rs +++ b/regex-syntax/src/ast/mod.rs @@ -202,13 +202,15 @@ impl error::Error for Error { GroupUnclosed => "unclosed group", GroupUnopened => "unopened group", NestLimitExceeded(_) => "nest limit exceeded", + RepetitionCountDecimalEmpty => { + "invalid decimal in repetition quantifier" + } RepetitionCountInvalid => "invalid repetition count range", RepetitionCountUnclosed => "unclosed counted repetition", RepetitionMissing => "repetition operator missing expression", UnicodeClassInvalid => "invalid Unicode character class", UnsupportedBackreference => "backreferences are not supported", UnsupportedLookAround => "look-around is not supported", - _ => unreachable!(), } } }