Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
dedbb4ed
R
Rust
项目概览
int
/
Rust
11 个月 前同步成功
通知
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,发现更多精彩内容 >>
未验证
提交
dedbb4ed
编写于
11月 30, 2017
作者:
D
David Wood
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Immutable unique closure upvars cannot be mutated.
上级
c3459b0c
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
64 addition
and
49 deletion
+64
-49
src/librustc/ich/impls_mir.rs
src/librustc/ich/impls_mir.rs
+1
-1
src/librustc/mir/mod.rs
src/librustc/mir/mod.rs
+3
-1
src/librustc_mir/borrow_check.rs
src/librustc_mir/borrow_check.rs
+50
-47
src/librustc_mir/build/mod.rs
src/librustc_mir/build/mod.rs
+10
-0
未找到文件。
src/librustc/ich/impls_mir.rs
浏览文件 @
dedbb4ed
...
...
@@ -31,7 +31,7 @@
lexical_scope
,
is_user_variable
});
impl_stable_hash_for!
(
struct
mir
::
UpvarDecl
{
debug_name
,
by_ref
});
impl_stable_hash_for!
(
struct
mir
::
UpvarDecl
{
debug_name
,
by_ref
,
mutability
});
impl_stable_hash_for!
(
struct
mir
::
BasicBlockData
<
'tcx
>
{
statements
,
terminator
,
is_cleanup
});
impl_stable_hash_for!
(
struct
mir
::
UnsafetyViolation
{
source_info
,
description
,
kind
});
impl_stable_hash_for!
(
struct
mir
::
UnsafetyCheckResult
{
violations
,
unsafe_blocks
});
...
...
src/librustc/mir/mod.rs
浏览文件 @
dedbb4ed
...
...
@@ -554,7 +554,9 @@ pub struct UpvarDecl {
pub
debug_name
:
Name
,
/// If true, the capture is behind a reference.
pub
by_ref
:
bool
pub
by_ref
:
bool
,
pub
mutability
:
Mutability
,
}
///////////////////////////////////////////////////////////////////////////
...
...
src/librustc_mir/borrow_check.rs
浏览文件 @
dedbb4ed
...
...
@@ -761,47 +761,34 @@ fn check_if_reassignment_to_immutable_state(&mut self,
let
move_data
=
self
.move_data
;
// determine if this path has a non-mut owner (and thus needs checking).
let
mut
l
=
lvalue
;
loop
{
match
*
l
{
Lvalue
::
Projection
(
ref
proj
)
=>
{
l
=
&
proj
.base
;
continue
;
}
Lvalue
::
Local
(
local
)
=>
{
match
self
.mir.local_decls
[
local
]
.mutability
{
Mutability
::
Not
=>
break
,
// needs check
Mutability
::
Mut
=>
return
,
}
}
Lvalue
::
Static
(
ref
static_
)
=>
{
// mutation of non-mut static is always illegal,
// independent of dataflow. However it will be catched by
// `check_access_permissions()`, we call delay_span_bug here
// to be sure that no case has been missed
if
!
self
.tcx
.is_static_mut
(
static_
.def_id
)
{
let
item_msg
=
match
self
.describe_lvalue
(
lvalue
)
{
Some
(
name
)
=>
format!
(
"immutable static item `{}`"
,
name
),
None
=>
"immutable static item"
.to_owned
()
};
self
.tcx.sess
.delay_span_bug
(
span
,
&
format!
(
"cannot assign to {}, should have been caught by
\
`check_access_permissions()`"
,
item_msg
));
}
return
;
}
}
if
let
Ok
(())
=
self
.is_mutable
(
lvalue
,
LocalMutationIsAllowed
::
No
)
{
return
;
}
if
let
Some
(
mpi
)
=
self
.move_path_for_lvalue
(
lvalue
)
{
for
ii
in
&
move_data
.init_path_map
[
mpi
]
{
if
flow_state
.ever_inits.curr_state
.contains
(
ii
)
{
let
first_assign_span
=
self
.move_data.inits
[
*
ii
]
.span
;
self
.report_illegal_reassignment
(
context
,
(
lvalue
,
span
),
first_assign_span
);
break
;
if
let
Err
(
_
)
=
self
.is_mutable
(
lvalue
,
LocalMutationIsAllowed
::
Yes
)
{
return
;
}
match
self
.move_path_closest_to
(
lvalue
)
{
Ok
(
mpi
)
=>
{
for
ii
in
&
move_data
.init_path_map
[
mpi
]
{
if
flow_state
.ever_inits.curr_state
.contains
(
ii
)
{
let
first_assign_span
=
self
.move_data.inits
[
*
ii
]
.span
;
self
.report_illegal_reassignment
(
context
,
(
lvalue
,
span
),
first_assign_span
);
break
;
}
}
}
},
Err
(
NoMovePathFound
::
ReachedStatic
)
=>
{
let
item_msg
=
match
self
.describe_lvalue
(
lvalue
)
{
Some
(
name
)
=>
format!
(
"immutable static item `{}`"
,
name
),
None
=>
"immutable static item"
.to_owned
()
};
self
.tcx.sess
.delay_span_bug
(
span
,
&
format!
(
"cannot assign to {}, should have been caught by
\
`check_access_permissions()`"
,
item_msg
));
},
}
}
...
...
@@ -1108,12 +1095,7 @@ fn is_mutable<'d>(&self,
ProjectionElem
::
Deref
=>
{
let
base_ty
=
proj
.base
.ty
(
self
.mir
,
self
.tcx
)
.to_ty
(
self
.tcx
);
// `Box<T>` owns its content, so mutable if its location is mutable
if
base_ty
.is_box
()
{
return
self
.is_mutable
(
&
proj
.base
,
LocalMutationIsAllowed
::
No
);
}
// Otherwise we check the kind of deref to decide
// Check the kind of deref to decide
match
base_ty
.sty
{
ty
::
TyRef
(
_
,
tnm
)
=>
{
match
tnm
.mutbl
{
...
...
@@ -1121,7 +1103,13 @@ fn is_mutable<'d>(&self,
hir
::
MutImmutable
=>
Err
(
lvalue
),
// Mutably borrowed data is mutable, but only if we have a
// unique path to the `&mut`
hir
::
MutMutable
=>
self
.is_unique
(
&
proj
.base
),
hir
::
MutMutable
=>
{
if
self
.is_upvar_field_projection
(
&
proj
.base
)
.is_some
()
{
self
.is_mutable
(
&
proj
.base
,
is_local_mutation_allowed
)
}
else
{
self
.is_unique
(
&
proj
.base
)
}
},
}
},
ty
::
TyRawPtr
(
tnm
)
=>
{
...
...
@@ -1133,8 +1121,11 @@ fn is_mutable<'d>(&self,
hir
::
MutMutable
=>
Ok
(()),
}
},
// `Box<T>` owns its content, so mutable if its location is mutable
_
if
base_ty
.is_box
()
=>
self
.is_mutable
(
&
proj
.base
,
LocalMutationIsAllowed
::
No
),
// Deref should only be for reference, pointers or boxes
_
=>
bug!
(
"Deref of unexpected type: {:?}"
,
base_ty
)
_
=>
bug!
(
"Deref of unexpected type: {:?}"
,
base_ty
)
,
}
},
// All other projections are owned by their base path, so mutable if
...
...
@@ -1143,8 +1134,20 @@ fn is_mutable<'d>(&self,
ProjectionElem
::
Index
(
..
)
|
ProjectionElem
::
ConstantIndex
{
..
}
|
ProjectionElem
::
Subslice
{
..
}
|
ProjectionElem
::
Downcast
(
..
)
=>
ProjectionElem
::
Downcast
(
..
)
=>
{
let
field_projection
=
self
.is_upvar_field_projection
(
lvalue
);
if
let
Some
(
field
)
=
field_projection
{
let
decl
=
&
self
.mir.upvar_decls
[
field
.index
()];
return
match
decl
.mutability
{
Mutability
::
Mut
=>
self
.is_unique
(
&
proj
.base
),
Mutability
::
Not
=>
Err
(
lvalue
),
};
}
self
.is_mutable
(
&
proj
.base
,
LocalMutationIsAllowed
::
No
)
}
}
}
}
...
...
src/librustc_mir/build/mod.rs
浏览文件 @
dedbb4ed
...
...
@@ -443,10 +443,20 @@ fn construct_fn<'a, 'gcx, 'tcx, A>(hir: Cx<'a, 'gcx, 'tcx>,
let
mut
decl
=
UpvarDecl
{
debug_name
:
keywords
::
Invalid
.name
(),
by_ref
,
mutability
:
Mutability
::
Not
,
};
if
let
Some
(
hir
::
map
::
NodeBinding
(
pat
))
=
tcx
.hir
.find
(
var_id
)
{
if
let
hir
::
PatKind
::
Binding
(
_
,
_
,
ref
ident
,
_
)
=
pat
.node
{
decl
.debug_name
=
ident
.node
;
let
bm
=
*
hir
.tables
.pat_binding_modes
()
.get
(
pat
.hir_id
)
.expect
(
"missing binding mode"
);
if
bm
==
ty
::
BindByValue
(
hir
::
MutMutable
)
{
decl
.mutability
=
Mutability
::
Mut
;
}
else
{
decl
.mutability
=
Mutability
::
Not
;
}
}
}
decl
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录