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 using trait lifetime in a method definition #10391

Closed
erickt opened this issue Nov 9, 2013 · 7 comments · Fixed by #10506
Closed

ICE using trait lifetime in a method definition #10391

erickt opened this issue Nov 9, 2013 · 7 comments · Fixed by #10506
Labels
A-lifetimes Area: Lifetimes / regions I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️

Comments

@erickt
Copy link
Contributor

erickt commented Nov 9, 2013

cc @nikomatsakis. I think this is related to #10153. Here's the example code:

pub enum Value<'self> {
    A(&'self str),
    B,
}

pub trait Decoder<'self> {
    fn read(&'self mut self) -> Value<'self>;
}

pub trait Decodable<'self, D: Decoder<'self>> {
    fn decode(d: &mut D) -> Self;
}

impl<'self, D: Decoder<'self>> Decodable<'self, D> for () {
    fn decode(d: &mut D) -> () {
        match d.read() {
            A(*) => (),
            B => Decodable::decode(d),
        }
    }
}

fn main() { }

It errors with:

foo.rs:18:17: 18:34 error: internal compiler error: Cannot relate bound region: ReLateBound(29, BrNamed(syntax::ast::DefId{crate: 0, node: 33}, self)) <= ReFree(62, BrNamed(syntax::ast::DefId{crate: 0, node: 38}, self))
This message reflects a bug in the Rust compiler.
We would appreciate a bug report: https:/mozilla/rust/wiki/HOWTO-submit-a-Rust-bug-report
foo.rs:18             B => Decodable::decode(d),
                           ^~~~~~~~~~~~~~~~~
@klutzy
Copy link
Contributor

klutzy commented Nov 10, 2013

I just got similar ICE with extern mod.

a.rs:

#[crate_type = "lib"];
pub struct Markdown<'self>(&'self str);

b.rs:

extern mod a;
fn main() {
    a::Markdown("abcd");
}

Building b.rs gives

b.rs:4:16: 4:22 error: internal compiler error: Cannot relate bound region: ReEarlyBound(12, 0, self) <= ReStatic

(Yes, I was using rustdoc::html::markdown::Markdown.)
It worked on rust revision 4ce7d57 but not on revision 3851f90.

@erickt
Copy link
Contributor Author

erickt commented Nov 10, 2013

Another variant:

fn foo<'a, T, Iter: Iterator<&'a T>>(_iter: Iter) { }

fn main() {
    let x = [1,2,3];
    foo(x.iter());
}

produces:

foo.rs:5:4: 5:7 error: internal compiler error: Cannot relate bound region: ReLateBound(6, BrNamed(syntax::ast::DefId{crate: 0, node: 19}, a)) <= ReInfer(11)
This message reflects a bug in the Rust compiler.
We would appreciate a bug report: https:/mozilla/rust/wiki/HOWTO-submit-a-Rust-bug-report
foo.rs:5     foo(x.iter());
             ^~~

@nikomatsakis
Copy link
Contributor

Seems likely to be related, yes. I'll take a look today. Probably something funky in the handling of static methods. Possibly related to what @dwrensha was trying to address in #9841 (though maybe not -- I'm sad to say I ddn't fully understand the problem there, was too caught up in trying to finish with #10153!)

@dwrensha
Copy link
Contributor

Here's an example relevant to me:

trait FromVec<'a> {
    fn fromVec(&'a [u8]) -> Self;
}

fn get<'a, T : FromVec<'a>>(v : &'a [u8]) -> T {
    FromVec::fromVec(v)
}

fn main() {}
test3.rs:6:4: 6:20 error: internal compiler error: Cannot relate bound region: ReLateBound(18, BrNamed(syntax::ast::DefId{crate: 0, node: 31}, a)) <= ReLateBound(16, BrNamed(syntax::ast::DefId{crate: 0, node: 17}, a))
This message reflects a bug in the Rust compiler. 
We would appreciate a bug report: https:/mozilla/rust/wiki/HOWTO-submit-a-Rust-bug-report
test3.rs:6     FromVec::fromVec(v)
               ^~~~~~~~~~~~~~~~

@nikomatsakis
Copy link
Contributor

