noir
noir copied to clipboard
Struct is not considered as "used" despite being passed as a generic when constructing another type.
Noticed this in noir-bignum but will add a smaller repro later.
The Test2048Params struct is never constructed within the library and should not be exposed as public:
https://github.com/noir-lang/noir-bignum/blob/910dcbb8c2f07efb45416d267e75fdb3361b5b01/src/bignum_test.nr#L12
However, I would consider the below line as a usage of this struct: https://github.com/noir-lang/noir-bignum/blob/910dcbb8c2f07efb45416d267e75fdb3361b5b01/src/bignum_test.nr#L366
We should also check to see whether this type is passed as a generic parameter when constructing another type. I imagine that passing it as a generic to a function should also satisfy this check.
A couple of examples where the LSP doesn't realise that a struct has been constructed:
Usage of std::mem::zeroed() is not considered to construct the Foo struct.
struct Foo {}
fn main() {
let _: Foo = std::mem::zeroed();
}
This is the case exhibited in noir-bignum. The construction of a Bar doesn't imply the usage of Foo.
struct Foo {}
struct Bar<T>{
inner: Field
}
fn main() {
let _: Bar<Foo> = Bar { inner: 0 };
}
Something to note is that Rust disallows this case and requires the usage of the PhantomData type. We could do the same which would ensure that the type is used but would cause annoyances around abi encoding for these types.
Maybe for now we can avoid issuing an error if the struct doesn't have any fields?
Or #[allow(dead_code)] on a struct in cases where the compiler can't catch these cases.