noir icon indicating copy to clipboard operation
noir copied to clipboard

Struct is not considered as "used" despite being passed as a generic when constructing another type.

Open TomAFrench opened this issue 1 year ago • 3 comments

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.

TomAFrench avatar Sep 30 '24 10:09 TomAFrench

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.

TomAFrench avatar Sep 30 '24 13:09 TomAFrench

Maybe for now we can avoid issuing an error if the struct doesn't have any fields?

asterite avatar Oct 04 '24 20:10 asterite

Or #[allow(dead_code)] on a struct in cases where the compiler can't catch these cases.

asterite avatar Oct 04 '24 20:10 asterite