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

Diagnostics for @ .. idiom use in tuple and tuple struct patterns is incorrect #72574

Closed
chrissimpkins opened this issue May 25, 2020 · 6 comments · Fixed by #72677
Closed

Diagnostics for @ .. idiom use in tuple and tuple struct patterns is incorrect #72574

chrissimpkins opened this issue May 25, 2020 · 6 comments · Fixed by #72677
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@chrissimpkins
Copy link
Member

chrissimpkins commented May 25, 2020

@estebank and I are working on diagnostics for the @ .. rest idiom in slice patterns (#72534) and came across this issue when an attempt is made to use it in tuples and tuple structs.

IIUC, this is allowed:

tuple (playground)

fn main() {
    let x = (1, 2, 3);
    match x {
        (_a, ..) => {}
        _ => {}
    }
}

tuple struct (playground)

struct Binder(i32, i32, i32);

fn main() {
    let x = Binder(1, 2, 3);
    match x {
        Binder(_a, ..) => {}
        _ => {}
    }
}

and this is not allowed (note the addition of [id] @):

tuple (playground)

fn main() {
    let x = (1, 2, 3);
    match x {
        (_a, _x @ ..) => {}
        _ => {}
    }
}

tuple struct (playground)

struct Binder(i32, i32, i32);

fn main() {
    let x = Binder(1, 2, 3);
    match x {
        Binder(_a, _x @ ..) => {}
        _ => {}
    }
}

The error messages for the tuple and tuple struct cases include the following incorrect information:

error: .. patterns are not allowed here

This should state that the @ .. id binding to rest is not allowed. .. patterns are allowed.

note: only allowed in tuple, tuple struct, and slice patterns

@ .. is only allowed in slice patterns, not in tuple or tuple struct patterns. This should state that the idiom is only allowed in slice patterns.

@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 25, 2020
@chrissimpkins
Copy link
Member Author

librustc_ast_lowering appears to be the site of the .. check:

PatKind::Rest => {
// If we reach here the `..` pattern is not semantically allowed.
self.ban_illegal_rest_pat(p.span)

This LoweringContext method is called:

/// Used to ban the `..` pattern in places it shouldn't be semantically.
fn ban_illegal_rest_pat(&self, sp: Span) -> hir::PatKind<'hir> {
self.diagnostic()
.struct_span_err(sp, "`..` patterns are not allowed here")
.note("only allowed in tuple, tuple struct, and slice patterns")
.emit();
// We're not in a list context so `..` can be reasonably treated
// as `_` because it should always be valid and roughly matches the
// intent of `..` (notice that the rest of a single slot is that slot).
hir::PatKind::Wild
}

@leonardo-m
Copy link

Perhaps it's worth supporting "tuple slicing" pattern too in Rust:

(a, xs @ ..) => {}

@estebank
Copy link
Contributor

@leonardo-m that would implicitly create a tuple for xs (or a slice?). I don't think it'd be a good addition. We already support (a, ..), which is much more useful in my eyes.

@leonardo-m
Copy link

That syntax is meant to produce a shorter tuple xs with the omission of the first item of the tuple. lf you think it's an useless idea then let's ignore it :)

@chrissimpkins
Copy link
Member Author

@rustbot claim

@chrissimpkins
Copy link
Member Author

#72677

@bors bors closed this as completed in ca8640e May 30, 2020
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 C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants