Skip to content

[BUG] Declaring a type named struct or class #394

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
msadeqhe opened this issue Apr 25, 2023 · 4 comments
Closed

[BUG] Declaring a type named struct or class #394

msadeqhe opened this issue Apr 25, 2023 · 4 comments
Labels
bug Something isn't working

Comments

@msadeqhe
Copy link

msadeqhe commented Apr 25, 2023

Bug description

struct and class are not keywords in Cpp2, but I cannot declare a type named struct or class.

To Reproduce It

This is the sample code:

struct: type = {
    member: int = 0;
}

It should generate the following code:

//=== Cpp2 type declarations ====================================================

#include "cpp2util.h"

class cpp2_struct;


//=== Cpp2 type definitions and function declarations ===========================

class cpp2_struct {
    private: int member {0}; 
    public: cpp2_struct() = default;
    public: cpp2_struct(cpp2_struct const&) = delete;
    public: auto operator=(cpp2_struct const&) -> void = delete;
};


//=== Cpp2 function definitions =================================================

But it leads to compilation faild:

example.cpp2(1,7): error: to define a type :, write ': : type = { /*body*/ }'

Compiler returned: 1

Additional context

Also typename, typedef, typeid and other Cpp1-only keywords if used as type name will generate wrong source code.

I think it's related to this issue that is fixed by applying prefix cpp2_ to identifiers which are keywords in Cpp1.

@msadeqhe msadeqhe added the bug Something isn't working label Apr 25, 2023
@msadeqhe msadeqhe changed the title [BUG] declare a type named struct or class [BUG] Declaring a type named struct or class Apr 25, 2023
@JohelEGP
Copy link
Contributor

and works: https://godbolt.org/z/7ddjd7boK.
But not as a @struct: https://godbolt.org/z/c6vv9zGxz.

main.cpp2:3:19: error: 'const' qualifier may not be applied to a reference
  public: and(and const& that);
                  ^

It was 1886ed2#diff-e683e1259bbc0d6de7f1fbf797fb772f31a0a1ef2254ec317d15c298102fa3f3R1551-R1552 which made struct a reclaimed identifier.

@hsutter
Copy link
Owner

hsutter commented Apr 26, 2023

Thanks!

@JohelEGP
Copy link
Contributor

JohelEGP commented Apr 28, 2023

https://godbolt.org/z/c6vv9zGxz still doesn't work.

-  public: and(and const& that);
+  public: cpp2_and(and const& that);

hsutter added a commit that referenced this issue Apr 28, 2023
@hsutter
Copy link
Owner

hsutter commented Apr 28, 2023

Ah, my bad -- fixed.

I had verified it was working, but then temporarily backed out a couple of the fixed places to debug some Cpp1 formatting artifacts, and forgot to re-enable them (including to re-run that test case) before commit. Should be good now with ddd6292.

zaucy pushed a commit to zaucy/cppfront that referenced this issue Dec 5, 2023
Allow `copy` statement/block parameter, closes hsutter#393

Allow `class` and `struct` and other reclaimed words as declared names, closes hsutter#394
zaucy pushed a commit to zaucy/cppfront that referenced this issue Dec 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants