▾ modified src/ty.rs +6 -0 Viewed
@@ -622,6 +622,12 @@ pub fn const_expr(e: &syn::Expr) -> Result<String, String> { 622 622 . ok_or_else ( | | format! ( " ` {nim} ` in an array length needs a type argument " ) ) ? ? ; 623 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 631 Expr ::Cast ( c ) = > const_expr ( & c . expr ) , 626 632 other = > Err ( format! ( 627 633 " 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 Viewed
@@ -3,6 +3,7 @@ 3 3 // rather than resolved to a number. 4 4 const BLOCK : usize = 4 ; 5 5 const DOUBLE : usize = BLOCK * 2 ; 6 + const FEATURES : [ u8 ; 3 ] = [ 1 , 2 , 3 ] ; 6 7 7 8 struct Oid ; 8 9 @@ -45,4 +46,9 @@ fn main() { 45 46 // output writes it with. 46 47 let q : [ u8 ; { Oid ::MAX_SIZE } ] = [ 2 u8 ; Oid ::MAX_SIZE ] ; 47 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 ( ) ] = [ 4 u8 ; FEATURES . len ( ) ] ; 53 + println! ( " {} {} " , r . len ( ) , sum ( & r ) ) ; 48 54 }
@@ -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 } ] = [ 2 u8 ; Oid ::MAX_SIZE ] ; 47 let q : [ u8 ; { Oid ::MAX_SIZE } ] = [ 2 u8 ; 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 ( ) ] = [ 4 u8 ; FEATURES . len ( ) ] ; 53 + println! ( " {} {} " , r . len ( ) , sum ( & r ) ) ; 48 } 54 }