Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
6c78fa82
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,发现更多精彩内容 >>
提交
6c78fa82
编写于
8月 26, 2018
作者:
R
Ralf Jung
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
use associated const for machine controlling mutable statics
So get rid of the IsStatic trait again
上级
5b737dbb
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
39 addition
and
67 deletion
+39
-67
src/librustc_mir/const_eval.rs
src/librustc_mir/const_eval.rs
+4
-22
src/librustc_mir/interpret/machine.rs
src/librustc_mir/interpret/machine.rs
+9
-14
src/librustc_mir/interpret/memory.rs
src/librustc_mir/interpret/memory.rs
+24
-29
src/librustc_mir/interpret/mod.rs
src/librustc_mir/interpret/mod.rs
+1
-1
src/librustc_mir/interpret/terminator.rs
src/librustc_mir/interpret/terminator.rs
+1
-1
未找到文件。
src/librustc_mir/const_eval.rs
浏览文件 @
6c78fa82
...
...
@@ -26,11 +26,11 @@
use
rustc
::
mir
::
interpret
::{
EvalResult
,
EvalError
,
EvalErrorKind
,
GlobalId
,
Scalar
,
Alloc
Id
,
Allocation
,
ConstValue
,
AllocTyp
e
,
Scalar
,
Alloc
ation
,
ConstValu
e
,
};
use
interpret
::{
self
,
Place
,
PlaceTy
,
MemPlace
,
OpTy
,
Operand
,
Value
,
EvalContext
,
StackPopCleanup
,
MemoryKind
,
Memory
,
EvalContext
,
StackPopCleanup
,
MemoryKind
,
};
pub
fn
mk_borrowck_eval_cx
<
'a
,
'mir
,
'tcx
>
(
...
...
@@ -232,17 +232,12 @@ fn cause(&self) -> Option<&dyn Error> {
}
}
impl
interpret
::
IsStatic
for
!
{
fn
is_static
(
self
)
->
bool
{
// unreachable
self
}
}
impl
<
'mir
,
'tcx
>
interpret
::
Machine
<
'mir
,
'tcx
>
for
CompileTimeEvaluator
{
type
MemoryData
=
();
type
MemoryKinds
=
!
;
const
MUT_STATIC_KIND
:
Option
<!>
=
None
;
// no mutating of statics allowed
fn
find_fn
<
'a
>
(
ecx
:
&
mut
EvalContext
<
'a
,
'mir
,
'tcx
,
Self
>
,
instance
:
ty
::
Instance
<
'tcx
>
,
...
...
@@ -308,19 +303,6 @@ fn try_ptr_op<'a>(
}
}
fn
access_static_mut
<
'a
,
'm
>
(
mem
:
&
'm
mut
Memory
<
'a
,
'mir
,
'tcx
,
Self
>
,
id
:
AllocId
,
)
->
EvalResult
<
'tcx
,
&
'm
mut
Allocation
>
{
// This is always an error, we do not allow mutating statics
match
mem
.tcx.alloc_map
.lock
()
.get
(
id
)
{
Some
(
AllocType
::
Memory
(
..
))
|
Some
(
AllocType
::
Static
(
..
))
=>
err!
(
ModifiedConstantMemory
),
Some
(
AllocType
::
Function
(
..
))
=>
err!
(
DerefFunctionPointer
),
None
=>
err!
(
DanglingPointerDeref
),
}
}
fn
find_foreign_static
<
'a
>
(
_
tcx
:
TyCtxtAt
<
'a
,
'tcx
,
'tcx
>
,
_
def_id
:
DefId
,
...
...
src/librustc_mir/interpret/machine.rs
浏览文件 @
6c78fa82
...
...
@@ -15,16 +15,11 @@
use
std
::
hash
::
Hash
;
use
rustc
::
hir
::
def_id
::
DefId
;
use
rustc
::
mir
::
interpret
::{
Alloc
Id
,
Alloc
ation
,
EvalResult
,
Scalar
};
use
rustc
::
mir
::
interpret
::{
Allocation
,
EvalResult
,
Scalar
};
use
rustc
::
mir
;
use
rustc
::
ty
::{
self
,
layout
::
TyLayout
,
query
::
TyCtxtAt
};
use
super
::{
EvalContext
,
PlaceTy
,
OpTy
,
Memory
};
/// Used by the machine to tell if a certain allocation is for static memory
pub
trait
IsStatic
{
fn
is_static
(
self
)
->
bool
;
}
use
super
::{
EvalContext
,
PlaceTy
,
OpTy
};
/// Methods of this trait signifies a point where CTFE evaluation would fail
/// and some use case dependent behaviour can instead be applied
...
...
@@ -33,7 +28,10 @@ pub trait Machine<'mir, 'tcx>: Clone + Eq + Hash {
type
MemoryData
:
Clone
+
Eq
+
Hash
;
/// Additional memory kinds a machine wishes to distinguish from the builtin ones
type
MemoryKinds
:
::
std
::
fmt
::
Debug
+
Copy
+
Clone
+
Eq
+
Hash
+
IsStatic
;
type
MemoryKinds
:
::
std
::
fmt
::
Debug
+
Copy
+
Clone
+
Eq
+
Hash
;
/// The memory kind to use for mutated statics -- or None if those are not supported.
const
MUT_STATIC_KIND
:
Option
<
Self
::
MemoryKinds
>
;
/// Entry point to all function calls.
///
...
...
@@ -63,6 +61,9 @@ fn call_intrinsic<'a>(
)
->
EvalResult
<
'tcx
>
;
/// Called for read access to a foreign static item.
/// This can be called multiple times for the same static item and should return consistent
/// results. Once the item is *written* the first time, as usual for statics a copy is
/// made and this function is not called again.
fn
find_foreign_static
<
'a
>
(
tcx
:
TyCtxtAt
<
'a
,
'tcx
,
'tcx
>
,
def_id
:
DefId
,
...
...
@@ -83,12 +84,6 @@ fn try_ptr_op<'a>(
right_layout
:
TyLayout
<
'tcx
>
,
)
->
EvalResult
<
'tcx
,
Option
<
(
Scalar
,
bool
)
>>
;
/// Called when requiring mutable access to data in a static.
fn
access_static_mut
<
'a
,
'm
>
(
mem
:
&
'm
mut
Memory
<
'a
,
'mir
,
'tcx
,
Self
>
,
id
:
AllocId
,
)
->
EvalResult
<
'tcx
,
&
'm
mut
Allocation
>
;
/// Heap allocations via the `box` keyword
///
/// Returns a pointer to the allocated memory
...
...
src/librustc_mir/interpret/memory.rs
浏览文件 @
6c78fa82
...
...
@@ -29,7 +29,7 @@
use
syntax
::
ast
::
Mutability
;
use
super
::
{
Machine
,
IsStatic
}
;
use
super
::
Machine
;
#[derive(Debug,
PartialEq,
Eq,
Copy,
Clone,
Hash)]
pub
enum
MemoryKind
<
T
>
{
...
...
@@ -39,15 +39,6 @@ pub enum MemoryKind<T> {
Machine
(
T
),
}
impl
<
T
:
IsStatic
>
IsStatic
for
MemoryKind
<
T
>
{
fn
is_static
(
self
)
->
bool
{
match
self
{
MemoryKind
::
Stack
=>
false
,
MemoryKind
::
Machine
(
kind
)
=>
kind
.is_static
(),
}
}
}
#[derive(Clone)]
pub
struct
Memory
<
'a
,
'mir
,
'tcx
:
'a
+
'mir
,
M
:
Machine
<
'mir
,
'tcx
>>
{
/// Additional data required by the Machine
...
...
@@ -55,7 +46,9 @@ pub struct Memory<'a, 'mir, 'tcx: 'a + 'mir, M: Machine<'mir, 'tcx>> {
/// Allocations local to this instance of the miri engine. The kind
/// helps ensure that the same mechanism is used for allocation and
/// deallocation.
/// deallocation. When an allocation is not found here, it is a
/// static and looked up in the `tcx` for read access. Writing to
/// a static creates a copy here, in the machine.
alloc_map
:
FxHashMap
<
AllocId
,
(
MemoryKind
<
M
::
MemoryKinds
>
,
Allocation
)
>
,
pub
tcx
:
TyCtxtAt
<
'a
,
'tcx
,
'tcx
>
,
...
...
@@ -223,10 +216,6 @@ pub fn reallocate(
Ok
(
new_ptr
)
}
pub
fn
is_static
(
&
self
,
alloc_id
:
AllocId
)
->
bool
{
self
.alloc_map
.get
(
&
alloc_id
)
.map_or
(
true
,
|
&
(
kind
,
_
)|
kind
.is_static
())
}
/// Deallocate a local, or do nothing if that local has been made into a static
pub
fn
deallocate_local
(
&
mut
self
,
ptr
:
Pointer
)
->
EvalResult
<
'tcx
>
{
// The allocation might be already removed by static interning.
...
...
@@ -354,10 +343,10 @@ pub fn check_bounds(&self, ptr: Pointer, access: bool) -> EvalResult<'tcx> {
/// Allocation accessors
impl
<
'a
,
'mir
,
'tcx
,
M
:
Machine
<
'mir
,
'tcx
>>
Memory
<
'a
,
'mir
,
'tcx
,
M
>
{
pub
fn
get
(
&
self
,
id
:
AllocId
)
->
EvalResult
<
'tcx
,
&
Allocation
>
{
// normal alloc?
match
self
.alloc_map
.get
(
&
id
)
{
// Normal alloc?
Some
(
alloc
)
=>
Ok
(
&
alloc
.1
),
// No need to make any copies, just provide read access to the global static
//
Static.
No need to make any copies, just provide read access to the global static
// memory in tcx.
None
=>
const_eval_static
::
<
M
>
(
self
.tcx
,
id
),
}
...
...
@@ -368,14 +357,18 @@ pub fn get_mut(
id
:
AllocId
,
)
->
EvalResult
<
'tcx
,
&
mut
Allocation
>
{
// Static?
let
alloc
=
if
self
.alloc_map
.contains_key
(
&
id
)
{
&
mut
self
.alloc_map
.get_mut
(
&
id
)
.unwrap
()
.1
}
else
{
// The machine controls to what extend we are allowed to mutate global
// statics. (We do not want to allow that during CTFE, but miri needs it.)
M
::
access_static_mut
(
self
,
id
)
?
};
// See if we can use this
if
!
self
.alloc_map
.contains_key
(
&
id
)
{
// Ask the machine for what to do
if
let
Some
(
kind
)
=
M
::
MUT_STATIC_KIND
{
// The machine supports mutating statics. Make a copy, use that.
self
.deep_copy_static
(
id
,
MemoryKind
::
Machine
(
kind
))
?
;
}
else
{
return
err!
(
ModifiedConstantMemory
)
}
}
// If we come here, we know the allocation is in our map
let
alloc
=
&
mut
self
.alloc_map
.get_mut
(
&
id
)
.unwrap
()
.1
;
// See if we are allowed to mutate this
if
alloc
.mutability
==
Mutability
::
Immutable
{
err!
(
ModifiedConstantMemory
)
}
else
{
...
...
@@ -489,10 +482,12 @@ pub fn dump_allocs(&self, mut allocs: Vec<AllocId>) {
pub
fn
leak_report
(
&
self
)
->
usize
{
trace!
(
"### LEAK REPORT ###"
);
let
mut_static_kind
=
M
::
MUT_STATIC_KIND
.map
(|
k
|
MemoryKind
::
Machine
(
k
));
let
leaks
:
Vec
<
_
>
=
self
.alloc_map
.iter
()
.filter_map
(|(
&
id
,
(
kind
,
_
))|
if
kind
.is_static
()
{
None
}
else
{
Some
(
id
)
}
)
.filter_map
(|(
&
id
,
&
(
kind
,
_
))|
// exclude mutable statics
if
Some
(
kind
)
==
mut_static_kind
{
None
}
else
{
Some
(
id
)
}
)
.collect
();
let
n
=
leaks
.len
();
self
.dump_allocs
(
leaks
);
...
...
@@ -609,7 +604,7 @@ pub fn intern_static(
/// The alloc_id must refer to a (mutable) static; a deep copy of that
/// static is made into this memory.
pub
fn
deep_copy_static
(
fn
deep_copy_static
(
&
mut
self
,
id
:
AllocId
,
kind
:
MemoryKind
<
M
::
MemoryKinds
>
,
...
...
@@ -619,7 +614,7 @@ pub fn deep_copy_static(
return
err!
(
ModifiedConstantMemory
);
}
let
old
=
self
.alloc_map
.insert
(
id
,
(
kind
,
alloc
.clone
()));
assert
!
(
old
.is_none
(),
"deep_copy_static: must not overwrite
memory with
"
);
assert
!
(
old
.is_none
(),
"deep_copy_static: must not overwrite
existing memory
"
);
Ok
(())
}
...
...
src/librustc_mir/interpret/mod.rs
浏览文件 @
6c78fa82
...
...
@@ -31,7 +31,7 @@
pub
use
self
::
memory
::{
Memory
,
MemoryKind
};
pub
use
self
::
machine
::
{
Machine
,
IsStatic
}
;
pub
use
self
::
machine
::
Machine
;
pub
use
self
::
operand
::{
Value
,
ValTy
,
Operand
,
OpTy
};
...
...
src/librustc_mir/interpret/terminator.rs
浏览文件 @
6c78fa82
...
...
@@ -110,7 +110,7 @@ pub(super) fn eval_terminator(
}
(
instance
,
sig
)
}
ref
other
=>
bug!
(
"instance def ty: {:?}"
,
other
),
_
=>
bug!
(
"unexpected fn ptr to ty: {:?}"
,
instance_ty
),
}
}
ty
::
FnDef
(
def_id
,
substs
)
=>
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录