Skip to content

struct fields in enum declarations generate junk types #11341

Closed
@jorgeadriano

Description

@jorgeadriano

A struct inside an enum generates a type identifier (as it does outside when used outside an enum).

enum S { C {a:int, b:int}};  
enum C {};

t.rs:10:4: 10:13 error: duplicate definition of type `C`
t.rs:10     enum C {};
        ^~~~~~~~~
t.rs:9:13: 9:29 note: first definition of type `C` here
t.rs:9     enum S { C {a:int, b:int}};  
                ^~~~~~~~~~~~~~~~

In particular the following declaration doesn't typecheck.

enum S { S {a:int, b:int}}; 

t.rs:6:13: 6:29 error: duplicate definition of type `S`
t.rs:6     enum S { S {a:int, b:int}};  
                    ^~~~~~~~~~~~~~~~
t.rs:6:4: 6:30 note: first definition of type `S` here
t.rs:6     enum S { S {a:int, b:int}};  

This is inconsistent with the general case in which such is allowed. E.g., both declarations below type check, despite type and value constructors having the same identifier:

enum S { S(int, int) }
struct S {a:int, b:int};

As a sidenote, we can't create values of these generated types even if wished to do so for some reason, since the value constructor associated with the struct field is set to create values of the enum type.

enum S { C {a:int, b:int} };  
let k : C = C{a:0, b:0};

t.rs:10:12: 10:13 error: found value name used as a type: DefVariant(syntax::ast::DefId{crate: 0, node: 7}, syntax::ast::DefId{crate: 0, node: 14}, true)
t.rs:10     let k : C = C{a:0, b:0};

My tests were performed on v0.8, but the bug was confirmed to be present in 0.9pre. Using Linux, OpenSUSE 13.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions