pattern-matching - 匹配表达式落空?

标签 pattern-matching rust

更新:这是一个老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/

相关文章:

rust - Rust说不使用导入,并且无法同时找到导入的语句

rest - 为什么warp对于错误键入的参数返回“HTTP 405方法不允许”?

rust - 如何修改我的构造函数以接受切片或对数组或向量的引用

syntax - 如何在 'let' 定义中使用模式匹配?

rust - 是否有更简单的方法来获取 Actix-Web HTTP header 的字符串值?

scala - 整数范围的模式匹配

java - 快速将字符串与 Java 中的集合进行比较

validation - 如何在Rust和loop中验证用户输入,直到给出有效输入?

python - 如何在 Python 中创建包含常规变量和一些固定文本的正则表达式?

haskell - 在模式保护或 case 表达式中重用模式