Skip to content

Consider desugaring the pattern in match to a let to improve error messages #14390

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

Closed
huonw opened this issue May 24, 2014 · 0 comments
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@huonw
Copy link
Member

huonw commented May 24, 2014

Proposal: desugar the Some branch a for loop to Some(value) => { let <pattern> = value; ..., rather than Some(<pattern>) => { ... as we do now.

#[cfg(normal)]
fn main() {
    for &1 in [1].iter() {}
}

// expanded forms:

#[cfg(now)]
fn main() {
    match &mut [1].iter() {
        it => {
            loop {
                match it.next() {
                    None => break,
                    Some(&1) => {}
                }
            }
        }
    }
}

#[cfg(future)]
fn main() {
    match &mut [1].iter() {
        it => {
            loop {
                match it.next() {
                    None => break,
                    Some(value) => {
                        let &1 = value;
                    }
                }
            }
        }
    }
}
$ rustc patterns.rs --cfg normal
patterns.rs:3:5: 4:2 error: non-exhaustive patterns
patterns.rs:3     for &1 in [1].iter() {}
patterns.rs:4 }
error: aborting due to previous error

$ rustc patterns.rs --cfg now
patterns.rs:11:17: 14:18 error: non-exhaustive patterns
patterns.rs:11                 match it.next() {
patterns.rs:12                     None => break,
patterns.rs:13                     Some(&1) => {}
patterns.rs:14                 }
error: aborting due to previous error

$ rustc patterns.rs --cfg future
patterns.rs:28:29: 28:31 error: refutable pattern in local binding
patterns.rs:28                         let &1 = value;
                                           ^~
error: aborting due to previous error

Using the let version would give two advantages:

  • mentions that the pattern is refutable
  • allows the span of the error to point exactly at the pattern, rather than the whole for loop (annoying for beginners if the for has a large body).
bors added a commit that referenced this issue May 27, 2014
Change `for` desugaring & make refutable pattern errors more precise

This changes for to desugar to the `let`-based pattern match as described in #14390, and adjusts the compiler to use this information for error messages that even mention that it's in a `for` loop.

Also, it makes the compiler record the exact positions of refutable parts of a pattern, to point to exactly them in error messages.
@huonw huonw closed this as completed in f2a1378 May 27, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints
Projects
None yet
Development

No branches or pull requests

1 participant