@@ -124,10 +124,11 @@ impl SyntacticAnalysis {
124124 let Some ( name) = fn_item. name ( ) else { continue } ;
125125 let fn_name = name. text ( ) . to_string ( ) ;
126126
127- if fn_name == "types" {
128- for ( name, ir) in TypeIrBuilder :: new ( & fn_item) . build ( ) {
129- file_ir. insert ( name, RuleIr :: Type ( ir) ) ;
130- }
127+ if fn_name. starts_with ( 't' ) {
128+ file_ir. insert (
129+ fn_name. clone ( ) ,
130+ RuleIr :: Type ( TypeIrBuilder :: new ( & fn_item) . build ( ) ) ,
131+ ) ;
131132 } else if fn_name. starts_with ( 'f' ) {
132133 if !cfg_matches_host ( & fn_item) {
133134 continue ;
@@ -531,35 +532,30 @@ impl<'a> TypeIrBuilder<'a> {
531532 Self { fn_item }
532533 }
533534
534- fn build ( & self ) -> Vec < ( String , TypeIr ) > {
535- let Some ( body) = self . fn_item . body ( ) else {
536- return Vec :: new ( ) ;
537- } ;
538- let mut results = Vec :: new ( ) ;
539- for stmt in body. syntax ( ) . descendants ( ) . filter_map ( ast:: LetStmt :: cast) {
540- let Some ( pat) = stmt. pat ( ) else { continue } ;
541- let pat_text = pat. syntax ( ) . text ( ) . to_string ( ) ;
542- if !pat_text. starts_with ( 't' ) {
543- continue ;
544- }
545- let Some ( ty) = stmt. ty ( ) else { continue } ;
546- let Some ( init) = stmt. initializer ( ) else {
547- continue ;
548- } ;
535+ fn build ( & self ) -> TypeIr {
536+ let ty = self
537+ . fn_item
538+ . ret_type ( )
539+ . and_then ( |rt| rt. ty ( ) )
540+ . expect ( "type rule must declare a return type" ) ;
541+ let ( is_refcount_pointer, is_unsafe_pointer) = pointer_flags ( & ty) ;
549542
550- let ( is_refcount_pointer, is_unsafe_pointer) = pointer_flags ( & ty) ;
551- results. push ( (
552- pat_text,
553- TypeIr {
554- init : init. syntax ( ) . text ( ) . to_string ( ) ,
555- type_info : TypeInfo {
556- ty : ty. syntax ( ) . text ( ) . to_string ( ) ,
557- is_refcount_pointer,
558- is_unsafe_pointer,
559- } ,
560- } ,
561- ) ) ;
543+ let body = self . fn_item . body ( ) . expect ( "type rule must have a body" ) ;
544+ let init = body
545+ . syntax ( )
546+ . descendants ( )
547+ . find_map ( ast:: ReturnExpr :: cast)
548+ . and_then ( |ret| ret. expr ( ) )
549+ . or_else ( || body. stmt_list ( ) . and_then ( |sl| sl. tail_expr ( ) ) )
550+ . expect ( "type rule must yield an initializer" ) ;
551+
552+ TypeIr {
553+ init : init. syntax ( ) . text ( ) . to_string ( ) ,
554+ type_info : TypeInfo {
555+ ty : ty. syntax ( ) . text ( ) . to_string ( ) ,
556+ is_refcount_pointer,
557+ is_unsafe_pointer,
558+ } ,
562559 }
563- results
564560 }
565561}
0 commit comments