Skip to content

Commit d67c8f6

Browse files
committed
---
yaml --- r: 273135 b: refs/heads/beta c: 8fe63e2 h: refs/heads/master i: 273133: 08ded25 273131: 794f6eb 273127: 9653561 273119: 574c651
1 parent 8f61bc0 commit d67c8f6

File tree

11 files changed

+151
-59
lines changed

11 files changed

+151
-59
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ refs/tags/0.9: 36870b185fc5f5486636d4515f0e22677493f225
2323
refs/tags/0.10: ac33f2b15782272ae348dbd7b14b8257b2148b5a
2424
refs/tags/0.11.0: e1247cb1d0d681be034adb4b558b5a0c0d5720f9
2525
refs/tags/0.12.0: f0c419429ef30723ceaf6b42f9b5a2aeb5d2e2d1
26-
refs/heads/beta: 659ba09b2d9dfe1c9ae50d86ab87fc9acb55a03e
26+
refs/heads/beta: 8fe63e23421f66b730afdbd14c3ec90e39950288
2727
refs/tags/1.0.0-alpha: e42bd6d93a1d3433c486200587f8f9e12590a4d7
2828
refs/heads/tmp: e06d2ad9fcd5027bcaac5b08fc9aa39a49d0ecd3
2929
refs/tags/1.0.0-alpha.2: 4c705f6bc559886632d3871b04f58aab093bfa2f

