nandi/rustnimpublic Fork
⑂ 04f5b3e
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

Fold `SOME_ARRAY.len()` in an array length

An array's length is part of its type in Rust and in Nim, so this is a
constant on both sides; Nim spells it without the parentheses.

Found by the dependency run, in a `[Feature; ARABIC_FEATURES.len()]`.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
nandithebull committed 2026-09-19T10:32:08-07:00 Browse files
04f5b3e parent: 62d5926
modified src/ty.rs +6 -0
@@ -622,6 +622,12 @@ pub fn const_expr(e: &syn::Expr) -> Result<String, String> {
622622 .ok_or_else(|| format!("`{nim}` in an array length needs a type argument"))??;
623623 Ok(format!("{}({})", nim, t.render()))
624624 }
625+ // `SOME_CONST_ARRAY.len()`. An array's length is part of its type in
626+ // both languages, so this is a constant on both sides -- Nim spells it
627+ // without the parentheses.
628+ Expr::MethodCall(m) if m.method == "len" && m.args.is_empty() => {
629+ Ok(format!("({}).len", const_expr(&m.receiver)?))
630+ }
625631 Expr::Cast(c) => const_expr(&c.expr),
626632 other => Err(format!(
627633 "array length is not a const expression rustnim can render: `{}`",
@@ -622,6 +622,12 @@ pub fn const_expr(e: &syn::Expr) -> Result<String, String> {
622 .ok_or_else(|| format!("`{nim}` in an array length needs a type argument"))??;622 .ok_or_else(|| format!("`{nim}` in an array length needs a type argument"))??;
623 Ok(format!("{}({})", nim, t.render()))623 Ok(format!("{}({})", nim, t.render()))
624 }624 }
625+ // `SOME_CONST_ARRAY.len()`. An array's length is part of its type in
626+ // both languages, so this is a constant on both sides -- Nim spells it
627+ // without the parentheses.
628+ Expr::MethodCall(m) if m.method == "len" && m.args.is_empty() => {
629+ Ok(format!("({}).len", const_expr(&m.receiver)?))
630+ }
625 Expr::Cast(c) => const_expr(&c.expr),631 Expr::Cast(c) => const_expr(&c.expr),
626 other => Err(format!(632 other => Err(format!(
627 "array length is not a const expression rustnim can render: `{}`",633 "array length is not a const expression rustnim can render: `{}`",
modified tests/cases/052-const-array-len.rs +6 -0
@@ -3,6 +3,7 @@
33 // rather than resolved to a number.
44 const BLOCK: usize = 4;
55 const DOUBLE: usize = BLOCK * 2;
6+const FEATURES: [u8; 3] = [1, 2, 3];
67
78 struct Oid;
89
@@ -45,4 +46,9 @@ fn main() {
4546 // output writes it with.
4647 let q: [u8; { Oid::MAX_SIZE }] = [2u8; Oid::MAX_SIZE];
4748 println!("{} {}", q.len(), sum(&q));
49+
50+ // The length of another const array. An array's length is part of its
51+ // type in both languages, so it is a constant on both sides.
52+ let r: [u8; FEATURES.len()] = [4u8; FEATURES.len()];
53+ println!("{} {}", r.len(), sum(&r));
4854 }
@@ -3,6 +3,7 @@
3 // rather than resolved to a number.3 // rather than resolved to a number.
4 const BLOCK: usize = 4;4 const BLOCK: usize = 4;
5 const DOUBLE: usize = BLOCK * 2;5 const DOUBLE: usize = BLOCK * 2;
6+const FEATURES: [u8; 3] = [1, 2, 3];
6 7
7 struct Oid;8 struct Oid;
8 9
@@ -45,4 +46,9 @@ fn main() {
45 // output writes it with.46 // output writes it with.
46 let q: [u8; { Oid::MAX_SIZE }] = [2u8; Oid::MAX_SIZE];47 let q: [u8; { Oid::MAX_SIZE }] = [2u8; Oid::MAX_SIZE];
47 println!("{} {}", q.len(), sum(&q));48 println!("{} {}", q.len(), sum(&q));
49+
50+ // The length of another const array. An array's length is part of its
51+ // type in both languages, so it is a constant on both sides.
52+ let r: [u8; FEATURES.len()] = [4u8; FEATURES.len()];
53+ println!("{} {}", r.len(), sum(&r));
48 }54 }