Skip to content

Coercion creates false region inference error #37655

Closed
@nikomatsakis

Description

@nikomatsakis

This test case, derived from @sfackler's PR #36340, fails to compile incorrectly:

trait SliceExt<T> {
    type Item;

    fn get_me<I>(&self, index: I) -> &I::Output
        where I: SliceIndex<Self::Item>;
}

impl<T> SliceExt<T> for [T] {
    type Item = T;

    fn get_me<I>(&self, index: I) -> &I::Output
        where I: SliceIndex<T>
    {
        panic!()
    }
}

pub trait SliceIndex<T> {
    type Output: ?Sized;
}

impl<T> SliceIndex<T> for usize {
    type Output = T;
}

fn foo<'a>(split: &[&'a [u8]]) -> &'a [u8] {
    split.get_me(0)
}

fn main() { }

I get the error:

lunch-box. rustc --stage0 ~/tmp/issue-36340.rs
error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
  --> /home/nmatsakis/tmp/issue-36340.rs:27:11
   |
27 |     split.get_me(0)
   |           ^^^^^^
   |
help: consider using an explicit lifetime parameter as shown: fn foo<'a>(split: &'a [&'a [u8]]) -> &'a [u8]
  --> /home/nmatsakis/tmp/issue-36340.rs:26:1
   |
26 | fn foo<'a>(split: &[&'a [u8]]) -> &'a [u8] {
   | ^

error: aborting due to previous error

This is because coercion is going wrong, basically. Note that small changes (such as &**split.get_me(0)) work fine.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions