@@ -1187,43 +1187,6 @@ impl TypeCheckedExpr {
11871187 TypeCheckedExprKind :: Loop ( ..) => Type :: Every ,
11881188 }
11891189 }
1190-
1191- /// Extracts the type returned from the expression.
1192- pub fn get_type_mut ( & mut self ) -> Option < & mut Type > {
1193- Some ( match & mut self . kind {
1194- TypeCheckedExprKind :: UnaryOp ( _, _, t) => t,
1195- TypeCheckedExprKind :: Binary ( _, _, _, t) => t,
1196- TypeCheckedExprKind :: Trinary ( _, _, _, _, t) => t,
1197- TypeCheckedExprKind :: LocalVariableRef ( .., t) => t,
1198- TypeCheckedExprKind :: GlobalVariableRef ( .., t) => t,
1199- TypeCheckedExprKind :: FuncRef ( .., t) => t,
1200- TypeCheckedExprKind :: TupleRef ( .., t) => t,
1201- TypeCheckedExprKind :: DotRef ( .., t) => t,
1202- TypeCheckedExprKind :: Const ( .., t) => t,
1203- TypeCheckedExprKind :: FunctionCall ( .., t, _) => t,
1204- TypeCheckedExprKind :: StructInitializer ( .., t) => t,
1205- TypeCheckedExprKind :: ArrayRef ( .., t) => t,
1206- TypeCheckedExprKind :: FixedArrayRef ( .., t) => t,
1207- TypeCheckedExprKind :: MapRef ( .., t) => t,
1208- TypeCheckedExprKind :: ClosureLoad ( .., t) => t,
1209- TypeCheckedExprKind :: Tuple ( .., t) => t,
1210- TypeCheckedExprKind :: NewArray ( .., t) => t,
1211- TypeCheckedExprKind :: NewFixedArray ( .., t) => t,
1212- TypeCheckedExprKind :: NewMap ( t) => t,
1213- TypeCheckedExprKind :: ArrayMod ( .., t) => t,
1214- TypeCheckedExprKind :: FixedArrayMod ( .., t) => t,
1215- TypeCheckedExprKind :: MapMod ( .., t) => t,
1216- TypeCheckedExprKind :: StructMod ( .., t) => t,
1217- TypeCheckedExprKind :: Cast ( .., t) => t,
1218- TypeCheckedExprKind :: Asm ( t, ..) => t,
1219- TypeCheckedExprKind :: Try ( .., t) => t,
1220- TypeCheckedExprKind :: If ( .., t) => t,
1221- TypeCheckedExprKind :: IfLet ( .., t) => t,
1222- TypeCheckedExprKind :: Variant ( sub) => return sub. get_type_mut ( ) , // be careful with these,
1223- TypeCheckedExprKind :: CodeBlock ( block) => return block. get_type_mut ( ) , // they are to options
1224- _ => return None ,
1225- } )
1226- }
12271190}
12281191
12291192type TypeCheckedFieldInitializer = FieldInitializer < TypeCheckedExpr > ;
@@ -2257,15 +2220,54 @@ fn typecheck_expr(
22572220 scopes,
22582221 ) ?) ) )
22592222 }
2260- ExprKind :: VariableRef ( id) => {
2223+ ExprKind :: VariableRef ( id, spec ) => {
22612224 if let Some ( tipe) = func_table. get ( id) {
2262- let tipe = tipe. rep ( type_tree) ?;
2225+ let template_type = tipe. rep ( type_tree) ?;
2226+ let num_generic_params = tipe. count_generic_slots ( ) ;
2227+
2228+ if spec. len ( ) != num_generic_params {
2229+ return Err ( CompileError :: new (
2230+ "Generics error" ,
2231+ format ! (
2232+ "Func {} has {} generic args but was passed {}" ,
2233+ Color :: red( string_table. name_from_id( * id) ) ,
2234+ Color :: red( num_generic_params) ,
2235+ Color :: red( spec. len( ) ) ,
2236+ ) ,
2237+ debug_info. locs ( ) ,
2238+ ) ) ;
2239+ }
2240+
2241+ let tipe = template_type. make_specific ( spec) ?;
22632242 Ok ( TypeCheckedExprKind :: FuncRef ( * id, tipe) )
22642243 } else if let Some ( tipe) = type_table. get ( id) {
2244+ if !spec. is_empty ( ) {
2245+ return Err ( CompileError :: new (
2246+ "Generics error" ,
2247+ format ! (
2248+ "Variable {} doesn't need specialization" ,
2249+ Color :: red( string_table. name_from_id( * id) ) ,
2250+ ) ,
2251+ debug_info. locs ( ) ,
2252+ ) ) ;
2253+ }
2254+
22652255 let tipe = tipe. rep ( type_tree) ?;
22662256 Ok ( TypeCheckedExprKind :: LocalVariableRef ( * id, tipe) )
22672257 } else if let Some ( tipe) = global_vars. get ( id) {
22682258 let tipe = tipe. rep ( type_tree) ?;
2259+
2260+ if !spec. is_empty ( ) {
2261+ return Err ( CompileError :: new (
2262+ "Generics error" ,
2263+ format ! (
2264+ "Global variable {} doesn't need specialization" ,
2265+ Color :: red( string_table. name_from_id( * id) ) ,
2266+ ) ,
2267+ debug_info. locs ( ) ,
2268+ ) ) ;
2269+ }
2270+
22692271 Ok ( TypeCheckedExprKind :: GlobalVariableRef ( * id, tipe) )
22702272 } else {
22712273 Err ( CompileError :: new_type_error (
@@ -2372,8 +2374,8 @@ fn typecheck_expr(
23722374 Constant :: Option ( o) => TypeCheckedExprKind :: Const ( o. value ( ) , o. type_of ( ) ) ,
23732375 Constant :: Null => TypeCheckedExprKind :: Const ( Value :: none ( ) , Type :: Any ) ,
23742376 } ) ,
2375- ExprKind :: FunctionCall ( expr, args, spec ) => {
2376- let mut expr = typecheck_expr (
2377+ ExprKind :: FunctionCall ( expr, args) => {
2378+ let expr = typecheck_expr (
23772379 expr,
23782380 type_table,
23792381 global_vars,
@@ -2386,40 +2388,9 @@ fn typecheck_expr(
23862388 scopes,
23872389 ) ?;
23882390
2389- let old_type = expr. get_type ( ) . rep ( type_tree) ?;
2390- let num_generic_params = old_type. count_generic_slots ( ) ;
2391+ let expr_type = expr. get_type ( ) . rep ( type_tree) ?;
23912392
2392- if spec. len ( ) != num_generic_params {
2393- return Err ( CompileError :: new (
2394- "Generics error" ,
2395- format ! (
2396- "Func has {} generic args but was passed {}" ,
2397- Color :: red( num_generic_params) ,
2398- Color :: red( spec. len( ) ) ,
2399- ) ,
2400- debug_info. locs ( ) ,
2401- ) ) ;
2402- }
2403-
2404- match expr. get_type_mut ( ) {
2405- Some ( tipe) => {
2406- * tipe = tipe
2407- . rep ( type_tree) ?
2408- . make_specific ( spec) ?
2409- . commit_params ( & func. generics ) ;
2410- }
2411- None => {
2412- return Err ( CompileError :: new_type_error (
2413- format ! (
2414- "tried to call a {}, which is not a function" ,
2415- Color :: red( old_type. print( type_tree) ) ,
2416- ) ,
2417- debug_info. locs ( ) ,
2418- ) ) ;
2419- }
2420- }
2421-
2422- match expr. get_type ( ) . rep ( type_tree) ? {
2393+ match expr_type. clone ( ) {
24232394 Type :: Func ( prop, arg_types, ret_type) => {
24242395 if args. len ( ) == arg_types. len ( ) {
24252396 let mut tc_args = Vec :: new ( ) ;
@@ -2439,7 +2410,6 @@ fn typecheck_expr(
24392410 tc_args. push ( tc_arg) ;
24402411
24412412 let resolved_arg_type = arg_types[ i] . clone ( ) ;
2442- //let resolved_arg_type = resolved_arg_type.make_generic(&func.generics);
24432413
24442414 if !resolved_arg_type. assignable (
24452415 & tc_args[ i] . get_type ( ) . rep ( type_tree) ?,
@@ -2472,7 +2442,7 @@ fn typecheck_expr(
24722442 _ => Err ( CompileError :: new_type_error (
24732443 format ! (
24742444 "tried to call a {}, which is not a function" ,
2475- Color :: red( old_type . print( type_tree) )
2445+ Color :: red( expr_type . print( type_tree) )
24762446 ) ,
24772447 debug_info. locs ( ) ,
24782448 ) ) ,
@@ -2495,12 +2465,6 @@ fn typecheck_expr(
24952465
24962466 // a closures inherits its parent's generics
24972467 closure_func. generics = func. generics . clone ( ) ;
2498- closure_func. tipe = closure_func. tipe . commit_params ( & func. generics ) ;
2499- closure_func. ret_type = closure_func. ret_type . commit_params ( & func. generics ) ;
2500- closure_func
2501- . args
2502- . iter_mut ( )
2503- . for_each ( |arg| arg. tipe = arg. tipe . commit_params ( & func. generics ) ) ;
25042468
25052469 let id = closure_func. id ;
25062470 let tipe = closure_func. tipe . clone ( ) ;
@@ -4372,12 +4336,6 @@ impl TypeCheckedCodeBlock {
43724336 . map ( |r| r. get_type ( ) )
43734337 . unwrap_or ( Type :: Void )
43744338 }
4375- pub fn get_type_mut ( & mut self ) -> Option < & mut Type > {
4376- match & mut self . ret_expr {
4377- Some ( expr) => expr. get_type_mut ( ) ,
4378- None => None ,
4379- }
4380- }
43814339}
43824340
43834341impl AbstractSyntaxTree for TypeCheckedCodeBlock {
0 commit comments