Skip to content

Commit d50d31b

Browse files
v-gbBurntSushi
authored andcommitted
hir: make is_alternation_literal say false on Empty
To avoid this assertion in tests when empty alternations are allowed: internal error: entered unreachable code: expected literal or concat, got Hir { kind: Empty, info: HirInfo { bools: 1795 } }', src/exec.rs:1568:18 The code in exec.rs relies on the documented invariant for is_alternation_literal: /// ... This is only true when this HIR expression is either /// itself a `Literal` or a concatenation of only `Literal`s or an /// alternation of only `Literal`s.
1 parent 0785926 commit d50d31b

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

regex-syntax/src/hir/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,8 @@ impl Hir {
241241
info.set_any_anchored_start(false);
242242
info.set_any_anchored_end(false);
243243
info.set_match_empty(true);
244-
info.set_literal(true);
245-
info.set_alternation_literal(true);
244+
info.set_literal(false);
245+
info.set_alternation_literal(false);
246246
Hir { kind: HirKind::Empty, info: info }
247247
}
248248

@@ -671,8 +671,8 @@ impl Hir {
671671
/// true when this HIR expression is either itself a `Literal` or a
672672
/// concatenation of only `Literal`s.
673673
///
674-
/// For example, `f` and `foo` are literals, but `f+`, `(foo)`, `foo()`
675-
/// are not (even though that contain sub-expressions that are literals).
674+
/// For example, `f` and `foo` are literals, but `f+`, `(foo)`, `foo()`,
675+
/// `` are not (even though that contain sub-expressions that are literals).
676676
pub fn is_literal(&self) -> bool {
677677
self.info.is_literal()
678678
}
@@ -682,8 +682,8 @@ impl Hir {
682682
/// true when this HIR expression is either itself a `Literal` or a
683683
/// concatenation of only `Literal`s or an alternation of only `Literal`s.
684684
///
685-
/// For example, `f`, `foo`, `a|b|c`, and `foo|bar|baz` are alternaiton
686-
/// literals, but `f+`, `(foo)`, `foo()`
685+
/// For example, `f`, `foo`, `a|b|c`, and `foo|bar|baz` are alternation
686+
/// literals, but `f+`, `(foo)`, `foo()`, ``
687687
/// are not (even though that contain sub-expressions that are literals).
688688
pub fn is_alternation_literal(&self) -> bool {
689689
self.info.is_alternation_literal()

regex-syntax/src/hir/translate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3105,13 +3105,13 @@ mod tests {
31053105
#[test]
31063106
fn analysis_is_literal() {
31073107
// Positive examples.
3108-
assert!(t(r"").is_literal());
31093108
assert!(t(r"a").is_literal());
31103109
assert!(t(r"ab").is_literal());
31113110
assert!(t(r"abc").is_literal());
31123111
assert!(t(r"(?m)abc").is_literal());
31133112

31143113
// Negative examples.
3114+
assert!(!t(r"").is_literal());
31153115
assert!(!t(r"^").is_literal());
31163116
assert!(!t(r"a|b").is_literal());
31173117
assert!(!t(r"(a)").is_literal());
@@ -3124,7 +3124,6 @@ mod tests {
31243124
#[test]
31253125
fn analysis_is_alternation_literal() {
31263126
// Positive examples.
3127-
assert!(t(r"").is_alternation_literal());
31283127
assert!(t(r"a").is_alternation_literal());
31293128
assert!(t(r"ab").is_alternation_literal());
31303129
assert!(t(r"abc").is_alternation_literal());
@@ -3135,6 +3134,7 @@ mod tests {
31353134
assert!(t(r"foo|bar|baz").is_alternation_literal());
31363135

31373136
// Negative examples.
3137+
assert!(!t(r"").is_alternation_literal());
31383138
assert!(!t(r"^").is_alternation_literal());
31393139
assert!(!t(r"(a)").is_alternation_literal());
31403140
assert!(!t(r"a+").is_alternation_literal());

0 commit comments

Comments
 (0)