Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
8fe63e23
R
Rust
项目概览
int
/
Rust
大约 1 年 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Rust
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
8fe63e23
编写于
12月 18, 2015
作者:
A
Aaron Turon
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add `default` as contextual keyword, and parse it for impl items.
上级
659ba09b
变更
10
隐藏空白更改
内联
并排
Showing
10 changed file
with
150 addition
and
58 deletion
+150
-58
src/librustc_front/fold.rs
src/librustc_front/fold.rs
+1
-0
src/librustc_front/hir.rs
src/librustc_front/hir.rs
+11
-4
src/librustc_front/lowering.rs
src/librustc_front/lowering.rs
+8
-0
src/libsyntax/ast.rs
src/libsyntax/ast.rs
+11
-4
src/libsyntax/ext/expand.rs
src/libsyntax/ext/expand.rs
+1
-0
src/libsyntax/fold.rs
src/libsyntax/fold.rs
+1
-0
src/libsyntax/parse/parser.rs
src/libsyntax/parse/parser.rs
+31
-2
src/libsyntax/parse/token.rs
src/libsyntax/parse/token.rs
+49
-48
src/libsyntax_ext/deriving/generic/mod.rs
src/libsyntax_ext/deriving/generic/mod.rs
+2
-0
src/test/parse-fail/default.rs
src/test/parse-fail/default.rs
+35
-0
未找到文件。
src/librustc_front/fold.rs
浏览文件 @
8fe63e23
...
...
@@ -839,6 +839,7 @@ pub fn noop_fold_impl_item<T: Folder>(i: ImplItem, folder: &mut T) -> ImplItem {
name
:
folder
.fold_name
(
i
.name
),
attrs
:
fold_attrs
(
i
.attrs
,
folder
),
vis
:
i
.vis
,
defaultness
:
i
.defaultness
,
node
:
match
i
.node
{
ImplItemKind
::
Const
(
ty
,
expr
)
=>
{
ImplItemKind
::
Const
(
folder
.fold_ty
(
ty
),
folder
.fold_expr
(
expr
))
...
...
src/librustc_front/hir.rs
浏览文件 @
8fe63e23
...
...
@@ -864,10 +864,10 @@ pub struct MethodSig {
pub
explicit_self
:
ExplicitSelf
,
}
/// Represents a
method declaration in a trait declaration, possibly including
///
a default implementation A trait method is either required (meaning it
///
doesn't have an implementation, just a signature) or provided (meaning it
/// has a default implementation).
/// Represents a
n item declaration within a trait declaration,
///
possibly including a default implementation. A trait item is
///
either required (meaning it doesn't have an implementation, just a
///
signature) or provided (meaning it
has a default implementation).
#[derive(Clone,
PartialEq,
Eq,
RustcEncodable,
RustcDecodable,
Hash,
Debug)]
pub
struct
TraitItem
{
pub
id
:
NodeId
,
...
...
@@ -889,6 +889,7 @@ pub struct ImplItem {
pub
id
:
NodeId
,
pub
name
:
Name
,
pub
vis
:
Visibility
,
pub
defaultness
:
Defaultness
,
pub
attrs
:
HirVec
<
Attribute
>
,
pub
node
:
ImplItemKind
,
pub
span
:
Span
,
...
...
@@ -1046,6 +1047,12 @@ pub enum Constness {
NotConst
,
}
#[derive(Copy,
Clone,
PartialEq,
Eq,
RustcEncodable,
RustcDecodable,
Hash,
Debug)]
pub
enum
Defaultness
{
Default
,
Final
,
}
impl
fmt
::
Display
for
Unsafety
{
fn
fmt
(
&
self
,
f
:
&
mut
fmt
::
Formatter
)
->
fmt
::
Result
{
fmt
::
Display
::
fmt
(
match
*
self
{
...
...
src/librustc_front/lowering.rs
浏览文件 @
8fe63e23
...
...
@@ -756,6 +756,7 @@ pub fn lower_impl_item(lctx: &LoweringContext, i: &ImplItem) -> hir::ImplItem {
name
:
i
.ident.name
,
attrs
:
lower_attrs
(
lctx
,
&
i
.attrs
),
vis
:
lower_visibility
(
lctx
,
i
.vis
),
defaultness
:
lower_defaultness
(
lctx
,
i
.defaultness
),
node
:
match
i
.node
{
ImplItemKind
::
Const
(
ref
ty
,
ref
expr
)
=>
{
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
}
}
pub
fn
lower_defaultness
(
_
lctx
:
&
LoweringContext
,
d
:
Defaultness
)
->
hir
::
Defaultness
{
match
d
{
Defaultness
::
Default
=>
hir
::
Defaultness
::
Default
,
Defaultness
::
Final
=>
hir
::
Defaultness
::
Final
,
}
}
pub
fn
lower_block_check_mode
(
lctx
:
&
LoweringContext
,
b
:
&
BlockCheckMode
)
->
hir
::
BlockCheckMode
{
match
*
b
{
BlockCheckMode
::
Default
=>
hir
::
DefaultBlock
,
...
...
src/libsyntax/ast.rs
浏览文件 @
8fe63e23
...
...
@@ -1328,10 +1328,10 @@ pub struct MethodSig {
pub
explicit_self
:
ExplicitSelf
,
}
/// Represents a
method declaration in a trait declaration, possibly including
///
a default implementation. A trait method is either required (meaning it
///
doesn't have an implementation, just a signature) or provided (meaning it
/// has a default implementation).
/// Represents a
n item declaration within a trait declaration,
///
possibly including a default implementation. A trait item is
///
either required (meaning it doesn't have an implementation, just a
///
signature) or provided (meaning it
has a default implementation).
#[derive(Clone,
PartialEq,
Eq,
RustcEncodable,
RustcDecodable,
Hash,
Debug)]
pub
struct
TraitItem
{
pub
id
:
NodeId
,
...
...
@@ -1353,6 +1353,7 @@ pub struct ImplItem {
pub
id
:
NodeId
,
pub
ident
:
Ident
,
pub
vis
:
Visibility
,
pub
defaultness
:
Defaultness
,
pub
attrs
:
Vec
<
Attribute
>
,
pub
node
:
ImplItemKind
,
pub
span
:
Span
,
...
...
@@ -1654,6 +1655,12 @@ pub enum Constness {
NotConst
,
}
#[derive(Copy,
Clone,
PartialEq,
Eq,
RustcEncodable,
RustcDecodable,
Hash,
Debug)]
pub
enum
Defaultness
{
Default
,
Final
,
}
impl
fmt
::
Display
for
Unsafety
{
fn
fmt
(
&
self
,
f
:
&
mut
fmt
::
Formatter
)
->
fmt
::
Result
{
fmt
::
Display
::
fmt
(
match
*
self
{
...
...
src/libsyntax/ext/expand.rs
浏览文件 @
8fe63e23
...
...
@@ -1061,6 +1061,7 @@ fn expand_impl_item(ii: ast::ImplItem, fld: &mut MacroExpander)
ident
:
ii
.ident
,
attrs
:
ii
.attrs
,
vis
:
ii
.vis
,
defaultness
:
ii
.defaultness
,
node
:
match
ii
.node
{
ast
::
ImplItemKind
::
Method
(
sig
,
body
)
=>
{
let
(
sig
,
body
)
=
expand_and_rename_method
(
sig
,
body
,
fld
);
...
...
src/libsyntax/fold.rs
浏览文件 @
8fe63e23
...
...
@@ -993,6 +993,7 @@ pub fn noop_fold_impl_item<T: Folder>(i: ImplItem, folder: &mut T)
ident
:
folder
.fold_ident
(
i
.ident
),
attrs
:
fold_attrs
(
i
.attrs
,
folder
),
vis
:
i
.vis
,
defaultness
:
i
.defaultness
,
node
:
match
i
.node
{
ast
::
ImplItemKind
::
Const
(
ty
,
expr
)
=>
{
ast
::
ImplItemKind
::
Const
(
folder
.fold_ty
(
ty
),
folder
.fold_expr
(
expr
))
...
...
src/libsyntax/parse/parser.rs
浏览文件 @
8fe63e23
...
...
@@ -18,7 +18,7 @@
use
ast
::
Block
;
use
ast
::{
BlockCheckMode
,
CaptureBy
};
use
ast
::{
Constness
,
Crate
,
CrateConfig
};
use
ast
::{
Decl
,
DeclKind
};
use
ast
::{
Decl
,
DeclKind
,
Defaultness
};
use
ast
::{
EMPTY_CTXT
,
EnumDef
,
ExplicitSelf
};
use
ast
::{
Expr
,
ExprKind
,
RangeLimits
};
use
ast
::{
Field
,
FnDecl
};
...
...
@@ -644,6 +644,25 @@ pub fn eat_keyword_noexpect(&mut self, kw: keywords::Keyword) -> bool {
}
}
pub
fn
check_contextual_keyword
(
&
mut
self
,
ident
:
Ident
)
->
bool
{
let
tok
=
token
::
Ident
(
ident
,
token
::
Plain
);
self
.expected_tokens
.push
(
TokenType
::
Token
(
tok
));
if
let
token
::
Ident
(
ref
cur_ident
,
_
)
=
self
.token
{
cur_ident
.name
==
ident
.name
}
else
{
false
}
}
pub
fn
eat_contextual_keyword
(
&
mut
self
,
ident
:
Ident
)
->
PResult
<
bool
>
{
if
self
.check_contextual_keyword
(
ident
)
{
try!
(
self
.bump
());
Ok
(
true
)
}
else
{
Ok
(
false
)
}
}
/// If the given word is not a keyword, signal an error.
/// If the next token is not the given word, signal an error.
/// Otherwise, eat it.
...
...
@@ -705,7 +724,6 @@ pub fn expect_no_suffix(&self, sp: Span, kind: &str, suffix: Option<ast::Name>)
}
}
/// Attempt to consume a `<`. If `<<` is seen, replace it with a single
/// `<` and continue. If a `<` is not seen, return false.
///
...
...
@@ -4846,6 +4864,7 @@ pub fn parse_impl_item(&mut self) -> PResult<'a, ImplItem> {
let
mut
attrs
=
try!
(
self
.parse_outer_attributes
());
let
lo
=
self
.span.lo
;
let
vis
=
try!
(
self
.parse_visibility
());
let
defaultness
=
try!
(
self
.parse_defaultness
());
let
(
name
,
node
)
=
if
self
.eat_keyword
(
keywords
::
Type
)
{
let
name
=
try!
(
self
.parse_ident
());
try!
(
self
.expect
(
&
token
::
Eq
));
...
...
@@ -4872,6 +4891,7 @@ pub fn parse_impl_item(&mut self) -> PResult<'a, ImplItem> {
span
:
mk_sp
(
lo
,
self
.last_span.hi
),
ident
:
name
,
vis
:
vis
,
defaultness
:
defaultness
,
attrs
:
attrs
,
node
:
node
})
...
...
@@ -5208,6 +5228,15 @@ fn parse_visibility(&mut self) -> PResult<'a, Visibility> {
else
{
Ok
(
Visibility
::
Inherited
)
}
}
/// Parse defaultness: DEFAULT or nothing
fn
parse_defaultness
(
&
mut
self
)
->
PResult
<
Defaultness
>
{
if
try!
(
self
.eat_contextual_keyword
(
special_idents
::
DEFAULT
))
{
Ok
(
Defaultness
::
Default
)
}
else
{
Ok
(
Defaultness
::
Final
)
}
}
/// Given a termination token, parse all of the items in a module
fn
parse_mod_items
(
&
mut
self
,
term
:
&
token
::
Token
,
inner_lo
:
BytePos
)
->
PResult
<
'a
,
Mod
>
{
let
mut
items
=
vec!
[];
...
...
src/libsyntax/parse/token.rs
浏览文件 @
8fe63e23
...
...
@@ -545,66 +545,67 @@ pub mod special_idents {
(
9
,
__u
nused1
,
"<__unused1>"
);
(
super
::
SELF_TYPE_KEYWORD_NAME_NUM
,
type_self
,
"Self"
);
(
11
,
prelude_import
,
"prelude_import"
);
(
12
,
DEFAULT
,
"default"
);
}
pub
mod
keywords
{
// These ones are variants of the Keyword enum
'strict
:
(
1
2
,
As
,
"as"
);
(
1
3
,
Break
,
"break"
);
(
1
4
,
Crate
,
"crate"
);
(
1
5
,
Else
,
"else"
);
(
1
6
,
Enum
,
"enum"
);
(
1
7
,
Extern
,
"extern"
);
(
1
8
,
False
,
"false"
);
(
19
,
Fn
,
"fn"
);
(
2
0
,
For
,
"for"
);
(
2
1
,
If
,
"if"
);
(
2
2
,
Impl
,
"impl"
);
(
2
3
,
In
,
"in"
);
(
2
4
,
Let
,
"let"
);
(
2
5
,
Loop
,
"loop"
);
(
2
6
,
Match
,
"match"
);
(
2
7
,
Mod
,
"mod"
);
(
2
8
,
Move
,
"move"
);
(
29
,
Mut
,
"mut"
);
(
3
0
,
Pub
,
"pub"
);
(
3
1
,
Ref
,
"ref"
);
(
3
2
,
Return
,
"return"
);
(
1
3
,
As
,
"as"
);
(
1
4
,
Break
,
"break"
);
(
1
5
,
Crate
,
"crate"
);
(
1
6
,
Else
,
"else"
);
(
1
7
,
Enum
,
"enum"
);
(
1
8
,
Extern
,
"extern"
);
(
1
9
,
False
,
"false"
);
(
20
,
Fn
,
"fn"
);
(
2
1
,
For
,
"for"
);
(
2
2
,
If
,
"if"
);
(
2
3
,
Impl
,
"impl"
);
(
2
4
,
In
,
"in"
);
(
2
5
,
Let
,
"let"
);
(
2
6
,
Loop
,
"loop"
);
(
2
7
,
Match
,
"match"
);
(
2
8
,
Mod
,
"mod"
);
(
2
9
,
Move
,
"move"
);
(
30
,
Mut
,
"mut"
);
(
3
1
,
Pub
,
"pub"
);
(
3
2
,
Ref
,
"ref"
);
(
3
3
,
Return
,
"return"
);
// Static and Self are also special idents (prefill de-dupes)
(
super
::
STATIC_KEYWORD_NAME_NUM
,
Static
,
"static"
);
(
super
::
SELF_KEYWORD_NAME_NUM
,
SelfValue
,
"self"
);
(
super
::
SELF_TYPE_KEYWORD_NAME_NUM
,
SelfType
,
"Self"
);
(
3
3
,
Struct
,
"struct"
);
(
3
4
,
Struct
,
"struct"
);
(
super
::
SUPER_KEYWORD_NAME_NUM
,
Super
,
"super"
);
(
3
4
,
True
,
"true"
);
(
3
5
,
Trait
,
"trait"
);
(
3
6
,
Type
,
"type"
);
(
3
7
,
Unsafe
,
"unsafe"
);
(
3
8
,
Use
,
"use"
);
(
39
,
While
,
"while"
);
(
4
0
,
Continue
,
"continue"
);
(
4
1
,
Box
,
"box"
);
(
4
2
,
Const
,
"const"
);
(
4
3
,
Where
,
"where"
);
(
3
5
,
True
,
"true"
);
(
3
6
,
Trait
,
"trait"
);
(
3
7
,
Type
,
"type"
);
(
3
8
,
Unsafe
,
"unsafe"
);
(
3
9
,
Use
,
"use"
);
(
40
,
While
,
"while"
);
(
4
1
,
Continue
,
"continue"
);
(
4
2
,
Box
,
"box"
);
(
4
3
,
Const
,
"const"
);
(
4
4
,
Where
,
"where"
);
'reserved
:
(
4
4
,
Virtual
,
"virtual"
);
(
4
5
,
Proc
,
"proc"
);
(
4
6
,
Alignof
,
"alignof"
);
(
4
7
,
Become
,
"become"
);
(
4
8
,
Offsetof
,
"offsetof"
);
(
49
,
Priv
,
"priv"
);
(
5
0
,
Pure
,
"pure"
);
(
5
1
,
Sizeof
,
"sizeof"
);
(
5
2
,
Typeof
,
"typeof"
);
(
5
3
,
Unsized
,
"unsized"
);
(
5
4
,
Yield
,
"yield"
);
(
5
5
,
Do
,
"do"
);
(
5
6
,
Abstract
,
"abstract"
);
(
5
7
,
Final
,
"final"
);
(
5
8
,
Override
,
"override"
);
(
59
,
Macro
,
"macro"
);
(
4
5
,
Virtual
,
"virtual"
);
(
4
6
,
Proc
,
"proc"
);
(
4
7
,
Alignof
,
"alignof"
);
(
4
8
,
Become
,
"become"
);
(
4
9
,
Offsetof
,
"offsetof"
);
(
50
,
Priv
,
"priv"
);
(
5
1
,
Pure
,
"pure"
);
(
5
2
,
Sizeof
,
"sizeof"
);
(
5
3
,
Typeof
,
"typeof"
);
(
5
4
,
Unsized
,
"unsized"
);
(
5
5
,
Yield
,
"yield"
);
(
5
6
,
Do
,
"do"
);
(
5
7
,
Abstract
,
"abstract"
);
(
5
8
,
Final
,
"final"
);
(
5
9
,
Override
,
"override"
);
(
60
,
Macro
,
"macro"
);
}
}
...
...
src/libsyntax_ext/deriving/generic/mod.rs
浏览文件 @
8fe63e23
...
...
@@ -476,6 +476,7 @@ fn create_derived_impl(&self,
span
:
self
.span
,
ident
:
ident
,
vis
:
ast
::
Visibility
::
Inherited
,
defaultness
:
ast
::
Defaultness
::
Final
,
attrs
:
Vec
::
new
(),
node
:
ast
::
ImplItemKind
::
Type
(
type_def
.to_ty
(
cx
,
self
.span
,
...
...
@@ -893,6 +894,7 @@ fn create_method(&self,
attrs
:
self
.attributes
.clone
(),
span
:
trait_
.span
,
vis
:
ast
::
Visibility
::
Inherited
,
defaultness
:
ast
::
Defaultness
::
Final
,
ident
:
method_ident
,
node
:
ast
::
ImplItemKind
::
Method
(
ast
::
MethodSig
{
generics
:
fn_generics
,
...
...
src/test/parse-fail/default.rs
0 → 100644
浏览文件 @
8fe63e23
// Copyright 2015 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
// Test successful and unsucessful parsing of the `default` contextual keyword
trait
Foo
{
fn
foo
<
T
:
Default
>
()
->
T
;
}
impl
Foo
for
u8
{
default
fn
foo
<
T
:
Default
>
()
->
T
{
T
::
default
()
}
}
impl
Foo
for
u16
{
pub
default
fn
foo
<
T
:
Default
>
()
->
T
{
T
::
default
()
}
}
impl
Foo
for
u32
{
default
pub
fn
foo
<
T
:
Default
>
()
->
T
{
T
::
default
()
}
//~ ERROR expected one of
}
fn
main
()
{}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录