Assigning structs to variables
Summary
gccrs lets you assign structs to variables, when rustc emits "expected value, found struct ..."
Reproducer
I tried this code:
struct A {
a: u32,
}
fn main() {
let a = A {a: 4};
let b = A;
a.a = 2;
}
Does the code make use of any (1.49) nightly feature ?
- [ ] Nightly
Godbolt link
No response
Actual behavior
The current behavior is...
The code compiles.
Expected behavior
I expected to see...
error[E0423]: expected value, found struct `A`
--> main.rs:7:13
|
1 | / struct A {
2 | | a: u32,
3 | | }
| |_- `A` defined here
...
7 | let b = A;
GCC Version
commit-hash: c83b22a6932240194879ea7d9e783a4c0daf1b79
Interesting issue this because in my head i thought it would just be like C:
struct test {
int a;
int b;
};
void test()
{
struct test x = { .a = 1, .b = 2};
struct test y = x;
}
Hmmm not sure if this is something we should just be doing at the code-generation level or a new lint pass.
Interesting issue this because in my head i thought it would just be like C:
struct test { int a; int b; }; void test() { struct test x = { .a = 1, .b = 2}; struct test y = x; }Hmmm not sure if this is something we should just be doing at the code-generation level or a new lint pass.
the example here is different than what I showed in the issue description, I think
I think the C equivalent would be
struct test {
int a;
int b;
};
void test()
{
struct test x = { .a = 1, .b = 2};
struct test y = struct test;
}
You should probably also get an error because a is not mutable...
Here's a reproducer link:https://rust.godbolt.org/z/TKqKnMMs4
I am such an idiot i misread this
I am such an idiot i misread this
I made the exact same mistake when writing the code. It was only after compiling with rustc that I realized
This is definitely not something that can be caught during parsing right? Unit structs can be instantiated using a similar pattern:
struct Y {
y: u32,
}
struct Z; // Unit struct. Stores no data
fn main () {
let y = Y {y: 1};
let z = Z;
}
Should this type of thing be caught at the beginning of type checking, end of name resolution, or a separate phase like CfgStrip?
Same question here.
I think this is a typechecking problem
A is a proper type name (since it refers to struct A), but we assign a type to a variable, which should be a type error
rustc treats this as a typechecking problem, since it emits "expected value, found struct ..."
I'd guess that the example @GlowingScrewdriver points out is an exception, so that 'A' is treated as a value when A is a unit struct, but as a type when it's not a unit struct.
its not a typechecking fix it needs to be caught during code-gen. Because its trying to assign a type but a type is not a value