So these examples are a mix of problems, but they are all related to #5121. The only one of these examples I expect to not ICE is the first one. The others don't work because of #5121. The basic problem is that you cannot have a function which has a lifetime parameter that appears in a trait bound (yet -- I want to fix this, see #5121 for details).

Interestingly, the reason that we have this problem with the first example, is that static functions like Decodable::decode get translated into functions with lifetime parameters that appear within a trait bound. That is, the type for Decodable::decode looks to the compiler like:

fn decode<'a, Self: Decodable<'a>>(...) -> Self

You can see here that 'a appears in the bound for Decodable. Anyway, I have the fix for this. This is good because it got me thinking about #5121 and I think it's not too hard to extend the fix to cover that case as well. But I'll push the limited fix for just this bug for now.

@erickt
Copy link
Contributor Author

erickt commented Nov 17, 2013

@nikomatsakis: Thanks for looking into this. I started playing around with your branch to fix this and I ran into another ICE. Is there something new, or is it also caused by #5121?

pub enum Value<'self> {
    A(&'self str),
    B,
}

pub trait Decoder<'self> {
    fn read(&mut self) -> Value<'self>;
}

pub trait Decodable<'self, D: Decoder<'self>> {
    fn decode(d: &mut D) -> Self;
}

pub fn decode<
    'a,
    D: Decoder<'a>,
    T: Decodable<'a, D>
>(d: &mut D) -> Option<T> {
    Decodable::decode(d)
}

impl<'self, D: Decoder<'self>> Decodable<'self, D> for () {
    fn decode(d: &mut D) -> () {
        match d.read() {
            A(*) => (),
            B => Decodable::decode(d),
        }
    }
}

fn main() { }

Errors with:

baz.rs:19:4: 19:21 error: internal compiler error: Cannot relate bound region: ReInfer(0) <= ReLateBound(34, BrNamed(syntax::ast::DefId{crate: 0, node: 52}, a))
This message reflects a bug in the Rust compiler.
We would appreciate a bug report: https:/mozilla/rust/wiki/HOWTO-submit-a-Rust-bug-report
baz.rs:19     Decodable::decode(d)
              ^~~~~~~~~~~~~~~~~

@nikomatsakis
Copy link
Contributor

On Sat, Nov 16, 2013 at 10:01:08PM -0800, Erick Tryzelaar wrote:

@nikomatsakis: Thanks for looking into this. I started playing around with your branch to fix this and I ran into another ICE. Is there something new, or is it also caused by #5121?

#5121. I will see how quickly I can prepare a fix for this.

The key identifying point is:

pub fn decode<
'a,
D: Decoder<'a>,
// ^^^^^
T: Decodable<'a, D>

(d: &mut D) -> Option {
Decodable::decode(d)
}

right here (I added a comment)

bors added a commit that referenced this issue Dec 2, 2013
…es, r=pnkfelix

Make trait lifetime parameters early bound in static fn type. Reasoning for this change is (hopefully) explained well enough in the comment, so I'll not duplicate it here. Fixes #10391.

r? @pnkfelix
dwrensha pushed a commit to dwrensha/rust that referenced this issue Dec 5, 2013
nikomatsakis added a commit to nikomatsakis/rust that referenced this issue Dec 5, 2013
bors added a commit that referenced this issue Dec 6, 2013
…es, r=pnkfelix

Make trait lifetime parameters early bound in static fn type. Reasoning for this change is (hopefully) explained well enough in the comment, so I'll not duplicate it here. Fixes #10391.

r? @pnkfelix
Jarcho pushed a commit to Jarcho/rust that referenced this issue Feb 26, 2023
…dnet

Fix test function checker in `unwrap_used`, `expect_used`

After rust-lang#9686 , `unwrap` and `expect` in integration tests and raw test functions won't be allowed.

fixes rust-lang#10011
fixes rust-lang#10238
fixes rust-lang#10264

---

changelog: Fix: [`expect_used`], [`unwrap_used`], [`dbg_macro`], [`print_stdout`], [`print_stderr`]: No longer lint in test functions, if the related configuration is set
[rust-lang#10391](rust-lang/rust-clippy#10391)
<!-- changelog_checked -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lifetimes Area: Lifetimes / regions I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants