Skip to content

gccrs: Fix ICE in handle_inline_attribute_on_fndecl #3754

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
79 changes: 50 additions & 29 deletions gcc/rust/backend/rust-compile-base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -364,46 +364,67 @@ HIRCompileBase::handle_inline_attribute_on_fndecl (tree fndecl,
}

const AST::AttrInput &input = attr.get_attr_input ();
bool is_token_tree
= input.get_attr_input_type () == AST::AttrInput::AttrInputType::TOKEN_TREE;
rust_assert (is_token_tree);
const auto &option = static_cast<const AST::DelimTokenTree &> (input);
AST::AttrInputMetaItemContainer *meta_item = option.parse_to_meta_item ();
if (meta_item->get_items ().size () != 1)
auto input_type = input.get_attr_input_type ();

if (input_type == AST::AttrInput::AttrInputType::LITERAL)
{
// Handle #[inline = "..."] syntax
rich_location rich_locus (line_table, attr.get_locus ());
rich_locus.add_fixit_replace ("expected one argument");
rust_error_at (rich_locus, ErrorCode::E0534,
"invalid number of arguments");
rich_locus.add_fixit_replace ("#[inline(always)] or #[inline(never)]");
rust_error_at (rich_locus, ErrorCode::E0535,
"invalid syntax, %<inline%> attribute does not accept "
"value with %<=%>, use parentheses instead: "
"%<#[inline(always)]%> or %<#[inline(never)]%>");
return;
}
else if (input_type == AST::AttrInput::AttrInputType::TOKEN_TREE)
{
const auto &option = static_cast<const AST::DelimTokenTree &> (input);
AST::AttrInputMetaItemContainer *meta_item = option.parse_to_meta_item ();
if (meta_item->get_items ().size () != 1)
{
rich_location rich_locus (line_table, attr.get_locus ());
rich_locus.add_fixit_replace ("expected one argument");
rust_error_at (rich_locus, ErrorCode::E0534,
"invalid number of arguments");
return;
}

const std::string inline_option
= meta_item->get_items ().at (0)->as_string ();
const std::string inline_option
= meta_item->get_items ().at (0)->as_string ();

// we only care about NEVER and ALWAYS else its an error
bool is_always = inline_option.compare ("always") == 0;
bool is_never = inline_option.compare ("never") == 0;
// we only care about NEVER and ALWAYS else its an error
bool is_always = inline_option.compare ("always") == 0;
bool is_never = inline_option.compare ("never") == 0;
Comment on lines -381 to +398
Copy link
Member

Choose a reason for hiding this comment

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

e.g. this is what I mean with the formatting, it shouldn't have changed here

Copy link
Author

Choose a reason for hiding this comment

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

I think this happened because I enclosed it in an if statement. I did try running clang-format with the file again, but it didn't really change anything. Please tell me why this is happening ? Also, it did pass the CI for clang-format so I am assuming this might be a CI bug as well if it is actually a problem.

Copy link
Member

Choose a reason for hiding this comment

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

You could reduce the nesting to avoid the extra changes by doing something like:

else if (input_type != AST::AttrInput::AttrInputType::TOKEN_TREE)
{
  // emit error
}

Then carry on with logic, I think should do it


// #[inline(never)]
if (is_never)
{
DECL_UNINLINABLE (fndecl) = 1;
}
// #[inline(always)]
else if (is_always)
{
DECL_DECLARED_INLINE_P (fndecl) = 1;
DECL_ATTRIBUTES (fndecl) = tree_cons (get_identifier ("always_inline"),
NULL, DECL_ATTRIBUTES (fndecl));
// #[inline(never)]
if (is_never)
{
DECL_UNINLINABLE (fndecl) = 1;
}
// #[inline(always)]
else if (is_always)
{
DECL_DECLARED_INLINE_P (fndecl) = 1;
DECL_ATTRIBUTES (fndecl)
= tree_cons (get_identifier ("always_inline"), NULL,
DECL_ATTRIBUTES (fndecl));
}
else
{
rich_location rich_locus (line_table, attr.get_locus ());
rich_locus.add_fixit_replace ("unknown inline option");
rust_error_at (rich_locus, ErrorCode::E0535,
"invalid argument, %<inline%> attribute only accepts "
"%<always%> or %<never%>");
}
}
else
{
// Handle unexpected input type
rich_location rich_locus (line_table, attr.get_locus ());
rich_locus.add_fixit_replace ("unknown inline option");
rust_error_at (rich_locus, ErrorCode::E0535,
"invalid argument, %<inline%> attribute only accepts "
"%<always%> or %<never%>");
rust_error_at (rich_locus,
"unexpected attribute input type for inline attribute");
}
}

Expand Down
3 changes: 3 additions & 0 deletions gcc/testsuite/rust/compile/issue-3658.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#[inline = ""]
// { dg-error "invalid syntax, .inline. attribute does not accept value with .=., use parentheses instead: ...inline.always... or ...inline.never... .E0535." "" { target *-*-* } .-1 }
fn main() {}