Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
9908711e
R
Rust
项目概览
int
/
Rust
11 个月 前同步成功
通知
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,发现更多精彩内容 >>
提交
9908711e
编写于
10月 27, 2016
作者:
E
Eduard Burtescu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Implement field shorthands in struct literal expressions.
上级
a5b6a9fa
变更
15
隐藏空白更改
内联
并排
Showing
15 changed file
with
179 addition
and
18 deletion
+179
-18
src/librustc/hir/lowering.rs
src/librustc/hir/lowering.rs
+2
-0
src/librustc/hir/mod.rs
src/librustc/hir/mod.rs
+1
-0
src/librustc/hir/print.rs
src/librustc/hir/print.rs
+4
-2
src/libsyntax/ast.rs
src/libsyntax/ast.rs
+1
-0
src/libsyntax/ext/build.rs
src/libsyntax/ext/build.rs
+1
-1
src/libsyntax/feature_gate.rs
src/libsyntax/feature_gate.rs
+11
-0
src/libsyntax/fold.rs
src/libsyntax/fold.rs
+5
-4
src/libsyntax/parse/parser.rs
src/libsyntax/parse/parser.rs
+21
-8
src/libsyntax/print/pprust.rs
src/libsyntax/print/pprust.rs
+4
-2
src/test/compile-fail/feature-gate-field-init-shorthand.rs
src/test/compile-fail/feature-gate-field-init-shorthand.rs
+24
-0
src/test/compile-fail/struct-fields-shorthand-unresolved.rs
src/test/compile-fail/struct-fields-shorthand-unresolved.rs
+24
-0
src/test/compile-fail/struct-fields-shorthand.rs
src/test/compile-fail/struct-fields-shorthand.rs
+24
-0
src/test/parse-fail/removed-syntax-with-2.rs
src/test/parse-fail/removed-syntax-with-2.rs
+1
-1
src/test/parse-fail/struct-field-numeric-shorthand.rs
src/test/parse-fail/struct-field-numeric-shorthand.rs
+19
-0
src/test/run-pass/struct-field-shorthand.rs
src/test/run-pass/struct-field-shorthand.rs
+37
-0
未找到文件。
src/librustc/hir/lowering.rs
浏览文件 @
9908711e
...
...
@@ -543,6 +543,7 @@ fn lower_field(&mut self, f: &Field) -> hir::Field {
name
:
respan
(
f
.ident.span
,
f
.ident.node.name
),
expr
:
self
.lower_expr
(
&
f
.expr
),
span
:
f
.span
,
is_shorthand
:
f
.is_shorthand
,
}
}
...
...
@@ -1682,6 +1683,7 @@ fn field(&mut self, name: Name, expr: P<hir::Expr>, span: Span) -> hir::Field {
},
span
:
span
,
expr
:
expr
,
is_shorthand
:
false
,
}
}
...
...
src/librustc/hir/mod.rs
浏览文件 @
9908711e
...
...
@@ -817,6 +817,7 @@ pub struct Field {
pub
name
:
Spanned
<
Name
>
,
pub
expr
:
P
<
Expr
>
,
pub
span
:
Span
,
pub
is_shorthand
:
bool
,
}
#[derive(Clone,
PartialEq,
Eq,
RustcEncodable,
RustcDecodable,
Hash,
Debug,
Copy)]
...
...
src/librustc/hir/print.rs
浏览文件 @
9908711e
...
...
@@ -1229,8 +1229,10 @@ fn print_expr_struct(&mut self,
&
fields
[
..
],
|
s
,
field
|
{
s
.ibox
(
indent_unit
)
?
;
s
.print_name
(
field
.name.node
)
?
;
s
.word_space
(
":"
)
?
;
if
!
field
.is_shorthand
{
s
.print_name
(
field
.name.node
)
?
;
s
.word_space
(
":"
)
?
;
}
s
.print_expr
(
&
field
.expr
)
?
;
s
.end
()
},
...
...
src/libsyntax/ast.rs
浏览文件 @
9908711e
...
...
@@ -900,6 +900,7 @@ pub struct Field {
pub
ident
:
SpannedIdent
,
pub
expr
:
P
<
Expr
>
,
pub
span
:
Span
,
pub
is_shorthand
:
bool
,
}
pub
type
SpannedIdent
=
Spanned
<
Ident
>
;
...
...
src/libsyntax/ext/build.rs
浏览文件 @
9908711e
...
...
@@ -713,7 +713,7 @@ fn expr_block(&self, b: P<ast::Block>) -> P<ast::Expr> {
self
.expr
(
b
.span
,
ast
::
ExprKind
::
Block
(
b
))
}
fn
field_imm
(
&
self
,
span
:
Span
,
name
:
Ident
,
e
:
P
<
ast
::
Expr
>
)
->
ast
::
Field
{
ast
::
Field
{
ident
:
respan
(
span
,
name
),
expr
:
e
,
span
:
span
}
ast
::
Field
{
ident
:
respan
(
span
,
name
),
expr
:
e
,
span
:
span
,
is_shorthand
:
false
}
}
fn
expr_struct
(
&
self
,
span
:
Span
,
path
:
ast
::
Path
,
fields
:
Vec
<
ast
::
Field
>
)
->
P
<
ast
::
Expr
>
{
self
.expr
(
span
,
ast
::
ExprKind
::
Struct
(
path
,
fields
,
None
))
...
...
src/libsyntax/feature_gate.rs
浏览文件 @
9908711e
...
...
@@ -306,6 +306,9 @@ pub fn new() -> Features {
// Allows attributes on lifetime/type formal parameters in generics (RFC 1327)
(
active
,
generic_param_attrs
,
"1.11.0"
,
Some
(
34761
)),
// Allows field shorthands (`x` meaning `x: x`) in struct literal expressions.
(
active
,
field_init_shorthand
,
"1.14.0"
,
Some
(
37340
)),
);
declare_features!
(
...
...
@@ -1087,6 +1090,14 @@ fn visit_expr(&mut self, e: &ast::Expr) {
ast
::
ExprKind
::
InPlace
(
..
)
=>
{
gate_feature_post!
(
&
self
,
placement_in_syntax
,
e
.span
,
EXPLAIN_PLACEMENT_IN
);
}
ast
::
ExprKind
::
Struct
(
_
,
ref
fields
,
_
)
=>
{
for
field
in
fields
{
if
field
.is_shorthand
{
gate_feature_post!
(
&
self
,
field_init_shorthand
,
field
.span
,
"struct field shorthands are unstable"
);
}
}
}
_
=>
{}
}
visit
::
walk_expr
(
self
,
e
);
...
...
src/libsyntax/fold.rs
浏览文件 @
9908711e
...
...
@@ -823,11 +823,12 @@ pub fn noop_fold_struct_field<T: Folder>(f: StructField, fld: &mut T) -> StructF
}
}
pub
fn
noop_fold_field
<
T
:
Folder
>
(
Field
{
ident
,
expr
,
span
}
:
Field
,
folder
:
&
mut
T
)
->
Field
{
pub
fn
noop_fold_field
<
T
:
Folder
>
(
f
:
Field
,
folder
:
&
mut
T
)
->
Field
{
Field
{
ident
:
respan
(
ident
.span
,
folder
.fold_ident
(
ident
.node
)),
expr
:
folder
.fold_expr
(
expr
),
span
:
folder
.new_span
(
span
)
ident
:
respan
(
f
.ident.span
,
folder
.fold_ident
(
f
.ident.node
)),
expr
:
folder
.fold_expr
(
f
.expr
),
span
:
folder
.new_span
(
f
.span
),
is_shorthand
:
f
.is_shorthand
,
}
}
...
...
src/libsyntax/parse/parser.rs
浏览文件 @
9908711e
...
...
@@ -2007,17 +2007,30 @@ pub fn parse_field_name(&mut self) -> PResult<'a, Ident> {
}
}
/// Parse ident
COLON expr
/// Parse ident
(COLON expr)?
pub
fn
parse_field
(
&
mut
self
)
->
PResult
<
'a
,
Field
>
{
let
lo
=
self
.span.lo
;
let
i
=
self
.parse_field_name
()
?
;
let
hi
=
self
.prev_span.hi
;
self
.expect
(
&
token
::
Colon
)
?
;
let
e
=
self
.parse_expr
()
?
;
let
hi
;
// Check if a colon exists one ahead. This means we're parsing a fieldname.
let
(
fieldname
,
expr
,
is_shorthand
)
=
if
self
.look_ahead
(
1
,
|
t
|
t
==
&
token
::
Colon
)
{
let
fieldname
=
self
.parse_field_name
()
?
;
self
.bump
();
hi
=
self
.prev_span.hi
;
(
fieldname
,
self
.parse_expr
()
?
,
false
)
}
else
{
let
fieldname
=
self
.parse_ident
()
?
;
hi
=
self
.prev_span.hi
;
// Mimic `x: x` for the `x` field shorthand.
let
path
=
ast
::
Path
::
from_ident
(
mk_sp
(
lo
,
hi
),
fieldname
);
(
fieldname
,
self
.mk_expr
(
lo
,
hi
,
ExprKind
::
Path
(
None
,
path
),
ThinVec
::
new
()),
true
)
};
Ok
(
ast
::
Field
{
ident
:
spanned
(
lo
,
hi
,
i
),
span
:
mk_sp
(
lo
,
e
.span.hi
),
expr
:
e
,
ident
:
spanned
(
lo
,
hi
,
fieldname
),
span
:
mk_sp
(
lo
,
expr
.span.hi
),
expr
:
expr
,
is_shorthand
:
is_shorthand
,
})
}
...
...
src/libsyntax/print/pprust.rs
浏览文件 @
9908711e
...
...
@@ -1893,8 +1893,10 @@ fn print_expr_struct(&mut self,
&
fields
[
..
],
|
s
,
field
|
{
try!
(
s
.ibox
(
INDENT_UNIT
));
try!
(
s
.print_ident
(
field
.ident.node
));
try!
(
s
.word_space
(
":"
));
if
!
field
.is_shorthand
{
try!
(
s
.print_ident
(
field
.ident.node
));
try!
(
s
.word_space
(
":"
));
}
try!
(
s
.print_expr
(
&
field
.expr
));
s
.end
()
},
...
...
src/test/compile-fail/feature-gate-field-init-shorthand.rs
0 → 100644
浏览文件 @
9908711e
// Copyright 2016 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.
struct
Foo
{
x
:
i32
,
y
:
bool
,
z
:
i32
}
fn
main
()
{
let
(
x
,
y
,
z
)
=
(
1
,
true
,
2
);
let
_
=
Foo
{
x
,
//~ ERROR struct field shorthands are unstable
y
:
y
,
z
//~ ERROR struct field shorthands are unstable
};
}
src/test/compile-fail/struct-fields-shorthand-unresolved.rs
0 → 100644
浏览文件 @
9908711e
// Copyright 2014 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.
#![feature(field_init_shorthand)]
struct
Foo
{
x
:
i32
,
y
:
i32
}
fn
main
()
{
let
x
=
0
;
let
foo
=
Foo
{
x
,
y
//~ ERROR unresolved name `y`
};
}
src/test/compile-fail/struct-fields-shorthand.rs
0 → 100644
浏览文件 @
9908711e
// Copyright 2014 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.
#![feature(field_init_shorthand)]
struct
Foo
{
x
:
i32
,
y
:
i32
}
fn
main
()
{
let
(
x
,
y
,
z
)
=
(
0
,
1
,
2
);
let
foo
=
Foo
{
x
,
y
,
z
//~ ERROR struct `Foo` has no field named `z`
};
}
src/test/parse-fail/removed-syntax-with-2.rs
浏览文件 @
9908711e
...
...
@@ -18,5 +18,5 @@ struct S {
let
a
=
S
{
foo
:
(),
bar
:
()
};
let
b
=
S
{
foo
:
(),
with
a
};
//~^ ERROR expected
`:
`, found `a`
//~^ ERROR expected
one of `,` or `}
`, found `a`
}
src/test/parse-fail/struct-field-numeric-shorthand.rs
0 → 100644
浏览文件 @
9908711e
// Copyright 2016 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
#![feature(field_init_shorthand)]
struct
Rgb
(
u8
,
u8
,
u8
);
fn
main
()
{
let
_
=
Rgb
{
0
,
1
,
2
};
//~ ERROR expected identifier, found `0`
}
src/test/run-pass/struct-field-shorthand.rs
0 → 100644
浏览文件 @
9908711e
// Copyright 2014 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.
#![feature(field_init_shorthand)]
struct
Foo
{
x
:
i32
,
y
:
bool
,
z
:
i32
}
struct
Bar
{
x
:
i32
}
pub
fn
main
()
{
let
(
x
,
y
,
z
)
=
(
1
,
true
,
2
);
let
a
=
Foo
{
x
,
y
:
y
,
z
};
assert_eq!
(
a
.x
,
x
);
assert_eq!
(
a
.y
,
y
);
assert_eq!
(
a
.z
,
z
);
let
b
=
Bar
{
x
,
};
assert_eq!
(
b
.x
,
x
);
let
c
=
Foo
{
z
,
y
,
x
};
assert_eq!
(
c
.x
,
x
);
assert_eq!
(
c
.y
,
y
);
assert_eq!
(
c
.z
,
z
);
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录