Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
a014323e
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,发现更多精彩内容 >>
提交
a014323e
编写于
8月 09, 2016
作者:
V
Vadim Petrochenkov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Lower unions from AST to HIR and from HIR to types
Parse union items and add a feature for them
上级
cbd912ba
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
69 addition
and
6 deletion
+69
-6
src/librustc/hir/lowering.rs
src/librustc/hir/lowering.rs
+4
-1
src/librustc/ty/context.rs
src/librustc/ty/context.rs
+5
-0
src/librustc/ty/mod.rs
src/librustc/ty/mod.rs
+7
-3
src/librustc_privacy/lib.rs
src/librustc_privacy/lib.rs
+1
-1
src/librustc_typeck/check/dropck.rs
src/librustc_typeck/check/dropck.rs
+2
-1
src/librustc_typeck/collect.rs
src/librustc_typeck/collect.rs
+10
-0
src/libsyntax/feature_gate.rs
src/libsyntax/feature_gate.rs
+9
-0
src/libsyntax/parse/parser.rs
src/libsyntax/parse/parser.rs
+31
-0
未找到文件。
src/librustc/hir/lowering.rs
浏览文件 @
a014323e
...
...
@@ -638,7 +638,10 @@ fn lower_item_kind(&mut self, i: &ItemKind) -> hir::Item_ {
let
struct_def
=
self
.lower_variant_data
(
struct_def
);
hir
::
ItemStruct
(
struct_def
,
self
.lower_generics
(
generics
))
}
ItemKind
::
Union
(
..
)
=>
panic!
(
"`union` is not yet implemented"
),
ItemKind
::
Union
(
ref
vdata
,
ref
generics
)
=>
{
let
vdata
=
self
.lower_variant_data
(
vdata
);
hir
::
ItemUnion
(
vdata
,
self
.lower_generics
(
generics
))
}
ItemKind
::
DefaultImpl
(
unsafety
,
ref
trait_ref
)
=>
{
hir
::
ItemDefaultImpl
(
self
.lower_unsafety
(
unsafety
),
self
.lower_trait_ref
(
trait_ref
))
...
...
src/librustc/ty/context.rs
浏览文件 @
a014323e
...
...
@@ -1321,6 +1321,11 @@ pub fn mk_struct(self, def: AdtDef<'tcx>, substs: &'tcx Substs<'tcx>) -> Ty<'tcx
self
.mk_ty
(
TyStruct
(
def
,
substs
))
}
pub
fn
mk_union
(
self
,
def
:
AdtDef
<
'tcx
>
,
substs
:
&
'tcx
Substs
<
'tcx
>
)
->
Ty
<
'tcx
>
{
// take a copy of substs so that we own the vectors inside
self
.mk_ty
(
TyUnion
(
def
,
substs
))
}
pub
fn
mk_closure
(
self
,
closure_id
:
DefId
,
substs
:
&
'tcx
Substs
<
'tcx
>
,
...
...
src/librustc/ty/mod.rs
浏览文件 @
a014323e
...
...
@@ -1514,7 +1514,7 @@ fn decode<D: Decoder>(d: &mut D) -> Result<AdtDef<'tcx>, D::Error> {
#[derive(Copy,
Clone,
Debug,
Eq,
PartialEq)]
pub
enum
AdtKind
{
Struct
,
Enum
}
pub
enum
AdtKind
{
Struct
,
Union
,
Enum
}
#[derive(Clone,
Copy,
PartialEq,
Eq,
Hash,
Debug,
RustcEncodable,
RustcDecodable)]
pub
enum
VariantKind
{
Struct
,
Tuple
,
Unit
}
...
...
@@ -1545,8 +1545,10 @@ fn new(tcx: TyCtxt<'a, 'gcx, 'tcx>,
if
Some
(
did
)
==
tcx
.lang_items
.phantom_data
()
{
flags
=
flags
|
AdtFlags
::
IS_PHANTOM_DATA
;
}
if
let
AdtKind
::
Enum
=
kind
{
flags
=
flags
|
AdtFlags
::
IS_ENUM
;
match
kind
{
AdtKind
::
Enum
=>
flags
=
flags
|
AdtFlags
::
IS_ENUM
,
AdtKind
::
Union
=>
flags
=
flags
|
AdtFlags
::
IS_UNION
,
AdtKind
::
Struct
=>
{}
}
AdtDefData
{
did
:
did
,
...
...
@@ -1569,6 +1571,8 @@ fn calculate_dtorck(&'gcx self, tcx: TyCtxt) {
pub
fn
adt_kind
(
&
self
)
->
AdtKind
{
if
self
.flags
.get
()
.intersects
(
AdtFlags
::
IS_ENUM
)
{
AdtKind
::
Enum
}
else
if
self
.flags
.get
()
.intersects
(
AdtFlags
::
IS_UNION
)
{
AdtKind
::
Union
}
else
{
AdtKind
::
Struct
}
...
...
src/librustc_privacy/lib.rs
浏览文件 @
a014323e
...
...
@@ -383,7 +383,7 @@ fn item_is_accessible(&self, did: DefId) -> bool {
// Checks that a field is in scope.
fn
check_field
(
&
mut
self
,
span
:
Span
,
def
:
ty
::
AdtDef
<
'tcx
>
,
field
:
ty
::
FieldDef
<
'tcx
>
)
{
if
def
.adt_kind
()
==
ty
::
AdtKind
::
Struct
&&
if
def
.adt_kind
()
!=
ty
::
AdtKind
::
Enum
&&
!
field
.vis
.is_accessible_from
(
self
.curitem
,
&
self
.tcx.map
)
{
struct_span_err!
(
self
.tcx.sess
,
span
,
E0451
,
"field `{}` of struct `{}` is private"
,
field
.name
,
self
.tcx
.item_path_str
(
def
.did
))
...
...
src/librustc_typeck/check/dropck.rs
浏览文件 @
a014323e
...
...
@@ -304,7 +304,8 @@ pub fn check_safety_of_destructor_if_necessary<'a, 'gcx, 'tcx>(
tcx
.item_path_str
(
def_id
),
variant
),
ty
::
AdtKind
::
Struct
=>
format!
(
"struct {}"
,
tcx
.item_path_str
(
def_id
))
tcx
.item_path_str
(
def_id
)),
ty
::
AdtKind
::
Union
=>
unimplemented_unions!
(),
};
span_note!
(
&
mut
err
,
...
...
src/librustc_typeck/collect.rs
浏览文件 @
a014323e
...
...
@@ -1069,6 +1069,16 @@ fn convert_struct_def<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
adt
}
fn
convert_union_def
<
'a
,
'tcx
>
(
ccx
:
&
CrateCtxt
<
'a
,
'tcx
>
,
it
:
&
hir
::
Item
,
def
:
&
hir
::
VariantData
)
->
ty
::
AdtDefMaster
<
'tcx
>
{
let
did
=
ccx
.tcx.map
.local_def_id
(
it
.id
);
let
variants
=
vec!
[
convert_struct_variant
(
ccx
,
did
,
it
.name
,
ConstInt
::
Infer
(
0
),
def
)];
ccx
.tcx
.intern_adt_def
(
did
,
ty
::
AdtKind
::
Union
,
variants
)
}
fn
evaluate_disr_expr
(
ccx
:
&
CrateCtxt
,
repr_ty
:
attr
::
IntType
,
e
:
&
hir
::
Expr
)
->
Option
<
ty
::
Disr
>
{
debug!
(
"disr expr, checking {}"
,
pprust
::
expr_to_string
(
e
));
...
...
src/libsyntax/feature_gate.rs
浏览文件 @
a014323e
...
...
@@ -292,6 +292,9 @@ pub fn new() -> Features {
// Macros 1.1
(
active
,
rustc_macro
,
"1.13.0"
,
Some
(
35900
)),
// Allows untagged unions `union U { ... }`
(
active
,
untagged_unions
,
"1.13.0"
,
Some
(
32836
)),
);
declare_features!
(
...
...
@@ -953,6 +956,12 @@ fn visit_item(&mut self, i: &ast::Item) {
}
}
ast
::
ItemKind
::
Union
(
..
)
=>
{
gate_feature_post!
(
&
self
,
untagged_unions
,
i
.span
,
"unions are unstable and not fully implemented"
);
}
ast
::
ItemKind
::
DefaultImpl
(
..
)
=>
{
gate_feature_post!
(
&
self
,
optin_builtin_traits
,
i
.span
,
...
...
src/libsyntax/parse/parser.rs
浏览文件 @
a014323e
...
...
@@ -5102,6 +5102,25 @@ fn parse_item_struct(&mut self) -> PResult<'a, ItemInfo> {
Ok
((
class_name
,
ItemKind
::
Struct
(
vdata
,
generics
),
None
))
}
/// Parse union Foo { ... }
fn
parse_item_union
(
&
mut
self
)
->
PResult
<
'a
,
ItemInfo
>
{
let
class_name
=
self
.parse_ident
()
?
;
let
mut
generics
=
self
.parse_generics
()
?
;
let
vdata
=
if
self
.token
.is_keyword
(
keywords
::
Where
)
{
generics
.where_clause
=
self
.parse_where_clause
()
?
;
VariantData
::
Struct
(
self
.parse_record_struct_body
()
?
,
ast
::
DUMMY_NODE_ID
)
}
else
if
self
.token
==
token
::
OpenDelim
(
token
::
Brace
)
{
VariantData
::
Struct
(
self
.parse_record_struct_body
()
?
,
ast
::
DUMMY_NODE_ID
)
}
else
{
let
token_str
=
self
.this_token_to_string
();
return
Err
(
self
.fatal
(
&
format!
(
"expected `where` or `{{` after union
\
name, found `{}`"
,
token_str
)))
};
Ok
((
class_name
,
ItemKind
::
Union
(
vdata
,
generics
),
None
))
}
pub
fn
parse_record_struct_body
(
&
mut
self
)
->
PResult
<
'a
,
Vec
<
StructField
>>
{
let
mut
fields
=
Vec
::
new
();
if
self
.eat
(
&
token
::
OpenDelim
(
token
::
Brace
))
{
...
...
@@ -5938,6 +5957,18 @@ fn parse_item_(&mut self, attrs: Vec<Attribute>,
maybe_append
(
attrs
,
extra_attrs
));
return
Ok
(
Some
(
item
));
}
if
self
.eat_keyword
(
keywords
::
Union
)
{
// UNION ITEM
let
(
ident
,
item_
,
extra_attrs
)
=
self
.parse_item_union
()
?
;
let
last_span
=
self
.last_span
;
let
item
=
self
.mk_item
(
lo
,
last_span
.hi
,
ident
,
item_
,
visibility
,
maybe_append
(
attrs
,
extra_attrs
));
return
Ok
(
Some
(
item
));
}
self
.parse_macro_use_or_failure
(
attrs
,
macros_allowed
,
attributes_allowed
,
lo
,
visibility
)
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录