我有一个由几个变体组成的枚举:
enum Foo {
A,
B,
C,
}
我有一个函数,里面有if和Match循环,它的所有分支都是发散的:
fn my_function() -> i32 {
// some vars
for ... in ... { // also may be `while` or `loop`
// some code
if some_condition {
let foo: Foo = ...;
match foo {
Foo::A => { return 42 }
Foo::B => { continue }
Foo::C => {
// do something with vars
continue
}
}
// this place is currently unreachable
// and must **always** be **totally** unreachable
// because the code after `if` must never execute in this(`some_condition`) case
}
// some code
}
}
但随着将来的一些更改,我可能会向枚举添加新的变体(S),所以我想compilation次判断这条线是不是无法到达的.我觉得这应该是可能的,但我不知道怎么做.
《之前的艺术》:
-
unreachable!
对我不起作用,因为这是一个运行时判断,在这段代码执行之前需要很多时间,而且不能保证它会命中这个unreachable,因为在到达那里的过程中涉及到rng. -
compile_error!
为cfg标志,在这种情况下,显然总是给出错误,而不是依赖于该位置的可达性. -
!
-type -不确定是否可以使用never-type来解决这个问题.
如果有像const_unreachable!
或soft_compile_error!
这样的东西来填补这个确切的场景,当且仅当存在导致编译错误的执行路径时,就好了……
那么有什么可能的解决方案(包括每晚Rust 和任何特征)来解决这个问题呢?