rust icon indicating copy to clipboard operation
rust copied to clipboard

typeck hang with recursive type

Open jruderman opened this issue 14 years ago • 19 comments

This hangs the compiler in typeck:

enum t = @t;
fn main() {
    let x : t;
    x.next = x;
}

jruderman avatar Mar 28 '12 19:03 jruderman

probably my fault.

nikomatsakis avatar Mar 28 '12 19:03 nikomatsakis

Not at all what I expected it to be. Rather, a loop in autoderef. I will fix the loop but I suppose it might also make sense to reject this type as uninstanitable---there should be some enum variant that does not require an instance of the enum itself. But I am not sure this is worth checking in general: after all, the user could never successfully construct an instance of that type, and to check it in general requires expanding and traversing enums and so forth.

nikomatsakis avatar Mar 28 '12 20:03 nikomatsakis

I think rejecting the type makes more sense. If we reject the type immediately, we won't need to change the autoderef loop, right?

jruderman avatar Mar 28 '12 20:03 jruderman

That's the idea. I independently came to the same conclusion. In particular, there are multiple autoderef loops (which ought to be consolidated more, but still) and I don't like the idea of this same kind of bug creeping up in another of them.

nikomatsakis avatar Mar 28 '12 21:03 nikomatsakis

Also, it's just a nicer error message.

nikomatsakis avatar Mar 28 '12 21:03 nikomatsakis

Unfortunately, we still need the loop check in the autoderef loop for typeck, because the actual checking that the enum is instantiable and the checking of the fn body occur in the same pass over the AST. We could change it to have three passes (one to compute types and generate info, one to check for instantiability, and one to check fn bodies and so forth) but that seems annoying.

nikomatsakis avatar Mar 28 '12 22:03 nikomatsakis

Could the pass bail out early after encountering an uninstantiable type?

jruderman avatar Mar 28 '12 22:03 jruderman

@jruderman No, the problem is that the autoderef loop could occur before we get around to checking whether the type is instantiable. As I said, I could restructure the pass to make sure we check all types for instantiability (and possibly other properties) before we delve into function bodies and expressions, but otherwise I think we still need to be careful when autoderef'ing. Such a restructuring might be a good idea though.

nikomatsakis avatar Mar 29 '12 00:03 nikomatsakis

Fixed in 23f92ea3701d515ac617c4c9ad18d4b25ecd4675

nikomatsakis avatar Mar 29 '12 03:03 nikomatsakis

Should this be closed, then?

msullivan avatar Aug 08 '12 19:08 msullivan

Yes.

nikomatsakis avatar Aug 08 '12 19:08 nikomatsakis

The issue-2063.rs test has been xfail'd for some time now, but this issue appears to be cropping up again.

alexcrichton avatar May 06 '13 19:05 alexcrichton

That test case emits an error before hanging: this type cannot be instantiated without an instance of itself

One easy way to fix this would be to make that error emission site do a span_fatal, so that the compilation phase stops running before it gets to the hang. Not sure if this is the right thing to do.

Nominating well-covered.

bblum avatar Jul 22 '13 21:07 bblum

It might be. The idea of this error was that code in typeck had to worry about this kind of infinite loop case, but not other code. But maybe it's too much trouble even within typeck.

nikomatsakis avatar Jul 23 '13 14:07 nikomatsakis

accepted for production-ready milestone

graydon avatar Aug 01 '13 17:08 graydon

High, but not 1.0

catamorphism avatar Oct 24 '13 17:10 catamorphism

Fixing this bug should also fix this code:

enum A {
    A(B)
}

enum B {
    B1, B2(B)
}

fn main() {}

alexcrichton avatar Dec 18 '13 23:12 alexcrichton

Not sure under what circumstances issue-2063.rs was originally xfailed, but as of now it's missing #[feature(managed_boxes)]; and emits an extra error: "type t does not implement any method in scope named to_str". I enabled managed boxes (and tried adding a &self parameter to the to_str call), but could not get the compiler to hang. It emits the expected error ("this type cannot be instantiated without an instance of itself; consider using Option<t>") and quits.

The recursive-enum stack overflow issue pointed referenced by @alexcrichton is described in #3008, and addressed in pull request #11839. I don't think it's related to this.

isabelmu avatar Jan 28 '14 08:01 isabelmu

issue-2063.rs is not xfail'd anymore and tests introduced in #3008 already test @alexcrichton example. Look at issue-3008-1.rs

This seems to be fixed, closing.

flaper87 avatar Mar 12 '14 22:03 flaper87