Skip to content

struct fields in enum declarations generate junk types #11341

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

Closed
jorgeadriano opened this issue Jan 6, 2014 · 1 comment
Closed

struct fields in enum declarations generate junk types #11341

jorgeadriano opened this issue Jan 6, 2014 · 1 comment
Labels
A-type-system Area: Type system

Comments

@jorgeadriano
Copy link

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

@huonw
Copy link
Member

huonw commented Nov 10, 2014

This is now actually by design, RFC #234 places enum variants into the type namespace as well as the value namespace. (This was not the case when this issue was filed, though, so was definitely worth a bug then; thanks!)

The bad error message you note is still an issue, but this is covered more specifically by #17546, so I'm closing this.

@huonw huonw closed this as completed Nov 10, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-type-system Area: Type system
Projects
None yet
Development

No branches or pull requests

2 participants