Skip to content

Commit 7f286a8

Browse files
Rollup merge of #42330 - qnighy:macro-named-default, r=petrochenkov
Parse macros named "default" correctly. Fixes #42292.
2 parents 0c339ff + b670930 commit 7f286a8

File tree

2 files changed

+50
-19
lines changed

2 files changed

+50
-19
lines changed

src/libsyntax/parse/parser.rs

+23-19
Original file line numberDiff line numberDiff line change
@@ -698,24 +698,6 @@ impl<'a> Parser<'a> {
698698
}
699699
}
700700

701-
pub fn check_contextual_keyword(&mut self, ident: Ident) -> bool {
702-
self.expected_tokens.push(TokenType::Token(token::Ident(ident)));
703-
if let token::Ident(ref cur_ident) = self.token {
704-
cur_ident.name == ident.name
705-
} else {
706-
false
707-
}
708-
}
709-
710-
pub fn eat_contextual_keyword(&mut self, ident: Ident) -> bool {
711-
if self.check_contextual_keyword(ident) {
712-
self.bump();
713-
true
714-
} else {
715-
false
716-
}
717-
}
718-
719701
/// If the given word is not a keyword, signal an error.
720702
/// If the next token is not the given word, signal an error.
721703
/// Otherwise, eat it.
@@ -3755,6 +3737,28 @@ impl<'a> Parser<'a> {
37553737
self.look_ahead(1, |t| t.is_ident() && !t.is_any_keyword())
37563738
}
37573739

3740+
fn is_defaultness(&self) -> bool {
3741+
// `pub` is included for better error messages
3742+
self.token.is_keyword(keywords::Default) &&
3743+
self.look_ahead(1, |t| t.is_keyword(keywords::Impl) ||
3744+
t.is_keyword(keywords::Const) ||
3745+
t.is_keyword(keywords::Fn) ||
3746+
t.is_keyword(keywords::Unsafe) ||
3747+
t.is_keyword(keywords::Extern) ||
3748+
t.is_keyword(keywords::Type) ||
3749+
t.is_keyword(keywords::Pub))
3750+
}
3751+
3752+
fn eat_defaultness(&mut self) -> bool {
3753+
let is_defaultness = self.is_defaultness();
3754+
if is_defaultness {
3755+
self.bump()
3756+
} else {
3757+
self.expected_tokens.push(TokenType::Keyword(keywords::Default));
3758+
}
3759+
is_defaultness
3760+
}
3761+
37583762
fn eat_macro_def(&mut self, attrs: &[Attribute], vis: &Visibility)
37593763
-> PResult<'a, Option<P<Item>>> {
37603764
let lo = self.span;
@@ -5229,7 +5233,7 @@ impl<'a> Parser<'a> {
52295233

52305234
/// Parse defaultness: DEFAULT or nothing
52315235
fn parse_defaultness(&mut self) -> PResult<'a, Defaultness> {
5232-
if self.eat_contextual_keyword(keywords::Default.ident()) {
5236+
if self.eat_defaultness() {
52335237
Ok(Defaultness::Default)
52345238
} else {
52355239
Ok(Defaultness::Final)
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
macro_rules! default {
12+
($($x:tt)*) => { $($x)* }
13+
}
14+
15+
default! {
16+
struct A;
17+
}
18+
19+
impl A {
20+
default! {
21+
fn foo(&self) {}
22+
}
23+
}
24+
25+
fn main() {
26+
A.foo();
27+
}

0 commit comments

Comments
 (0)