Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
0a22a4ed
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,发现更多精彩内容 >>
提交
0a22a4ed
编写于
3月 08, 2011
作者:
P
Patrick Walton
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
rustc: Factor out path type instantiation so that tag patterns can use it as well
上级
e64085b0
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
108 addition
and
103 deletion
+108
-103
src/comp/middle/typeck.rs
src/comp/middle/typeck.rs
+108
-103
未找到文件。
src/comp/middle/typeck.rs
浏览文件 @
0a22a4ed
...
...
@@ -128,6 +128,112 @@ fn fold_simple_ty(@ty.t typ) -> @ty.t {
ret
ty
.fold_ty
(
substituter
,
typ
);
}
// Instantiates the given path, which must refer to an item with the given
// definition.
fn
instantiate_path
(
@
fn_ctxt
fcx
,
&
ast
.path
pth
,
&
ast
.def
defn
,
&
span
sp
)
->
ast
.ann
{
auto
t
;
auto
ty_params
;
alt
(
defn
)
{
case
(
ast
.def_arg
(
?
id
))
{
check
(
fcx
.locals
.contains_key
(
id
));
t
=
fcx
.locals
.get
(
id
);
ty_params
=
none
[
vec
[
ast
.def_id
]];
}
case
(
ast
.def_local
(
?
id
))
{
alt
(
fcx
.locals
.find
(
id
))
{
case
(
some
[
@
ty
.t
](
?
t1
))
{
t
=
t1
;
}
case
(
none
[
@
ty
.t
])
{
t
=
plain_ty
(
ty
.ty_local
(
id
));
}
}
ty_params
=
none
[
vec
[
ast
.def_id
]];
}
case
(
ast
.def_obj_field
(
?
id
))
{
check
(
fcx
.locals
.contains_key
(
id
));
t
=
fcx
.locals
.get
(
id
);
ty_params
=
none
[
vec
[
ast
.def_id
]];
}
case
(
ast
.def_fn
(
?
id
))
{
check
(
fcx
.ccx.item_types
.contains_key
(
id
));
t
=
fcx
.ccx.item_types
.get
(
id
);
ty_params
=
some
(
fcx
.ccx.item_ty_params
.get
(
id
));
}
case
(
ast
.def_native_fn
(
?
id
))
{
check
(
fcx
.ccx.item_types
.contains_key
(
id
));
t
=
fcx
.ccx.item_types
.get
(
id
);
ty_params
=
some
(
fcx
.ccx.item_ty_params
.get
(
id
));
}
case
(
ast
.def_const
(
?
id
))
{
check
(
fcx
.ccx.item_types
.contains_key
(
id
));
t
=
fcx
.ccx.item_types
.get
(
id
);
ty_params
=
none
[
vec
[
ast
.def_id
]];
}
case
(
ast
.def_variant
(
?
tag_id
,
?
variant_id
))
{
check
(
fcx
.ccx.item_types
.contains_key
(
variant_id
));
t
=
fcx
.ccx.item_types
.get
(
variant_id
);
ty_params
=
some
(
fcx
.ccx.item_ty_params
.get
(
tag_id
));
}
case
(
ast
.def_binding
(
?
id
))
{
check
(
fcx
.locals
.contains_key
(
id
));
t
=
fcx
.locals
.get
(
id
);
ty_params
=
none
[
vec
[
ast
.def_id
]];
}
case
(
ast
.def_obj
(
?
id
))
{
check
(
fcx
.ccx.item_types
.contains_key
(
id
));
t
=
fcx
.ccx.item_types
.get
(
id
);
ty_params
=
some
(
fcx
.ccx.item_ty_params
.get
(
id
));
}
case
(
ast
.def_mod
(
_
))
{
// Hopefully part of a path.
t
=
plain_ty
(
ty
.ty_nil
);
// TODO: something more poisonous?
ty_params
=
none
[
vec
[
ast
.def_id
]];
}
case
(
_
)
{
// FIXME: handle other names.
fcx
.ccx.sess
.unimpl
(
"definition variant for: "
+
_
str
.connect
(
pth
.node.idents
,
"."
));
fail
;
}
}
auto
ty_substs_opt
;
auto
ty_substs_len
=
_
vec
.len
[
@
ast
.ty
](
pth
.node.types
);
if
(
ty_substs_len
>
0u
)
{
let
vec
[
@
ty
.t
]
ty_substs
=
vec
();
auto
i
=
0u
;
while
(
i
<
ty_substs_len
)
{
ty_substs
+=
vec
(
ast_ty_to_ty_crate
(
fcx
.ccx
,
pth
.node.types
.
(
i
)));
i
+=
1u
;
}
ty_substs_opt
=
some
[
vec
[
@
ty
.t
]](
ty_substs
);
alt
(
ty_params
)
{
case
(
none
[
vec
[
ast
.def_id
]])
{
fcx
.ccx.sess
.span_err
(
sp
,
"this kind of item may not take "
+
"type parameters"
);
fail
;
}
case
(
some
[
vec
[
ast
.def_id
]](
?
tps
))
{
t
=
substitute_ty_params
(
fcx
.ccx
,
t
,
tps
,
ty_substs
,
sp
);
}
}
}
else
{
ty_substs_opt
=
none
[
vec
[
@
ty
.t
]];
alt
(
ty_params
)
{
case
(
none
[
vec
[
ast
.def_id
]])
{
/* nothing */
}
case
(
some
[
vec
[
ast
.def_id
]](
_
))
{
// We will acquire the type parameters through
// unification.
t
=
generalize_ty
(
fcx
.ccx
,
t
);
}
}
}
ret
ast
.ann_type
(
t
,
ty_substs_opt
);
}
// Returns the type parameters and polytype of an item, if it's an item that
// supports type parameters.
fn
ty_params_for_item
(
@
crate_ctxt
ccx
,
&
ast
.def
d
)
...
...
@@ -1561,109 +1667,8 @@ fn check_call_or_bind(&@fn_ctxt fcx, &@ast.expr f,
case
(
ast
.expr_path
(
?
pth
,
?
defopt
,
_
))
{
auto
t
=
plain_ty
(
ty
.ty_nil
);
check
(
defopt
!=
none
[
ast
.def
]);
auto
ty_params
;
alt
(
option
.get
[
ast
.def
](
defopt
))
{
case
(
ast
.def_arg
(
?
id
))
{
check
(
fcx
.locals
.contains_key
(
id
));
t
=
fcx
.locals
.get
(
id
);
ty_params
=
none
[
vec
[
ast
.def_id
]];
}
case
(
ast
.def_local
(
?
id
))
{
alt
(
fcx
.locals
.find
(
id
))
{
case
(
some
[
@
ty
.t
](
?
t1
))
{
t
=
t1
;
}
case
(
none
[
@
ty
.t
])
{
t
=
plain_ty
(
ty
.ty_local
(
id
));
}
}
ty_params
=
none
[
vec
[
ast
.def_id
]];
}
case
(
ast
.def_obj_field
(
?
id
))
{
check
(
fcx
.locals
.contains_key
(
id
));
t
=
fcx
.locals
.get
(
id
);
ty_params
=
none
[
vec
[
ast
.def_id
]];
}
case
(
ast
.def_fn
(
?
id
))
{
check
(
fcx
.ccx.item_types
.contains_key
(
id
));
t
=
fcx
.ccx.item_types
.get
(
id
);
ty_params
=
some
(
fcx
.ccx.item_ty_params
.get
(
id
));
}
case
(
ast
.def_native_fn
(
?
id
))
{
check
(
fcx
.ccx.item_types
.contains_key
(
id
));
t
=
fcx
.ccx.item_types
.get
(
id
);
ty_params
=
some
(
fcx
.ccx.item_ty_params
.get
(
id
));
}
case
(
ast
.def_const
(
?
id
))
{
check
(
fcx
.ccx.item_types
.contains_key
(
id
));
t
=
fcx
.ccx.item_types
.get
(
id
);
ty_params
=
none
[
vec
[
ast
.def_id
]];
}
case
(
ast
.def_variant
(
?
tag_id
,
?
variant_id
))
{
check
(
fcx
.ccx.item_types
.contains_key
(
variant_id
));
t
=
fcx
.ccx.item_types
.get
(
variant_id
);
ty_params
=
some
(
fcx
.ccx.item_ty_params
.get
(
tag_id
));
}
case
(
ast
.def_binding
(
?
id
))
{
check
(
fcx
.locals
.contains_key
(
id
));
t
=
fcx
.locals
.get
(
id
);
ty_params
=
none
[
vec
[
ast
.def_id
]];
}
case
(
ast
.def_obj
(
?
id
))
{
check
(
fcx
.ccx.item_types
.contains_key
(
id
));
t
=
fcx
.ccx.item_types
.get
(
id
);
ty_params
=
some
(
fcx
.ccx.item_ty_params
.get
(
id
));
}
case
(
ast
.def_mod
(
_
))
{
// Hopefully part of a path.
ty_params
=
none
[
vec
[
ast
.def_id
]];
}
case
(
_
)
{
// FIXME: handle other names.
fcx
.ccx.sess
.unimpl
(
"definition variant for: "
+
_
str
.connect
(
pth
.node.idents
,
"."
));
fail
;
}
}
// Substitute type parameters if the user provided some.
auto
ty_substs_opt
;
auto
ty_substs_len
=
_
vec
.len
[
@
ast
.ty
](
pth
.node.types
);
if
(
ty_substs_len
>
0u
)
{
let
vec
[
@
ty
.t
]
ty_substs
=
vec
();
auto
i
=
0u
;
while
(
i
<
ty_substs_len
)
{
ty_substs
+=
vec
(
ast_ty_to_ty_crate
(
fcx
.ccx
,
pth
.node.types
.
(
i
)));
i
+=
1u
;
}
ty_substs_opt
=
some
[
vec
[
@
ty
.t
]](
ty_substs
);
alt
(
ty_params
)
{
case
(
none
[
vec
[
ast
.def_id
]])
{
fcx
.ccx.sess
.span_err
(
expr
.span
,
"this kind of "
+
"item may not take type "
+
"parameters"
);
fail
;
}
case
(
some
[
vec
[
ast
.def_id
]](
?
tps
))
{
t
=
substitute_ty_params
(
fcx
.ccx
,
t
,
tps
,
ty_substs
,
expr
.span
);
}
}
}
else
{
ty_substs_opt
=
none
[
vec
[
@
ty
.t
]];
alt
(
ty_params
)
{
case
(
none
[
vec
[
ast
.def_id
]])
{
/* nothing */
}
case
(
some
[
vec
[
ast
.def_id
]](
_
))
{
// We will acquire the type parameters through
// unification.
t
=
generalize_ty
(
fcx
.ccx
,
t
);
}
}
}
auto
ann
=
ast
.ann_type
(
t
,
ty_substs_opt
);
auto
defn
=
option
.get
[
ast
.def
](
defopt
);
auto
ann
=
instantiate_path
(
fcx
,
pth
,
defn
,
expr
.span
);
ret
@
fold
.respan
[
ast
.expr_
](
expr
.span
,
ast
.expr_path
(
pth
,
defopt
,
ann
));
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录