1
Fork 0

Don't ICE on malformed rustc_args_required_const attribute

This commit is contained in:
Tomasz Miąsko 2020-12-06 00:00:00 +00:00
parent a68864b688
commit 50c6fd6dd5
3 changed files with 75 additions and 49 deletions

View file

@ -545,9 +545,24 @@ impl CheckAttrVisitor<'tcx> {
target: Target, target: Target,
item: Option<ItemLike<'_>>, item: Option<ItemLike<'_>>,
) -> bool { ) -> bool {
if let Target::Fn | Target::Method(..) | Target::ForeignFn = target { let is_function = matches!(target, Target::Fn | Target::Method(..) | Target::ForeignFn);
if !is_function {
self.tcx
.sess
.struct_span_err(attr.span, "attribute should be applied to a function")
.span_label(*span, "not a function")
.emit();
return false;
}
let list = match attr.meta_item_list() {
// The attribute form is validated on AST.
None => return false,
Some(it) => it,
};
let mut invalid_args = vec![]; let mut invalid_args = vec![];
for meta in attr.meta_item_list().expect("no meta item list") { for meta in list {
if let Some(LitKind::Int(val, _)) = meta.literal().map(|lit| &lit.kind) { if let Some(LitKind::Int(val, _)) = meta.literal().map(|lit| &lit.kind) {
if let Some(ItemLike::Item(Item { if let Some(ItemLike::Item(Item {
kind: ItemKind::Fn(FnSig { decl, .. }, ..), kind: ItemKind::Fn(FnSig { decl, .. }, ..),
@ -583,6 +598,7 @@ impl CheckAttrVisitor<'tcx> {
invalid_args.push(meta.span()); invalid_args.push(meta.span());
} }
} }
if !invalid_args.is_empty() { if !invalid_args.is_empty() {
self.tcx self.tcx
.sess .sess
@ -592,14 +608,6 @@ impl CheckAttrVisitor<'tcx> {
} else { } else {
true true
} }
} else {
self.tcx
.sess
.struct_span_err(attr.span, "attribute should be applied to a function")
.span_label(*span, "not a function")
.emit();
false
}
} }
/// Checks if `#[link_section]` is applied to a function or static. /// Checks if `#[link_section]` is applied to a function or static.

View file

@ -23,4 +23,10 @@ extern {
fn foo7(_: u8); fn foo7(_: u8);
} }
#[rustc_args_required_const] //~ ERROR malformed `rustc_args_required_const` attribute
fn bar1() {}
#[rustc_args_required_const = 1] //~ ERROR malformed `rustc_args_required_const` attribute
fn bar2() {}
fn main() {} fn main() {}

View file

@ -6,6 +6,18 @@ LL | #[rustc_args_required_const(0usize)]
| |
= help: instead of using a suffixed literal (`1u8`, `1.0f32`, etc.), use an unsuffixed version (`1`, `1.0`, etc.) = help: instead of using a suffixed literal (`1u8`, `1.0f32`, etc.), use an unsuffixed version (`1`, `1.0`, etc.)
error: malformed `rustc_args_required_const` attribute input
--> $DIR/invalid-rustc_args_required_const-arguments.rs:26:1
|
LL | #[rustc_args_required_const]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[rustc_args_required_const(N)]`
error: malformed `rustc_args_required_const` attribute input
--> $DIR/invalid-rustc_args_required_const-arguments.rs:29:1
|
LL | #[rustc_args_required_const = 1]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[rustc_args_required_const(N)]`
error: index exceeds number of arguments error: index exceeds number of arguments
--> $DIR/invalid-rustc_args_required_const-arguments.rs:3:29 --> $DIR/invalid-rustc_args_required_const-arguments.rs:3:29
| |
@ -44,5 +56,5 @@ error: index exceeds number of arguments
LL | #[rustc_args_required_const(1)] LL | #[rustc_args_required_const(1)]
| ^ there is only 1 argument | ^ there is only 1 argument
error: aborting due to 7 previous errors error: aborting due to 9 previous errors