nandi/rustnimpublic Fork
⑂ 08bebb7
Commits
⬇ Clone ▾
git clone https://git.rickub.com/nandi/rustnim.git
git clone ssh://git@rickub.com/nandi/rustnim.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

Download ZIP

Support the rest of the const-foldable operators in an array length

Comparison, bitwise and logical operators, which `!Self::COMPRESS && false`
and `(N << 2) | 1` need. Nim spells the bitwise and logical ones with the same
keywords where Rust gives two symbols each, and which is meant follows from
the operand types there exactly as it does here.

The catch-all now names the operator it refused instead of listing the five it
accepts.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
nandithebull committed 2026-09-19T13:16:14-07:00 Browse files
08bebb7 parent: 8867e55
modified src/ty.rs +18 -3
@@ -613,10 +613,25 @@ pub fn const_expr(e: &syn::Expr) -> Result<String, String> {
613613 // Rust's `/` and `%` on integers are Nim's `div` and `mod`.
614614 BinOp::Div(_) => "div",
615615 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(_) => ">=",
616630 _ => {
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+ ))
620635 }
621636 };
622637 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
@@ -37,6 +37,9 @@ struct Table {
3737 impl Table {
3838 const WIDE: bool = false;
3939 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;
4043 }
4144
4245 struct Buf {
@@ -72,6 +75,10 @@ fn main() {
7275
7376 // An associated const as a length, and in the braces rustc's expanded
7477 // 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+
7582 let tb = Table { data: [1u8; if Table::WIDE { 8 } else { Table::N * 2 }] };
7683 println!("{} {}", tb.data.len(), sum(&tb.data));
7784
@@ -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 expanded76 // 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