Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
5fa6e857
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,发现更多精彩内容 >>
提交
5fa6e857
编写于
9月 10, 2015
作者:
V
Vadim Petrochenkov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Implement empty struct with braces (RFC 218)
上级
cff04117
变更
10
隐藏空白更改
内联
并排
Showing
10 changed file
with
114 addition
and
113 deletion
+114
-113
src/librustc_front/print/pprust.rs
src/librustc_front/print/pprust.rs
+25
-25
src/librustc_typeck/check/mod.rs
src/librustc_typeck/check/mod.rs
+2
-2
src/libsyntax/parse/parser.rs
src/libsyntax/parse/parser.rs
+3
-18
src/libsyntax/print/pprust.rs
src/libsyntax/print/pprust.rs
+25
-25
src/test/compile-fail/empty-struct-with-braces-1.rs
src/test/compile-fail/empty-struct-with-braces-1.rs
+5
-8
src/test/compile-fail/empty-struct-with-braces-2.rs
src/test/compile-fail/empty-struct-with-braces-2.rs
+12
-8
src/test/compile-fail/issue-27831.rs
src/test/compile-fail/issue-27831.rs
+2
-2
src/test/parse-fail/struct-no-fields-5.rs
src/test/parse-fail/struct-no-fields-5.rs
+0
-20
src/test/run-pass/empty-struct-with-braces.rs
src/test/run-pass/empty-struct-with-braces.rs
+31
-0
src/test/run-pass/issue-16819.rs
src/test/run-pass/issue-16819.rs
+9
-5
未找到文件。
src/librustc_front/print/pprust.rs
浏览文件 @
5fa6e857
...
...
@@ -1218,34 +1218,34 @@ fn print_expr_struct(&mut self,
fields
:
&
[
hir
::
Field
],
wth
:
&
Option
<
P
<
hir
::
Expr
>>
)
->
io
::
Result
<
()
>
{
try!
(
self
.print_path
(
path
,
true
,
0
));
if
!
(
fields
.is_empty
()
&&
wth
.is_none
())
{
try!
(
word
(
&
mut
self
.s
,
"{"
));
try!
(
self
.commasep_cmnt
(
Consistent
,
&
fields
[
..
],
|
s
,
field
|
{
try!
(
s
.ibox
(
indent_unit
));
try!
(
s
.print_ident
(
field
.ident.node
));
try!
(
s
.word_space
(
":"
));
try!
(
s
.print_expr
(
&*
field
.expr
));
s
.end
()
},
|
f
|
f
.span
));
match
*
wth
{
Some
(
ref
expr
)
=>
{
try!
(
self
.ibox
(
indent_unit
));
if
!
fields
.is_empty
()
{
try!
(
word
(
&
mut
self
.s
,
","
));
try!
(
space
(
&
mut
self
.s
));
}
try!
(
word
(
&
mut
self
.s
,
".."
));
try!
(
self
.print_expr
(
&**
expr
));
try!
(
self
.end
());
try!
(
word
(
&
mut
self
.s
,
"{"
));
try!
(
self
.commasep_cmnt
(
Consistent
,
&
fields
[
..
],
|
s
,
field
|
{
try!
(
s
.ibox
(
indent_unit
));
try!
(
s
.print_ident
(
field
.ident.node
));
try!
(
s
.word_space
(
":"
));
try!
(
s
.print_expr
(
&*
field
.expr
));
s
.end
()
},
|
f
|
f
.span
));
match
*
wth
{
Some
(
ref
expr
)
=>
{
try!
(
self
.ibox
(
indent_unit
));
if
!
fields
.is_empty
()
{
try!
(
word
(
&
mut
self
.s
,
","
));
try!
(
space
(
&
mut
self
.s
));
}
_
=>
try!
(
word
(
&
mut
self
.s
,
","
)),
try!
(
word
(
&
mut
self
.s
,
".."
));
try!
(
self
.print_expr
(
&**
expr
));
try!
(
self
.end
());
}
_
=>
if
!
fields
.is_empty
()
{
try!
(
word
(
&
mut
self
.s
,
","
))
}
try!
(
word
(
&
mut
self
.s
,
"}"
));
}
try!
(
word
(
&
mut
self
.s
,
"}"
));
Ok
(())
}
...
...
src/librustc_typeck/check/mod.rs
浏览文件 @
5fa6e857
...
...
@@ -1473,14 +1473,14 @@ pub fn def_struct_variant(&self,
_
=>
return
None
};
if
let
ty
::
VariantKind
::
Dict
=
variant
.kind
()
{
let
var_kind
=
variant
.kind
();
if
var_kind
==
ty
::
VariantKind
::
Dict
||
var_kind
==
ty
::
VariantKind
::
Unit
{
Some
((
adt
,
variant
))
}
else
{
None
}
}
pub
fn
write_nil
(
&
self
,
node_id
:
ast
::
NodeId
)
{
self
.write_ty
(
node_id
,
self
.tcx
()
.mk_nil
());
}
...
...
src/libsyntax/parse/parser.rs
浏览文件 @
5fa6e857
...
...
@@ -2231,14 +2231,6 @@ pub fn parse_bottom_expr(&mut self) -> PResult<P<Expr>> {
&
[
token
::
CloseDelim
(
token
::
Brace
)]));
}
if
fields
.is_empty
()
&&
base
.is_none
()
{
let
last_span
=
self
.last_span
;
self
.span_err
(
last_span
,
"structure literal must either
\
have at least one field or use
\
structure update syntax"
);
}
hi
=
self
.span.hi
;
try!
(
self
.expect
(
&
token
::
CloseDelim
(
token
::
Brace
)));
ex
=
ExprStruct
(
pth
,
fields
,
base
);
...
...
@@ -4713,14 +4705,14 @@ fn parse_item_struct(&mut self) -> PResult<ItemInfo> {
(
Vec
::
new
(),
Some
(
ast
::
DUMMY_NODE_ID
))
}
else
{
// If we see: `struct Foo<T> where T: Copy { ... }`
(
try!
(
self
.parse_record_struct_body
(
&
class_name
)),
None
)
(
try!
(
self
.parse_record_struct_body
()),
None
)
}
// No `where` so: `struct Foo<T>;`
}
else
if
try!
(
self
.eat
(
&
token
::
Semi
)
){
(
Vec
::
new
(),
Some
(
ast
::
DUMMY_NODE_ID
))
// Record-style struct definition
}
else
if
self
.token
==
token
::
OpenDelim
(
token
::
Brace
)
{
let
fields
=
try!
(
self
.parse_record_struct_body
(
&
class_name
));
let
fields
=
try!
(
self
.parse_record_struct_body
());
(
fields
,
None
)
// Tuple-style struct definition with optional where-clause.
}
else
if
self
.token
==
token
::
OpenDelim
(
token
::
Paren
)
{
...
...
@@ -4740,20 +4732,13 @@ fn parse_item_struct(&mut self) -> PResult<ItemInfo> {
None
))
}
pub
fn
parse_record_struct_body
(
&
mut
self
,
class_name
:
&
ast
::
Ident
)
->
PResult
<
Vec
<
StructField
>>
{
pub
fn
parse_record_struct_body
(
&
mut
self
)
->
PResult
<
Vec
<
StructField
>>
{
let
mut
fields
=
Vec
::
new
();
if
try!
(
self
.eat
(
&
token
::
OpenDelim
(
token
::
Brace
))
){
while
self
.token
!=
token
::
CloseDelim
(
token
::
Brace
)
{
fields
.push
(
try!
(
self
.parse_struct_decl_field
(
true
)));
}
if
fields
.is_empty
()
{
return
Err
(
self
.fatal
(
&
format!
(
"unit-like struct definition should be
\
written as `struct {};`"
,
class_name
)));
}
try!
(
self
.bump
());
}
else
{
let
token_str
=
self
.this_token_to_string
();
...
...
src/libsyntax/print/pprust.rs
浏览文件 @
5fa6e857
...
...
@@ -1855,34 +1855,34 @@ fn print_expr_struct(&mut self,
fields
:
&
[
ast
::
Field
],
wth
:
&
Option
<
P
<
ast
::
Expr
>>
)
->
io
::
Result
<
()
>
{
try!
(
self
.print_path
(
path
,
true
,
0
));
if
!
(
fields
.is_empty
()
&&
wth
.is_none
())
{
try!
(
word
(
&
mut
self
.s
,
"{"
));
try!
(
self
.commasep_cmnt
(
Consistent
,
&
fields
[
..
],
|
s
,
field
|
{
try!
(
s
.ibox
(
indent_unit
));
try!
(
s
.print_ident
(
field
.ident.node
));
try!
(
s
.word_space
(
":"
));
try!
(
s
.print_expr
(
&*
field
.expr
));
s
.end
()
},
|
f
|
f
.span
));
match
*
wth
{
Some
(
ref
expr
)
=>
{
try!
(
self
.ibox
(
indent_unit
));
if
!
fields
.is_empty
()
{
try!
(
word
(
&
mut
self
.s
,
","
));
try!
(
space
(
&
mut
self
.s
));
}
try!
(
word
(
&
mut
self
.s
,
".."
));
try!
(
self
.print_expr
(
&**
expr
));
try!
(
self
.end
());
try!
(
word
(
&
mut
self
.s
,
"{"
));
try!
(
self
.commasep_cmnt
(
Consistent
,
&
fields
[
..
],
|
s
,
field
|
{
try!
(
s
.ibox
(
indent_unit
));
try!
(
s
.print_ident
(
field
.ident.node
));
try!
(
s
.word_space
(
":"
));
try!
(
s
.print_expr
(
&*
field
.expr
));
s
.end
()
},
|
f
|
f
.span
));
match
*
wth
{
Some
(
ref
expr
)
=>
{
try!
(
self
.ibox
(
indent_unit
));
if
!
fields
.is_empty
()
{
try!
(
word
(
&
mut
self
.s
,
","
));
try!
(
space
(
&
mut
self
.s
));
}
_
=>
try!
(
word
(
&
mut
self
.s
,
","
)),
try!
(
word
(
&
mut
self
.s
,
".."
));
try!
(
self
.print_expr
(
&**
expr
));
try!
(
self
.end
());
}
_
=>
if
!
fields
.is_empty
()
{
try!
(
word
(
&
mut
self
.s
,
","
))
}
try!
(
word
(
&
mut
self
.s
,
"}"
));
}
try!
(
word
(
&
mut
self
.s
,
"}"
));
Ok
(())
}
...
...
src/test/
parse-fail/struct-no-fields-2
.rs
→
src/test/
compile-fail/empty-struct-with-braces-1
.rs
浏览文件 @
5fa6e857
// Copyright 201
3
The Rust Project Developers. See the COPYRIGHT
// Copyright 201
5
The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
...
...
@@ -8,13 +8,10 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//
compile-flags: -Z parse-only
//
Empty struct defined with braces shouldn't add names into value namespace
struct
Foo
;
struct
Empty
{}
fn
f2
()
{
let
_
end_stmt
=
Foo
{
};
//~^ ERROR: structure literal must either have at least one field
fn
main
()
{
let
e
=
Empty
;
//~ ERROR `Empty` is the name of a struct or struct variant
}
fn
main
()
{}
src/test/
parse-fail/struct-no-fields-3
.rs
→
src/test/
compile-fail/empty-struct-with-braces-2
.rs
浏览文件 @
5fa6e857
// Copyright 201
3
The Rust Project Developers. See the COPYRIGHT
// Copyright 201
5
The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
...
...
@@ -8,13 +8,17 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//
compile-flags: -Z parse-only
//
Empty struct defined with braces shouldn't add names into value namespace
struct
Foo
;
#![deny(warnings)]
fn
g3
()
{
let
_
mid_tuple
=
(
Foo
{
},
2
);
//~^ ERROR: structure literal must either have at least one field
}
struct
Empty
{}
fn
main
()
{
let
e
=
Empty
{};
fn
main
()
{}
match
e
{
Empty
=>
()
//~ ERROR unused variable: `Empty`
//~^ ERROR variable `Empty` should have a snake case name such as `empty`
}
}
src/test/compile-fail/issue-27831.rs
浏览文件 @
5fa6e857
...
...
@@ -22,8 +22,8 @@ fn main() {
let
Foo
{
..
}
=
x
;
//~ ERROR `Foo` does not name a struct
let
x
=
Bar
;
Bar
{
..
x
};
//~ ERROR `Bar` does not name a structure
let
Bar
{
..
}
=
x
;
//~ ERROR `Bar` does not name a struct
Bar
{
..
x
};
let
Bar
{
..
}
=
x
;
match
Enum
::
Bar
{
Enum
::
Bar
{
..
}
//~ ERROR `Enum::Bar` does not name a struct
...
...
src/test/parse-fail/struct-no-fields-5.rs
已删除
100644 → 0
浏览文件 @
cff04117
// Copyright 2013 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
struct
Foo
;
fn
i5
()
{
let
_
end_of_block
=
{
Foo
{
}
};
//~^ ERROR: structure literal must either have at least one field
}
fn
main
()
{}
src/test/
parse-fail/struct-no-fields-4
.rs
→
src/test/
run-pass/empty-struct-with-braces
.rs
浏览文件 @
5fa6e857
// Copyright 201
3
The Rust Project Developers. See the COPYRIGHT
// Copyright 201
5
The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
...
...
@@ -8,13 +8,24 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// compile-flags: -Z parse-only
// Empty struct defined with braces add names into type namespace
// Empty struct defined without braces add names into both type and value namespaces
struct
Foo
;
struct
Empty1
{}
struct
Empty2
;
fn
h4
()
{
let
_
end_of_tuple
=
(
3
,
Foo
{
})
;
//~^ ERROR: structure literal must either have at least one field
}
fn
main
()
{
let
e1
:
Empty1
=
Empty1
{}
;
let
e2
:
Empty2
=
Empty2
{};
let
e2
:
Empty2
=
Empty2
;
fn
main
()
{}
match
e1
{
Empty1
{}
=>
()
}
match
e2
{
Empty2
{}
=>
()
}
match
e2
{
Empty2
=>
()
}
}
src/test/
parse-fail/struct-no-fields
.rs
→
src/test/
run-pass/issue-16819
.rs
浏览文件 @
5fa6e857
// Copyright 201
2
The Rust Project Developers. See the COPYRIGHT
// Copyright 201
5
The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
...
...
@@ -8,9 +8,13 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//
compile-flags: -Z parse-only
//
`#[cfg]` on struct field permits empty unusable struct
struct
Foo
{}
//~^ ERROR: unit-like struct definition should be written as `struct Foo;`
struct
S
{
#[cfg(untrue)]
a
:
int
,
}
fn
main
()
{}
fn
main
()
{
let
s
=
S
{};
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录