Skip to content

Multiple assignment after declaration #16759

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
tstorch opened this issue Aug 26, 2014 · 2 comments
Closed

Multiple assignment after declaration #16759

tstorch opened this issue Aug 26, 2014 · 2 comments

Comments

@tstorch
Copy link
Contributor

tstorch commented Aug 26, 2014

Currently multiple assignemnt is supported at declaration like this

let (critPos, period) =
    if critPos1 > critPos2 {
        (critPos1, period1)
    } else {
        (critPos2, period2)
    };

I think it would be nice to be able do something like this:

// "improved" version of  maximal_suffix in src/libcore/str.rs

fn maximal_suffix(arr: &[u8], reversed: bool) -> (uint, uint) {
    let mut left = -1; // Corresponds to i in the paper
    let mut right = 0; // Corresponds to j in the paper
    let mut offset = 1; // Corresponds to k in the paper
    let mut period = 1; // Corresponds to p in the paper

    while right + offset < arr.len() {
        let (a, b) =
            if reversed {
                (arr[left + offset], arr[right + offset])
            } else {
                (arr[right + offset], arr[left + offset])
            };
        // THIS IS THE IMPORTANT PART
        (left, right, offset, period) =
            if a < b {
                // Suffix is smaller, period is entire prefix so far.
                (left, right + offset, 1, right - left)
            } else if a == b {
                // Advance through repetition of the current period.
                if offset == period {
                    (left, right + offset, 1, period)
                } else {
                    (left, right, offset + 1, period)
                }
            } else {
                // Suffix is larger, start over from current location.
                (right, right + 1, 1, 1)
            };
    }
    (left + 1, period)
}

Currently something similar is at the moment only possible with a small workaround:

fn maximal_suffix(arr: &[u8], reversed: bool) -> (uint, uint) {
    let mut left = -1; // Corresponds to i in the paper
    let mut right = 0; // Corresponds to j in the paper
    let mut offset = 1; // Corresponds to k in the paper
    let mut period = 1; // Corresponds to p in the paper

    while right + offset < arr.len() {
        let (a, b) =
            if reversed {
                (arr[left + offset], arr[right + offset])
            } else {
                (arr[right + offset], arr[left + offset])
            };
        // HERE I NEED TO INTRODUCE TEMPORARY VARIABLES
        let (left_temp, right_temp, offset_temp, period_temp) =
            if a < b {
                // Suffix is smaller, period is entire prefix so far.
                (left, right + offset, 1, right - left)
            } else if a == b {
                // Advance through repetition of the current period.
                if offset == period {
                    (left, right + offset, 1, period)
                } else {
                    (left, right, offset + 1, period)
                }
            } else {
                // Suffix is larger, start over from current location.
                (right, right + 1, 1, 1)
            };
        // AND NOW I CAN PUT THE VALUES BACK IN THE REAL VARIABLES
        left = left_temp;
        right = right_temp;
        offset = offset_temp;
        period = period_temp;
    }
    (left + 1, period)
}
@rofrol
Copy link

rofrol commented Dec 30, 2014

dup of #10174

@steveklabnik
Copy link
Member

Yes, closing as a duplicate.

matthiaskrgr pushed a commit to matthiaskrgr/rust that referenced this issue Mar 10, 2024
…ange-pattern, r=Veykril

fix: goto-definition for constants inside range pattern

Fix rust-lang#15653.

This PR addresses the issue where it was not possible to navigate to constants in range patterns, specifically including two major changes:

1. Previously, both the `start` and `end` fields in `Pat::Range` were of type `LiteralOrConst`. When performing `goto-definition` on constants inside range patterns, r-a would use `resolve_bind_pat_to_const` to find their definitions. However, because the content of a `Const` is not `Pat` but `Path`, it was not stored in the `source_map`, so `resolve_bind_pat_to_const` would returns `None`. This PR changes them to `Const(PatId)`, so that during the lowering process, they are considered as a `pat`, allowing their definitions to be found later through `resolve_bind_pat_to_const`.

2. The process related to range patterns in MIR-lowering has been modified to correctly handle the above changes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants