Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
c9e3cb67
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,发现更多精彩内容 >>
提交
c9e3cb67
编写于
2月 10, 2014
作者:
N
Niko Matsakis
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
test -- add new tests specifically examining closure borrows
上级
3805c541
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
296 addition
and
0 deletion
+296
-0
src/test/compile-fail/borrowck-closures-mut-and-imm.rs
src/test/compile-fail/borrowck-closures-mut-and-imm.rs
+79
-0
src/test/compile-fail/borrowck-closures-mut-of-imm.rs
src/test/compile-fail/borrowck-closures-mut-of-imm.rs
+31
-0
src/test/compile-fail/borrowck-closures-two-mut.rs
src/test/compile-fail/borrowck-closures-two-mut.rs
+56
-0
src/test/compile-fail/borrowck-closures-unique.rs
src/test/compile-fail/borrowck-closures-unique.rs
+50
-0
src/test/compile-fail/borrowck-closures-use-after-free.rs
src/test/compile-fail/borrowck-closures-use-after-free.rs
+31
-0
src/test/run-pass/borrowck-closures-two-imm.rs
src/test/run-pass/borrowck-closures-two-imm.rs
+49
-0
未找到文件。
src/test/compile-fail/borrowck-closures-mut-and-imm.rs
0 → 100644
浏览文件 @
c9e3cb67
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Tests that two closures cannot simultaneously have mutable
// and immutable access to the variable. Issue #6801.
fn
get
(
x
:
&
int
)
->
int
{
*
x
}
fn
set
(
x
:
&
mut
int
)
{
*
x
=
4
;
}
fn
a
()
{
let
mut
x
=
3
;
let
c1
=
||
x
=
4
;
let
c2
=
||
x
*
5
;
//~ ERROR cannot borrow `x`
}
fn
b
()
{
let
mut
x
=
3
;
let
c1
=
||
set
(
&
mut
x
);
let
c2
=
||
get
(
&
x
);
//~ ERROR cannot borrow `x`
}
fn
c
()
{
let
mut
x
=
3
;
let
c1
=
||
set
(
&
mut
x
);
let
c2
=
||
x
*
5
;
//~ ERROR cannot borrow `x`
}
fn
d
()
{
let
mut
x
=
3
;
let
c2
=
||
x
*
5
;
x
=
5
;
//~ ERROR cannot assign
}
fn
e
()
{
let
mut
x
=
3
;
let
c1
=
||
get
(
&
x
);
x
=
5
;
//~ ERROR cannot assign
}
fn
f
()
{
let
mut
x
=
~
3
;
let
c1
=
||
get
(
&*
x
);
*
x
=
5
;
//~ ERROR cannot assign
}
fn
g
()
{
struct
Foo
{
f
:
~
int
}
let
mut
x
=
~
Foo
{
f
:
~
3
};
let
c1
=
||
get
(
&*
x
.f
);
*
x
.f
=
5
;
//~ ERROR cannot assign to `*x.f`
}
fn
h
()
{
struct
Foo
{
f
:
~
int
}
let
mut
x
=
~
Foo
{
f
:
~
3
};
let
c1
=
||
get
(
&*
x
.f
);
let
c2
=
||
*
x
.f
=
5
;
//~ ERROR cannot borrow `x` as mutable
}
fn
main
()
{
}
src/test/compile-fail/borrowck-closures-mut-of-imm.rs
0 → 100644
浏览文件 @
c9e3cb67
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Tests that two closures cannot simultaneously have mutable
// and immutable access to the variable. Issue #6801.
fn
get
(
x
:
&
int
)
->
int
{
*
x
}
fn
set
(
x
:
&
mut
int
)
{
*
x
=
4
;
}
fn
a
(
x
:
&
int
)
{
let
c1
=
||
set
(
&
mut
*
x
);
//~^ ERROR cannot borrow
let
c2
=
||
set
(
&
mut
*
x
);
//~^ ERROR closure requires unique access to `x`
//~^^ ERROR cannot borrow
}
fn
main
()
{
}
src/test/compile-fail/borrowck-closures-two-mut.rs
0 → 100644
浏览文件 @
c9e3cb67
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Tests that two closures cannot simultaneously have mutable
// access to the variable, whether that mutable access be used
// for direct assignment or for taking mutable ref. Issue #6801.
fn
a
()
{
let
mut
x
=
3
;
let
c1
=
||
x
=
4
;
let
c2
=
||
x
=
5
;
//~ ERROR cannot borrow `x` as mutable more than once
}
fn
set
(
x
:
&
mut
int
)
{
*
x
=
4
;
}
fn
b
()
{
let
mut
x
=
3
;
let
c1
=
||
set
(
&
mut
x
);
let
c2
=
||
set
(
&
mut
x
);
//~ ERROR cannot borrow `x` as mutable more than once
}
fn
c
()
{
let
mut
x
=
3
;
let
c1
=
||
x
=
5
;
let
c2
=
||
set
(
&
mut
x
);
//~ ERROR cannot borrow `x` as mutable more than once
}
fn
d
()
{
let
mut
x
=
3
;
let
c1
=
||
x
=
5
;
let
c2
=
||
{
let
_
y
=
||
set
(
&
mut
x
);
};
// (nested closure)
//~^ ERROR cannot borrow `x` as mutable more than once
}
fn
g
()
{
struct
Foo
{
f
:
~
int
}
let
mut
x
=
~
Foo
{
f
:
~
3
};
let
c1
=
||
set
(
&
mut
*
x
.f
);
let
c2
=
||
set
(
&
mut
*
x
.f
);
//~^ ERROR cannot borrow `x` as mutable more than once
}
fn
main
()
{
}
src/test/compile-fail/borrowck-closures-unique.rs
0 → 100644
浏览文件 @
c9e3cb67
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Tests that a closure which requires mutable access to the referent
// of an `&mut` requires a "unique" borrow -- that is, the variable to
// be borrowed (here, `x`) will not be borrowed *mutably*, but
// may be *immutable*, but we cannot allow
// multiple borrows.
fn
get
(
x
:
&
int
)
->
int
{
*
x
}
fn
set
(
x
:
&
mut
int
)
->
int
{
*
x
}
fn
a
(
x
:
&
mut
int
)
{
let
c1
=
||
get
(
x
);
let
c2
=
||
get
(
x
);
}
fn
b
(
x
:
&
mut
int
)
{
let
c1
=
||
get
(
x
);
let
c2
=
||
set
(
x
);
//~ ERROR closure requires unique access to `x`
}
fn
c
(
x
:
&
mut
int
)
{
let
c1
=
||
get
(
x
);
let
c2
=
||
{
get
(
x
);
set
(
x
);
};
//~ ERROR closure requires unique access to `x`
}
fn
d
(
x
:
&
mut
int
)
{
let
c1
=
||
set
(
x
);
let
c2
=
||
set
(
x
);
//~ ERROR closure requires unique access to `x`
}
fn
e
(
x
:
&
mut
int
)
{
let
c1
:
||
=
||
x
=
fail
!
();
//~ ERROR closure cannot assign to immutable argument `x`
}
fn
main
()
{
}
src/test/compile-fail/borrowck-closures-use-after-free.rs
0 → 100644
浏览文件 @
c9e3cb67
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Tests that a closure which mutates a local variable
// cannot also be supplied a borrowed version of that
// variable's contents. Issue #11192.
struct
Foo
{
x
:
int
}
impl
Drop
for
Foo
{
fn
drop
(
&
mut
self
)
{
println!
(
"drop {}"
,
self
.x
);
}
}
fn
main
()
{
let
mut
ptr
=
~
Foo
{
x
:
0
};
let
test
=
|
foo
:
&
Foo
|
{
ptr
=
~
Foo
{
x
:
ptr
.x
+
1
};
};
test
(
ptr
);
//~ ERROR cannot borrow `*ptr`
}
src/test/run-pass/borrowck-closures-two-imm.rs
0 → 100644
浏览文件 @
c9e3cb67
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Tests that two closures can simultaneously have immutable
// access to the variable, whether that immutable access be used
// for direct reads or for taking immutable ref. Also check
// that the main function can read the variable too while
// the closures are in scope. Issue #6801.
fn
a
()
->
int
{
let
mut
x
=
3
;
x
+=
1
;
let
c1
=
||
x
*
4
;
let
c2
=
||
x
*
5
;
c1
()
*
c2
()
*
x
}
fn
get
(
x
:
&
int
)
->
int
{
*
x
*
4
}
fn
b
()
->
int
{
let
mut
x
=
3
;
x
+=
1
;
let
c1
=
||
get
(
&
x
);
let
c2
=
||
get
(
&
x
);
c1
()
*
c2
()
*
x
}
fn
c
()
->
int
{
let
mut
x
=
3
;
x
+=
1
;
let
c1
=
||
x
*
5
;
let
c2
=
||
get
(
&
x
);
c1
()
*
c2
()
*
x
}
pub
fn
main
()
{
assert_eq!
(
a
(),
1280
);
assert_eq!
(
b
(),
1024
);
assert_eq!
(
c
(),
1280
);
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录