Skip to content

Commit a429b09

Browse files
committed
Allow match_class match expressions without parens around the target
1 parent dfeeda1 commit a429b09

File tree

11 files changed

+38
-22
lines changed

11 files changed

+38
-22
lines changed

vm/src/function.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ pub fn single_or_tuple_any<T: PyValue, F: Fn(PyRef<T>) -> PyResult<bool>>(
559559
message: fn(&PyObjectRef) -> String,
560560
vm: &VirtualMachine,
561561
) -> PyResult<bool> {
562-
match_class!(match (obj) {
562+
match_class!(match obj {
563563
obj @ T => predicate(obj),
564564
tuple @ PyTuple => {
565565
for obj in tuple.elements.iter() {

vm/src/macros.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ macro_rules! match_class {
237237
$default
238238
}};
239239

240+
// An arm taken when the object is an instance of the specified built-in
241+
// class and binding the downcasted object to the specified identifier and
242+
// the target expression is a block.
240243
(match ($obj:expr) { $binding:ident @ $class:ty => $expr:block $($rest:tt)* }) => {
241244
$crate::match_class!(match ($obj) { $binding @ $class => ($expr), $($rest)* })
242245
};
@@ -250,7 +253,9 @@ macro_rules! match_class {
250253
}
251254
};
252255

253-
(match ($obj:expr) { $class:ty => $expr:block, $($rest:tt)* }) => {
256+
// An arm taken when the object is an instance of the specified built-in
257+
// class and the target expression is a block.
258+
(match ($obj:expr) { $class:ty => $expr:block $($rest:tt)* }) => {
254259
$crate::match_class!(match ($obj) { $class => ($expr), $($rest)* })
255260
};
256261

@@ -263,6 +268,17 @@ macro_rules! match_class {
263268
$crate::match_class!(match ($obj) { $($rest)* })
264269
}
265270
};
271+
272+
// To allow match expressions without parens around the match target
273+
(match $($rest:tt)*) => {
274+
$crate::match_class!(@parse_match () ($($rest)*))
275+
};
276+
(@parse_match ($($target:tt)*) ({ $($inner:tt)* })) => {
277+
$crate::match_class!(match ($($target)*) { $($inner)* })
278+
};
279+
(@parse_match ($($target:tt)*) ($next:tt $($rest:tt)*)) => {
280+
$crate::match_class!(@parse_match ($($target)* $next) ($($rest)*))
281+
};
266282
}
267283

268284
/// Super detailed logging. Might soon overflow your logbuffers

vm/src/obj/objbyteinner.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub struct PyByteInner {
3737

3838
impl TryFromObject for PyByteInner {
3939
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
40-
match_class!(match (obj) {
40+
match_class!(match obj {
4141
i @ PyBytes => Ok(PyByteInner {
4242
elements: i.get_value().to_vec()
4343
}),
@@ -115,7 +115,7 @@ impl ByteInnerNewOptions {
115115
// Only one argument
116116
} else {
117117
let value = if let OptionalArg::Present(ival) = self.val_option {
118-
match_class!(match (ival.clone()) {
118+
match_class!(match ival.clone() {
119119
i @ PyInt => {
120120
let size = objint::get_value(&i.into_object()).to_usize().unwrap();
121121
Ok(vec![0; size])
@@ -466,7 +466,7 @@ impl PyByteInner {
466466

467467
fn setindex(&mut self, int: PyIntRef, object: PyObjectRef, vm: &VirtualMachine) -> PyResult {
468468
if let Some(idx) = self.elements.get_pos(int.as_bigint().to_i32().unwrap()) {
469-
let result = match_class!(match (object) {
469+
let result = match_class!(match object {
470470
i @ PyInt => {
471471
if let Some(value) = i.as_bigint().to_u8() {
472472
Ok(value)
@@ -498,7 +498,7 @@ impl PyByteInner {
498498
.map(|obj| u8::try_from_object(vm, obj))
499499
.collect::<PyResult<Vec<_>>>()?)
500500
}
501-
_ => match_class!(match (object) {
501+
_ => match_class!(match object {
502502
i @ PyMemoryView => Ok(i.get_obj_value().unwrap()),
503503
_ => Err(vm.new_index_error(
504504
"can assign only bytes, buffers, or iterables of ints in range(0, 256)"
@@ -1149,7 +1149,7 @@ impl PyByteInner {
11491149
}
11501150

11511151
pub fn try_as_byte(obj: &PyObjectRef) -> Option<Vec<u8>> {
1152-
match_class!(match (obj.clone()) {
1152+
match_class!(match obj.clone() {
11531153
i @ PyBytes => Some(i.get_value().to_vec()),
11541154
j @ PyByteArray => Some(j.inner.borrow().elements.to_vec()),
11551155
_ => None,

vm/src/obj/objint.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ impl PyInt {
587587
let mut signed = false;
588588
for (key, value) in kwargs.into_iter() {
589589
if key == "signed" {
590-
signed = match_class!(match (value) {
590+
signed = match_class!(match value {
591591
b @ PyInt => !b.as_bigint().is_zero(),
592592
_ => false,
593593
});
@@ -624,7 +624,7 @@ impl PyInt {
624624
let value = self.as_bigint();
625625
for (key, value) in kwargs.into_iter() {
626626
if key == "signed" {
627-
signed = match_class!(match (value) {
627+
signed = match_class!(match value {
628628
b @ PyInt => !b.as_bigint().is_zero(),
629629
_ => false,
630630
});
@@ -742,7 +742,7 @@ pub fn to_int(vm: &VirtualMachine, obj: &PyObjectRef, base: u32) -> PyResult<Big
742742
return Err(vm.new_value_error("int() base must be >= 2 and <= 36, or 0".to_string()));
743743
}
744744

745-
match_class!(match (obj.clone()) {
745+
match_class!(match obj.clone() {
746746
string @ PyString => {
747747
let s = string.value.as_str().trim();
748748
str_to_int(vm, s, base)

vm/src/obj/objrange.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ pub enum RangeIndex {
425425

426426
impl TryFromObject for RangeIndex {
427427
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
428-
match_class!(match (obj) {
428+
match_class!(match obj {
429429
i @ PyInt => Ok(RangeIndex::Int(i)),
430430
s @ PySlice => Ok(RangeIndex::Slice(s)),
431431
obj => Err(vm.new_type_error(format!(

vm/src/obj/objsequence.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ pub enum SequenceIndex {
168168

169169
impl TryFromObject for SequenceIndex {
170170
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
171-
match_class!(match (obj) {
171+
match_class!(match obj {
172172
i @ PyInt => Ok(SequenceIndex::Int(i32::try_from_object(
173173
vm,
174174
i.into_object()
@@ -471,7 +471,7 @@ pub fn is_valid_slice_arg(
471471
vm: &VirtualMachine,
472472
) -> Result<Option<BigInt>, PyObjectRef> {
473473
if let OptionalArg::Present(value) = arg {
474-
match_class!(match (value) {
474+
match_class!(match value {
475475
i @ PyInt => Ok(Some(i.as_bigint().clone())),
476476
_obj @ PyNone => Ok(None),
477477
_ => Err(vm.new_type_error(

vm/src/stdlib/collections.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ impl PyDeque {
233233
return Ok(vm.new_bool(true));
234234
}
235235

236-
let other = match_class!(match (other) {
236+
let other = match_class!(match other {
237237
other @ Self => other,
238238
_ => return Ok(vm.ctx.not_implemented()),
239239
});
@@ -251,7 +251,7 @@ impl PyDeque {
251251
return Ok(vm.new_bool(true));
252252
}
253253

254-
let other = match_class!(match (other) {
254+
let other = match_class!(match other {
255255
other @ Self => other,
256256
_ => return Ok(vm.ctx.not_implemented()),
257257
});
@@ -269,7 +269,7 @@ impl PyDeque {
269269
return Ok(vm.new_bool(true));
270270
}
271271

272-
let other = match_class!(match (other) {
272+
let other = match_class!(match other {
273273
other @ Self => other,
274274
_ => return Ok(vm.ctx.not_implemented()),
275275
});
@@ -287,7 +287,7 @@ impl PyDeque {
287287
return Ok(vm.new_bool(true));
288288
}
289289

290-
let other = match_class!(match (other) {
290+
let other = match_class!(match other {
291291
other @ Self => other,
292292
_ => return Ok(vm.ctx.not_implemented()),
293293
});
@@ -305,7 +305,7 @@ impl PyDeque {
305305
return Ok(vm.new_bool(true));
306306
}
307307

308-
let other = match_class!(match (other) {
308+
let other = match_class!(match other {
309309
other @ Self => other,
310310
_ => return Ok(vm.ctx.not_implemented()),
311311
});

vm/src/stdlib/io.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ fn file_io_write(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
424424
//to support windows - i.e. raw file_handles
425425
let mut handle = os::rust_file(raw_fd);
426426

427-
let bytes = match_class!(match (obj.clone()) {
427+
let bytes = match_class!(match obj.clone() {
428428
i @ PyBytes => Ok(i.get_value().to_vec()),
429429
j @ PyByteArray => Ok(j.inner.borrow().elements.to_vec()),
430430
obj => Err(vm.new_type_error(format!(

vm/src/stdlib/json.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub fn json_dump(obj: PyObjectRef, fs: PyObjectRef, vm: &VirtualMachine) -> PyRe
2121

2222
/// Implement json.loads
2323
pub fn json_loads(obj: PyObjectRef, vm: &VirtualMachine) -> PyResult {
24-
let de_result = match_class!(match (obj) {
24+
let de_result = match_class!(match obj {
2525
s @ PyString => {
2626
py_serde::deserialize(vm, &mut serde_json::Deserializer::from_str(s.as_str()))
2727
}

vm/src/stdlib/re.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ impl PyMatch {
392392
}
393393

394394
fn get_bounds(&self, id: PyObjectRef, vm: &VirtualMachine) -> PyResult<Option<(usize, usize)>> {
395-
match_class!(match (id) {
395+
match_class!(match id {
396396
i @ PyInt => {
397397
let i = usize::try_from_object(vm, i.into_object())?;
398398
match self.captures.get(i) {

vm/src/vm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,7 @@ impl VirtualMachine {
974974
}
975975

976976
pub fn is_callable(&self, obj: &PyObjectRef) -> bool {
977-
match_class!(match (obj) {
977+
match_class!(match obj {
978978
PyFunction => true,
979979
PyMethod => true,
980980
PyBuiltinFunction => true,

0 commit comments

Comments
 (0)