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

ICE with Self in function bodies, associated types, and lifetimes #27281

Closed
arielb1 opened this issue Jul 25, 2015 · 0 comments
Closed

ICE with Self in function bodies, associated types, and lifetimes #27281

arielb1 opened this issue Jul 25, 2015 · 0 comments

Comments

@arielb1
Copy link
Contributor

arielb1 commented Jul 25, 2015

STR

pub trait Trait<'a> {                        
    type T;                                                              
    fn foo(&self, s: &'a ()) -> &'a ();
}                                                                               

impl<'a> Trait<'a> for () {
    type T = &'a ();

    fn foo(&self, s: &'a ()) -> &'a () {
        let t: Self::T = s; s
    }                                                                           
}

fn main() {}

Result

<anon>:10:16: 10:23 error: internal compiler error: cannot relate bound region: '_#0r <= ReEarlyBound(48, TypeSpace, 0, 'a)
<anon>:10         let t: Self::T = s; s
                         ^~~~~~~
note: the compiler unexpectedly panicked. this is a bug.
note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports
note: run with `RUST_BACKTRACE=1` for a backtrace
thread 'rustc' panicked at 'Box<Any>', /home/rustbuild/src/rust-buildbot/slave/stable-dist-rustc-linux/build/src/libsyntax/diagnostic.rs:170

Orig

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)                                      
    }                                                                           
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant