Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
29932db0
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,发现更多精彩内容 >>
提交
29932db0
编写于
1月 26, 2022
作者:
W
woppopo
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
`const_deallocate`: Don't deallocate memory allocated in an another const. Does nothing at runtime.
`const_allocate`: Returns a null pointer at runtime.
上级
aa6795e2
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
69 addition
and
11 deletion
+69
-11
compiler/rustc_codegen_ssa/src/mir/intrinsic.rs
compiler/rustc_codegen_ssa/src/mir/intrinsic.rs
+10
-0
compiler/rustc_const_eval/src/const_eval/machine.rs
compiler/rustc_const_eval/src/const_eval/machine.rs
+15
-5
library/core/src/intrinsics.rs
library/core/src/intrinsics.rs
+3
-2
library/core/tests/intrinsics.rs
library/core/tests/intrinsics.rs
+13
-0
library/core/tests/lib.rs
library/core/tests/lib.rs
+1
-0
src/test/ui/consts/const-eval/heap/alloc_intrinsic_errors.rs
src/test/ui/consts/const-eval/heap/alloc_intrinsic_errors.rs
+1
-2
src/test/ui/consts/const-eval/heap/alloc_intrinsic_errors.stderr
...t/ui/consts/const-eval/heap/alloc_intrinsic_errors.stderr
+1
-1
src/test/ui/consts/const-eval/heap/dealloc_intrinsic.rs
src/test/ui/consts/const-eval/heap/dealloc_intrinsic.rs
+25
-1
未找到文件。
compiler/rustc_codegen_ssa/src/mir/intrinsic.rs
浏览文件 @
29932db0
...
...
@@ -369,6 +369,16 @@ pub fn codegen_intrinsic_call(
}
}
sym
::
const_allocate
=>
{
// returns a null pointer at runtime.
bx
.const_null
(
bx
.type_i8p
())
}
sym
::
const_deallocate
=>
{
// nop at runtime.
return
;
}
// This requires that atomic intrinsics follow a specific naming pattern:
// "atomic_<operation>[_<ordering>]", and no ordering means SeqCst
name
if
name_str
.starts_with
(
"atomic_"
)
=>
{
...
...
compiler/rustc_const_eval/src/const_eval/machine.rs
浏览文件 @
29932db0
...
...
@@ -358,11 +358,21 @@ fn call_intrinsic(
Err
(
err
)
=>
throw_ub_format!
(
"align has to be a power of 2, {}"
,
err
),
};
ecx
.memory
.deallocate
(
ptr
,
Some
((
size
,
align
)),
interpret
::
MemoryKind
::
Machine
(
MemoryKind
::
Heap
),
)
?
;
// If an allocation is created in an another const,
// we don't deallocate it.
let
(
alloc_id
,
_
,
_
)
=
ecx
.memory
.ptr_get_alloc
(
ptr
)
?
;
let
is_allocated_in_another_const
=
matches!
(
ecx
.tcx
.get_global_alloc
(
alloc_id
),
Some
(
interpret
::
GlobalAlloc
::
Memory
(
_
))
);
if
!
is_allocated_in_another_const
{
ecx
.memory
.deallocate
(
ptr
,
Some
((
size
,
align
)),
interpret
::
MemoryKind
::
Machine
(
MemoryKind
::
Heap
),
)
?
;
}
}
_
=>
{
return
Err
(
ConstEvalErrKind
::
NeedsRfc
(
format!
(
...
...
library/core/src/intrinsics.rs
浏览文件 @
29932db0
...
...
@@ -1914,12 +1914,13 @@ pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {
#[rustc_const_unstable(feature
=
"const_raw_ptr_comparison"
,
issue
=
"53020"
)]
pub
fn
ptr_guaranteed_ne
<
T
>
(
ptr
:
*
const
T
,
other
:
*
const
T
)
->
bool
;
/// Allocate at compile time. Should not be called at runtime.
/// Allocate at compile time.
/// Returns a null pointer at runtime.
#[rustc_const_unstable(feature
=
"const_heap"
,
issue
=
"79597"
)]
pub
fn
const_allocate
(
size
:
usize
,
align
:
usize
)
->
*
mut
u8
;
/// Deallocate a memory which allocated by `intrinsics::const_allocate` at compile time.
///
Should not be called
at runtime.
///
Does nothing
at runtime.
#[rustc_const_unstable(feature
=
"const_heap"
,
issue
=
"79597"
)]
#[cfg(not(bootstrap))]
pub
fn
const_deallocate
(
ptr
:
*
mut
u8
,
size
:
usize
,
align
:
usize
);
...
...
library/core/tests/intrinsics.rs
浏览文件 @
29932db0
...
...
@@ -80,3 +80,16 @@ fn test_hints_in_const_contexts() {
assert
!
(
42u32
==
core
::
hint
::
black_box
(
42u32
));
}
}
#[cfg(not(bootstrap))]
#[test]
fn
test_const_dealocate_at_runtime
()
{
use
core
::
intrinsics
::
const_deallocate
;
const
X
:
&
u32
=
&
42u32
;
let
x
=
&
0u32
;
unsafe
{
const_deallocate
(
X
as
*
const
_
as
*
mut
u8
,
4
,
4
);
// nop
const_deallocate
(
x
as
*
const
_
as
*
mut
u8
,
4
,
4
);
// nop
const_deallocate
(
core
::
ptr
::
null_mut
(),
1
,
1
);
// nop
}
}
library/core/tests/lib.rs
浏览文件 @
29932db0
...
...
@@ -13,6 +13,7 @@
#![feature(const_bool_to_option)]
#![feature(const_cell_into_inner)]
#![feature(const_convert)]
#![feature(const_heap)]
#![feature(const_maybe_uninit_as_mut_ptr)]
#![feature(const_maybe_uninit_assume_init)]
#![feature(const_maybe_uninit_assume_init_read)]
...
...
src/test/ui/consts/const-eval/heap/alloc_intrinsic_errors.rs
浏览文件 @
29932db0
...
...
@@ -6,11 +6,10 @@
const
FOO
:
i32
=
foo
();
const
fn
foo
()
->
i32
{
unsafe
{
let
_
=
intrinsics
::
const_allocate
(
4
,
3
)
as
*
mut
i32
;
let
_
=
intrinsics
::
const_allocate
(
4
,
3
)
as
*
mut
i32
;
//~^ error: evaluation of constant value failed
}
1
}
fn
main
()
{}
src/test/ui/consts/const-eval/heap/alloc_intrinsic_errors.stderr
浏览文件 @
29932db0
...
...
@@ -4,7 +4,7 @@ error[E0080]: evaluation of constant value failed
LL | const FOO: i32 = foo();
| ----- inside `FOO` at $DIR/alloc_intrinsic_errors.rs:6:18
...
LL | let _ = intrinsics::const_allocate(4, 3) as *
mut i32;
LL | let _ = intrinsics::const_allocate(4, 3) as *mut i32;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| align has to be a power of 2, `3` is not a power of 2
...
...
src/test/ui/consts/const-eval/heap/dealloc_intrinsic.rs
浏览文件 @
29932db0
// run-pass
#![feature(core_intrinsics)]
#![feature(const_heap)]
#![feature(const_mut_refs)]
use
std
::
intrinsics
;
...
...
@@ -9,4 +10,27 @@
intrinsics
::
const_deallocate
(
ptr
,
4
,
4
);
};
fn
main
()
{}
const
Y
:
&
u32
=
unsafe
{
let
ptr
=
intrinsics
::
const_allocate
(
4
,
4
)
as
*
mut
u32
;
*
ptr
=
42
;
&*
ptr
};
const
Z
:
&
u32
=
&
42
;
const
_
Z
:
()
=
unsafe
{
let
ptr1
=
Y
as
*
const
_
as
*
mut
u8
;
intrinsics
::
const_deallocate
(
ptr1
,
4
,
4
);
// nop
intrinsics
::
const_deallocate
(
ptr1
,
2
,
4
);
// nop
intrinsics
::
const_deallocate
(
ptr1
,
4
,
2
);
// nop
let
ptr2
=
Z
as
*
const
_
as
*
mut
u8
;
intrinsics
::
const_deallocate
(
ptr2
,
4
,
4
);
// nop
intrinsics
::
const_deallocate
(
ptr2
,
2
,
4
);
// nop
intrinsics
::
const_deallocate
(
ptr2
,
4
,
2
);
// nop
};
fn
main
()
{
assert_eq!
(
*
Y
,
42
);
assert_eq!
(
*
Z
,
42
);
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录