Skip to content

Commit ed1a9ad

Browse files
committed
Merge branch 'rust'
2 parents 5b6ba8c + b3614a5 commit ed1a9ad

File tree

7 files changed

+109
-55
lines changed

7 files changed

+109
-55
lines changed

syntex_syntax/src/ast.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,10 +1328,10 @@ pub struct MethodSig {
13281328
pub explicit_self: ExplicitSelf,
13291329
}
13301330

1331-
/// Represents a method declaration in a trait declaration, possibly including
1332-
/// a default implementation. A trait method is either required (meaning it
1333-
/// doesn't have an implementation, just a signature) or provided (meaning it
1334-
/// has a default implementation).
1331+
/// Represents an item declaration within a trait declaration,
1332+
/// possibly including a default implementation. A trait item is
1333+
/// either required (meaning it doesn't have an implementation, just a
1334+
/// signature) or provided (meaning it has a default implementation).
13351335
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
13361336
pub struct TraitItem {
13371337
pub id: NodeId,
@@ -1353,6 +1353,7 @@ pub struct ImplItem {
13531353
pub id: NodeId,
13541354
pub ident: Ident,
13551355
pub vis: Visibility,
1356+
pub defaultness: Defaultness,
13561357
pub attrs: Vec<Attribute>,
13571358
pub node: ImplItemKind,
13581359
pub span: Span,
@@ -1654,6 +1655,12 @@ pub enum Constness {
16541655
NotConst,
16551656
}
16561657

1658+
#[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1659+
pub enum Defaultness {
1660+
Default,
1661+
Final,
1662+
}
1663+
16571664
impl fmt::Display for Unsafety {
16581665
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
16591666
fmt::Display::fmt(match *self {

syntex_syntax/src/ext/expand.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,7 @@ fn expand_impl_item(ii: ast::ImplItem, fld: &mut MacroExpander)
10751075
ident: ii.ident,
10761076
attrs: ii.attrs,
10771077
vis: ii.vis,
1078+
defaultness: ii.defaultness,
10781079
node: match ii.node {
10791080
ast::ImplItemKind::Method(sig, body) => {
10801081
let (sig, body) = expand_and_rename_method(sig, body, fld);

syntex_syntax/src/feature_gate.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,10 @@ const KNOWN_FEATURES: &'static [(&'static str, &'static str, Option<u32>, Status
247247
("inclusive_range_syntax", "1.7.0", Some(28237), Active),
248248

249249
// `expr?`
250-
("question_mark", "1.9.0", Some(31436), Active)
250+
("question_mark", "1.9.0", Some(31436), Active),
251+
252+
// impl specialization (RFC 1210)
253+
("specialization", "1.7.0", Some(31844), Active),
251254
];
252255
// (changing above list without updating src/doc/reference.md makes @cmr sad)
253256

@@ -574,6 +577,7 @@ pub struct Features {
574577
pub stmt_expr_attributes: bool,
575578
pub deprecated: bool,
576579
pub question_mark: bool,
580+
pub specialization: bool,
577581
}
578582

579583
impl Features {
@@ -608,6 +612,7 @@ impl Features {
608612
stmt_expr_attributes: false,
609613
deprecated: false,
610614
question_mark: false,
615+
specialization: false,
611616
}
612617
}
613618
}
@@ -1102,6 +1107,12 @@ impl<'a, 'v> Visitor<'v> for PostExpansionVisitor<'a> {
11021107
}
11031108

11041109
fn visit_impl_item(&mut self, ii: &'v ast::ImplItem) {
1110+
if ii.defaultness == ast::Defaultness::Default {
1111+
self.gate_feature("specialization",
1112+
ii.span,
1113+
"specialization is unstable");
1114+
}
1115+
11051116
match ii.node {
11061117
ast::ImplItemKind::Const(..) => {
11071118
self.gate_feature("associated_consts",
@@ -1212,6 +1223,7 @@ fn check_crate_inner<F>(cm: &CodeMap, span_handler: &Handler,
12121223
stmt_expr_attributes: cx.has_feature("stmt_expr_attributes"),
12131224
deprecated: cx.has_feature("deprecated"),
12141225
question_mark: cx.has_feature("question_mark"),
1226+
specialization: cx.has_feature("specialization"),
12151227
}
12161228
}
12171229

syntex_syntax/src/fold.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,7 @@ pub fn noop_fold_impl_item<T: Folder>(i: ImplItem, folder: &mut T)
993993
ident: folder.fold_ident(i.ident),
994994
attrs: fold_attrs(i.attrs, folder),
995995
vis: i.vis,
996+
defaultness: i.defaultness,
996997
node: match i.node {
997998
ast::ImplItemKind::Const(ty, expr) => {
998999
ast::ImplItemKind::Const(folder.fold_ty(ty), folder.fold_expr(expr))

syntex_syntax/src/parse/parser.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use ast::{Mod, Arg, Arm, Attribute, BindingMode, TraitItemKind};
1818
use ast::Block;
1919
use ast::{BlockCheckMode, CaptureBy};
2020
use ast::{Constness, Crate, CrateConfig};
21-
use ast::{Decl, DeclKind};
21+
use ast::{Decl, DeclKind, Defaultness};
2222
use ast::{EMPTY_CTXT, EnumDef, ExplicitSelf};
2323
use ast::{Expr, ExprKind, RangeLimits};
2424
use ast::{Field, FnDecl};
@@ -651,6 +651,25 @@ impl<'a> Parser<'a> {
651651
}
652652
}
653653

654+
pub fn check_contextual_keyword(&mut self, ident: Ident) -> bool {
655+
let tok = token::Ident(ident, token::Plain);
656+
self.expected_tokens.push(TokenType::Token(tok));
657+
if let token::Ident(ref cur_ident, _) = self.token {
658+
cur_ident.name == ident.name
659+
} else {
660+
false
661+
}
662+
}
663+
664+
pub fn eat_contextual_keyword(&mut self, ident: Ident) -> bool {
665+
if self.check_contextual_keyword(ident) {
666+
self.bump();
667+
true
668+
} else {
669+
false
670+
}
671+
}
672+
654673
/// If the given word is not a keyword, signal an error.
655674
/// If the next token is not the given word, signal an error.
656675
/// Otherwise, eat it.
@@ -712,7 +731,6 @@ impl<'a> Parser<'a> {
712731
}
713732
}
714733

715-
716734
/// Attempt to consume a `<`. If `<<` is seen, replace it with a single
717735
/// `<` and continue. If a `<` is not seen, return false.
718736
///
@@ -4853,6 +4871,7 @@ impl<'a> Parser<'a> {
48534871
let mut attrs = try!(self.parse_outer_attributes());
48544872
let lo = self.span.lo;
48554873
let vis = try!(self.parse_visibility());
4874+
let defaultness = try!(self.parse_defaultness());
48564875
let (name, node) = if self.eat_keyword(keywords::Type) {
48574876
let name = try!(self.parse_ident());
48584877
try!(self.expect(&token::Eq));
@@ -4879,6 +4898,7 @@ impl<'a> Parser<'a> {
48794898
span: mk_sp(lo, self.last_span.hi),
48804899
ident: name,
48814900
vis: vis,
4901+
defaultness: defaultness,
48824902
attrs: attrs,
48834903
node: node
48844904
})
@@ -5215,6 +5235,15 @@ impl<'a> Parser<'a> {
52155235
else { Ok(Visibility::Inherited) }
52165236
}
52175237

5238+
/// Parse defaultness: DEFAULT or nothing
5239+
fn parse_defaultness(&mut self) -> PResult<'a, Defaultness> {
5240+
if self.eat_contextual_keyword(special_idents::DEFAULT) {
5241+
Ok(Defaultness::Default)
5242+
} else {
5243+
Ok(Defaultness::Final)
5244+
}
5245+
}
5246+
52185247
/// Given a termination token, parse all of the items in a module
52195248
fn parse_mod_items(&mut self, term: &token::Token, inner_lo: BytePos) -> PResult<'a, Mod> {
52205249
let mut items = vec![];

syntex_syntax/src/parse/token.rs

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -545,66 +545,67 @@ declare_special_idents_and_keywords! {
545545
(9, __unused1, "<__unused1>");
546546
(super::SELF_TYPE_KEYWORD_NAME_NUM, type_self, "Self");
547547
(11, prelude_import, "prelude_import");
548+
(12, DEFAULT, "default");
548549
}
549550

550551
pub mod keywords {
551552
// These ones are variants of the Keyword enum
552553

553554
'strict:
554-
(12, As, "as");
555-
(13, Break, "break");
556-
(14, Crate, "crate");
557-
(15, Else, "else");
558-
(16, Enum, "enum");
559-
(17, Extern, "extern");
560-
(18, False, "false");
561-
(19, Fn, "fn");
562-
(20, For, "for");
563-
(21, If, "if");
564-
(22, Impl, "impl");
565-
(23, In, "in");
566-
(24, Let, "let");
567-
(25, Loop, "loop");
568-
(26, Match, "match");
569-
(27, Mod, "mod");
570-
(28, Move, "move");
571-
(29, Mut, "mut");
572-
(30, Pub, "pub");
573-
(31, Ref, "ref");
574-
(32, Return, "return");
555+
(13, As, "as");
556+
(14, Break, "break");
557+
(15, Crate, "crate");
558+
(16, Else, "else");
559+
(17, Enum, "enum");
560+
(18, Extern, "extern");
561+
(19, False, "false");
562+
(20, Fn, "fn");
563+
(21, For, "for");
564+
(22, If, "if");
565+
(23, Impl, "impl");
566+
(24, In, "in");
567+
(25, Let, "let");
568+
(26, Loop, "loop");
569+
(27, Match, "match");
570+
(28, Mod, "mod");
571+
(29, Move, "move");
572+
(30, Mut, "mut");
573+
(31, Pub, "pub");
574+
(32, Ref, "ref");
575+
(33, Return, "return");
575576
// Static and Self are also special idents (prefill de-dupes)
576577
(super::STATIC_KEYWORD_NAME_NUM, Static, "static");
577578
(super::SELF_KEYWORD_NAME_NUM, SelfValue, "self");
578579
(super::SELF_TYPE_KEYWORD_NAME_NUM, SelfType, "Self");
579-
(33, Struct, "struct");
580+
(34, Struct, "struct");
580581
(super::SUPER_KEYWORD_NAME_NUM, Super, "super");
581-
(34, True, "true");
582-
(35, Trait, "trait");
583-
(36, Type, "type");
584-
(37, Unsafe, "unsafe");
585-
(38, Use, "use");
586-
(39, While, "while");
587-
(40, Continue, "continue");
588-
(41, Box, "box");
589-
(42, Const, "const");
590-
(43, Where, "where");
582+
(35, True, "true");
583+
(36, Trait, "trait");
584+
(37, Type, "type");
585+
(38, Unsafe, "unsafe");
586+
(39, Use, "use");
587+
(40, While, "while");
588+
(41, Continue, "continue");
589+
(42, Box, "box");
590+
(43, Const, "const");
591+
(44, Where, "where");
591592
'reserved:
592-
(44, Virtual, "virtual");
593-
(45, Proc, "proc");
594-
(46, Alignof, "alignof");
595-
(47, Become, "become");
596-
(48, Offsetof, "offsetof");
597-
(49, Priv, "priv");
598-
(50, Pure, "pure");
599-
(51, Sizeof, "sizeof");
600-
(52, Typeof, "typeof");
601-
(53, Unsized, "unsized");
602-
(54, Yield, "yield");
603-
(55, Do, "do");
604-
(56, Abstract, "abstract");
605-
(57, Final, "final");
606-
(58, Override, "override");
607-
(59, Macro, "macro");
593+
(45, Virtual, "virtual");
594+
(46, Proc, "proc");
595+
(47, Alignof, "alignof");
596+
(48, Become, "become");
597+
(49, Offsetof, "offsetof");
598+
(50, Priv, "priv");
599+
(51, Pure, "pure");
600+
(52, Sizeof, "sizeof");
601+
(53, Typeof, "typeof");
602+
(54, Unsized, "unsized");
603+
(55, Yield, "yield");
604+
(56, Do, "do");
605+
(57, Abstract, "abstract");
606+
(58, Final, "final");
607+
(59, Override, "override");
608+
(60, Macro, "macro");
608609
}
609610
}
610611

syntex_syntax/src/print/pprust.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,6 +1588,9 @@ impl<'a> State<'a> {
15881588
try!(self.hardbreak_if_not_bol());
15891589
try!(self.maybe_print_comment(ii.span.lo));
15901590
try!(self.print_outer_attributes(&ii.attrs));
1591+
if let ast::Defaultness::Default = ii.defaultness {
1592+
try!(self.word_nbsp("default"));
1593+
}
15911594
match ii.node {
15921595
ast::ImplItemKind::Const(ref ty, ref expr) => {
15931596
try!(self.print_associated_const(ii.ident, &ty, Some(&expr), ii.vis));

0 commit comments

Comments
 (0)