▾ modified src/ty.rs +18 -3 Viewed
@@ -613,10 +613,25 @@ pub fn const_expr(e: &syn::Expr) -> Result<String, String> { 613 613 // Rust's `/` and `%` on integers are Nim's `div` and `mod`. 614 614 BinOp ::Div ( _ ) = > " div " , 615 615 BinOp ::Rem ( _ ) = > " mod " , 616 + // Nim spells the bitwise and logical operators with the same 617 + // keywords Rust gives two symbols each; which one is meant 618 + // follows from the operand types, exactly as it does in Rust. 619 + BinOp ::And ( _ ) | BinOp ::BitAnd ( _ ) = > " and " , 620 + BinOp ::Or ( _ ) | BinOp ::BitOr ( _ ) = > " or " , 621 + BinOp ::BitXor ( _ ) = > " xor " , 622 + BinOp ::Shl ( _ ) = > " shl " , 623 + BinOp ::Shr ( _ ) = > " shr " , 624 + BinOp ::Eq ( _ ) = > " == " , 625 + BinOp ::Ne ( _ ) = > " != " , 626 + BinOp ::Lt ( _ ) = > " < " , 627 + BinOp ::Le ( _ ) = > " <= " , 628 + BinOp ::Gt ( _ ) = > " > " , 629 + BinOp ::Ge ( _ ) = > " >= " , 616 630 _ = > { 617 - return Err ( 618 - " only `+ - * / %` are supported in an array length " . into ( ) 619 - ) 631 + return Err ( format! ( 632 + " operator ` {} ` is not supported in an array length " , 633 + quote ::quote! ( #e ) 634 + ) ) 620 635 } 621 636 } ; 622 637 Ok ( format! (
@@ -613,10 +613,25 @@ pub fn const_expr(e: &syn::Expr) -> Result<String, String> { 613 // Rust's `/` and `%` on integers are Nim's `div` and `mod`. 613 // Rust's `/` and `%` on integers are Nim's `div` and `mod`. 614 BinOp ::Div ( _ ) = > " div " , 614 BinOp ::Div ( _ ) = > " div " , 615 BinOp ::Rem ( _ ) = > " mod " , 615 BinOp ::Rem ( _ ) = > " mod " , 616 + // Nim spells the bitwise and logical operators with the same 617 + // keywords Rust gives two symbols each; which one is meant 618 + // follows from the operand types, exactly as it does in Rust. 619 + BinOp ::And ( _ ) | BinOp ::BitAnd ( _ ) = > " and " , 620 + BinOp ::Or ( _ ) | BinOp ::BitOr ( _ ) = > " or " , 621 + BinOp ::BitXor ( _ ) = > " xor " , 622 + BinOp ::Shl ( _ ) = > " shl " , 623 + BinOp ::Shr ( _ ) = > " shr " , 624 + BinOp ::Eq ( _ ) = > " == " , 625 + BinOp ::Ne ( _ ) = > " != " , 626 + BinOp ::Lt ( _ ) = > " < " , 627 + BinOp ::Le ( _ ) = > " <= " , 628 + BinOp ::Gt ( _ ) = > " > " , 629 + BinOp ::Ge ( _ ) = > " >= " , 616 _ = > { 630 _ = > { 617 - return Err ( 631 + return Err ( format! ( 618 - " only `+ - * / %` are supported in an array length " . into ( ) 632 + " operator ` {} ` is not supported in an array length " , 619 - ) 633 + quote ::quote! ( #e ) 634 + ) ) 620 } 635 } 621 } ; 636 } ; 622 Ok ( format! ( 637 Ok ( format! (
▾ modified tests/cases/052-const-array-len.rs +7 -0 Viewed
@@ -37,6 +37,9 @@ struct Table { 37 37 impl Table { 38 38 const WIDE: bool = false; 39 39 const N: usize = 3; 40 + // The rest of the const-foldable operators. 41 + const SPLIT: bool = !Self::WIDE && Self::N > 2; 42 + const MASK: usize = (Self::N << 2) | 1; 40 43 } 41 44 42 45 struct Buf { @@ -72,6 +75,10 @@ fn main() { 72 75 73 76 // An associated const as a length, and in the braces rustc's expanded 74 77 // output writes it with. 78 + let sp: [u8; if Table::SPLIT { Table::MASK } else { 1 }] = 79 + [3u8; if Table::SPLIT { Table::MASK } else { 1 }]; 80 + println!("{} {}", sp.len(), sum(&sp)); 81 + 75 82 let tb = Table { data: [1u8; if Table::WIDE { 8 } else { Table::N * 2 }] }; 76 83 println!("{} {}", tb.data.len(), sum(&tb.data)); 77 84
@@ -37,6 +37,9 @@ struct Table { 37 impl Table { 37 impl Table { 38 const WIDE: bool = false; 38 const WIDE: bool = false; 39 const N: usize = 3; 39 const N: usize = 3; 40 + // The rest of the const-foldable operators. 41 + const SPLIT: bool = !Self::WIDE && Self::N > 2; 42 + const MASK: usize = (Self::N << 2) | 1; 40 } 43 } 41 44 42 struct Buf { 45 struct Buf { @@ -72,6 +75,10 @@ fn main() { 72 75 73 // An associated const as a length, and in the braces rustc's expanded 76 // An associated const as a length, and in the braces rustc's expanded 74 // output writes it with. 77 // output writes it with. 78 + let sp: [u8; if Table::SPLIT { Table::MASK } else { 1 }] = 79 + [3u8; if Table::SPLIT { Table::MASK } else { 1 }]; 80 + println!("{} {}", sp.len(), sum(&sp)); 81 + 75 let tb = Table { data: [1u8; if Table::WIDE { 8 } else { Table::N * 2 }] }; 82 let tb = Table { data: [1u8; if Table::WIDE { 8 } else { Table::N * 2 }] }; 76 println!("{} {}", tb.data.len(), sum(&tb.data)); 83 println!("{} {}", tb.data.len(), sum(&tb.data)); 77 84