-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Inference failure with for-loop and associated type that contains lifetimes #22066
Comments
The ICE is due to #20300, which already has an fix about to be merged. With that out of the way, the more interesting issue here is the type inference error. The compiler is happy if #[cfg(not(works))]
pub trait LineFormatter<'a> {
type Iter: Iterator<Item=&'a str> + 'a;
fn iter(&'a self, line: &'a str) -> Self::Iter;
fn dimensions(&'a self, line: &'a str) {
for _grapheme in self.iter(line) {
}
}
}
#[cfg(works)]
pub trait LineFormatter {
type Iter: Iterator<Item=&'static str>;
fn iter<'a>(&'a self, line: &'a str) -> Self::Iter;
fn dimensions<'a>(&'a self, line: &'a str) {
for _grapheme in self.iter(line) {
}
}
}
fn main() {} It is probably related to #21974. And this ticket could use a more precise title; it is more about trait lifetime parameter interferes with type inference than ICE. |
as @edwardw says, the ICE here is fixed. @steveklabnik could you remove the label? |
The inference problem also occurs with pub trait LineFormatter<'a> {
type Iter: Iterator<Item=&'a str> + 'a;
fn iter(&'a self, line: &'a str) -> Self::Iter;
fn dimensions(&'a self, line: &'a str) {
let mut iter: Self::Iter = self.iter(line);
<_ as IntoIterator>::into_iter(iter);
}
}
fn main() {} |
I think I still got this ICE.
The error:
Extracting the code as best I can I got it down to the following: use std::marker::PhantomData;
pub struct GILGuard;
trait PythonObject<'a> {
fn dummy() -> &'a i32;
}
#[derive(Copy, Clone)]
pub struct Python<'p>(PhantomData<&'p GILGuard>);
pub struct PyErr<'p> {
pub ptype : PyObject<'p>,
}
type PyResult<'p, T> = Result<T, PyErr<'p>>;
pub struct PyObject<'a> {
dummy : &'a i32
}
impl <'p> PyObject<'p> {
pub fn cast_into<T>(self) -> Result<T, PythonObjectDowncastError<'p>> where T: PythonObjectWithCheckedDowncast<'p> {
PythonObjectWithCheckedDowncast::downcast_from(self)
}
}
pub struct PythonObjectDowncastError<'p>(pub Python<'p>);
pub trait PythonObjectWithCheckedDowncast<'p> : PythonObject<'p> {
fn downcast_from(PyObject<'p>) -> Result<Self, PythonObjectDowncastError<'p>>;
}
impl <'p> PythonObjectWithCheckedDowncast<'p> for PyObject<'p> {
#[inline]
fn downcast_from(obj: PyObject<'p>) -> Result<PyObject<'p>, PythonObjectDowncastError<'p>> {
Ok(obj)
}
}
impl <'p> PythonObject<'p> for PyObject<'p> {
fn dummy() -> &'p i32 { &7 }
}
pub trait PyExtractor<'python, 'source, 'prepared> {
type Prepared;
fn extract<T>(prepared: &'prepared Self::Prepared) -> PyResult<'python, T>
where T: PythonObjectWithCheckedDowncast<'python> ;
}
pub struct PyObjectExtractor;
impl <'python, 'source, 'prepared> PyExtractor<'python, 'source, 'prepared>
for PyObjectExtractor {
type Prepared = &'source PyObject<'python>;
#[inline]
fn extract<T>(&&ref obj: &'prepared Self::Prepared) -> PyResult<'python, T>
where T: PythonObjectWithCheckedDowncast<'python> {
Ok(try!(obj.clone().cast_into()))
}
}
pub trait ExtractPyObject<'python, 'source, 'prepared> {
type Prepared;
fn prepare_extract(obj: &'source PyObject<'python>) -> PyResult<'python, Self::Prepared>;
fn extract<E: PyExtractor<'python, 'source, 'prepared>>(prepared: &'prepared Self::Prepared) -> PyResult<'python, Self>;
}
impl <'python, 'source, 'prepared, T> ExtractPyObject<'python, 'source, 'prepared>
for T where T: PythonObjectWithCheckedDowncast<'python> {
type Prepared = &'source PyObject<'python>;
#[inline]
fn prepare_extract(obj: &'source PyObject<'python>) -> PyResult<'python, Self::Prepared> {
Ok(obj)
}
#[inline]
fn extract<E: PyExtractor<'python, 'source, 'prepared>>(&&ref obj: &'prepared Self::Prepared) -> PyResult<'python, T> {
E::extract::<Self::Prepared>(&obj)
}
} |
This seems to work on Rust 1.10. |
…alexcrichton Add test for an inference failure. Fixes #22066
System Info:
I'm trying to create a trait for formatting lines of text (e.g. do line wrapping, manage font size, etc.). I want implementers to only have to provide an iterator and a method for producing those iterators, and everything else is provided with default implementations that utilize those.
The following is a minified version of what I'm trying to do that produced an error.
Code:
Error:
This error seems strange enough on its own, because rustc ought to be able to infer the type of
grapheme
from the return type ofSelf::Iter::next()
. So I thought perhaps the problem is that it somehow lacked type information aboutself.iter()
. Thus I changed the code so that the iterator type is explicit, and got a different error:Code:
Error (with backtrace):
I wouldn't be surprised if I'm implementing this stuff wrong, but since it's producing an ICE it seemed like I ought to submit a bug report.
The text was updated successfully, but these errors were encountered: