Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
730 views
in Technique[技术] by (71.8m points)

data structures - "overflow while adding drop-check rules" while implementing a fingertree

I'm trying to define a finger tree structure and implement its basic operations as an exercise in Rust. I've come up with the following, which is basically what's described in this paper.

use self::FingerTree::{Empty, Single, Deep};
use self::Digit::{One, Two, Three, Four};

enum Digit<A> {
    One(A),
    Two(A, A),
    Three(A, A, A),
    Four(A, A, A, A),
}

enum Node<V, A> {
    Node2(V, A, A),
    Node3(V, A, A, A),
}

enum FingerTree<V, A> {
    Empty,
    Single(A),
    Deep {
        size: V,
        prefix: Digit<A>,
        tree: Box<FingerTree<V, Node<V, A>>>,
        suffix: Digit<A>,
    },
}

fn main() {
    let e: FingerTree<i32, String> = Empty;
}

Compilation gives me an error that I don't understand:

error[E0320]: overflow while adding drop-check rules for FingerTree<i32, std::string::String>
  --> fingertree.rs:28:9
   |
28 |     let e: FingerTree<i32, String> = Empty;
   |         ^
   |
note: overflowed on enum Node variant Node2 field 0 type: i32
  --> fingertree.rs:28:9
   |
28 |     let e: FingerTree<i32, String> = Empty;
   |         ^

error[E0320]: overflow while adding drop-check rules for FingerTree<i32, std::string::String>
  --> fingertree.rs:28:38
   |
28 |     let e: FingerTree<i32, String> = Empty;
   |                                      ^^^^^
   |
note: overflowed on enum Node variant Node2 field 0 type: i32
  --> fingertree.rs:28:38
   |
28 |     let e: FingerTree<i32, String> = Empty;
   |                                      ^^^^^

Why is this not working? How do I make it work?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have created an infinite type.

Instantiating FingerTree<V, A> instantiates FingerTree<V, Node<V, A>> which instantiates FingerTree<V, Node<V, Node<V, A>>> which instantiates, ... and there's no end in sight.

The compiler cannot tell that the type will not actually be used at run-time, so prepares itself for the worst. And the worst is infinite.

Simply replacing the type of tree by Box<FingerTree<V, A>> solves the issue, though it may not be correct for the situation at hand.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...