Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5d8fd8b

Browse files
committedFeb 26, 2019
Mention unwind(aborts) in diagnostics for #[unwind]
Simplify input validation for `#[unwind]`, add tests
1 parent ea43c3c commit 5d8fd8b

File tree

6 files changed

+69
-32
lines changed

6 files changed

+69
-32
lines changed
 

‎src/libsyntax/attr/builtin.rs

+17-31
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::parse::ParseSess;
77
use errors::{Applicability, Handler};
88
use syntax_pos::{symbol::Symbol, Span};
99

10-
use super::{list_contains_name, mark_used, MetaItemKind};
10+
use super::{mark_used, MetaItemKind};
1111

1212
enum AttrError {
1313
MultipleItem(Name),
@@ -79,40 +79,26 @@ pub enum UnwindAttr {
7979

8080
/// Determine what `#[unwind]` attribute is present in `attrs`, if any.
8181
pub fn find_unwind_attr(diagnostic: Option<&Handler>, attrs: &[Attribute]) -> Option<UnwindAttr> {
82-
let syntax_error = |attr: &Attribute| {
83-
mark_used(attr);
84-
diagnostic.map(|d| {
85-
span_err!(d, attr.span, E0633, "malformed `#[unwind]` attribute");
86-
});
87-
None
88-
};
89-
9082
attrs.iter().fold(None, |ia, attr| {
91-
if attr.path != "unwind" {
92-
return ia;
93-
}
94-
let meta = match attr.meta() {
95-
Some(meta) => meta.node,
96-
None => return ia,
97-
};
98-
match meta {
99-
MetaItemKind::Word => {
100-
syntax_error(attr)
101-
}
102-
MetaItemKind::List(ref items) => {
103-
mark_used(attr);
104-
if items.len() != 1 {
105-
syntax_error(attr)
106-
} else if list_contains_name(&items[..], "allowed") {
107-
Some(UnwindAttr::Allowed)
108-
} else if list_contains_name(&items[..], "aborts") {
109-
Some(UnwindAttr::Aborts)
110-
} else {
111-
syntax_error(attr)
83+
if attr.check_name("unwind") {
84+
if let Some(meta) = attr.meta() {
85+
if let MetaItemKind::List(items) = meta.node {
86+
if items.len() == 1 {
87+
if items[0].check_name("allowed") {
88+
return Some(UnwindAttr::Allowed);
89+
} else if items[0].check_name("aborts") {
90+
return Some(UnwindAttr::Aborts);
91+
}
92+
}
93+
94+
diagnostic.map(|d| {
95+
span_err!(d, attr.span, E0633, "malformed `#[unwind]` attribute");
96+
});
11297
}
11398
}
114-
_ => ia,
11599
}
100+
101+
ia
116102
})
117103
}
118104

‎src/libsyntax/feature_gate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1173,7 +1173,7 @@ pub const BUILTIN_ATTRIBUTES: &[(&str, AttributeType, AttributeTemplate, Attribu
11731173
"dropck_eyepatch",
11741174
"may_dangle has unstable semantics and may be removed in the future",
11751175
cfg_fn!(dropck_eyepatch))),
1176-
("unwind", Whitelisted, template!(List: "allowed"), Gated(Stability::Unstable,
1176+
("unwind", Whitelisted, template!(List: "allowed|aborts"), Gated(Stability::Unstable,
11771177
"unwind_attributes",
11781178
"#[unwind] is experimental",
11791179
cfg_fn!(unwind_attributes))),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![feature(unwind_attributes)]
2+
3+
#[unwind]
4+
//~^ ERROR attribute must be of the form
5+
extern "C" fn f1() {}
6+
7+
#[unwind = ""]
8+
//~^ ERROR attribute must be of the form
9+
extern "C" fn f2() {}
10+
11+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: attribute must be of the form `#[unwind(allowed|aborts)]`
2+
--> $DIR/malformed-unwind-1.rs:3:1
3+
|
4+
LL | #[unwind]
5+
| ^^^^^^^^^
6+
7+
error: attribute must be of the form `#[unwind(allowed|aborts)]`
8+
--> $DIR/malformed-unwind-1.rs:7:1
9+
|
10+
LL | #[unwind = ""]
11+
| ^^^^^^^^^^^^^^
12+
13+
error: aborting due to 2 previous errors
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![feature(unwind_attributes)]
2+
3+
#[unwind(allowed, aborts)]
4+
//~^ ERROR malformed `#[unwind]` attribute
5+
extern "C" fn f1() {}
6+
7+
#[unwind(unsupported)]
8+
//~^ ERROR malformed `#[unwind]` attribute
9+
extern "C" fn f2() {}
10+
11+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0633]: malformed `#[unwind]` attribute
2+
--> $DIR/malformed-unwind-2.rs:3:1
3+
|
4+
LL | #[unwind(allowed, aborts)]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
7+
error[E0633]: malformed `#[unwind]` attribute
8+
--> $DIR/malformed-unwind-2.rs:7:1
9+
|
10+
LL | #[unwind(unsupported)]
11+
| ^^^^^^^^^^^^^^^^^^^^^^
12+
13+
error: aborting due to 2 previous errors
14+
15+
For more information about this error, try `rustc --explain E0633`.

0 commit comments

Comments
 (0)
Please sign in to comment.