Skip to content

Commit 4e3953b

Browse files
leoyvenspetrochenkov
authored andcommitted
Parse auto trait inside fns.
Also refactored parsing auto traits.
1 parent 9ccd3ac commit 4e3953b

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

src/libsyntax/parse/parser.rs

+16-12
Original file line numberDiff line numberDiff line change
@@ -4084,14 +4084,14 @@ impl<'a> Parser<'a> {
40844084
self.token.is_keyword(keywords::Extern) && self.look_ahead(1, |t| t != &token::ModSep)
40854085
}
40864086

4087-
fn eat_auto_trait(&mut self) -> bool {
4088-
if self.token.is_keyword(keywords::Auto)
4089-
&& self.look_ahead(1, |t| t.is_keyword(keywords::Trait))
4090-
{
4091-
self.eat_keyword(keywords::Auto) && self.eat_keyword(keywords::Trait)
4092-
} else {
4093-
false
4094-
}
4087+
fn is_auto_trait_item(&mut self) -> bool {
4088+
// auto trait
4089+
(self.token.is_keyword(keywords::Auto)
4090+
&& self.look_ahead(1, |t| t.is_keyword(keywords::Trait)))
4091+
|| // unsafe auto trait
4092+
(self.token.is_keyword(keywords::Unsafe) &&
4093+
self.look_ahead(1, |t| t.is_keyword(keywords::Auto)) &&
4094+
self.look_ahead(2, |t| t.is_keyword(keywords::Trait)))
40954095
}
40964096

40974097
fn is_defaultness(&self) -> bool {
@@ -4194,7 +4194,8 @@ impl<'a> Parser<'a> {
41944194
node: StmtKind::Item(macro_def),
41954195
span: lo.to(self.prev_span),
41964196
}
4197-
// Starts like a simple path, but not a union item or item with `crate` visibility.
4197+
// Starts like a simple path, being careful to avoid contextual keywords
4198+
// such as a union items, item with `crate` visibility or auto trait items.
41984199
// Our goal here is to parse an arbitrary path `a::b::c` but not something that starts
41994200
// like a path (1 token), but it fact not a path.
42004201
// `union::b::c` - path, `union U { ... }` - not a path.
@@ -4204,7 +4205,8 @@ impl<'a> Parser<'a> {
42044205
!self.token.is_qpath_start() &&
42054206
!self.is_union_item() &&
42064207
!self.is_crate_vis() &&
4207-
!self.is_extern_non_path() {
4208+
!self.is_extern_non_path() &&
4209+
!self.is_auto_trait_item() {
42084210
let pth = self.parse_path(PathStyle::Expr)?;
42094211

42104212
if !self.eat(&token::Not) {
@@ -6368,7 +6370,8 @@ impl<'a> Parser<'a> {
63686370
let is_auto = if self.eat_keyword(keywords::Trait) {
63696371
IsAuto::No
63706372
} else {
6371-
self.eat_auto_trait();
6373+
self.eat_keyword(keywords::Auto);
6374+
self.eat_keyword(keywords::Trait);
63726375
IsAuto::Yes
63736376
};
63746377
let (ident, item_, extra_attrs) =
@@ -6482,7 +6485,8 @@ impl<'a> Parser<'a> {
64826485
let is_auto = if self.eat_keyword(keywords::Trait) {
64836486
IsAuto::No
64846487
} else {
6485-
self.eat_auto_trait();
6488+
self.eat_keyword(keywords::Auto);
6489+
self.eat_keyword(keywords::Trait);
64866490
IsAuto::Yes
64876491
};
64886492
// TRAIT ITEM

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#![feature(optin_builtin_traits)]
1212

1313
auto trait Auto {}
14-
1514
unsafe auto trait AutoUnsafe {}
1615

1716
impl !Auto for bool {}
@@ -26,6 +25,10 @@ fn take_auto<T: Auto>(_: T) {}
2625
fn take_auto_unsafe<T: AutoUnsafe>(_: T) {}
2726

2827
fn main() {
28+
// Parse inside functions.
29+
auto trait AutoInner {}
30+
unsafe auto trait AutoUnsafeInner {}
31+
2932
take_auto(0);
3033
take_auto(AutoBool(true));
3134
take_auto_unsafe(0);

0 commit comments

Comments
 (0)