From 6c2f322f827ca2f2109bceab0b9630e624ac8810 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Wed, 15 Jun 2011 17:51:57 -0700 Subject: [PATCH] Improve and test failure behavior for malformed attributes --- src/comp/front/parser.rs | 14 +++++++++++++- src/test/compile-fail/attr-before-stmt.rs | 10 ++++++++++ src/test/compile-fail/attr-before-view-item.rs | 8 ++++++++ src/test/compile-fail/attr-dangling-in-fn.rs | 9 +++++++++ src/test/compile-fail/attr-dangling-in-mod.rs | 7 +++++++ 5 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 src/test/compile-fail/attr-before-stmt.rs create mode 100644 src/test/compile-fail/attr-before-view-item.rs create mode 100644 src/test/compile-fail/attr-dangling-in-fn.rs create mode 100644 src/test/compile-fail/attr-dangling-in-mod.rs diff --git a/src/comp/front/parser.rs b/src/comp/front/parser.rs index 88d8516043b..1ee1e2cf0e5 100644 --- a/src/comp/front/parser.rs +++ b/src/comp/front/parser.rs @@ -1451,7 +1451,19 @@ fn parse_source_stmt(&parser p) -> @ast::stmt { } } - alt (parse_item(p, item_attrs)) { + auto maybe_item = parse_item(p, item_attrs); + + // If we have attributes then we should have an item + if (vec::len(item_attrs) > 0u) { + alt (maybe_item) { + case (got_item(_)) { /* fallthrough */ } + case (_) { + ret p.err("expected item"); + } + } + } + + alt (maybe_item) { case (got_item(?i)) { auto hi = i.span.hi; auto decl = @spanned(lo, hi, ast::decl_item(i)); diff --git a/src/test/compile-fail/attr-before-stmt.rs b/src/test/compile-fail/attr-before-stmt.rs new file mode 100644 index 00000000000..27dae8c35d2 --- /dev/null +++ b/src/test/compile-fail/attr-before-stmt.rs @@ -0,0 +1,10 @@ +// xfail-stage0 +// error-pattern:expected item + +fn f() { + #[foo = "bar"] + auto x = 10; +} + +fn main() { +} diff --git a/src/test/compile-fail/attr-before-view-item.rs b/src/test/compile-fail/attr-before-view-item.rs new file mode 100644 index 00000000000..56e2ab7f54f --- /dev/null +++ b/src/test/compile-fail/attr-before-view-item.rs @@ -0,0 +1,8 @@ +// xfail-stage0 +// error-pattern:expected item + +#[foo = "bar"] +use std; + +fn main() { +} diff --git a/src/test/compile-fail/attr-dangling-in-fn.rs b/src/test/compile-fail/attr-dangling-in-fn.rs new file mode 100644 index 00000000000..c31b3250d76 --- /dev/null +++ b/src/test/compile-fail/attr-dangling-in-fn.rs @@ -0,0 +1,9 @@ +// xfail-stage0 +// error-pattern:expected item + +fn f() { + #[foo = "bar"] +} + +fn main() { +} diff --git a/src/test/compile-fail/attr-dangling-in-mod.rs b/src/test/compile-fail/attr-dangling-in-mod.rs new file mode 100644 index 00000000000..bd072402d25 --- /dev/null +++ b/src/test/compile-fail/attr-dangling-in-mod.rs @@ -0,0 +1,7 @@ +// xfail-stage0 +// error-pattern:expected item + +fn main() { +} + +#[foo = "bar"]