Skip to content

appropriate lifetime and match #7502

Closed
Closed
@remifontan

Description

@remifontan

following code does not compile:

struct Vec { x:float, y:float, z:float }
impl Vec {
    pub fn getRef<'a>(&'a mut self, i:uint) -> &'a mut float {
        // if(i==0) { &mut self.x }
        // else if(i==1) { &mut self.x }
        // else {&mut self.x }

        match(i) {
            0 => &mut self.x,
            1 => &mut self.y,
            _ => &mut self.z
        }
    }
}

I get following errors:

rustc test.rs -o test-test --test
test.rs:122:17: 122:28 error: cannot infer an appropriate lifetime due to conflicting requirements
test.rs:122             _ => &mut self.z
                             ^~~~~~~~~~~
test.rs:121:17: 121:28 note: first, the lifetime must be contained by the expression at 121:17...
test.rs:121             1 => &mut self.y,
                             ^~~~~~~~~~~
test.rs:121:17: 121:28 note: ...due to the following expression
test.rs:121             1 => &mut self.y,
                             ^~~~~~~~~~~
test.rs:120:17: 120:28 note: but, the lifetime must also be contained by the expression at 120:17...
test.rs:120             0 => &mut self.x,
                             ^~~~~~~~~~~
test.rs:120:17: 120:28 note: ...due to the following expression
test.rs:120             0 => &mut self.x,
                             ^~~~~~~~~~~
error: aborting due to previous error
make: *** [test-test] Error 101

The commented version that is using if statements does compile correctly.

I haven't tried yet with very latest build, so I'm not sure whether the problem is still there with rust 0.7

cheers,

Rémi

Activity

nikomatsakis

nikomatsakis commented on Jul 3, 2013

@nikomatsakis
Contributor

This looks like a bug, I agree. I suspect I know the cause of the error, but I'l need to investigate.l

thestinger

thestinger commented on Sep 23, 2013

@thestinger
Contributor

This now compiles fine, due to the borrow checking improvements since 0.6.

added a commit that references this issue on Jul 29, 2021

Auto merge of rust-lang#7502 - flip1995:rollup-y3ho3w0, r=flip1995

added 2 commits that reference this issue on Nov 4, 2021

Auto merge of rust-lang#7857 - flip1995:fix_deploy, r=xFrednet

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

Metadata

Metadata

Assignees

Labels

A-lifetimesArea: Lifetimes / regions

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @nikomatsakis@thestinger@remifontan

      Issue actions

        appropriate lifetime and match · Issue #7502 · rust-lang/rust