Skip to content

Commit 75689f2

Browse files
committed
internal: Enforce Resolver to always have a module scope
1 parent ef92453 commit 75689f2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+360
-398
lines changed

crates/hir/src/lib.rs

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ impl Crate {
159159
.map(|dep| {
160160
let krate = Crate { id: dep.crate_id };
161161
let name = dep.as_name();
162-
CrateDependency { krate, name, }
162+
CrateDependency { krate, name }
163163
})
164164
.collect()
165165
}
@@ -2224,7 +2224,7 @@ impl BuiltinAttr {
22242224
Some(BuiltinAttr { krate: Some(krate.id), idx })
22252225
}
22262226

2227-
pub(crate) fn builtin(name: &str) -> Option<Self> {
2227+
fn builtin(name: &str) -> Option<Self> {
22282228
hir_def::builtin_attr::INERT_ATTRIBUTES
22292229
.iter()
22302230
.position(|tool| tool.name == name)
@@ -2263,7 +2263,7 @@ impl ToolModule {
22632263
Some(ToolModule { krate: Some(krate.id), idx })
22642264
}
22652265

2266-
pub(crate) fn builtin(name: &str) -> Option<Self> {
2266+
fn builtin(name: &str) -> Option<Self> {
22672267
hir_def::builtin_attr::TOOL_MODULES
22682268
.iter()
22692269
.position(|&tool| tool == name)
@@ -2613,13 +2613,9 @@ pub struct Type {
26132613
}
26142614

26152615
impl Type {
2616-
pub(crate) fn new_with_resolver(
2617-
db: &dyn HirDatabase,
2618-
resolver: &Resolver,
2619-
ty: Ty,
2620-
) -> Option<Type> {
2621-
let krate = resolver.krate()?;
2622-
Some(Type::new_with_resolver_inner(db, krate, resolver, ty))
2616+
pub(crate) fn new_with_resolver(db: &dyn HirDatabase, resolver: &Resolver, ty: Ty) -> Type {
2617+
let krate = resolver.krate();
2618+
Type::new_with_resolver_inner(db, krate, resolver, ty)
26232619
}
26242620

26252621
pub(crate) fn new_with_resolver_inner(
@@ -3038,10 +3034,7 @@ impl Type {
30383034
// There should be no inference vars in types passed here
30393035
let canonical = hir_ty::replace_errors_with_variables(&self.ty);
30403036

3041-
let krate = match scope.krate() {
3042-
Some(k) => k,
3043-
None => return,
3044-
};
3037+
let krate = scope.krate();
30453038
let environment = scope.resolver().generic_def().map_or_else(
30463039
|| Arc::new(TraitEnvironment::empty(krate.id)),
30473040
|d| db.trait_environment(d),
@@ -3098,10 +3091,7 @@ impl Type {
30983091
) {
30993092
let canonical = hir_ty::replace_errors_with_variables(&self.ty);
31003093

3101-
let krate = match scope.krate() {
3102-
Some(k) => k,
3103-
None => return,
3104-
};
3094+
let krate = scope.krate();
31053095
let environment = scope.resolver().generic_def().map_or_else(
31063096
|| Arc::new(TraitEnvironment::empty(krate.id)),
31073097
|d| db.trait_environment(d),

crates/hir/src/semantics.rs

Lines changed: 73 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -403,11 +403,15 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
403403
self.imp.to_module_def(file)
404404
}
405405

406-
pub fn scope(&self, node: &SyntaxNode) -> SemanticsScope<'db> {
406+
pub fn scope(&self, node: &SyntaxNode) -> Option<SemanticsScope<'db>> {
407407
self.imp.scope(node)
408408
}
409409

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>> {
411415
self.imp.scope_at_offset(node, offset)
412416
}
413417

@@ -456,7 +460,7 @@ impl<'db> SemanticsImpl<'db> {
456460
}
457461

458462
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())?;
460464
let file_id = sa.expand(self.db, InFile::new(sa.file_id, macro_call))?;
461465
let node = self.parse_or_expand(file_id)?;
462466
Some(node)
@@ -535,9 +539,9 @@ impl<'db> SemanticsImpl<'db> {
535539
token_to_map: SyntaxToken,
536540
) -> Option<(SyntaxNode, SyntaxToken)> {
537541
let SourceAnalyzer { file_id, resolver, .. } =
538-
self.analyze_no_infer(actual_macro_call.syntax());
542+
self.analyze_no_infer(actual_macro_call.syntax())?;
539543
let macro_call = InFile::new(file_id, actual_macro_call);
540-
let krate = resolver.krate()?;
544+
let krate = resolver.krate();
541545
let macro_call_id = macro_call.as_call_id(self.db.upcast(), krate, |path| {
542546
resolver
543547
.resolve_path_as_macro(self.db.upcast(), &path)
@@ -669,7 +673,10 @@ impl<'db> SemanticsImpl<'db> {
669673
Some(it) => it,
670674
None => return,
671675
};
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+
};
673680
let mut stack: SmallVec<[_; 4]> = smallvec![InFile::new(sa.file_id, token)];
674681
let mut cache = self.expansion_info_cache.borrow_mut();
675682
let mut mcache = self.macro_call_cache.borrow_mut();
@@ -903,70 +910,74 @@ impl<'db> SemanticsImpl<'db> {
903910
}
904911

905912
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)
909916
.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))
911918
}
912919

913920
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)
915922
}
916923

917924
fn type_of_expr(&self, expr: &ast::Expr) -> Option<TypeInfo> {
918-
self.analyze(expr.syntax())
925+
self.analyze(expr.syntax())?
919926
.type_of_expr(self.db, expr)
920927
.map(|(ty, coerced)| TypeInfo { original: ty, adjusted: coerced })
921928
}
922929

923930
fn type_of_pat(&self, pat: &ast::Pat) -> Option<TypeInfo> {
924-
self.analyze(pat.syntax())
931+
self.analyze(pat.syntax())?
925932
.type_of_pat(self.db, pat)
926933
.map(|(ty, coerced)| TypeInfo { original: ty, adjusted: coerced })
927934
}
928935

929936
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)
931938
}
932939

933940
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)
935942
}
936943

