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

Can't return &mut str ("values differ in mutability") from a method when implementing a trait for str #17361

Closed
japaric opened this issue Sep 18, 2014 · 2 comments · Fixed by #18383
Labels
A-dst Area: Dynamically Sized Types

Comments

@japaric
Copy link
Member

japaric commented Sep 18, 2014

STR

trait Trait for Sized? {
    fn method(&mut Self) -> &mut Self;
}

impl<T> Trait for [T] {
    // This works
    fn method(s: &mut [T]) -> &mut [T] {
        s
    }
}

impl Trait for str {
    // This doesn't work
    fn method(s: &mut str) -> &mut str {
        s
    }
}

// But this works
fn function(s: &mut str) -> &mut str {
    s
}

fn main() {}

Output

str.rs:14:5: 16:6 error: method `method` has an incompatible type for trait: values differ in mutability [E0053]
str.rs:14     fn method(s: &mut str) -> &mut str {
str.rs:15         s
str.rs:16     }
error: aborting due to previous error

Version

rustc 0.12.0-pre (9508faa22 2014-09-17 23:45:36 +0000)

The compiler complains that &mut str and &mut Self where Self = str differ in mutability, but that looks like a bug to me.

As a side note, I found this issue while trying to implement the Borrow trait for str as proposed by the Collections reform RFC.

cc @nick29581

@nrc nrc added the A-dst Area: Dynamically Sized Types label Sep 18, 2014
@nrc
Copy link
Member

nrc commented Sep 18, 2014

Hmm, this does indeed look like it should work and the mutability thing suggests a bug. It is possible that it is just blocked on #17178 though.

@japaric
Copy link
Member Author

japaric commented Oct 22, 2014

Triage: Still reproducible on

rustc 0.13.0-dev (d44ea720f 2014-10-22 09:22:09 +0000)

#17178 has been closed/fixed.

bkoropoff added a commit to bkoropoff/rust that referenced this issue Oct 28, 2014
bkoropoff added a commit to bkoropoff/rust that referenced this issue Oct 28, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-dst Area: Dynamically Sized Types
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants