Skip to content

Infer type in irrefutable slice patterns with fixed length as array #113199

Merged
bors merged 11 commits intorust-lang:masterfrom
b-naber:slice-pattern-type-inference
Aug 3, 2023
Merged

Infer type in irrefutable slice patterns with fixed length as array #113199
bors merged 11 commits intorust-lang:masterfrom
b-naber:slice-pattern-type-inference

Conversation

@b-naber
Copy link
Contributor

@b-naber b-naber commented Jun 30, 2023

Fixes #76342

In irrefutable slice patterns with a fixed length, we can infer the type as an array type. We now choose to prefer some implementations over others, e.g. in:

struct Zeroes;

const ARR: [usize; 2] = [0; 2];
const ARR2: [usize; 2] = [2; 2];

impl Into<&'static [usize; 2]> for Zeroes {
    fn into(self) -> &'static [usize; 2] {
        &ARR
    }
}

impl Into<&'static [usize]> for Zeroes {
    fn into(self) -> &'static [usize] {
        &ARR2
    }
}

fn main() {
    let &[a, b] = Zeroes.into();
}

We now prefer the impl candidate impl Into<&'static [usize; 2]> for Zeroes, it's not entirely clear to me that this is correct, but given that the slice impl would require a type annotation anyway, this doesn't seem unreasonable.

r? @lcnr

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. merged-by-bors This PR was explicitly merged by bors. relnotes Marks issues that should be documented in the release notes of the next release. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-types Relevant to the types team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Irrefutable slice patterns should be inferred as arrays of that length

9 participants