Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
ce3cc46c
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,发现更多精彩内容 >>
提交
ce3cc46c
编写于
8月 21, 2012
作者:
N
Niko Matsakis
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix generation of generic methods with explicit self
There used to be two distinct code paths. Now there is one.
上级
b5411f76
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
105 addition
and
34 deletion
+105
-34
src/rustc/middle/trans/base.rs
src/rustc/middle/trans/base.rs
+4
-7
src/rustc/middle/trans/impl.rs
src/rustc/middle/trans/impl.rs
+67
-26
src/rustc/middle/ty.rs
src/rustc/middle/ty.rs
+1
-1
src/test/run-pass/explicit-self-generic.rs
src/test/run-pass/explicit-self-generic.rs
+33
-0
未找到文件。
src/rustc/middle/trans/base.rs
浏览文件 @
ce3cc46c
...
...
@@ -2246,10 +2246,7 @@ fn monomorphic_fn(ccx: @crate_ctxt, fn_id: ast::def_id,
ast_map
::
node_method
(
mth
,
impl_def_id
,
_
)
=>
{
let
d
=
mk_lldecl
();
set_inline_hint_if_appr
(
mth
.attrs
,
d
);
let
selfty
=
ty
::
node_id_to_type
(
ccx
.tcx
,
mth
.self_id
);
let
selfty
=
ty
::
subst_tps
(
ccx
.tcx
,
substs
,
selfty
);
trans_fn
(
ccx
,
pt
,
mth
.decl
,
mth
.body
,
d
,
impl_self
(
selfty
),
psubsts
,
fn_id
.node
);
impl
::
trans_method
(
ccx
,
pt
,
mth
,
psubsts
,
d
);
d
}
ast_map
::
node_ctor
(
nm
,
tps
,
ctor
,
parent_id
,
_
)
=>
{
...
...
@@ -5321,13 +5318,13 @@ fn get_item_val(ccx: @crate_ctxt, id: ast::node_id) -> ValueRef {
}
ast
::
provided
(
m
)
=>
{
exprt
=
true
;
trans
_method
(
ccx
,
id
,
pth
,
m
)
register
_method
(
ccx
,
id
,
pth
,
m
)
}
}
}
ast_map
::
node_method
(
m
,
_
,
pth
)
=>
{
exprt
=
true
;
trans
_method
(
ccx
,
id
,
pth
,
m
)
register
_method
(
ccx
,
id
,
pth
,
m
)
}
ast_map
::
node_foreign_item
(
ni
,
_
,
pth
)
=>
{
exprt
=
true
;
...
...
@@ -5397,7 +5394,7 @@ fn get_item_val(ccx: @crate_ctxt, id: ast::node_id) -> ValueRef {
}
}
fn
trans
_method
(
ccx
:
@
crate_ctxt
,
id
:
ast
::
node_id
,
pth
:
@
ast_map
::
path
,
fn
register
_method
(
ccx
:
@
crate_ctxt
,
id
:
ast
::
node_id
,
pth
:
@
ast_map
::
path
,
m
:
@
ast
::
method
)
->
ValueRef
{
let
mty
=
ty
::
node_id_to_type
(
ccx
.tcx
,
id
);
let
pth
=
vec
::
append
(
*
pth
,
~
[
path_name
(
@
ccx
.names
(
~
"meth"
)),
...
...
src/rustc/middle/trans/impl.rs
浏览文件 @
ce3cc46c
...
...
@@ -18,41 +18,82 @@
import
syntax
::
print
::
pprust
::
expr_to_str
;
/**
The main "translation" pass for methods. Generates code
for non-monomorphized methods only. Other methods will
be generated once they are invoked with specific type parameters,
see `trans::base::lval_static_fn()` or `trans::base::monomorphic_fn()`.
*/
fn
trans_impl
(
ccx
:
@
crate_ctxt
,
path
:
path
,
name
:
ast
::
ident
,
methods
:
~
[
@
ast
::
method
],
tps
:
~
[
ast
::
ty_param
])
{
let
_
icx
=
ccx
.insn_ctxt
(
"impl::trans_impl"
);
if
tps
.len
()
>
0u
{
return
;
}
let
sub_path
=
vec
::
append_one
(
path
,
path_name
(
name
));
for
vec
::
each
(
methods
)
|
m
|
{
if
m
.tps
.len
()
==
0u
{
let
llfn
=
get_item_val
(
ccx
,
m
.id
);
let
self_ty
=
ty
::
node_id_to_type
(
ccx
.tcx
,
m
.self_id
);
let
self_arg
=
match
m
.self_ty.node
{
ast
::
sty_static
=>
{
no_self
}
ast
::
sty_box
(
_
)
=>
{
impl_self
(
ty
::
mk_imm_box
(
ccx
.tcx
,
self_ty
))
}
ast
::
sty_uniq
(
_
)
=>
{
impl_self
(
ty
::
mk_imm_uniq
(
ccx
.tcx
,
self_ty
))
}
ast
::
sty_region
(
*
)
=>
{
impl_self
(
ty
::
mk_imm_ptr
(
ccx
.tcx
,
self_ty
))
}
ast
::
sty_value
=>
{
impl_owned_self
(
self_ty
)
}
ast
::
sty_by_ref
=>
{
impl_self
(
self_ty
)
}
};
trans_fn
(
ccx
,
vec
::
append_one
(
sub_path
,
path_name
(
m
.ident
)),
m
.decl
,
m
.body
,
llfn
,
self_arg
,
none
,
m
.id
);
for
vec
::
each
(
methods
)
|
method
|
{
if
method
.tps
.len
()
==
0u
{
let
llfn
=
get_item_val
(
ccx
,
method
.id
);
let
path
=
vec
::
append_one
(
sub_path
,
path_name
(
method
.ident
));
trans_method
(
ccx
,
path
,
method
,
none
,
llfn
);
}
}
}
/**
Translates a (possibly monomorphized) method body.
# Parameters
- `path`: the path to the method
- `method`: the AST node for the method
- `param_substs`: if this is a generic method, the current values for
type parameters and so forth, else none
- `llfn`: the LLVM ValueRef for the method
*/
fn
trans_method
(
ccx
:
@
crate_ctxt
,
path
:
path
,
method
:
&
ast
::
method
,
param_substs
:
option
<
param_substs
>
,
llfn
:
ValueRef
)
{
// determine the (monomorphized) type that `self` maps to for this method
let
self_ty
=
ty
::
node_id_to_type
(
ccx
.tcx
,
method
.self_id
);
let
self_ty
=
match
param_substs
{
none
=>
self_ty
,
some
({
tys
:
ref
tys
,
_
})
=>
ty
::
subst_tps
(
ccx
.tcx
,
*
tys
,
self_ty
)
};
// apply any transformations from the explicit self declaration
let
self_arg
=
match
method
.self_ty.node
{
ast
::
sty_static
=>
{
no_self
}
ast
::
sty_box
(
_
)
=>
{
impl_self
(
ty
::
mk_imm_box
(
ccx
.tcx
,
self_ty
))
}
ast
::
sty_uniq
(
_
)
=>
{
impl_self
(
ty
::
mk_imm_uniq
(
ccx
.tcx
,
self_ty
))
}
ast
::
sty_region
(
*
)
=>
{
impl_self
(
ty
::
mk_imm_ptr
(
ccx
.tcx
,
self_ty
))
}
ast
::
sty_value
=>
{
impl_owned_self
(
self_ty
)
}
ast
::
sty_by_ref
=>
{
impl_self
(
self_ty
)
}
};
// generate the actual code
trans_fn
(
ccx
,
path
,
method
.decl
,
method
.body
,
llfn
,
self_arg
,
param_substs
,
method
.id
);
}
fn
trans_self_arg
(
bcx
:
block
,
base
:
@
ast
::
expr
,
mentry
:
typeck
::
method_map_entry
)
->
result
{
let
_
icx
=
bcx
.insn_ctxt
(
"impl::trans_self_arg"
);
...
...
src/rustc/middle/ty.rs
浏览文件 @
ce3cc46c
...
...
@@ -1106,7 +1106,7 @@ fn do_fold(cx: ctxt, t0: t, under_r: bool,
}
// Substitute *only* type parameters. Used in trans where regions are erased.
fn
subst_tps
(
cx
:
ctxt
,
tps
:
~
[
t
],
typ
:
t
)
->
t
{
fn
subst_tps
(
cx
:
ctxt
,
tps
:
&
[
t
],
typ
:
t
)
->
t
{
if
tps
.len
()
==
0u
{
return
typ
;
}
let
tb
=
ty
::
get
(
typ
);
if
!
tbox_has_flag
(
tb
,
has_params
)
{
return
typ
;
}
...
...
src/test/run-pass/explicit-self-generic.rs
0 → 100644
浏览文件 @
ce3cc46c
use
std
;
/**
* A function that returns a hash of a value
*
* The hash should concentrate entropy in the lower bits.
*/
type
HashFn
<
K
>
=
pure
fn
~
(
K
)
->
uint
;
type
EqFn
<
K
>
=
pure
fn
~
(
K
,
K
)
->
bool
;
enum
LinearMap
<
K
,
V
>
{
LinearMap_
({
resize_at
:
uint
,
size
:
uint
})
}
fn
linear_map
<
K
,
V
>
()
->
LinearMap
<
K
,
V
>
{
LinearMap_
({
resize_at
:
32
,
size
:
0
})
}
impl
<
K
,
V
>
LinearMap
<
K
,
V
>
{
fn
len
(
&
mut
self
)
->
uint
{
self
.size
}
}
fn
main
()
{
let
mut
m
=
~
linear_map
::
<
(),()
>
();
assert
m
.len
()
==
0
;
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录