Add tests for auto trait, fix parsing bug

Now we can do the well formedness checks in the parser, yay!
This commit is contained in:
leonardo.yvens 2017-10-12 19:00:30 -03:00
parent 37dfc0c598
commit acf50ee236
8 changed files with 141 additions and 32 deletions

View file

@ -1394,7 +1394,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
i.span,
"auto traits are experimental and possibly buggy");
}
ast::ItemKind::MacroDef(ast::MacroDef { legacy: false, .. }) => {
let msg = "`macro` is experimental";
gate_feature_post!(&self, decl_macro, i.span, msg);

View file

@ -6014,6 +6014,37 @@ impl<'a> Parser<'a> {
maybe_append(attrs, extra_attrs));
return Ok(Some(item));
}
if self.eat_keyword(keywords::Auto) {
self.expect_keyword(keywords::Trait)?;
// AUTO TRAIT ITEM
let (ident,
item_,
extra_attrs) = self.parse_item_auto_trait(ast::Unsafety::Normal)?;
let prev_span = self.prev_span;
let item = self.mk_item(lo.to(prev_span),
ident,
item_,
visibility,
maybe_append(attrs, extra_attrs));
return Ok(Some(item));
}
if self.check_keyword(keywords::Unsafe) &&
self.look_ahead(1, |t| t.is_keyword(keywords::Auto)) {
self.expect_keyword(keywords::Unsafe)?;
self.expect_keyword(keywords::Auto)?;
self.expect_keyword(keywords::Trait)?;
// UNSAFE AUTO TRAIT ITEM
let (ident,
item_,
extra_attrs) = self.parse_item_auto_trait(ast::Unsafety::Unsafe)?;
let prev_span = self.prev_span;
let item = self.mk_item(lo.to(prev_span),
ident,
item_,
visibility,
maybe_append(attrs, extra_attrs));
return Ok(Some(item));
}
if (self.check_keyword(keywords::Unsafe) &&
self.look_ahead(1, |t| t.is_keyword(keywords::Impl))) ||
(self.check_keyword(keywords::Default) &&
@ -6138,37 +6169,6 @@ impl<'a> Parser<'a> {
maybe_append(attrs, extra_attrs));
return Ok(Some(item));
}
if self.eat_keyword(keywords::Auto) {
self.expect_keyword(keywords::Trait)?;
// AUTO TRAIT ITEM
let (ident,
item_,
extra_attrs) = self.parse_item_auto_trait(ast::Unsafety::Normal)?;
let prev_span = self.prev_span;
let item = self.mk_item(lo.to(prev_span),
ident,
item_,
visibility,
maybe_append(attrs, extra_attrs));
return Ok(Some(item));
}
if self.check_keyword(keywords::Unsafe) &&
self.look_ahead(1, |t| t.is_keyword(keywords::Auto)) {
self.expect_keyword(keywords::Unsafe)?;
self.expect_keyword(keywords::Auto)?;
self.expect_keyword(keywords::Trait)?;
// UNSAFE AUTO TRAIT ITEM
let (ident,
item_,
extra_attrs) = self.parse_item_auto_trait(ast::Unsafety::Unsafe)?;
let prev_span = self.prev_span;
let item = self.mk_item(lo.to(prev_span),
ident,
item_,
visibility,
maybe_append(attrs, extra_attrs));
return Ok(Some(item));
}
if self.eat_keyword(keywords::Struct) {
// STRUCT ITEM
let (ident, item_, extra_attrs) = self.parse_item_struct()?;

View file

@ -0,0 +1,29 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
type A0 = auto;
//~^ ERROR cannot find type `auto` in this scope
type A1 = auto::auto;
//~^ ERROR Use of undeclared type or module `auto`
type A2 = auto<auto, auto>;
//~^ ERROR cannot find type `auto` in this scope
//~| ERROR cannot find type `auto` in this scope
//~| ERROR cannot find type `auto` in this scope
type A3 = auto<<auto as auto>::auto>;
//~^ ERROR cannot find type `auto` in this scope
//~| ERROR cannot find type `auto` in this scope
//~| ERROR Use of undeclared type or module `auto`
type A4 = auto(auto, auto) -> auto;
//~^ ERROR cannot find type `auto` in this scope
//~| ERROR cannot find type `auto` in this scope
//~| ERROR cannot find type `auto` in this scope
//~| ERROR cannot find type `auto` in this scope
fn main() {}

View file

@ -17,6 +17,9 @@ trait DummyTrait {
fn dummy(&self) {}
}
auto trait AutoDummyTrait {}
//~^ ERROR auto traits are experimental and possibly buggy
impl DummyTrait for .. {}
//~^ ERROR auto trait implementations are experimental and possibly buggy

View file

@ -0,0 +1,14 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// compile-flags: -Z parse-only
auto trait Auto<T> { }
//~^ ERROR: expected `{`, found `<`

View file

@ -0,0 +1,14 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// compile-flags: -Z parse-only
auto trait Auto { fn item() }
//~^ ERROR: expected `}`, found `fn`

View file

@ -0,0 +1,14 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// compile-flags: -Z parse-only
auto trait Auto : Send { }
//~^ ERROR: expected `{`, found `:`

View file

@ -0,0 +1,35 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(optin_builtin_traits)]
auto trait Auto {}
// Redundant but accepted until we remove it.
impl Auto for .. {}
unsafe auto trait AutoUnsafe {}
impl !Auto for bool {}
impl !AutoUnsafe for bool {}
struct AutoBool(bool);
impl Auto for AutoBool {}
unsafe impl AutoUnsafe for AutoBool {}
fn take_auto<T: Auto>(_: T) {}
fn take_auto_unsafe<T: AutoUnsafe>(_: T) {}
fn main() {
take_auto(0);
take_auto(AutoBool(true));
take_auto_unsafe(0);
take_auto_unsafe(AutoBool(true));
}