@@ -25,8 +25,14 @@ use crate::ast::node::RangeTrait;
2525
2626#[ node]
2727pub struct AsNode {
28+ /// expr is the expression which calculates a value as output
2829 pub expr : Box < NodeEnum > ,
29- pub ty : Box < TypeNodeEnum > ,
30+
31+ /// target_type refers the desired type for the expr
32+ pub target_type : Box < TypeNodeEnum > ,
33+
34+ /// tail is the question or exclaimation mark following an 'as' statement
35+ /// it will be None if no marks exist
3036 pub tail : Option < ( TokenType , Range ) > ,
3137}
3238
@@ -37,10 +43,10 @@ impl Node for AsNode {
3743 builder : & ' b BuilderEnum < ' a , ' _ > ,
3844 ) -> NodeResult {
3945 let re = self . expr . emit ( ctx, builder) ;
40- self . ty . emit_highlight ( ctx) ;
46+ self . target_type . emit_highlight ( ctx) ;
4147 let v = re?. get_value ( ) ;
4248 let v = v. unwrap ( ) ;
43- let target_tp = self . ty . get_type ( ctx, builder, true ) ?;
49+ let target_tp = self . target_type . get_type ( ctx, builder, true ) ?;
4450 let ( val, target_tp) = ctx. force_cast_safe (
4551 v. get_value ( ) ,
4652 & v. get_ty ( ) . borrow ( ) ,
@@ -58,7 +64,7 @@ impl PrintTrait for AsNode {
5864 tab ( tabs, line. clone ( ) , end) ;
5965 println ! ( "AsNode" ) ;
6066 self . expr . print ( tabs + 1 , false , line. clone ( ) ) ;
61- self . ty . print ( tabs + 1 , true , line. clone ( ) ) ;
67+ self . target_type . print ( tabs + 1 , true , line. clone ( ) ) ;
6268 }
6369}
6470
@@ -88,14 +94,14 @@ impl<'a, 'ctx> Ctx<'a> {
8894 }
8995 ( PLType :: Union ( union) , target_ty) => {
9096 if node. tail . is_none ( ) {
91- let pos = node. ty . range ( ) . end ;
97+ let pos = node. target_type . range ( ) . end ;
9298 let end_range = Range {
9399 start : pos,
94100 end : pos,
95101 } ;
96102 return Err ( node. range . new_err ( ErrorCode :: INVALID_DIRECT_UNION_CAST )
97103 . add_label ( node. expr . range ( ) , self . get_file ( ) , format_label ! ( "type of the expression is `{}`" , & union . name) )
98- . add_label ( node. ty . range ( ) , self . get_file ( ) , format_label ! ( "target type is `{}`" , target_ty. get_name( ) ) )
104+ . add_label ( node. target_type . range ( ) , self . get_file ( ) , format_label ! ( "target type is `{}`" , target_ty. get_name( ) ) )
99105 . add_label ( end_range, self . get_file ( ) , format_label ! ( "add `{}` or `{}` to make it legal" , "?" , "!" ) )
100106 . add_help ( "cast a union to its member type directly is not allowed, use `?` or `!` after the cast expression" )
101107 . add_to_ctx ( self ) ) ;
@@ -124,7 +130,7 @@ impl<'a, 'ctx> Ctx<'a> {
124130 format_label ! ( "type of the expression is `{}`" , & union . name) ,
125131 )
126132 . add_label (
127- node. ty . range ( ) ,
133+ node. target_type . range ( ) ,
128134 self . get_file ( ) ,
129135 format_label ! ( "target type is `{}`" , target_ty. get_name( ) ) ,
130136 )
@@ -137,14 +143,14 @@ impl<'a, 'ctx> Ctx<'a> {
137143 }
138144 ( PLType :: Trait ( t) , target_ty) => {
139145 if node. tail . is_none ( ) {
140- let pos = node. ty . range ( ) . end ;
146+ let pos = node. target_type . range ( ) . end ;
141147 let end_range = Range {
142148 start : pos,
143149 end : pos,
144150 } ;
145151 return Err ( node. range . new_err ( ErrorCode :: INVALID_DIRECT_TRAIT_CAST )
146152 . add_label ( node. expr . range ( ) , self . get_file ( ) , format_label ! ( "type of the expression is `{}`" , & t. name) )
147- . add_label ( node. ty . range ( ) , self . get_file ( ) , format_label ! ( "target type is `{}`" , target_ty. get_name( ) ) )
153+ . add_label ( node. target_type . range ( ) , self . get_file ( ) , format_label ! ( "target type is `{}`" , target_ty. get_name( ) ) )
148154 . add_label ( end_range, self . get_file ( ) , format_label ! ( "add `{}` or `{}` to make it legal" , "?" , "!" ) )
149155 . add_help ( "cast a trait to specific type directly is not allowed, use `?` or `!` after the cast expression" )
150156 . add_to_ctx ( self ) ) ;
@@ -161,7 +167,7 @@ impl<'a, 'ctx> Ctx<'a> {
161167 . up_cast (
162168 target_ty. clone ( ) ,
163169 Arc :: new ( RefCell :: new ( ty. clone ( ) ) ) ,
164- node. ty . range ( ) ,
170+ node. target_type . range ( ) ,
165171 node. expr . range ( ) ,
166172 val,
167173 builder,
@@ -175,7 +181,7 @@ impl<'a, 'ctx> Ctx<'a> {
175181 format_label ! ( "type of the expression is `{}`" , ty. get_name( ) ) ,
176182 )
177183 . add_label (
178- node. ty . range ( ) ,
184+ node. target_type . range ( ) ,
179185 self . get_file ( ) ,
180186 format_label ! ( "target type is `{}`" , target_ty. borrow( ) . get_name( ) ) ,
181187 )
@@ -486,8 +492,10 @@ pub fn get_option_type<'a, 'b>(
486492
487493#[ node]
488494pub struct IsNode {
495+ /// expr is the expression which calculates a value as output
489496 pub expr : Box < NodeEnum > ,
490- pub ty : Box < TypeNodeEnum > ,
497+ /// target_type refers the desired type for the expr
498+ pub target_type : Box < TypeNodeEnum > ,
491499}
492500
493501impl Node for IsNode {
@@ -497,9 +505,9 @@ impl Node for IsNode {
497505 builder : & ' b BuilderEnum < ' a , ' _ > ,
498506 ) -> NodeResult {
499507 let re = self . expr . emit ( ctx, builder) ;
500- self . ty . emit_highlight ( ctx) ;
508+ self . target_type . emit_highlight ( ctx) ;
501509 let v = re?. get_value ( ) . unwrap ( ) ;
502- let target_tp = self . ty . get_type ( ctx, builder, true ) ?;
510+ let target_tp = self . target_type . get_type ( ctx, builder, true ) ?;
503511 let val = v. get_value ( ) ;
504512 let binding = v. get_ty ( ) ;
505513 let tp = & * binding. borrow ( ) ;
@@ -528,7 +536,7 @@ impl Node for IsNode {
528536 . to_result ( )
529537 } else {
530538 Err ( self
531- . ty
539+ . target_type
532540 . range ( )
533541 . new_err ( ErrorCode :: UNION_DOES_NOT_CONTAIN_TYPE )
534542 . add_to_ctx ( ctx) )
@@ -570,6 +578,6 @@ impl PrintTrait for IsNode {
570578 tab ( tabs, line. clone ( ) , end) ;
571579 println ! ( "IsNode" ) ;
572580 self . expr . print ( tabs + 1 , false , line. clone ( ) ) ;
573- self . ty . print ( tabs + 1 , true , line. clone ( ) ) ;
581+ self . target_type . print ( tabs + 1 , true , line. clone ( ) ) ;
574582 }
575583}
0 commit comments