moonbitlang / core

MoonBit's Core library
https://moonbitlang.com/
Apache License 2.0
606 stars 76 forks source link

Compiler bug: use of identical traits reports `Error [4018]` #1124

Open gmlewis opened 6 days ago

gmlewis commented 6 days ago
glenn@glenn-MacM2Pro ~/src/github.com/gmlewis/moonbit-gzip (master) $ moon version --all
moon 0.1.20241011 (ca50f51 2024-10-11) ~/.moon/bin/moon
moonc v0.1.20241011+9ea637707 ~/.moon/bin/moonc
moonrun 0.1.20241011 (ca50f51 2024-10-11) ~/.moon/bin/moonrun

EDIT: Please see SIGNIFICANTLY BETTER demonstration test case in comment below.

In gmlewis/moonbit-flate, I have two public traits:

In gmlewis/moonbit-gzip, I have two identical public traits:

They should be 100% compatible.

However, when I attempt to use a @gzip.IOReader as a @flate.IOReader (or a @gzip.IOWriter as a @flate.IOWriter), the compiler says that the methods are missing. See screenshots:

trait-bug-2-2024-10-14_09-43-36 trait-bug-1-2024-10-14_09-42-36

gmlewis commented 2 days ago

This still exists in version:

$ moon version --all
moon 0.1.20241017 (e833bec 2024-10-17) ~/.moon/bin/moon
moonc v0.1.20241017+e8d7befb8 ~/.moon/bin/moonc
moonrun 0.1.20241017 (e833bec 2024-10-17) ~/.moon/bin/moonrun
gmlewis commented 1 day ago

Here's a better example that demonstrates this compiler bug:

Given two different traits in one package:

pub trait Reader {
  read(Self, Slice[Byte]) -> (Int, IOError?)
}

pub trait ByteReader {
  read_byte(Self) -> (Byte, IOError?)
}

when those two traits are combined together in a different package:

typealias Slice[T] = @io.Slice[T]

typealias IOError = @io.IOError

pub trait Reader {
  // @io.Reader
  read(Self, Slice[Byte]) -> (Int, IOError?)
  // @io.ByteReader
  read_byte(Self) -> (Byte, IOError?)
}

the compiler reports Error [4018]:

error-4018-2024-10-19_09-11-50

For completeness, the @flate.Reader does indeed implement the read method here:

https://github.com/gmlewis/moonbit-flate/pull/1#pullrequestreview-2379432826

fn read(self : Decompressor, b : Slice[Byte]) -> (Int, IOError?) {
...
}
hackwaly commented 1 day ago

/cc @Guest0x0