Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Structs are required to have one field #3212

Closed
Dretch opened this issue Aug 16, 2012 · 9 comments
Closed

Structs are required to have one field #3212

Dretch opened this issue Aug 16, 2012 · 9 comments
Labels
A-typesystem Area: The type system

Comments

@Dretch
Copy link
Contributor

Dretch commented Aug 16, 2012

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

@eholk
Copy link
Contributor

eholk commented Aug 16, 2012

+1

@bblum
Copy link
Contributor

bblum commented Aug 16, 2012

#3192 related

@graydon
Copy link
Contributor

graydon commented Aug 17, 2012

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)?

@eholk
Copy link
Contributor

eholk commented Aug 17, 2012

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

@nikomatsakis
Copy link
Contributor

For reference, an empty struct in clang generates:

 %struct.foo = type {}

I propose we do that.

@bblum
Copy link
Contributor

bblum commented Aug 17, 2012

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

@yjh0502
Copy link
Contributor

yjh0502 commented Feb 27, 2013

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 added a commit to yjh0502/rust that referenced this issue Feb 27, 2013
The fix is straight-forward, but there are several changes
while fixing the issue.

    1) disallow `mut` keyword when making a new struct

      In code base, there are following code,

      struct Foo { mut a: int };
      let a = Foo { mut a: 1 };

      This is because of structural record, which is
      deprecated corrently (see issue rust-lang#3089) In structural
      record, `mut` keyword should be allowd to control
      mutability. But without structural record, we don't
      need to allow `mut` keyword while constructing struct.

    2) disallow structural records in parser level
      This is related to 1). With structural records, there
      is an ambiguity between empty block and empty struct
      To solve the problem, I change parser to stop parsing
      structural records. I think this is not a problem,
      because structural records are not compiled already.

Misc. issues

There is an ambiguity between empty struct vs. empty match stmt.
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
    }
}
@14427
Copy link
Contributor

14427 commented Mar 1, 2013

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 { ... }.

@nikomatsakis
Copy link
Contributor

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

RalfJung pushed a commit to RalfJung/rust that referenced this issue Dec 17, 2023
Don't explicitly warn against `semicolon_in_expressions_from_macros`

This warns-by-default since 2 years and already has been added to the future-incompat group since Rust 1.68.

See rust-lang#79813 for the tracking issue.

Just something I noticed when trying to wrap my head around lints warned against in the rust-lang/rust and noticed it's not warned against anymore; let me know if the change makes sense 🙏
jaisnan pushed a commit to jaisnan/rust-dev that referenced this issue Jul 29, 2024
> Please ensure your PR description includes the following:
> 1. A description of how your changes improve Kani.
> 2. Some context on the problem you are solving.
> 3. A list of issues that are resolved by this PR.
> 4. If you had to perform any manual test, please describe them.
>
> **Make sure you remove this list from the final PR description.**

Fix usage of `tcx.crates()`:
rust-lang#124976.

By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache 2.0 and MIT licenses.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-typesystem Area: The type system
Projects
None yet
Development

No branches or pull requests

7 participants