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

#[cfg] on struct field permits empty unusable struct #16819

Closed
klutzy opened this issue Aug 28, 2014 · 3 comments
Closed

#[cfg] on struct field permits empty unusable struct #16819

klutzy opened this issue Aug 28, 2014 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-grammar Area: The grammar of Rust

Comments

@klutzy
Copy link
Contributor

klutzy commented Aug 28, 2014

struct S {
    #[cfg(untrue)]
    a: int,
}

fn main() {
    let s = S;
}
<anon>:7:13: 7:14 error: `S` is a structure name, but this expression uses it like a function name
<anon>:7     let s = S;
                     ^
<anon>:7:13: 7:14 note: Did you mean to write: `S { /* fields */ }`?
<anon>:7     let s = S;
                     ^

Here S { } is suggested but it is also not allowed:

<anon>:7:14: 7:15 error: structure literal must either have at least one field or use functional structure update syntax
<anon>:7     let s = S{};
                      ^
@steveklabnik steveklabnik added A-diagnostics Area: Messages for errors, warnings, and lints A-grammar Area: The grammar of Rust labels Jan 27, 2015
@klutzy
Copy link
Contributor Author

klutzy commented Apr 17, 2015

Triage: still an issue. Sub-bug of #24266.

@petrochenkov
Copy link
Contributor

Note, that after #28336 empty tuple structs are still disallowed, but obtainable with #[cfg].

struct TS (
    #[cfg(untrue)]
    int,
);

IMO, amending rust-lang/rfcs#218 to allow empty tuple structs would be the best solution.

@pnkfelix
Copy link
Member

(note that rust-lang/rfcs#218 did include discussion of the generalization to empty tuple structs... in short, I do not think you can directly generalize the RFC's strategy to tuple structs, though there may be some more limited generalization available.)

(I personally think the better approach here would be to explicitly disallow using #[cfg] attributes with tuple structs. I.e. If you need that kind of flexibility, use a record struct.)

bors added a commit that referenced this issue Sep 18, 2015
bors added a commit that referenced this issue Oct 14, 2015
This patch uses the same data structures for structs and enum variants in AST and HIR. These changes in data structures lead to noticeable simplification in most of code dealing with them.
I didn't touch the top level, i.e. `ItemStruct` is still `ItemStruct` and not `ItemEnum` with one variant, like in the type checker.
As part of this patch, structures and variants get the `kind` field making distinction between "normal" structs, tuple structs and unit structs explicit instead of relying on the number of fields and presence of constructor `NodeId`. In particular, we can now distinguish empty tuple structs from unit structs, which was impossible before! Comprehensive tests for empty structs are added and some improvements to empty struct feature gates are made. Some tests don't pass due to issue #28692 , they are still there for completeness, but are commented out.
This patch fixes issue mentioned in #16819 (comment), now emptiness of tuple structs is checked after expansion.
It also touches #28750 by providing span for visit_struct_def
cc #28336

r? @nrc
bors added a commit to rust-lang-ci/rust that referenced this issue Mar 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-grammar Area: The grammar of Rust
Projects
None yet
Development

No branches or pull requests

4 participants