Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
35a41775
R
Rust
项目概览
int
/
Rust
12 个月 前同步成功
通知
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,发现更多精彩内容 >>
提交
35a41775
编写于
8月 27, 2013
作者:
N
Niko Matsakis
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Extend aliasability check to uncover `& &mut &mut` and the like
上级
6b23d204
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
87 addition
and
18 deletion
+87
-18
src/librustc/middle/borrowck/check_loans.rs
src/librustc/middle/borrowck/check_loans.rs
+47
-9
src/librustc/middle/mem_categorization.rs
src/librustc/middle/mem_categorization.rs
+10
-9
src/test/compile-fail/borrowck-assign-to-andmut-in-aliasable-loc.rs
...ompile-fail/borrowck-assign-to-andmut-in-aliasable-loc.rs
+30
-0
未找到文件。
src/librustc/middle/borrowck/check_loans.rs
浏览文件 @
35a41775
...
@@ -391,15 +391,7 @@ fn check_for_aliasable_mutable_writes(this: &CheckLoanCtxt,
...
@@ -391,15 +391,7 @@ fn check_for_aliasable_mutable_writes(this: &CheckLoanCtxt,
mc
::
cat_deref
(
b
,
_
,
mc
::
region_ptr
(
m_mutbl
,
_
))
=>
{
mc
::
cat_deref
(
b
,
_
,
mc
::
region_ptr
(
m_mutbl
,
_
))
=>
{
// Statically prohibit writes to `&mut` when aliasable
// Statically prohibit writes to `&mut` when aliasable
match
b
.freely_aliasable
()
{
check_for_aliasability_violation
(
this
,
expr
,
b
);
None
=>
{}
Some
(
cause
)
=>
{
this
.bccx
.report_aliasability_violation
(
expr
.span
,
MutabilityViolation
,
cause
);
}
}
}
}
mc
::
cat_deref
(
_
,
deref_count
,
mc
::
gc_ptr
(
ast
::
m_mutbl
))
=>
{
mc
::
cat_deref
(
_
,
deref_count
,
mc
::
gc_ptr
(
ast
::
m_mutbl
))
=>
{
...
@@ -419,6 +411,52 @@ fn check_for_aliasable_mutable_writes(this: &CheckLoanCtxt,
...
@@ -419,6 +411,52 @@ fn check_for_aliasable_mutable_writes(this: &CheckLoanCtxt,
return
true
;
// no errors reported
return
true
;
// no errors reported
}
}
fn
check_for_aliasability_violation
(
this
:
&
CheckLoanCtxt
,
expr
:
@
ast
::
expr
,
cmt
:
mc
::
cmt
)
->
bool
{
let
mut
cmt
=
cmt
;
loop
{
match
cmt
.cat
{
mc
::
cat_deref
(
b
,
_
,
mc
::
region_ptr
(
m_mutbl
,
_
))
|
mc
::
cat_downcast
(
b
)
|
mc
::
cat_stack_upvar
(
b
)
|
mc
::
cat_deref
(
b
,
_
,
mc
::
uniq_ptr
)
|
mc
::
cat_interior
(
b
,
_
)
|
mc
::
cat_discr
(
b
,
_
)
=>
{
// Aliasability depends on base cmt
cmt
=
b
;
}
mc
::
cat_copied_upvar
(
_
)
|
mc
::
cat_rvalue
(
*
)
|
mc
::
cat_local
(
*
)
|
mc
::
cat_arg
(
_
)
|
mc
::
cat_self
(
*
)
|
mc
::
cat_deref
(
_
,
_
,
mc
::
unsafe_ptr
(
*
))
|
mc
::
cat_static_item
(
*
)
|
mc
::
cat_implicit_self
(
*
)
|
mc
::
cat_deref
(
_
,
_
,
mc
::
gc_ptr
(
_
))
|
mc
::
cat_deref
(
_
,
_
,
mc
::
region_ptr
(
m_const
,
_
))
|
mc
::
cat_deref
(
_
,
_
,
mc
::
region_ptr
(
m_imm
,
_
))
=>
{
// Aliasability is independent of base cmt
match
cmt
.freely_aliasable
()
{
None
=>
{
return
true
;
}
Some
(
cause
)
=>
{
this
.bccx
.report_aliasability_violation
(
expr
.span
,
MutabilityViolation
,
cause
);
return
false
;
}
}
}
}
}
}
fn
check_for_assignment_to_restricted_or_frozen_location
(
fn
check_for_assignment_to_restricted_or_frozen_location
(
this
:
&
CheckLoanCtxt
,
this
:
&
CheckLoanCtxt
,
expr
:
@
ast
::
expr
,
expr
:
@
ast
::
expr
,
...
...
src/librustc/middle/mem_categorization.rs
浏览文件 @
35a41775
...
@@ -1146,9 +1146,10 @@ pub fn is_freely_aliasable(&self) -> bool {
...
@@ -1146,9 +1146,10 @@ pub fn is_freely_aliasable(&self) -> bool {
}
}
pub
fn
freely_aliasable
(
&
self
)
->
Option
<
AliasableReason
>
{
pub
fn
freely_aliasable
(
&
self
)
->
Option
<
AliasableReason
>
{
//! True if this lvalue resides in an area that is
/*!
//! freely aliasable, meaning that rustc cannot track
* Returns `Some(_)` if this lvalue represents a freely aliasable
//! the alias//es with precision.
* pointer type.
*/
// Maybe non-obvious: copied upvars can only be considered
// Maybe non-obvious: copied upvars can only be considered
// non-aliasable in once closures, since any other kind can be
// non-aliasable in once closures, since any other kind can be
...
@@ -1180,12 +1181,12 @@ pub fn freely_aliasable(&self) -> Option<AliasableReason> {
...
@@ -1180,12 +1181,12 @@ pub fn freely_aliasable(&self) -> Option<AliasableReason> {
Some
(
AliasableBorrowed
(
m
))
Some
(
AliasableBorrowed
(
m
))
}
}
cat_downcast
(
b
)
|
cat_downcast
(
*
)
|
cat_stack_upvar
(
b
)
|
cat_stack_upvar
(
*
)
|
cat_deref
(
b
,
_
,
uniq_ptr
)
|
cat_deref
(
_
,
_
,
uniq_ptr
)
|
cat_interior
(
b
,
_
)
|
cat_interior
(
*
)
|
cat_discr
(
b
,
_
)
=>
{
cat_discr
(
*
)
=>
{
b
.freely_aliasable
()
None
}
}
}
}
}
}
...
...
src/test/compile-fail/borrowck-assign-to-andmut-in-aliasable-loc.rs
0 → 100644
浏览文件 @
35a41775
// Copyright 2012 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.
// Test that assignments to an `&mut` pointer which is found in a
// borrowed (but otherwise non-aliasable) location is illegal.
struct
S
<
'self
>
{
pointer
:
&
'self
mut
int
}
fn
a
(
s
:
&
S
)
{
*
s
.pointer
+=
1
;
//~ ERROR cannot assign
}
fn
b
(
s
:
&
mut
S
)
{
*
s
.pointer
+=
1
;
}
fn
c
(
s
:
&
&
mut
S
)
{
*
s
.pointer
+=
1
;
//~ ERROR cannot assign
}
fn
main
()
{}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录