更新:这是一个老bug solved在 1.12
这是一些人为但简单的模式匹配示例 (demo) :
fn main() {
let x = 'a';
match x {
'a'...'b' if false => {
println!("one");
},
'a' => {
println!("two");
},
'a'...'b' => {
println!("three");
},
_ => panic!("what?")
}
}
当我运行它时,我得到 three
作为输出。为什么会这样?不是应该先匹配第二个分支吗?
最佳答案
在Debug中查看LLVM IR,已经有缺陷,所以这绝对是rustc
中的一个bug ;我们将使用下面的精选 IR 来检查发生了什么。
所以,%x
被分配为 'a'(ASCII 中的 97),并且 %10
分配给 x >= 'a' and x <= 'b'
的结果;如果这是真的,我们去match_case
, 否则为 compare_next
. match_case
重定向到 cond
, 重定向到 case_body2
打印"three"
.
理论上,我们本来想去case_body1
(打印 "two"
),来自 cond7
, 来自 match_case4
, 来自 compare_next
.但是compare_next
只有在 x
时才能达到不在 ['a', 'b']
中根据 IR。
这显然看起来像一个错误。
; Function Attrs: uwtable
define internal void @_ZN4main20h4f7b0d7962de19d8eaaE() unnamed_addr #0 {
entry-block:
%x = alloca i32
; [...]
store i32 97, i32* %x
%7 = load i32* %x, !range !0
%8 = icmp uge i32 %7, 97
%9 = icmp ule i32 %7, 98
%10 = and i1 %8, %9
br i1 %10, label %match_case, label %compare_next
case_body: ; preds = %next6, %next
; println!("one")
br label %case_body8
case_body1: ; preds = %cond7
; println!("two")
br label %case_body10
case_body2: ; preds = %cond
; println!("three")
br label %case_body15
case_body3: ; preds = %match_else
; panic!("what")
unreachable
match_else: ; preds = %compare_next5
br label %case_body3
match_case: ; preds = %entry-block
br i1 true, label %cond, label %next
compare_next: ; preds = %entry-block
%16 = icmp eq i32 %7, 97
br i1 %16, label %match_case4, label %compare_next5
next: ; preds = %match_case
br label %case_body
cond: ; preds = %match_case
br label %case_body2
match_case4: ; preds = %compare_next
br i1 true, label %cond7, label %next6
; [...]
cond7: ; preds = %match_case4
br label %case_body1
; [...]
}
关于pattern-matching - 匹配表达式落空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30808882/