Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
0930b953
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,发现更多精彩内容 >>
提交
0930b953
编写于
7月 24, 2012
作者:
P
Patrick Walton
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
rustc: Typecheck struct literals
上级
bf96298f
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
134 addition
and
3 deletion
+134
-3
src/rustc/middle/typeck/check.rs
src/rustc/middle/typeck/check.rs
+134
-3
未找到文件。
src/rustc/middle/typeck/check.rs
浏览文件 @
0930b953
...
...
@@ -66,7 +66,8 @@
*/
import
astconv
::{
ast_conv
,
ast_ty_to_ty
,
ast_region_to_region
};
import
astconv
::{
ast_conv
,
ast_path_to_ty
,
ast_ty_to_ty
};
import
astconv
::{
ast_region_to_region
};
import
collect
::{
methods
};
// ccx.to_ty()
import
middle
::
ty
::{
tv_vid
,
vid
};
import
regionmanip
::{
replace_bound_regions_in_fn_ty
,
region_of
};
...
...
@@ -76,6 +77,8 @@
import
typeck
::
infer
::{
unify_methods
};
// infcx.set()
import
typeck
::
infer
::{
resolve_type
,
force_tvar
};
import
std
::
map
::
str_hash
;
type
fn_ctxt_
=
// var_bindings, locals and next_var_id are shared
// with any nested functions that capture the environment
...
...
@@ -1628,8 +1631,136 @@ fn get_node(f: spanned<field>) -> field { f.node }
}
}
}
ast
::
expr_struct
(
*
)
{
fail
~
"XXX structs"
;
ast
::
expr_struct
(
path
,
fields
)
{
// Resolve the path.
let
class_id
;
alt
tcx
.def_map
.find
(
id
)
{
some
(
ast
::
def_class
(
type_def_id
))
=>
{
class_id
=
type_def_id
;
}
_
=>
{
tcx
.sess
.span_bug
(
path
.span
,
~
"structure constructor does
\
not name a structure type"
);
}
}
// Look up the number of type parameters and the raw type, and
// determine whether the class is region-parameterized.
let
type_parameter_count
,
region_parameterized
,
raw_type
;
if
class_id
.crate
==
ast
::
local_crate
{
region_parameterized
=
tcx
.region_paramd_items
.contains_key
(
class_id
.node
);
alt
tcx
.items
.find
(
class_id
.node
)
{
some
(
ast_map
::
node_item
(
@
{
node
:
ast
::
item_class
(
type_parameters
,
_
,
_
,
_
,
_
),
_
},
_
))
=>
{
type_parameter_count
=
type_parameters
.len
();
let
self_region
;
if
region_parameterized
{
self_region
=
some
(
ty
::
re_bound
(
ty
::
br_self
));
}
else
{
self_region
=
none
;
}
raw_type
=
ty
::
mk_class
(
tcx
,
class_id
,
{
self_r
:
self_region
,
self_ty
:
none
,
tps
:
ty
::
ty_params_to_tys
(
tcx
,
type_parameters
)
});
}
_
=>
{
tcx
.sess
.span_bug
(
expr
.span
,
~
"resolve didn't map this to a class"
);
}
}
}
else
{
let
item_type
=
ty
::
lookup_item_type
(
tcx
,
class_id
);
type_parameter_count
=
(
*
item_type
.bounds
)
.len
();
region_parameterized
=
item_type
.rp
;
raw_type
=
item_type
.ty
;
}
// Generate the struct type.
let
self_region
;
if
region_parameterized
{
self_region
=
some
(
fcx
.infcx
.next_region_var_nb
());
}
else
{
self_region
=
none
;
}
let
type_parameters
=
fcx
.infcx
.next_ty_vars
(
type_parameter_count
);
let
substitutions
=
{
self_r
:
self_region
,
self_ty
:
none
,
tps
:
type_parameters
};
let
struct_type
=
ty
::
subst
(
tcx
,
substitutions
,
raw_type
);
// Look up the class fields and build up a map.
let
class_fields
=
ty
::
lookup_class_fields
(
tcx
,
class_id
);
let
class_field_map
=
str_hash
();
let
mut
fields_found
=
0
;
for
class_fields
.each
|
field
|
{
// XXX: Check visibility here.
class_field_map
.insert
(
*
field
.ident
,
(
field
.id
,
false
));
}
// Typecheck each field.
for
fields
.each
|
field
|
{
alt
class_field_map
.find
(
*
field
.node.ident
)
{
none
=>
{
tcx
.sess
.span_err
(
field
.span
,
#
fmt
(
"structure has no field named
\
field named `%s`"
,
*
field
.node.ident
));
}
some
((
_
,
true
))
=>
{
tcx
.sess
.span_err
(
field
.span
,
#
fmt
(
"field `%s` specified more than
\
once"
,
*
field
.node.ident
));
}
some
((
field_id
,
false
))
=>
{
let
expected_field_type
=
ty
::
lookup_field_type
(
tcx
,
class_id
,
field_id
,
substitutions
);
bot
|
=
check_expr
(
fcx
,
field
.node.expr
,
some
(
expected_field_type
));
fields_found
+=
1
;
}
}
}
// Make sure the programmer specified all the fields.
assert
fields_found
<=
class_fields
.len
();
if
fields_found
<
class_fields
.len
()
{
let
mut
missing_fields
=
~
[];
for
class_fields
.each
|
class_field
|
{
let
name
=
*
class_field
.ident
;
let
(
_
,
seen
)
=
class_field_map
.get
(
name
);
if
!
seen
{
vec
::
push
(
missing_fields
,
~
"`"
+
name
+
~
"`"
);
}
}
tcx
.sess
.span_err
(
expr
.span
,
#
fmt
(
"missing field%s: %s"
,
if
missing_fields
.len
()
==
1
{
~
""
}
else
{
~
"s"
},
str
::
connect
(
missing_fields
,
~
", "
)));
}
// Write in the resulting type.
fcx
.write_ty
(
id
,
struct_type
);
}
ast
::
expr_field
(
base
,
field
,
tys
)
{
bot
=
check_field
(
fcx
,
expr
,
false
,
base
,
field
,
tys
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录