11pub ( crate ) mod src;
22
33use self :: src:: HasSource ;
4- use crate :: adt:: { StructData , StructFieldId } ;
4+ use crate :: adt:: { StructData , StructFieldId , TypeAliasData } ;
55use crate :: builtin_type:: BuiltinType ;
66use crate :: code_model:: diagnostics:: ModuleDefinitionDiagnostic ;
77use crate :: diagnostics:: DiagnosticSink ;
8- use crate :: expr:: validator:: ExprValidator ;
8+ use crate :: expr:: validator:: { ExprValidator , TypeAliasValidator } ;
99use crate :: expr:: { Body , BodySourceMap } ;
1010use crate :: ids:: AstItemDef ;
1111use crate :: ids:: LocationCtx ;
@@ -15,7 +15,7 @@ use crate::resolve::{Resolution, Resolver};
1515use crate :: ty:: { lower:: LowerBatchResult , InferenceResult } ;
1616use crate :: type_ref:: { TypeRefBuilder , TypeRefId , TypeRefMap , TypeRefSourceMap } ;
1717use crate :: {
18- ids:: { FunctionId , StructId } ,
18+ ids:: { FunctionId , StructId , TypeAliasId } ,
1919 AsName , DefDatabase , FileId , HirDatabase , Name , Ty ,
2020} ;
2121use mun_syntax:: ast:: { ExternOwner , NameOwner , TypeAscriptionOwner , VisibilityOwner } ;
@@ -52,11 +52,11 @@ impl Module {
5252 diag. add_to ( db. upcast ( ) , self , sink) ;
5353 }
5454 for decl in self . declarations ( db) {
55- #[ allow( clippy:: single_match) ]
5655 match decl {
5756 ModuleDef :: Function ( f) => f. diagnostics ( db, sink) ,
5857 ModuleDef :: Struct ( s) => s. diagnostics ( db, sink) ,
59- _ => ( ) ,
58+ ModuleDef :: TypeAlias ( t) => t. diagnostics ( db, sink) ,
59+ ModuleDef :: BuiltinType ( _) => ( ) ,
6060 }
6161 }
6262 }
@@ -104,6 +104,11 @@ impl ModuleData {
104104 id : StructId :: from_ast_id ( loc_ctx, ast_id) ,
105105 } ) )
106106 }
107+ DefKind :: TypeAlias ( ast_id) => {
108+ data. definitions . push ( ModuleDef :: TypeAlias ( TypeAlias {
109+ id : TypeAliasId :: from_ast_id ( loc_ctx, ast_id) ,
110+ } ) )
111+ }
107112 }
108113 }
109114 } ;
@@ -121,6 +126,7 @@ pub enum ModuleDef {
121126 Function ( Function ) ,
122127 BuiltinType ( BuiltinType ) ,
123128 Struct ( Struct ) ,
129+ TypeAlias ( TypeAlias ) ,
124130}
125131
126132impl From < Function > for ModuleDef {
@@ -328,7 +334,8 @@ impl Function {
328334 }
329335
330336 pub fn ty ( self , db : & dyn HirDatabase ) -> Ty {
331- db. type_for_def ( self . into ( ) , Namespace :: Values )
337+ // TODO: Add detection of cyclick types
338+ db. type_for_def ( self . into ( ) , Namespace :: Values ) . 0
332339 }
333340
334341 pub fn infer ( self , db : & dyn HirDatabase ) -> Arc < InferenceResult > {
@@ -418,7 +425,8 @@ impl Struct {
418425 }
419426
420427 pub fn ty ( self , db : & dyn HirDatabase ) -> Ty {
421- db. type_for_def ( self . into ( ) , Namespace :: Types )
428+ // TODO: Add detection of cyclick types
429+ db. type_for_def ( self . into ( ) , Namespace :: Types ) . 0
422430 }
423431
424432 pub fn lower ( self , db : & dyn HirDatabase ) -> Arc < LowerBatchResult > {
@@ -442,6 +450,54 @@ impl Struct {
442450 }
443451}
444452
453+ #[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash ) ]
454+ pub struct TypeAlias {
455+ pub ( crate ) id : TypeAliasId ,
456+ }
457+
458+ impl TypeAlias {
459+ pub fn module ( self , db : & dyn DefDatabase ) -> Module {
460+ Module {
461+ file_id : self . id . file_id ( db) ,
462+ }
463+ }
464+
465+ pub fn data ( self , db : & dyn DefDatabase ) -> Arc < TypeAliasData > {
466+ db. type_alias_data ( self . id )
467+ }
468+
469+ pub fn name ( self , db : & dyn DefDatabase ) -> Name {
470+ self . data ( db) . name . clone ( )
471+ }
472+
473+ pub fn type_ref ( self , db : & dyn HirDatabase ) -> TypeRefId {
474+ self . data ( db. upcast ( ) ) . type_ref_id
475+ }
476+
477+ pub fn lower ( self , db : & dyn HirDatabase ) -> Arc < LowerBatchResult > {
478+ db. lower_type_alias ( self )
479+ }
480+
481+ pub ( crate ) fn resolver ( self , db : & dyn HirDatabase ) -> Resolver {
482+ // take the outer scope...
483+ self . module ( db. upcast ( ) ) . resolver ( db. upcast ( ) )
484+ }
485+
486+ pub fn diagnostics ( self , db : & dyn HirDatabase , sink : & mut DiagnosticSink ) {
487+ let data = self . data ( db. upcast ( ) ) ;
488+ let lower = self . lower ( db) ;
489+ lower. add_diagnostics (
490+ db,
491+ self . module ( db. upcast ( ) ) . file_id ,
492+ data. type_ref_source_map ( ) ,
493+ sink,
494+ ) ;
495+
496+ let validator = TypeAliasValidator :: new ( self , db) ;
497+ validator. validate_target_type_existence ( sink) ;
498+ }
499+ }
500+
445501mod diagnostics {
446502 use super :: Module ;
447503 use crate :: diagnostics:: { DiagnosticSink , DuplicateDefinition } ;
@@ -466,6 +522,9 @@ mod diagnostics {
466522 DefKind :: Struct ( id) => {
467523 SyntaxNodePtr :: new ( id. with_file_id ( owner. file_id ) . to_node ( db) . syntax ( ) )
468524 }
525+ DefKind :: TypeAlias ( id) => {
526+ SyntaxNodePtr :: new ( id. with_file_id ( owner. file_id ) . to_node ( db) . syntax ( ) )
527+ }
469528 }
470529 }
471530
0 commit comments