937944
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)?;
939947
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);
942950
let mut res = ty.as_callable(self.db)?;
943951
res.is_bound_method = true;
944952
Some(res)
945953
}
946954

947955
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)
949957
}
950958

951959
fn resolve_record_field(
952960
&self,
953961
field: &ast::RecordExprField,
954962
) -> 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)
956964
}
957965

958966
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)
960968
}
961969

962970
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())?;
964972
let macro_call = self.find_file(macro_call.syntax()).with_value(macro_call);
965973
sa.resolve_macro_call(self.db, macro_call)
966974
}
967975

968976
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+
};
970981
let macro_call = self.find_file(macro_call.syntax()).with_value(macro_call);
971982
sa.is_unsafe_macro_call(self.db, macro_call)
972983
}
@@ -981,11 +992,11 @@ impl<'db> SemanticsImpl<'db> {
981992
}
982993

983994
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)
985996
}
986997

987998
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();
9891000
let name = extern_crate.name_ref()?.as_name();
9901001
if name == known::SELF_PARAM {
9911002
return Some(krate);
@@ -997,22 +1008,22 @@ impl<'db> SemanticsImpl<'db> {
9971008
}
9981009

9991010
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)
10011012
}
10021013

10031014
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)
10051016
}
10061017

10071018
fn record_literal_missing_fields(&self, literal: &ast::RecordExpr) -> Vec<(Field, Type)> {
10081019
self.analyze(literal.syntax())
1009-
.record_literal_missing_fields(self.db, literal)
1020+
.and_then(|it| it.record_literal_missing_fields(self.db, literal))
10101021
.unwrap_or_default()
10111022
}
10121023

