@@ -403,11 +403,15 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
403
403
self . imp . to_module_def ( file)
404
404
}
405
405
406
- pub fn scope ( & self , node : & SyntaxNode ) -> SemanticsScope < ' db > {
406
+ pub fn scope ( & self , node : & SyntaxNode ) -> Option < SemanticsScope < ' db > > {
407
407
self . imp . scope ( node)
408
408
}
409
409
410
- pub fn scope_at_offset ( & self , node : & SyntaxNode , offset : TextSize ) -> SemanticsScope < ' db > {
410
+ pub fn scope_at_offset (
411
+ & self ,
412
+ node : & SyntaxNode ,
413
+ offset : TextSize ,
414
+ ) -> Option < SemanticsScope < ' db > > {
411
415
self . imp . scope_at_offset ( node, offset)
412
416
}
413
417
@@ -456,7 +460,7 @@ impl<'db> SemanticsImpl<'db> {
456
460
}
457
461
458
462
fn expand ( & self , macro_call : & ast:: MacroCall ) -> Option < SyntaxNode > {
459
- let sa = self . analyze_no_infer ( macro_call. syntax ( ) ) ;
463
+ let sa = self . analyze_no_infer ( macro_call. syntax ( ) ) ? ;
460
464
let file_id = sa. expand ( self . db , InFile :: new ( sa. file_id , macro_call) ) ?;
461
465
let node = self . parse_or_expand ( file_id) ?;
462
466
Some ( node)
@@ -535,9 +539,9 @@ impl<'db> SemanticsImpl<'db> {
535
539
token_to_map : SyntaxToken ,
536
540
) -> Option < ( SyntaxNode , SyntaxToken ) > {
537
541
let SourceAnalyzer { file_id, resolver, .. } =
538
- self . analyze_no_infer ( actual_macro_call. syntax ( ) ) ;
542
+ self . analyze_no_infer ( actual_macro_call. syntax ( ) ) ? ;
539
543
let macro_call = InFile :: new ( file_id, actual_macro_call) ;
540
- let krate = resolver. krate ( ) ? ;
544
+ let krate = resolver. krate ( ) ;
541
545
let macro_call_id = macro_call. as_call_id ( self . db . upcast ( ) , krate, |path| {
542
546
resolver
543
547
. resolve_path_as_macro ( self . db . upcast ( ) , & path)
@@ -669,7 +673,10 @@ impl<'db> SemanticsImpl<'db> {
669
673
Some ( it) => it,
670
674
None => return ,
671
675
} ;
672
- let sa = self . analyze_no_infer ( & parent) ;
676
+ let sa = match self . analyze_no_infer ( & parent) {
677
+ Some ( it) => it,
678
+ None => return ,
679
+ } ;
673
680
let mut stack: SmallVec < [ _ ; 4 ] > = smallvec ! [ InFile :: new( sa. file_id, token) ] ;
674
681
let mut cache = self . expansion_info_cache . borrow_mut ( ) ;
675
682
let mut mcache = self . macro_call_cache . borrow_mut ( ) ;
@@ -903,70 +910,74 @@ impl<'db> SemanticsImpl<'db> {
903
910
}
904
911
905
912
fn resolve_type ( & self , ty : & ast:: Type ) -> Option < Type > {
906
- let scope = self . scope ( ty. syntax ( ) ) ;
907
- let ctx = body:: LowerCtx :: new ( self . db . upcast ( ) , scope . file_id ) ;
908
- let ty = hir_ty:: TyLoweringContext :: new ( self . db , & scope . resolver )
913
+ let analyze = self . analyze ( ty. syntax ( ) ) ? ;
914
+ let ctx = body:: LowerCtx :: new ( self . db . upcast ( ) , analyze . file_id ) ;
915
+ let ty = hir_ty:: TyLoweringContext :: new ( self . db , & analyze . resolver )
909
916
. lower_ty ( & crate :: TypeRef :: from_ast ( & ctx, ty. clone ( ) ) ) ;
910
- Type :: new_with_resolver ( self . db , & scope . resolver , ty)
917
+ Some ( Type :: new_with_resolver ( self . db , & analyze . resolver , ty) )
911
918
}
912
919
913
920
fn is_implicit_reborrow ( & self , expr : & ast:: Expr ) -> Option < Mutability > {
914
- self . analyze ( expr. syntax ( ) ) . is_implicit_reborrow ( self . db , expr)
921
+ self . analyze ( expr. syntax ( ) ) ? . is_implicit_reborrow ( self . db , expr)
915
922
}
916
923
917
924
fn type_of_expr ( & self , expr : & ast:: Expr ) -> Option < TypeInfo > {
918
- self . analyze ( expr. syntax ( ) )
925
+ self . analyze ( expr. syntax ( ) ) ?
919
926
. type_of_expr ( self . db , expr)
920
927
. map ( |( ty, coerced) | TypeInfo { original : ty, adjusted : coerced } )
921
928
}
922
929
923
930
fn type_of_pat ( & self , pat : & ast:: Pat ) -> Option < TypeInfo > {
924
- self . analyze ( pat. syntax ( ) )
931
+ self . analyze ( pat. syntax ( ) ) ?
925
932
. type_of_pat ( self . db , pat)
926
933
. map ( |( ty, coerced) | TypeInfo { original : ty, adjusted : coerced } )
927
934
}
928
935
929
936
fn type_of_self ( & self , param : & ast:: SelfParam ) -> Option < Type > {
930
- self . analyze ( param. syntax ( ) ) . type_of_self ( self . db , param)
937
+ self . analyze ( param. syntax ( ) ) ? . type_of_self ( self . db , param)
931
938
}
932
939
933
940
fn resolve_method_call ( & self , call : & ast:: MethodCallExpr ) -> Option < FunctionId > {
934
- self . analyze ( call. syntax ( ) ) . resolve_method_call ( self . db , call) . map ( |( id, _) | id)
941
+ self . analyze ( call. syntax ( ) ) ? . resolve_method_call ( self . db , call) . map ( |( id, _) | id)
935
942
}
936
943
937
944
fn resolve_method_call_as_callable ( & self , call : & ast:: MethodCallExpr ) -> Option < Callable > {
938
- let ( func, subst) = self . analyze ( call. syntax ( ) ) . resolve_method_call ( self . db , call) ?;
945
+ let source_analyzer = self . analyze ( call. syntax ( ) ) ?;
946
+ let ( func, subst) = source_analyzer. resolve_method_call ( self . db , call) ?;
939
947
let ty = self . db . value_ty ( func. into ( ) ) . substitute ( Interner , & subst) ;
940
- let resolver = self . analyze ( call . syntax ( ) ) . resolver ;
941
- let ty = Type :: new_with_resolver ( self . db , & resolver, ty) ? ;
948
+ let resolver = source_analyzer . resolver ;
949
+ let ty = Type :: new_with_resolver ( self . db , & resolver, ty) ;
942
950
let mut res = ty. as_callable ( self . db ) ?;
943
951
res. is_bound_method = true ;
944
952
Some ( res)
945
953
}
946
954
947
955
fn resolve_field ( & self , field : & ast:: FieldExpr ) -> Option < Field > {
948
- self . analyze ( field. syntax ( ) ) . resolve_field ( self . db , field)
956
+ self . analyze ( field. syntax ( ) ) ? . resolve_field ( self . db , field)
949
957
}
950
958
951
959
fn resolve_record_field (
952
960
& self ,
953
961
field : & ast:: RecordExprField ,
954
962
) -> Option < ( Field , Option < Local > , Type ) > {
955
- self . analyze ( field. syntax ( ) ) . resolve_record_field ( self . db , field)
963
+ self . analyze ( field. syntax ( ) ) ? . resolve_record_field ( self . db , field)
956
964
}
957
965
958
966
fn resolve_record_pat_field ( & self , field : & ast:: RecordPatField ) -> Option < Field > {
959
- self . analyze ( field. syntax ( ) ) . resolve_record_pat_field ( self . db , field)
967
+ self . analyze ( field. syntax ( ) ) ? . resolve_record_pat_field ( self . db , field)
960
968
}
961
969
962
970
fn resolve_macro_call ( & self , macro_call : & ast:: MacroCall ) -> Option < Macro > {
963
- let sa = self . analyze ( macro_call. syntax ( ) ) ;
971
+ let sa = self . analyze ( macro_call. syntax ( ) ) ? ;
964
972
let macro_call = self . find_file ( macro_call. syntax ( ) ) . with_value ( macro_call) ;
965
973
sa. resolve_macro_call ( self . db , macro_call)
966
974
}
967
975
968
976
fn is_unsafe_macro_call ( & self , macro_call : & ast:: MacroCall ) -> bool {
969
- let sa = self . analyze ( macro_call. syntax ( ) ) ;
977
+ let sa = match self . analyze ( macro_call. syntax ( ) ) {
978
+ Some ( it) => it,
979
+ None => return false ,
980
+ } ;
970
981
let macro_call = self . find_file ( macro_call. syntax ( ) ) . with_value ( macro_call) ;
971
982
sa. is_unsafe_macro_call ( self . db , macro_call)
972
983
}
@@ -981,11 +992,11 @@ impl<'db> SemanticsImpl<'db> {
981
992
}
982
993
983
994
fn resolve_path ( & self , path : & ast:: Path ) -> Option < PathResolution > {
984
- self . analyze ( path. syntax ( ) ) . resolve_path ( self . db , path)
995
+ self . analyze ( path. syntax ( ) ) ? . resolve_path ( self . db , path)
985
996
}
986
997
987
998
fn resolve_extern_crate ( & self , extern_crate : & ast:: ExternCrate ) -> Option < Crate > {
988
- let krate = self . scope ( extern_crate. syntax ( ) ) . krate ( ) ? ;
999
+ let krate = self . scope ( extern_crate. syntax ( ) ) ? . krate ( ) ;
989
1000
let name = extern_crate. name_ref ( ) ?. as_name ( ) ;
990
1001
if name == known:: SELF_PARAM {
991
1002
return Some ( krate) ;
@@ -997,22 +1008,22 @@ impl<'db> SemanticsImpl<'db> {
997
1008
}
998
1009
999
1010
fn resolve_variant ( & self , record_lit : ast:: RecordExpr ) -> Option < VariantId > {
1000
- self . analyze ( record_lit. syntax ( ) ) . resolve_variant ( self . db , record_lit)
1011
+ self . analyze ( record_lit. syntax ( ) ) ? . resolve_variant ( self . db , record_lit)
1001
1012
}
1002
1013
1003
1014
fn resolve_bind_pat_to_const ( & self , pat : & ast:: IdentPat ) -> Option < ModuleDef > {
1004
- self . analyze ( pat. syntax ( ) ) . resolve_bind_pat_to_const ( self . db , pat)
1015
+ self . analyze ( pat. syntax ( ) ) ? . resolve_bind_pat_to_const ( self . db , pat)
1005
1016
}
1006
1017
1007
1018
fn record_literal_missing_fields ( & self , literal : & ast:: RecordExpr ) -> Vec < ( Field , Type ) > {
1008
1019
self . analyze ( literal. syntax ( ) )
1009
- . record_literal_missing_fields ( self . db , literal)
1020
+ . and_then ( |it| it . record_literal_missing_fields ( self . db , literal) )
1010
1021
. unwrap_or_default ( )
1011
1022
}
1012
1023
1013
1024
fn record_pattern_missing_fields ( & self , pattern : & ast:: RecordPat ) -> Vec < ( Field , Type ) > {
1014
1025
self . analyze ( pattern. syntax ( ) )
1015
- . record_pattern_missing_fields ( self . db , pattern)
1026
+ . and_then ( |it| it . record_pattern_missing_fields ( self . db , pattern) )
1016
1027
. unwrap_or_default ( )
1017
1028
}
1018
1029
@@ -1026,15 +1037,22 @@ impl<'db> SemanticsImpl<'db> {
1026
1037
self . with_ctx ( |ctx| ctx. file_to_def ( file) ) . into_iter ( ) . map ( Module :: from)
1027
1038
}
1028
1039
1029
- fn scope ( & self , node : & SyntaxNode ) -> SemanticsScope < ' db > {
1030
- let SourceAnalyzer { file_id, resolver, .. } = self . analyze_no_infer ( node) ;
1031
- SemanticsScope { db : self . db , file_id, resolver }
1040
+ fn scope ( & self , node : & SyntaxNode ) -> Option < SemanticsScope < ' db > > {
1041
+ self . analyze_no_infer ( node) . map ( |SourceAnalyzer { file_id, resolver, .. } | SemanticsScope {
1042
+ db : self . db ,
1043
+ file_id,
1044
+ resolver,
1045
+ } )
1032
1046
}
1033
1047
1034
- fn scope_at_offset ( & self , node : & SyntaxNode , offset : TextSize ) -> SemanticsScope < ' db > {
1035
- let SourceAnalyzer { file_id, resolver, .. } =
1036
- self . analyze_with_offset_no_infer ( node, offset) ;
1037
- SemanticsScope { db : self . db , file_id, resolver }
1048
+ fn scope_at_offset ( & self , node : & SyntaxNode , offset : TextSize ) -> Option < SemanticsScope < ' db > > {
1049
+ self . analyze_with_offset_no_infer ( node, offset) . map (
1050
+ |SourceAnalyzer { file_id, resolver, .. } | SemanticsScope {
1051
+ db : self . db ,
1052
+ file_id,
1053
+ resolver,
1054
+ } ,
1055
+ )
1038
1056
}
1039
1057
1040
1058
fn scope_for_def ( & self , def : Trait ) -> SemanticsScope < ' db > {
@@ -1052,15 +1070,21 @@ impl<'db> SemanticsImpl<'db> {
1052
1070
Some ( res)
1053
1071
}
1054
1072
1055
- fn analyze ( & self , node : & SyntaxNode ) -> SourceAnalyzer {
1073
+ /// Returns none if the file of the node is not part of a crate.
1074
+ fn analyze ( & self , node : & SyntaxNode ) -> Option < SourceAnalyzer > {
1056
1075
self . analyze_impl ( node, None , true )
1057
1076
}
1058
1077
1059
- fn analyze_no_infer ( & self , node : & SyntaxNode ) -> SourceAnalyzer {
1078
+ /// Returns none if the file of the node is not part of a crate.
1079
+ fn analyze_no_infer ( & self , node : & SyntaxNode ) -> Option < SourceAnalyzer > {
1060
1080
self . analyze_impl ( node, None , false )
1061
1081
}
1062
1082
1063
- fn analyze_with_offset_no_infer ( & self , node : & SyntaxNode , offset : TextSize ) -> SourceAnalyzer {
1083
+ fn analyze_with_offset_no_infer (
1084
+ & self ,
1085
+ node : & SyntaxNode ,
1086
+ offset : TextSize ,
1087
+ ) -> Option < SourceAnalyzer > {
1064
1088
self . analyze_impl ( node, Some ( offset) , false )
1065
1089
}
1066
1090
@@ -1069,22 +1093,22 @@ impl<'db> SemanticsImpl<'db> {
1069
1093
node : & SyntaxNode ,
1070
1094
offset : Option < TextSize > ,
1071
1095
infer_body : bool ,
1072
- ) -> SourceAnalyzer {
1096
+ ) -> Option < SourceAnalyzer > {
1073
1097
let _p = profile:: span ( "Semantics::analyze_impl" ) ;
1074
1098
let node = self . find_file ( node) ;
1075
1099
1076
1100
let container = match self . with_ctx ( |ctx| ctx. find_container ( node) ) {
1077
1101
Some ( it) => it,
1078
- None => return SourceAnalyzer :: new_for_resolver ( Resolver :: default ( ) , node ) ,
1102
+ None => return None ,
1079
1103
} ;
1080
1104
1081
1105
let resolver = match container {
1082
1106
ChildContainer :: DefWithBodyId ( def) => {
1083
- return if infer_body {
1107
+ return Some ( if infer_body {
1084
1108
SourceAnalyzer :: new_for_body ( self . db , def, node, offset)
1085
1109
} else {
1086
1110
SourceAnalyzer :: new_for_body_no_infer ( self . db , def, node, offset)
1087
- }
1111
+ } )
1088
1112
}
1089
1113
ChildContainer :: TraitId ( it) => it. resolver ( self . db . upcast ( ) ) ,
1090
1114
ChildContainer :: ImplId ( it) => it. resolver ( self . db . upcast ( ) ) ,
@@ -1094,7 +1118,7 @@ impl<'db> SemanticsImpl<'db> {
1094
1118
ChildContainer :: TypeAliasId ( it) => it. resolver ( self . db . upcast ( ) ) ,
1095
1119
ChildContainer :: GenericDefId ( it) => it. resolver ( self . db . upcast ( ) ) ,
1096
1120
} ;
1097
- SourceAnalyzer :: new_for_resolver ( resolver, node)
1121
+ Some ( SourceAnalyzer :: new_for_resolver ( resolver, node) )
1098
1122
}
1099
1123
1100
1124
fn cache ( & self , root_node : SyntaxNode , file_id : HirFileId ) {
@@ -1118,6 +1142,7 @@ impl<'db> SemanticsImpl<'db> {
1118
1142
InFile :: new ( file_id, node)
1119
1143
}
1120
1144
1145
+ /// Wraps the node in a [`InFile`] with the file id it belongs to.
1121
1146
fn find_file < ' node > ( & self , node : & ' node SyntaxNode ) -> InFile < & ' node SyntaxNode > {
1122
1147
let root_node = find_root ( node) ;
1123
1148
let file_id = self . lookup ( & root_node) . unwrap_or_else ( || {
@@ -1319,12 +1344,12 @@ pub struct SemanticsScope<'a> {
1319
1344
}
1320
1345
1321
1346
impl < ' a > SemanticsScope < ' a > {
1322
- pub fn module ( & self ) -> Option < Module > {
1323
- Some ( Module { id : self . resolver . module ( ) ? } )
1347
+ pub fn module ( & self ) -> Module {
1348
+ Module { id : self . resolver . module ( ) }
1324
1349
}
1325
1350
1326
- pub fn krate ( & self ) -> Option < Crate > {
1327
- Some ( Crate { id : self . resolver . krate ( ) ? } )
1351
+ pub fn krate ( & self ) -> Crate {
1352
+ Crate { id : self . resolver . krate ( ) }
1328
1353
}
1329
1354
1330
1355
pub ( crate ) fn resolver ( & self ) -> & Resolver {
0 commit comments