Skip to content

Commit caecfd5

Browse files
gccrs: Fix ICE in handle_inline_attribute_on_fndecl
I added a check to see if the attr_input_type was a TOKEN_TREE and added an error message if a LITERAL is passed to it. Fixes #3658 gcc/rust/ChangeLog: * backend/rust-compile-base.cc (HIRCompileBase::handle_inline_attribute_on_fndecl): Handle LITERAL attribute input type. Signed-off-by: varun-r-mallya <[email protected]>
1 parent bb01719 commit caecfd5

File tree

1 file changed

+50
-29
lines changed

1 file changed

+50
-29
lines changed

gcc/rust/backend/rust-compile-base.cc

+50-29
Original file line numberDiff line numberDiff line change
@@ -364,46 +364,67 @@ HIRCompileBase::handle_inline_attribute_on_fndecl (tree fndecl,
364364
}
365365

366366
const AST::AttrInput &input = attr.get_attr_input ();
367-
bool is_token_tree
368-
= input.get_attr_input_type () == AST::AttrInput::AttrInputType::TOKEN_TREE;
369-
rust_assert (is_token_tree);
370-
const auto &option = static_cast<const AST::DelimTokenTree &> (input);
371-
AST::AttrInputMetaItemContainer *meta_item = option.parse_to_meta_item ();
372-
if (meta_item->get_items ().size () != 1)
367+
auto input_type = input.get_attr_input_type ();
368+
369+
if (input_type == AST::AttrInput::AttrInputType::LITERAL)
373370
{
371+
// Handle #[inline = "..."] syntax
374372
rich_location rich_locus (line_table, attr.get_locus ());
375-
rich_locus.add_fixit_replace ("expected one argument");
376-
rust_error_at (rich_locus, ErrorCode::E0534,
377-
"invalid number of arguments");
373+
rich_locus.add_fixit_replace ("#[inline(always)] or #[inline(never)]");
374+
rust_error_at (rich_locus, ErrorCode::E0535,
375+
"invalid syntax, %<inline%> attribute does not accept "
376+
"value with %<=%>, use parentheses instead: "
377+
"%<#[inline(always)]%> or %<#[inline(never)]%>");
378378
return;
379379
}
380+
else if (input_type == AST::AttrInput::AttrInputType::TOKEN_TREE)
381+
{
382+
const auto &option = static_cast<const AST::DelimTokenTree &> (input);
383+
AST::AttrInputMetaItemContainer *meta_item = option.parse_to_meta_item ();
384+
if (meta_item->get_items ().size () != 1)
385+
{
386+
rich_location rich_locus (line_table, attr.get_locus ());
387+
rich_locus.add_fixit_replace ("expected one argument");
388+
rust_error_at (rich_locus, ErrorCode::E0534,
389+
"invalid number of arguments");
390+
return;
391+
}
380392

381-
const std::string inline_option
382-
= meta_item->get_items ().at (0)->as_string ();
393+
const std::string inline_option
394+
= meta_item->get_items ().at (0)->as_string ();
383395

384-
// we only care about NEVER and ALWAYS else its an error
385-
bool is_always = inline_option.compare ("always") == 0;
386-
bool is_never = inline_option.compare ("never") == 0;
396+
// we only care about NEVER and ALWAYS else its an error
397+
bool is_always = inline_option.compare ("always") == 0;
398+
bool is_never = inline_option.compare ("never") == 0;
387399

388-
// #[inline(never)]
389-
if (is_never)
390-
{
391-
DECL_UNINLINABLE (fndecl) = 1;
392-
}
393-
// #[inline(always)]
394-
else if (is_always)
395-
{
396-
DECL_DECLARED_INLINE_P (fndecl) = 1;
397-
DECL_ATTRIBUTES (fndecl) = tree_cons (get_identifier ("always_inline"),
398-
NULL, DECL_ATTRIBUTES (fndecl));
400+
// #[inline(never)]
401+
if (is_never)
402+
{
403+
DECL_UNINLINABLE (fndecl) = 1;
404+
}
405+
// #[inline(always)]
406+
else if (is_always)
407+
{
408+
DECL_DECLARED_INLINE_P (fndecl) = 1;
409+
DECL_ATTRIBUTES (fndecl)
410+
= tree_cons (get_identifier ("always_inline"), NULL,
411+
DECL_ATTRIBUTES (fndecl));
412+
}
413+
else
414+
{
415+
rich_location rich_locus (line_table, attr.get_locus ());
416+
rich_locus.add_fixit_replace ("unknown inline option");
417+
rust_error_at (rich_locus, ErrorCode::E0535,
418+
"invalid argument, %<inline%> attribute only accepts "
419+
"%<always%> or %<never%>");
420+
}
399421
}
400422
else
401423
{
424+
// Handle unexpected input type
402425
rich_location rich_locus (line_table, attr.get_locus ());
403-
rich_locus.add_fixit_replace ("unknown inline option");
404-
rust_error_at (rich_locus, ErrorCode::E0535,
405-
"invalid argument, %<inline%> attribute only accepts "
406-
"%<always%> or %<never%>");
426+
rust_error_at (rich_locus,
427+
"unexpected attribute input type for inline attribute");
407428
}
408429
}
409430

0 commit comments

Comments
 (0)