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

(WIP) rustc: Implement nested C-like enums #17050

Closed
wants to merge 2 commits into from

Conversation

pczarn
Copy link
Contributor

@pczarn pczarn commented Sep 6, 2014

Allows #[repr(XX)] on more complex enums. Checks for inner variant collisions.

Example

// exception.rs
#[repr(u8)]
pub enum Fault {
    DivideError = 0,
    NMI = 2,
    Breakpoint = 3,
    // ...
}

// interrupt.rs
#[repr(u8)]    // < this is allowed
pub enum Interrupt {
    Fault(Fault),
    IRQ0 = 32,
    IRQ1 = 33,
    Syscall = 0x80
}

@pczarn pczarn changed the title rustc: Implement nested C-like enums (WIP) rustc: Implement nested C-like enums Sep 6, 2014
@huonw
Copy link
Member

huonw commented Sep 7, 2014

I wonder if this could be approached as a general enum-discriminant collapsing optimisation, with C-like enums + #[repr] a natural consequence?

E.g.

enum Foo {
    A, B, C
}
enum Bar {
     X(Foo), Y
}

enum Baz {
      S(Bar), T(int)
}

would "inline" the discriminant values of their sub-enum.

@brson
Copy link
Contributor

brson commented Sep 9, 2014

This seems pretty RFC-worthy to me. I've never heard any suggestion that we extend C-like enums in this way.

@alexcrichton
Copy link
Member

I agree with @brson that this looks like it needs an RFC to move forward as it's a bit of a language change. Closing.

lnicola pushed a commit to lnicola/rust that referenced this pull request Apr 20, 2024
…r=Veykril

internal: make function builder create ast directly

I am working on rust-lang#17050.
In the process, I noticed a place in the code that could be refactored.
Currently, the `function builder` creates the `ast` through the `function template` , but those two processes can be combined into one function.
I thought I should work on this first and created a PR.
lnicola pushed a commit to lnicola/rust that referenced this pull request Apr 20, 2024
…r=Veykril

internal: make function builder create ast directly

I am working on rust-lang#17050.
In the process, I noticed a place in the code that could be refactored.
Currently, the `function builder` creates the `ast` through the `function template` , but those two processes can be combined into one function.
I thought I should work on this first and created a PR.
lnicola pushed a commit to lnicola/rust that referenced this pull request May 19, 2024
…-new, r=Veykril

feature: Make generate function assist generate a function as a constructor if the generated function has the name "new" and is an asscociated function.

close rust-lang#17050
This PR makes `generate function assist` generate a function as a constructor if the generated function has the name "new" and is an asscociated function.
If the asscociate type is a record struct, it generates the constructor like this.
```rust
impl Foo {
    fn new() -> Self {
        Self { field_1: todo!(), field_2: todo!() }
    }
}
```
If the asscociate type is a tuple struct, it generates the constructor like this.
```rust
impl Foo {
    fn new() -> Self {
        Self(todo!(), todo!())
    }
}
```
If the asscociate type is a unit struct, it generates the constructor like this.
```rust
impl Foo {
    fn new() -> Self {
        Self
    }
}
```
If the asscociate type is another adt, it generates the constructor like this.
```rust
impl Foo {
    fn new() -> Self {
        todo!()
    }
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants