Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow evaluation and pattern matching within guards (what Haskell calls "pattern guards") #12830

Closed
kmcallister opened this issue Mar 11, 2014 · 3 comments
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@kmcallister
Copy link
Contributor

What Rust calls "pattern guards" are just called "guards" in Haskell. Pattern guards in Haskell allow additional evaluation and a refutable pattern match. If that pattern match fails, it's as if a regular guard returned false. Here's an example, adapted from the HTML5 tokenizer I'm working on.

fn process_char(&mut self, chars: CharSource) {
    match self.state {
        TagOpen => match chars.next() {
            '/' => { self.state = EndTagOpen; }
            c if (Some(a) <= c.to_ascii_opt()) => { do_something(a.to_lower()); }
            c if other_condition => { ... }
            _ => parse_error()

(I'm not advocating for this particular concrete syntax, just trying to get the idea across.)

One can always refactor to avoid the fancy guard, but in general it can produce ugly, hard-to-follow trees of nested matches. In this case I would love to have a single match per tokenizer state which closely follows the specification.

Pattern guards have proven tremendously useful in GHC and were one of the few GHC extensions accepted into Haskell 2010. I think Rust could benefit just as much.

@Aatch
Copy link
Contributor

Aatch commented Mar 11, 2014

Hmm, while not ideal, this does work today:

let a;
let b = Some(1);
match b {
  Some(c) if {
    a = c+1;
    a == 2
  } => println!("{}", a),
  Some(_) => println!("Some"),
  None => println!("None")
}

Prints 2;

@kmcallister
Copy link
Contributor Author

I started using something like that in another part of the library. In this style, the example above would become

match chars.next() {
    '/' => { self.state = EndTagOpen; }
    c if match c.to_ascii_opt() {
        Some(a) => { do_something(a.to_lower()); true }
        _ => false,
    } => (),
    c if other_condition => { ... }
    _ => parse_error()

Which is sort of weird, but easy enough to get used to.

@kmcallister kmcallister added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Sep 27, 2014
@ghost ghost added the B-RFC label Oct 25, 2014
@steveklabnik
Copy link
Member

I'm pulling a massive triage effort to get us ready for 1.0. As part of this, I'm moving stuff that's wishlist-like to the RFCs repo, as that's where major new things should get discussed/prioritized.

This issue has been moved to the RFCs repo: rust-lang/rfcs#680

matthiaskrgr pushed a commit to matthiaskrgr/rust that referenced this issue Aug 2, 2022
…x, r=Veykril

Find original ast node before compute ref match

ref rust-lang/rust-analyzer#12717
flip1995 pushed a commit to flip1995/rust that referenced this issue Jul 25, 2024
Use ControlFlow in more places

Now, instead of manually using variables in visitors to signify that a visit is "done" and that the visitor should stop traversing. We use the trait type "Result" to signify this (in relevant places).

I'll schedule a perf run, I don't think it will be much of a difference, but every bit of performance is welcomed :)

changelog: Improve performance, less memory use in visitors

Fixes rust-lang#12829
r? `@y21`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

3 participants