Skip to content

Commit acf50ee

Browse files
committed
Add tests for auto trait, fix parsing bug
Now we can do the well formedness checks in the parser, yay!
1 parent 37dfc0c commit acf50ee

File tree

8 files changed

+141
-32
lines changed

8 files changed

+141
-32
lines changed

src/libsyntax/feature_gate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1394,7 +1394,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
13941394
i.span,
13951395
"auto traits are experimental and possibly buggy");
13961396
}
1397-
1397+
13981398
ast::ItemKind::MacroDef(ast::MacroDef { legacy: false, .. }) => {
13991399
let msg = "`macro` is experimental";
14001400
gate_feature_post!(&self, decl_macro, i.span, msg);

src/libsyntax/parse/parser.rs

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6014,6 +6014,37 @@ impl<'a> Parser<'a> {
60146014
maybe_append(attrs, extra_attrs));
60156015
return Ok(Some(item));
60166016
}
6017+
if self.eat_keyword(keywords::Auto) {
6018+
self.expect_keyword(keywords::Trait)?;
6019+
// AUTO TRAIT ITEM
6020+
let (ident,
6021+
item_,
6022+
extra_attrs) = self.parse_item_auto_trait(ast::Unsafety::Normal)?;
6023+
let prev_span = self.prev_span;
6024+
let item = self.mk_item(lo.to(prev_span),
6025+
ident,
6026+
item_,
6027+
visibility,
6028+
maybe_append(attrs, extra_attrs));
6029+
return Ok(Some(item));
6030+
}
6031+
if self.check_keyword(keywords::Unsafe) &&
6032+
self.look_ahead(1, |t| t.is_keyword(keywords::Auto)) {
6033+
self.expect_keyword(keywords::Unsafe)?;
6034+
self.expect_keyword(keywords::Auto)?;
6035+
self.expect_keyword(keywords::Trait)?;
6036+
// UNSAFE AUTO TRAIT ITEM
6037+
let (ident,
6038+
item_,
6039+
extra_attrs) = self.parse_item_auto_trait(ast::Unsafety::Unsafe)?;
6040+
let prev_span = self.prev_span;
6041+
let item = self.mk_item(lo.to(prev_span),
6042+
ident,
6043+
item_,
6044+
visibility,
6045+
maybe_append(attrs, extra_attrs));
6046+
return Ok(Some(item));
6047+
}
60176048
if (self.check_keyword(keywords::Unsafe) &&
60186049
self.look_ahead(1, |t| t.is_keyword(keywords::Impl))) ||
60196050
(self.check_keyword(keywords::Default) &&
@@ -6138,37 +6169,6 @@ impl<'a> Parser<'a> {
61386169
maybe_append(attrs, extra_attrs));
61396170
return Ok(Some(item));
61406171
}
6141-
if self.eat_keyword(keywords::Auto) {
6142-
self.expect_keyword(keywords::Trait)?;
6143-
// AUTO TRAIT ITEM
6144-
let (ident,
6145-
item_,
6146-
extra_attrs) = self.parse_item_auto_trait(ast::Unsafety::Normal)?;
6147-
let prev_span = self.prev_span;
6148-
let item = self.mk_item(lo.to(prev_span),
6149-
ident,
6150-
item_,
6151-
visibility,
6152-
maybe_append(attrs, extra_attrs));
6153-
return Ok(Some(item));
6154-
}
6155-
if self.check_keyword(keywords::Unsafe) &&
6156-
self.look_ahead(1, |t| t.is_keyword(keywords::Auto)) {
6157-
self.expect_keyword(keywords::Unsafe)?;
6158-
self.expect_keyword(keywords::Auto)?;
6159-
self.expect_keyword(keywords::Trait)?;
6160-
// UNSAFE AUTO TRAIT ITEM
6161-
let (ident,
6162-
item_,
6163-
extra_attrs) = self.parse_item_auto_trait(ast::Unsafety::Unsafe)?;
6164-
let prev_span = self.prev_span;
6165-
let item = self.mk_item(lo.to(prev_span),
6166-
ident,
6167-
item_,
6168-
visibility,
6169-
maybe_append(attrs, extra_attrs));
6170-
return Ok(Some(item));
6171-
}
61726172
if self.eat_keyword(keywords::Struct) {
61736173
// STRUCT ITEM
61746174
let (ident, item_, extra_attrs) = self.parse_item_struct()?;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
type A0 = auto;
12+
//~^ ERROR cannot find type `auto` in this scope
13+
type A1 = auto::auto;
14+
//~^ ERROR Use of undeclared type or module `auto`
15+
type A2 = auto<auto, auto>;
16+
//~^ ERROR cannot find type `auto` in this scope
17+
//~| ERROR cannot find type `auto` in this scope
18+
//~| ERROR cannot find type `auto` in this scope
19+
type A3 = auto<<auto as auto>::auto>;
20+
//~^ ERROR cannot find type `auto` in this scope
21+
//~| ERROR cannot find type `auto` in this scope
22+
//~| ERROR Use of undeclared type or module `auto`
23+
type A4 = auto(auto, auto) -> auto;
24+
//~^ ERROR cannot find type `auto` in this scope
25+
//~| ERROR cannot find type `auto` in this scope
26+
//~| ERROR cannot find type `auto` in this scope
27+
//~| ERROR cannot find type `auto` in this scope
28+
29+
fn main() {}

src/test/compile-fail/feature-gate-optin-builtin-traits.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ trait DummyTrait {
1717
fn dummy(&self) {}
1818
}
1919

20+
auto trait AutoDummyTrait {}
21+
//~^ ERROR auto traits are experimental and possibly buggy
22+
2023
impl DummyTrait for .. {}
2124
//~^ ERROR auto trait implementations are experimental and possibly buggy
2225

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: -Z parse-only
12+
13+
auto trait Auto<T> { }
14+
//~^ ERROR: expected `{`, found `<`
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: -Z parse-only
12+
13+
auto trait Auto { fn item() }
14+
//~^ ERROR: expected `}`, found `fn`
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: -Z parse-only
12+
13+
auto trait Auto : Send { }
14+
//~^ ERROR: expected `{`, found `:`

src/test/run-pass/auto-traits.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(optin_builtin_traits)]
12+
13+
auto trait Auto {}
14+
// Redundant but accepted until we remove it.
15+
impl Auto for .. {}
16+
17+
unsafe auto trait AutoUnsafe {}
18+
19+
impl !Auto for bool {}
20+
impl !AutoUnsafe for bool {}
21+
22+
struct AutoBool(bool);
23+
24+
impl Auto for AutoBool {}
25+
unsafe impl AutoUnsafe for AutoBool {}
26+
27+
fn take_auto<T: Auto>(_: T) {}
28+
fn take_auto_unsafe<T: AutoUnsafe>(_: T) {}
29+
30+
fn main() {
31+
take_auto(0);
32+
take_auto(AutoBool(true));
33+
take_auto_unsafe(0);
34+
take_auto_unsafe(AutoBool(true));
35+
}

0 commit comments

Comments
 (0)