- Feature Name: compile_time_asserts
- Start Date: 2015-07-30
- RFC PR: rust-lang/rfcs#1229
- Rust Issue: rust-lang/rust#28238
Summary
If the constant
Definition定義 of constant定数 evaluation評価 context文脈、背景
There are exactly
- the initializer of a constant定数
const foo: ty = EXPR
orstatic foo: ty = EXPR
- the size of an array配列
[T; EXPR]
- the length of a repeat expression式
[VAL; LEN_EXPR]
- C-Like enum variant discriminant values
- patterns
In the future the body of const fn
might also be interpreted
Any other expression
Motivation
Expressions
For example
will not cause5 << 42
will. If the constantblub
function. This would be a breaking change, since the code would not compile anymore. (this occurred
Detailed design設計(する)
The PRs https://github.com/rust-lang/rust/pull/26848 and https://github.com/rust-lang/rust/pull/25570 will be setting
When the constant
Drawbacks
None, if we don't do anything, the const evaluator cannot get much smarter.
Alternatives代わりのもの、選択肢
allow breaking changes
Let the compiler error on things that will unconditionally panic at runtime.
insert挿入する an unconditional panic instead of generating生成する regular普通の、正規の code
GNAT (an Ada compiler) does this already:
procedure Hello is
Var: Integer range 15 .. 20 := 21;
begin
null;
end Hello;
The anonymousInteger range 15 .. 20
only accepts[15, 20]
. This knowledge is used by GNAT to emit the following warning during compilation:
warning: value not in range of subtype of "Standard.Integer" defined at line 2
warning: "Constraint_Error" will be raised at run time
I don't have a GNAT with -emit-llvm
handy, but here's the asm with -O0
:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movl $2, %esi
movl $.LC0, %edi
movl $0, %eax
call __gnat_rcheck_CE_Range_Check
Unresolved questions
Const-eval the body of const fn
that are never used in a constant定数 environment環境
Currently a const fn
that is called
In case there is a statically known erroneous
The same applies