Skip to content

lifetime inference fails on *const &'a T but succeeds for *mut &'a T -- why? #21422

Closed
@pnkfelix

Description

@pnkfelix

Intuitively, I would think that replacing an occurrence of *mut T with *const T is just loosening constraints.

Unfortunately, if you remove too many constraints, apparently lifetime inference fails.

Here is an example reduced down from my attempt to make Rc<T>/Box<T>/Vec<T> all covariant in T and get rustc bootstrapping:

pub struct P<'a> {
    #[cfg(not(use_mut))]
    _ptr: *const &'a u8,
    #[cfg(use_mut)]
    _ptr: *mut &'a u8,
}

impl <'a> PartialEq for P<'a> {
    fn eq(&self, other: &P<'a>) -> bool {
        (self as *const _) == (other as *const _)
    }
}

fn main() {}

Transcript of compilation attempts:

% rustc --version
rustc 1.0.0-dev (71a71ce4f 2015-01-11 09:01:00 +0000)
% rustc --cfg use_mut /tmp/isolate-infer2.rs
% rustc --cfg use_const /tmp/isolate-infer2.rs
/tmp/isolate-infer2.rs:10:9: 11:6 error: cannot infer an appropriate lifetime due to conflicting requirements
/tmp/isolate-infer2.rs:10         (self as *const _) == (other as *const _)
/tmp/isolate-infer2.rs:11     }
/tmp/isolate-infer2.rs:10:9: 10:30 note: first, the lifetime must be contained by the expression at 10:8...
/tmp/isolate-infer2.rs:10         (self as *const _) == (other as *const _)
                                  ^~~~~~~~~~~~~~~~~~~~~
/tmp/isolate-infer2.rs:10:9: 10:30 note: ...so type `*const P<'_>` of expression is valid during the expression
/tmp/isolate-infer2.rs:10         (self as *const _) == (other as *const _)
                                  ^~~~~~~~~~~~~~~~~~~~~
/tmp/isolate-infer2.rs:10:31: 11:6 note: but, the lifetime must also be contained by the expression at 10:30...
/tmp/isolate-infer2.rs:10         (self as *const _) == (other as *const _)
/tmp/isolate-infer2.rs:11     }
/tmp/isolate-infer2.rs:10:31: 11:6 note: ...so type `*const P<'_>` of expression is valid during the expression
/tmp/isolate-infer2.rs:10         (self as *const _) == (other as *const _)
/tmp/isolate-infer2.rs:11     }
error: aborting due to previous error

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

Issue actions