@@ -1554,6 +1554,7 @@ def compare_strings(self, lhs: Value, rhs: Value, op: str, line: int) -> Value:
15541554 """Compare two strings"""
15551555 if op == "==" :
15561556 # We can specialize this case if one or both values are string literals
1557+ literal_fastpath = False
15571558
15581559 def is_string_literal (value : Value ) -> TypeGuard [LoadLiteral ]:
15591560 return isinstance (value , LoadLiteral ) and is_str_rprimitive (value .type )
@@ -1562,11 +1563,18 @@ def is_string_literal(value: Value) -> TypeGuard[LoadLiteral]:
15621563 if is_string_literal (rhs ):
15631564 # we can optimize out the check entirely in some constant-folded cases
15641565 return self .true () if lhs .value == rhs .value else self .false ()
1565- literal_length = Integer (len (lhs .value ), c_pyssize_t_rprimitive , line ) # type: ignore [arg-type]
1566- return self .primitive_op (str_eq_literal , [rhs , lhs , literal_length ], line )
1566+
1567+ # if lhs argument is string literal, switch sides to match specializer C api
1568+ lhs , rhs = rhs , lhs
1569+ literal_fastpath = True
15671570 elif is_string_literal (rhs ):
1568- literal_length = Integer (len (rhs .value ), c_pyssize_t_rprimitive , line ) # type: ignore [arg-type]
1571+ literal_fastpath = True
1572+
1573+ if literal_fastpath :
1574+ literal_string = cast (str , cast (LoadLiteral , rhs ).value )
1575+ literal_length = Integer (len (literal_string ), c_pyssize_t_rprimitive , line )
15691576 return self .primitive_op (str_eq_literal , [lhs , rhs , literal_length ], line )
1577+
15701578 return self .primitive_op (str_eq , [lhs , rhs ], line )
15711579
15721580 elif op == "!=" :
0 commit comments