Subject: bug#49809: [PATCH] Add macro 'pcase-setq'

From: Stefan Monnier <[email-masked]>
Date: Thu, 12 Aug 2021 12:13:58 -0400

> Undefined behavior has no use per se, unless it has a larger impact on
> efficiency.

There is indeed a significant difference in efficiency.
E.g. the code for (pcase-let ((`(,x ,y) (EXP))) (VAL x y)) looks like:

    byte code:
      args: nil
    0       constant  EXP
    1       call      0
    2       dup       
    3       car-safe  
    4       stack-ref 1
    5       cdr-safe  
    6       dup       
    7       car-safe  
    8       stack-ref 1
    9       cdr-safe  
    10      stack-ref 3
    11      stack-ref 2
    12      constant  VAL
    13      stack-ref 2
    14      stack-ref 2
    15      call      2
    16      return    

Instead of:

    byte code:
      args: nil
    0       constant  EXP
    1       call      0
    2       dup       
    3       consp     
    4       goto-if-nil-else-pop 3
    7       dup       
    8       car-safe  
    9       stack-ref 1
    10      cdr-safe  
    11      dup       
    12      consp     
    13      goto-if-nil-else-pop 2
    16      dup       
    17      car-safe  
    18      stack-ref 1
    19      cdr-safe  
    20      dup       
    21      not       
    22      goto-if-nil-else-pop 1
    25      stack-ref 3
    26      stack-ref 2
    27      constant  VAL
    28      stack-ref 2
    29      stack-ref 2
    30      call      2
    31      discardN-preserve-tos 2
    33:1    discardN-preserve-tos 2
    35:2    discardN-preserve-tos 2
    37:3    return    

For a semantic where the let is skipped when the pattern fails to match
and

    byte code:
      args: nil
    0       constant  EXP
    1       call      0
    2       dup       
    3       consp     
    4       goto-if-nil 3
    7       dup       
    8       car-safe  
    9       stack-ref 1
    10      cdr-safe  
    11      dup       
    12      consp     
    13      goto-if-nil 2
    16      dup       
    17      car-safe  
    18      stack-ref 1
    19      cdr-safe  
    20      dup       
    21      goto-if-nil 1
    24      stack-ref 4
    25      constant  error
    26      constant  "No clause matching `%S'"
    27      stack-ref 2
    28      call      2
    29      return    
    30:1    stack-ref 3
    31      stack-ref 2
    32      constant  VAL
    33      stack-ref 2
    34      stack-ref 2
    35      call      2
    36      return    
    37:2    stack-ref 2
    38      constant  error
    39      constant  "No clause matching `%S'"
    40      stack-ref 2
    41      call      2
    42      return    
    43:3    dup       
    44      constant  error
    45      constant  "No clause matching `%S'"
    46      stack-ref 2
    47      call      2
    48      return    

For the semantics where we signal an error when the pattern fails to match.

> And why again do you think it is not a good idea?

Because if you want different behaviors when the pattern matches and
when it doesn't, you should use `pcase`.

        Stefan