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

Parse const with no value #2708

Merged
merged 2 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions gcc/rust/ast/rust-item.h
Original file line number Diff line number Diff line change
Expand Up @@ -2618,6 +2618,13 @@ class ConstantItem : public VisItem,
const_expr (std::move (const_expr)), locus (locus)
{}

ConstantItem (std::string ident, Visibility vis, std::unique_ptr<Type> type,
std::vector<Attribute> outer_attrs, location_t locus)
: VisItem (std::move (vis), std::move (outer_attrs)),
identifier (std::move (ident)), type (std::move (type)),
const_expr (nullptr), locus (locus)
{}

ConstantItem (ConstantItem const &other)
: VisItem (other), identifier (other.identifier), locus (other.locus)
{
Expand Down
10 changes: 10 additions & 0 deletions gcc/rust/parse/rust-parse-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -4783,6 +4783,16 @@ Parser<ManagedTokenSource>::parse_const_item (AST::Visibility vis,
// parse constant type (required)
std::unique_ptr<AST::Type> type = parse_type ();

// A const with no given expression value
if (lexer.peek_token ()->get_id () == SEMICOLON)
{
lexer.skip_token ();
return std::unique_ptr<AST::ConstantItem> (
new AST::ConstantItem (std::move (ident), std::move (vis),
std::move (type), std::move (outer_attrs),
locus));
}
Comment on lines +4786 to +4794
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is good, but we need to handle it somewhere else, right? e.g. the rest of the AST and HIR are probably expecting that an impl's const always has a value, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, this ICEs:

struct S;

impl S {
    const D: u8;
}

while it should produce a nice error: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=9b8643a863d5350e8b226e8bc9789b88

this should be adressed in a later PR however :)


if (!skip_token (EQUAL))
{
skip_after_semicolon ();
Expand Down
6 changes: 6 additions & 0 deletions gcc/testsuite/rust/compile/issue-2665.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fn main() {}

#[cfg(FALSE)]
impl X {
const Y: u8;
}
Loading