branches/beta/src/librustc_front/fold.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,7 @@ pub fn noop_fold_impl_item<T: Folder>(i: ImplItem, folder: &mut T) -> ImplItem {
839839
name: folder.fold_name(i.name),
840840
attrs: fold_attrs(i.attrs, folder),
841841
vis: i.vis,
842+
defaultness: i.defaultness,
842843
node: match i.node {
843844
ImplItemKind::Const(ty, expr) => {
844845
ImplItemKind::Const(folder.fold_ty(ty), folder.fold_expr(expr))

branches/beta/src/librustc_front/hir.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -864,10 +864,10 @@ pub struct MethodSig {
864864
pub explicit_self: ExplicitSelf,
865865
}
866866

867-
/// Represents a method declaration in a trait declaration, possibly including
868-
/// a default implementation A trait method is either required (meaning it
869-
/// doesn't have an implementation, just a signature) or provided (meaning it
870-
/// has a default implementation).
867+
/// Represents an item declaration within a trait declaration,
868+
/// possibly including a default implementation. A trait item is
869+
/// either required (meaning it doesn't have an implementation, just a
870+
/// signature) or provided (meaning it has a default implementation).
871871
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
872872
pub struct TraitItem {
873873
pub id: NodeId,
@@ -889,6 +889,7 @@ pub struct ImplItem {
889889
pub id: NodeId,
890890
pub name: Name,
891891
pub vis: Visibility,
892+
pub defaultness: Defaultness,
892893
pub attrs: HirVec<Attribute>,
893894
pub node: ImplItemKind,
894895
pub span: Span,
@@ -1046,6 +1047,12 @@ pub enum Constness {
10461047
NotConst,
10471048
}
10481049

1050+
#[derive(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1051+
pub enum Defaultness {
1052+
Default,
1053+
Final,
1054+
}
1055+
10491056
impl fmt::Display for Unsafety {
10501057
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
10511058
fmt::Display::fmt(match *self {

branches/beta/src/librustc_front/lowering.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,7 @@ pub fn lower_impl_item(lctx: &LoweringContext, i: &ImplItem) -> hir::ImplItem {
756756
name: i.ident.name,
757757
attrs: lower_attrs(lctx, &i.attrs),
758758
vis: lower_visibility(lctx, i.vis),
759+
defaultness: lower_defaultness(lctx, i.defaultness),
759760
node: match i.node {
760761
ImplItemKind::Const(ref ty, ref expr) => {
761762
hir::ImplItemKind::Const(lower_ty(lctx, ty), lower_expr(lctx, expr))
@@ -1707,6 +1708,13 @@ pub fn lower_visibility(_lctx: &LoweringContext, v: Visibility) -> hir::Visibili
17071708
}
17081709
}
17091710

1711+
pub fn lower_defaultness(_lctx: &LoweringContext, d: Defaultness) -> hir::Defaultness {
1712+
match d {
1713+
Defaultness::Default => hir::Defaultness::Default,
1714+
Defaultness::Final => hir::Defaultness::Final,
1715+
}
1716+
}
1717+
17101718
pub fn lower_block_check_mode(lctx: &LoweringContext, b: &BlockCheckMode) -> hir::BlockCheckMode {
17111719
match *b {
17121720
BlockCheckMode::Default => hir::DefaultBlock,

branches/beta/src/libsyntax/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 {

branches/beta/src/libsyntax/ext/expand.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,7 @@ fn expand_impl_item(ii: ast::ImplItem, fld: &mut MacroExpander)
10611061
ident: ii.ident,
10621062
attrs: ii.attrs,
10631063
vis: ii.vis,
1064+
defaultness: ii.defaultness,
10641065
node: match ii.node {
10651066
ast::ImplItemKind::Method(sig, body) => {
10661067
let (sig, body) = expand_and_rename_method(sig, body, fld);

branches/beta/src/libsyntax/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))

branches/beta/src/libsyntax/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};
@@ -644,6 +644,25 @@ impl<'a> Parser<'a> {
644644
}
645645
}
646646

647+
pub fn check_contextual_keyword(&mut self, ident: Ident) -> bool {
648+
let tok = token::Ident(ident, token::Plain);
649+
self.expected_tokens.push(TokenType::Token(tok));
650+
if let token::Ident(ref cur_ident, _) = self.token {
651+
cur_ident.name == ident.name
652+
} else {
653+
false
654+
}
655+
}
656+
657+
pub fn eat_contextual_keyword(&mut self, ident: Ident) -> PResult<bool> {
658+
if self.check_contextual_keyword(ident) {
659+
try!(self.bump());
660+
Ok(true)
661+
} else {
662+
Ok(false)
663+
}
664+
}
665+
647666
/// If the given word is not a keyword, signal an error.
648667
/// If the next token is not the given word, signal an error.
649668
/// Otherwise, eat it.
@@ -705,7 +724,6 @@ impl<'a> Parser<'a> {
705724
}
706725
}
707726

708-
709727
/// Attempt to consume a `<`. If `<<` is seen, replace it with a single
710728
/// `<` and continue. If a `<` is not seen, return false.
711729
///
@@ -4846,6 +4864,7 @@ impl<'a> Parser<'a> {
48464864
let mut attrs = try!(self.parse_outer_attributes());
48474865
let lo = self.span.lo;
48484866
let vis = try!(self.parse_visibility());
4867+
let defaultness = try!(self.parse_defaultness());
48494868
let (name, node) = if self.eat_keyword(keywords::Type) {
48504869
let name = try!(self.parse_ident());
48514870
try!(self.expect(&token::Eq));
@@ -4872,6 +4891,7 @@ impl<'a> Parser<'a> {
48724891
span: mk_sp(lo, self.last_span.hi),
48734892
ident: name,
48744893
vis: vis,
4894+
defaultness: defaultness,
48754895
attrs: attrs,
48764896
node: node
48774897
})
@@ -5208,6 +5228,15 @@ impl<'a> Parser<'a> {
52085228
else { Ok(Visibility::Inherited) }
52095229
}
52105230

5231+
/// Parse defaultness: DEFAULT or nothing
5232+
fn parse_defaultness(&mut self) -> PResult<Defaultness> {
5233+
if try!(self.eat_contextual_keyword(special_idents::DEFAULT)) {
5234+
Ok(Defaultness::Default)
5235+
} else {
5236+
Ok(Defaultness::Final)
5237+
}
5238+
}
5239+
52115240
/// Given a termination token, parse all of the items in a module
52125241
fn parse_mod_items(&mut self, term: &token::Token, inner_lo: BytePos) -> PResult<'a, Mod> {
52135242
let mut items = vec![];

branches/beta/src/libsyntax/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

branches/beta/src/libsyntax_ext/deriving/generic/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@ impl<'a> TraitDef<'a> {
476476
span: self.span,
477477
ident: ident,
478478
vis: ast::Visibility::Inherited,
479+
defaultness: ast::Defaultness::Final,
479480
attrs: Vec::new(),
480481
node: ast::ImplItemKind::Type(type_def.to_ty(cx,
481482
self.span,
@@ -893,6 +894,7 @@ impl<'a> MethodDef<'a> {
893894
attrs: self.attributes.clone(),
894895
span: trait_.span,
895896
vis: ast::Visibility::Inherited,
897+
defaultness: ast::Defaultness::Final,
896898
ident: method_ident,
897899
node: ast::ImplItemKind::Method(ast::MethodSig {
898900
generics: fn_generics,
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2015 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+
// Test successful and unsucessful parsing of the `default` contextual keyword
14+
15+
trait Foo {
16+
fn foo<T: Default>() -> T;
17+
}
18+
19+
impl Foo for u8 {
20+
default fn foo<T: Default>() -> T {
21+
T::default()
22+
}
23+
}
24+
25+
impl Foo for u16 {
26+
pub default fn foo<T: Default>() -> T {
27+
T::default()
28+
}
29+
}
30+
31+
impl Foo for u32 {
32+
default pub fn foo<T: Default>() -> T { T::default() } //~ ERROR expected one of
33+
}
34+
35+
fn main() {}

0 commit comments

Comments
 (0)