10131024
fn record_pattern_missing_fields(&self, pattern: &ast::RecordPat) -> Vec<(Field, Type)> {
10141025
self.analyze(pattern.syntax())
1015-
.record_pattern_missing_fields(self.db, pattern)
1026+
.and_then(|it| it.record_pattern_missing_fields(self.db, pattern))
10161027
.unwrap_or_default()
10171028
}
10181029

@@ -1026,15 +1037,22 @@ impl<'db> SemanticsImpl<'db> {
10261037
self.with_ctx(|ctx| ctx.file_to_def(file)).into_iter().map(Module::from)
10271038
}
10281039

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+
})
10321046
}
10331047

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+
)
10381056
}
10391057

10401058
fn scope_for_def(&self, def: Trait) -> SemanticsScope<'db> {
@@ -1052,15 +1070,21 @@ impl<'db> SemanticsImpl<'db> {
10521070
Some(res)
10531071
}
10541072

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> {
10561075
self.analyze_impl(node, None, true)
10571076
}
10581077

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> {
10601080
self.analyze_impl(node, None, false)
10611081
}
10621082

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> {
10641088
self.analyze_impl(node, Some(offset), false)
10651089
}
10661090

@@ -1069,22 +1093,22 @@ impl<'db> SemanticsImpl<'db> {
10691093
node: &SyntaxNode,
10701094
offset: Option<TextSize>,
10711095
infer_body: bool,
1072-
) -> SourceAnalyzer {
1096+
) -> Option<SourceAnalyzer> {
10731097
let _p = profile::span("Semantics::analyze_impl");
10741098
let node = self.find_file(node);
10751099

10761100
let container = match self.with_ctx(|ctx| ctx.find_container(node)) {
10771101
Some(it) => it,
1078-
None => return SourceAnalyzer::new_for_resolver(Resolver::default(), node),
1102+
None => return None,
10791103
};
10801104

10811105
let resolver = match container {
10821106
ChildContainer::DefWithBodyId(def) => {
1083-
return if infer_body {
1107+
return Some(if infer_body {
10841108
SourceAnalyzer::new_for_body(self.db, def, node, offset)
10851109
} else {
10861110
SourceAnalyzer::new_for_body_no_infer(self.db, def, node, offset)
1087-
}
1111+
})
10881112
}
10891113
ChildContainer::TraitId(it) => it.resolver(self.db.upcast()),
10901114
ChildContainer::ImplId(it) => it.resolver(self.db.upcast()),
@@ -1094,7 +1118,7 @@ impl<'db> SemanticsImpl<'db> {
10941118
ChildContainer::TypeAliasId(it) => it.resolver(self.db.upcast()),
10951119
ChildContainer::GenericDefId(it) => it.resolver(self.db.upcast()),
10961120
};
1097-
SourceAnalyzer::new_for_resolver(resolver, node)
1121+
Some(SourceAnalyzer::new_for_resolver(resolver, node))
10981122
}
10991123

11001124
fn cache(&self, root_node: SyntaxNode, file_id: HirFileId) {
@@ -1118,6 +1142,7 @@ impl<'db> SemanticsImpl<'db> {
11181142
InFile::new(file_id, node)
11191143
}
11201144

1145+
/// Wraps the node in a [`InFile`] with the file id it belongs to.
11211146
fn find_file<'node>(&self, node: &'node SyntaxNode) -> InFile<&'node SyntaxNode> {
11221147
let root_node = find_root(node);
11231148
let file_id = self.lookup(&root_node).unwrap_or_else(|| {
@@ -1319,12 +1344,12 @@ pub struct SemanticsScope<'a> {
13191344
}
13201345

13211346
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() }
13241349
}
13251350

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() }
13281353
}
13291354

13301355
pub(crate) fn resolver(&self) -> &Resolver {

0 commit comments

Comments
 (0)