Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
44e2dc27
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,发现更多精彩内容 >>
提交
44e2dc27
编写于
7月 23, 2010
作者:
G
Graydon Hoare
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Improve mutability checking. Closes #118.
上级
8bd84139
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
60 addition
and
15 deletion
+60
-15
src/boot/me/effect.ml
src/boot/me/effect.ml
+1
-1
src/boot/me/type.ml
src/boot/me/type.ml
+32
-11
src/test/compile-fail/writing-through-read-alias.rs
src/test/compile-fail/writing-through-read-alias.rs
+1
-1
src/test/compile-fail/writing-to-immutable-obj.rs
src/test/compile-fail/writing-to-immutable-obj.rs
+8
-0
src/test/compile-fail/writing-to-immutable-rec.rs
src/test/compile-fail/writing-to-immutable-rec.rs
+5
-0
src/test/compile-fail/writing-to-immutable-tup.rs
src/test/compile-fail/writing-to-immutable-tup.rs
+5
-0
src/test/compile-fail/writing-to-immutable-vec.rs
src/test/compile-fail/writing-to-immutable-vec.rs
+5
-0
src/test/run-pass/foreach-nested-2.rs
src/test/run-pass/foreach-nested-2.rs
+2
-1
src/test/run-pass/foreach-nested.rs
src/test/run-pass/foreach-nested.rs
+1
-1
未找到文件。
src/boot/me/effect.ml
浏览文件 @
44e2dc27
...
...
@@ -51,7 +51,7 @@ let mutability_checking_visitor
if
(
is_mutable
or
is_init
)
then
()
else
err
(
Some
s
.
id
)
"writing to
non-mutable slot of
type %a in statement %a"
"writing to
immutable
type %a in statement %a"
Ast
.
sprintf_ty
dst_ty
Ast
.
sprintf_stmt
s
in
(* FIXME (issue #75): enforce the no-write-alias-to-immutable-slot
...
...
src/boot/me/type.ml
浏览文件 @
44e2dc27
...
...
@@ -45,7 +45,7 @@ let check_stmt (cx:Semant.ctxt) : (fn_ctx -> Ast.stmt -> unit) =
let
get_slot_ty
(
slot
:
Ast
.
slot
)
:
Ast
.
ty
=
match
slot
.
Ast
.
slot_ty
with
Some
ty
->
ty
Some
ty
->
ty
|
None
->
Common
.
bug
()
"get_slot_ty: no type in slot"
in
...
...
@@ -62,7 +62,11 @@ let check_stmt (cx:Semant.ctxt) : (fn_ctx -> Ast.stmt -> unit) =
in
let
maybe_mutable
(
mutability
:
Ast
.
mutability
)
(
ty
:
Ast
.
ty
)
:
Ast
.
ty
=
if
mutability
=
Ast
.
MUT_mutable
then
Ast
.
TY_mutable
ty
else
ty
let
res
=
if
mutability
=
Ast
.
MUT_mutable
then
Ast
.
TY_mutable
ty
else
ty
in
log
cx
"maybe_mutable: %a -> %a"
Ast
.
sprintf_ty
ty
Ast
.
sprintf_ty
res
;
res
in
(*
...
...
@@ -229,7 +233,7 @@ let check_stmt (cx:Semant.ctxt) : (fn_ctx -> Ast.stmt -> unit) =
()
"internal_check_slot: supplied defn wasn't a slot at all"
in
match
infer
,
slot
.
Ast
.
slot_ty
with
match
infer
,
slot
.
Ast
.
slot_ty
with
Some
expected
,
Some
actual
->
demand
expected
actual
;
actual
...
...
@@ -301,6 +305,10 @@ let check_stmt (cx:Semant.ctxt) : (fn_ctx -> Ast.stmt -> unit) =
|
`Module
items
->
Ast
.
sprintf_mod_items
chan
items
in
let
_
=
log
cx
"base lval %a, base type %a"
Ast
.
sprintf_lval
base
sprintf_itype
()
in
let
rec
typecheck
base_ity
=
match
base_ity
,
comp
with
`Type
(
Ast
.
TY_rec
ty_rec
)
,
Ast
.
COMP_named
(
Ast
.
COMP_ident
id
)
->
...
...
@@ -455,14 +463,14 @@ let check_stmt (cx:Semant.ctxt) : (fn_ctx -> Ast.stmt -> unit) =
:
(
Ast
.
ty
*
int
)
=
let
yield_ty
ty
=
let
(
ty
,
n_boxes
)
=
if
deref
then
unbox
ty
else
(
ty
,
0
)
in
(
maybe_mutable
mut
ty
,
n_boxes
)
(
maybe_mutable
mut
ty
,
n_boxes
)
in
match
infer
,
internal_check_lval
infer
lval
with
|
None
,
LTYPE_mono
ty
->
yield_ty
ty
|
Some
expected
,
LTYPE_mono
actual
->
demand
expected
actual
;
yield_ty
actual
|
None
,
(
LTYPE_poly
_
as
lty
)
->
|
None
,
(
LTYPE_poly
_
as
lty
)
->
Common
.
err
None
"not enough context to automatically instantiate the polymorphic \
...
...
@@ -487,9 +495,21 @@ let check_stmt (cx:Semant.ctxt) : (fn_ctx -> Ast.stmt -> unit) =
* Get the real one. *)
let
lval_id
=
Semant
.
lval_base_id
lval
in
let
lval
=
Hashtbl
.
find
cx
.
Semant
.
ctxt_all_lvals
lval_id
in
let
_
=
log
cx
"generic_check_lval %a mut=%s deref=%s infer=%s"
Ast
.
sprintf_lval
lval
(
if
mut
=
Ast
.
MUT_mutable
then
"mutable"
else
"immutable"
)
(
if
deref
then
"true"
else
"false"
)
(
match
infer
with
None
->
"<none>"
|
Some
t
->
Fmt
.
fmt_to_str
Ast
.
fmt_ty
t
)
in
let
(
lval_ty
,
n_boxes
)
=
internal_check_outer_lval
~
mut
:
mut
~
deref
:
deref
infer
lval
in
let
_
=
log
cx
"checked lval %a with type %a"
Ast
.
sprintf_lval
lval
Ast
.
sprintf_ty
lval_ty
in
if
Hashtbl
.
mem
cx
.
Semant
.
ctxt_all_lval_types
lval_id
then
assert
((
Hashtbl
.
find
cx
.
Semant
.
ctxt_all_lval_types
lval_id
)
=
lval_ty
)
...
...
@@ -514,8 +534,8 @@ let check_stmt (cx:Semant.ctxt) : (fn_ctx -> Ast.stmt -> unit) =
implemented; please add explicit dereference operators"
;
Hashtbl
.
replace
cx
.
Semant
.
ctxt_auto_deref_lval
lval_id
(
n_boxes
>
0
);
(* Before demoting the lval to a value, strip off mutability. *)
fundamental_ty
lval_ty
(* Before demoting the lval to a value, strip off mutability. *)
fundamental_ty
lval_ty
(* Note that this function should be avoided when possible, because it
* cannot perform type inference. In general you should prefer
...
...
@@ -538,11 +558,12 @@ let check_stmt (cx:Semant.ctxt) : (fn_ctx -> Ast.stmt -> unit) =
in
let
infer_lval
?
mut
:
(
mut
=
Ast
.
MUT_mutable
)
?
mut
:
(
mut
=
Ast
.
MUT_
im
mutable
)
(
ty
:
Ast
.
ty
)
(
lval
:
Ast
.
lval
)
:
unit
=
ignore
(
generic_check_lval
?
mut
:
mut
~
deref
:
false
(
Some
ty
)
lval
)
ignore
(
generic_check_lval
?
mut
:
mut
~
deref
:
false
(
Some
(
Ast
.
TY_mutable
ty
))
lval
)
in
(*
...
...
@@ -574,7 +595,7 @@ let check_stmt (cx:Semant.ctxt) : (fn_ctx -> Ast.stmt -> unit) =
|
Ast
.
EXPR_binary
(
binop
,
lhs
,
rhs
)
->
let
operand_ty
=
check_atom
~
deref
:
true
lhs
in
demand
operand_ty
(
check_atom
~
deref
:
true
rhs
);
check_binop
binop
operand_ty
check_binop
binop
operand_ty
|
Ast
.
EXPR_unary
(
Ast
.
UNOP_not
,
atom
)
->
demand
Ast
.
TY_bool
(
check_atom
~
deref
:
true
atom
);
Ast
.
TY_bool
...
...
@@ -596,7 +617,7 @@ let check_stmt (cx:Semant.ctxt) : (fn_ctx -> Ast.stmt -> unit) =
let
check_fn
(
callee
:
Ast
.
lval
)
(
args
:
Ast
.
atom
array
)
:
Ast
.
ty
=
let
arg_tys
=
Array
.
map
check_atom
args
in
let
callee_ty
=
check_lval
callee
in
demand_fn
(
Array
.
map
(
fun
ty
->
Some
ty
)
arg_tys
)
callee_ty
demand_fn
(
Array
.
map
(
fun
ty
->
Some
ty
)
arg_tys
)
callee_ty
in
let
rec
check_pat
(
expected
:
Ast
.
ty
)
(
pat
:
Ast
.
pat
)
:
unit
=
...
...
src/test/compile-fail/writing-through-read-alias.rs
浏览文件 @
44e2dc27
// -*- rust -*-
// error-pattern: writing to
non-mutable slot
// error-pattern: writing to
immutable type
type
point
=
rec
(
int
x
,
int
y
,
int
z
);
...
...
src/test/compile-fail/writing-to-immutable-obj.rs
0 → 100644
浏览文件 @
44e2dc27
// error-pattern: writing to immutable type
obj
objy
(
int
x
)
{
fn
foo
()
->
()
{
x
=
5
;
}
}
fn
main
()
{
}
src/test/compile-fail/writing-to-immutable-rec.rs
0 → 100644
浏览文件 @
44e2dc27
// error-pattern: writing to immutable type
fn
main
()
{
let
rec
(
int
x
)
r
=
rec
(
x
=
1
);
r
.x
=
6
;
}
src/test/compile-fail/writing-to-immutable-tup.rs
0 → 100644
浏览文件 @
44e2dc27
// error-pattern: writing to immutable type
fn
main
()
{
let
tup
(
int
)
t
=
tup
(
1
);
t
._0
=
5
;
}
src/test/compile-fail/writing-to-immutable-vec.rs
0 → 100644
浏览文件 @
44e2dc27
// error-pattern: writing to immutable type
fn
main
()
{
let
vec
[
int
]
v
=
vec
(
1
,
2
,
3
);
v
.
(
1
)
=
4
;
}
\ No newline at end of file
src/test/run-pass/foreach-nested-2.rs
浏览文件 @
44e2dc27
...
...
@@ -14,7 +14,8 @@
}
fn
main
()
{
let
vec
[
int
]
a
=
vec
(
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
);
let
vec
[
mutable
int
]
a
=
vec
[
mutable
](
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
,
-
1
);
let
int
p
=
0
;
for
each
(
int
i
in
two
())
{
...
...
src/test/run-pass/foreach-nested.rs
浏览文件 @
44e2dc27
...
...
@@ -6,7 +6,7 @@
}
fn
main
()
{
let
vec
[
int
]
a
=
vec
(
-
1
,
-
1
,
-
1
,
-
1
);
let
vec
[
mutable
int
]
a
=
vec
[
mutable
]
(
-
1
,
-
1
,
-
1
,
-
1
);
let
int
p
=
0
;
for
each
(
int
i
in
two
())
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录