rust icon indicating copy to clipboard operation
rust copied to clipboard

Structs are required to have one field

Open Dretch opened this issue 14 years ago • 9 comments

Structs are awesome. Allowing empty structs would make them complete.

Dretch avatar Aug 16 '12 20:08 Dretch

+1

eholk avatar Aug 16 '12 20:08 eholk

#3192 related

bblum avatar Aug 16 '12 20:08 bblum

This appears to be related to an LLVM limitation? If so, would it be too much to make empty structs take 1 byte (as we do for nil, for I think similar reasons)?

graydon avatar Aug 17 '12 15:08 graydon

That would be okay with me. Mostly I just want to be able to write structs that are nothing but a destructor.

eholk avatar Aug 17 '12 17:08 eholk

For reference, an empty struct in clang generates:

 %struct.foo = type {}

I propose we do that.

nikomatsakis avatar Aug 17 '12 17:08 nikomatsakis

@eholk use util::noncopyable. it is basically that, except it has a unit field for now.

bblum avatar Aug 17 '12 17:08 bblum

I'm looking for the issue. While I'm trying to fix the issue, I found an ambiguity between empty struct vs. empty match statement.

With following code

match x{} {}

Two interpretation is possible, which is listed blow

match (x{}) {} //matching with newly-constructed empty struct
(match x{}) {} //matching with empty enum(or struct) x and then empty block

It seems that there is no such code in rust code base, but there is one test which uses empty match statement: https://github.com/mozilla/rust/blob/incoming/src/test/run-pass/issue-3037.rs

All other cases could be distinguished with look-ahead, but this can't be. One possible solution is wrapping with parentheses when matching with an uninhabited type.

enum what { }
fn match_with_empty(x: what) -> ~str {
    match (x) { //use parentheses to remove the ambiguity
    }
}

How do you think about the issue?

yjh0502 avatar Feb 27 '13 04:02 yjh0502

Would it be possible to solve the ambiguity here by not needing {} to construct an empty struct? That way a struct X{} could just be declared like let var: X = X; instead of let var: X = X{};, and it would just be match X { ... }.

14427 avatar Mar 01 '13 03:03 14427

It is possible to have an empty struct. You simply write struct X;. You can reference an instance of this type as X.

nikomatsakis avatar Mar 03 '13 02:03 nikomatsakis