Skip to content

Commit 25038e3

Browse files
committed
Check for deprecated attributes
Deprecated attributes need to be checked and proper errors should be produced on bad arguments. gcc/rust/ChangeLog: * util/rust-attributes.cc: New function to handle deprecated attribute checking Signed-off-by: Ashwani Kumar Kamal <[email protected]>
1 parent e830d3c commit 25038e3

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

gcc/rust/util/rust-attributes.cc

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,97 @@ check_doc_attribute (const AST::Attribute &attribute)
277277
}
278278
}
279279

280+
static void
281+
check_deprecated_attribute (const AST::Attribute &attribute)
282+
{
283+
const auto &input = attribute.get_attr_input ();
284+
285+
if (input.get_attr_input_type () != AST::AttrInput::META_ITEM)
286+
return;
287+
288+
auto &meta = static_cast<const AST::AttrInputMetaItemContainer &> (input);
289+
290+
for (auto &current : meta.get_items ())
291+
{
292+
switch (current->get_kind ())
293+
{
294+
case AST::MetaItemInner::Kind::MetaItem:
295+
{
296+
auto *meta_item = static_cast<AST::MetaItem *> (current.get ());
297+
298+
switch (meta_item->get_item_kind ())
299+
{
300+
case AST::MetaItem::ItemKind::NameValueStr:
301+
{
302+
auto *nv = static_cast<AST::MetaNameValueStr *> (meta_item);
303+
304+
const std::string key = nv->get_name ().as_string ();
305+
306+
if (key != "since" && key != "note")
307+
{
308+
rust_error_at (nv->get_locus (), "unknown meta item %qs",
309+
key.c_str ());
310+
rust_inform (nv->get_locus (),
311+
"expected one of %<since%>, %<note%>");
312+
}
313+
}
314+
break;
315+
316+
case AST::MetaItem::ItemKind::Path:
317+
{
318+
// #[deprecated(a,a)]
319+
auto *p = static_cast<AST::MetaItemPath *> (meta_item);
320+
321+
std::string ident = p->get_path ().as_string ();
322+
323+
rust_error_at (p->get_locus (), "unknown meta item %qs",
324+
ident.c_str ());
325+
rust_inform (p->get_locus (),
326+
"expected one of %<since%>, %<note%>");
327+
}
328+
break;
329+
330+
case AST::MetaItem::ItemKind::Word:
331+
{
332+
// #[deprecated("a")]
333+
auto *w = static_cast<AST::MetaWord *> (meta_item);
334+
335+
rust_error_at (
336+
w->get_locus (),
337+
"item in %<deprecated%> must be a key/value pair");
338+
}
339+
break;
340+
341+
case AST::MetaItem::ItemKind::PathExpr:
342+
{
343+
// #[deprecated(since=a)]
344+
auto *px = static_cast<AST::MetaItemPathExpr *> (meta_item);
345+
346+
rust_error_at (
347+
px->get_locus (),
348+
"expected unsuffixed literal or identifier, found %qs",
349+
px->get_expr ().as_string ().c_str ());
350+
}
351+
break;
352+
353+
case AST::MetaItem::ItemKind::Seq:
354+
case AST::MetaItem::ItemKind::ListPaths:
355+
case AST::MetaItem::ItemKind::ListNameValueStr:
356+
default:
357+
gcc_unreachable ();
358+
break;
359+
}
360+
}
361+
break;
362+
363+
case AST::MetaItemInner::Kind::LitExpr:
364+
default:
365+
gcc_unreachable ();
366+
break;
367+
}
368+
}
369+
}
370+
280371
static bool
281372
is_proc_macro_type (const AST::Attribute &attribute)
282373
{
@@ -370,6 +461,8 @@ AttributeChecker::check_attribute (const AST::Attribute &attribute)
370461
// and costly
371462
if (result.name == Attrs::DOC)
372463
check_doc_attribute (attribute);
464+
else if (result.name == Attrs::DEPRECATED)
465+
check_deprecated_attribute (attribute);
373466
}
374467

375468
void

0 commit comments

Comments
 (0)