Skip to content

Commit a7dc60a

Browse files
committed
return single option
Signed-off-by: Hayashi Mikihiro <34ttrweoewiwe28@gmail.com>
1 parent 2854ad9 commit a7dc60a

File tree

3 files changed

+59
-78
lines changed

3 files changed

+59
-78
lines changed

crates/hir-def/src/resolver.rs

Lines changed: 51 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -169,33 +169,26 @@ impl Resolver {
169169
self.resolve_module_path(db, path, BuiltinShadowMode::Module)
170170
}
171171

172-
pub fn resolve_path_in_type_ns<'a>(
173-
&'a self,
174-
db: &'a dyn DefDatabase,
175-
path: &'a Path,
176-
) -> impl Iterator<Item = (ModuleOrTypeNs, Option<usize>, Option<ImportOrExternCrate>)> + 'a
177-
{
172+
pub fn resolve_path_in_type_ns(
173+
&self,
174+
db: &dyn DefDatabase,
175+
path: &Path,
176+
) -> Option<(ModuleOrTypeNs, Option<usize>, Option<ImportOrExternCrate>)> {
178177
self.resolve_path_in_type_ns_with_prefix_info(db, path).map(
179-
move |(resolution, remaining_segments, import, _)| {
180-
(resolution, remaining_segments, import)
181-
},
178+
|(resolution, remaining_segments, import, _)| (resolution, remaining_segments, import),
182179
)
183180
}
184181

185-
pub fn resolve_path_in_type_ns_with_prefix_info<'a>(
186-
&'a self,
187-
db: &'a dyn DefDatabase,
188-
path: &'a Path,
189-
) -> Box<
190-
dyn Iterator<
191-
Item = (
192-
ModuleOrTypeNs,
193-
Option<usize>,
194-
Option<ImportOrExternCrate>,
195-
ResolvePathResultPrefixInfo,
196-
),
197-
> + 'a,
198-
> {
182+
pub fn resolve_path_in_type_ns_with_prefix_info(
183+
&self,
184+
db: &dyn DefDatabase,
185+
path: &Path,
186+
) -> Option<(
187+
ModuleOrTypeNs,
188+
Option<usize>,
189+
Option<ImportOrExternCrate>,
190+
ResolvePathResultPrefixInfo,
191+
)> {
199192
let path = match path {
200193
Path::BarePath(mod_path) => mod_path,
201194
Path::Normal(it) => &it.mod_path,
@@ -209,30 +202,29 @@ impl Resolver {
209202
LangItemTarget::Trait(it) => TypeNs::TraitId(it),
210203
LangItemTarget::Function(_)
211204
| LangItemTarget::ImplDef(_)
212-
| LangItemTarget::Static(_) => return Box::new(iter::empty()),
205+
| LangItemTarget::Static(_) => return None,
213206
};
214-
return Box::new(iter::once((
207+
return Some((
215208
ModuleOrTypeNs::TypeNs(type_ns),
216209
seg.as_ref().map(|_| 1),
217210
None,
218211
ResolvePathResultPrefixInfo::default(),
219-
)));
212+
));
220213
}
221214
};
222-
let Some(first_name) = path.segments().first() else { return Box::new(iter::empty()) };
215+
let first_name = path.segments().first()?;
223216
let skip_to_mod = path.kind != PathKind::Plain;
224217
if skip_to_mod {
225-
return Box::new(self.module_scope.resolve_path_in_type_ns(db, path).into_iter());
218+
return self.module_scope.resolve_path_in_module_or_type_ns(db, path);
226219
}
227220

228221
let remaining_idx = || {
229222
if path.segments().len() == 1 { None } else { Some(1) }
230223
};
231224

232-
let ns = self
233-
.scopes()
234-
.filter_map(move |scope| match scope {
235-
Scope::ExprScope(_) | Scope::MacroDefScope(_) => None,
225+
for scope in self.scopes() {
226+
match scope {
227+
Scope::ExprScope(_) | Scope::MacroDefScope(_) => continue,
236228
Scope::GenericParams { params, def } => {
237229
if let Some(id) = params.find_type_by_name(first_name, *def) {
238230
return Some((
@@ -242,7 +234,6 @@ impl Resolver {
242234
ResolvePathResultPrefixInfo::default(),
243235
));
244236
}
245-
None
246237
}
247238
&Scope::ImplDefScope(impl_) => {
248239
if *first_name == sym::Self_.clone() {
@@ -253,7 +244,6 @@ impl Resolver {
253244
ResolvePathResultPrefixInfo::default(),
254245
));
255246
}
256-
None
257247
}
258248
&Scope::AdtScope(adt) => {
259249
if *first_name == sym::Self_.clone() {
@@ -264,36 +254,45 @@ impl Resolver {
264254
ResolvePathResultPrefixInfo::default(),
265255
));
266256
}
267-
None
268257
}
269258
Scope::BlockScope(m) => {
270-
if let Some(res) = m.resolve_path_in_type_ns(db, path) {
259+
if let Some(res) = m.resolve_path_in_module_or_type_ns(db, path) {
260+
let res = match res.0 {
261+
ModuleOrTypeNs::TypeNs(_) => res,
262+
ModuleOrTypeNs::ModuleNs(_) => {
263+
if let Some(ModuleDefId::BuiltinType(builtin)) = BUILTIN_SCOPE
264+
.get(first_name)
265+
.and_then(|builtin| builtin.take_types())
266+
{
267+
(
268+
ModuleOrTypeNs::TypeNs(TypeNs::BuiltinType(builtin)),
269+
remaining_idx(),
270+
None,
271+
ResolvePathResultPrefixInfo::default(),
272+
)
273+
} else {
274+
res
275+
}
276+
}
277+
};
271278
return Some(res);
272279
}
273-
None
274280
}
275-
})
276-
.chain(self.module_scope.resolve_path_in_type_ns(db, path));
277-
278-
Box::new(ns)
281+
}
282+
}
283+
self.module_scope.resolve_path_in_module_or_type_ns(db, path)
279284
}
280285

281286
pub fn resolve_path_in_type_ns_fully(
282287
&self,
283288
db: &dyn DefDatabase,
284289
path: &Path,
285290
) -> Option<TypeNs> {
286-
let (res, unresolved) = self
287-
.resolve_path_in_type_ns(db, path)
288-
.filter_map(|(res, unresolved, _)| match res {
289-
ModuleOrTypeNs::TypeNs(it) => Some((it, unresolved)),
290-
ModuleOrTypeNs::ModuleNs(_) => None,
291-
})
292-
.next()?;
293-
if unresolved.is_some() {
294-
return None;
291+
if let (ModuleOrTypeNs::TypeNs(res), None, _) = self.resolve_path_in_type_ns(db, path)? {
292+
Some(res)
293+
} else {
294+
None
295295
}
296-
Some(res)
297296
}
298297

299298
pub fn resolve_visibility(
@@ -1183,7 +1182,7 @@ impl ModuleItemMap {
11831182
}
11841183
}
11851184

1186-
fn resolve_path_in_type_ns(
1185+
fn resolve_path_in_module_or_type_ns(
11871186
&self,
11881187
db: &dyn DefDatabase,
11891188
path: &ModPath,

crates/hir-ty/src/lower/path.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -333,15 +333,14 @@ impl<'a, 'b> PathLoweringContext<'a, 'b> {
333333
}
334334

335335
pub(crate) fn resolve_path_in_type_ns(&mut self) -> Option<(TypeNs, Option<usize>)> {
336-
let (resolution, remaining_index, prefix_info) = self
336+
let (resolution, remaining_index, _, prefix_info) = self
337337
.ctx
338338
.resolver
339-
.resolve_path_in_type_ns_with_prefix_info(self.ctx.db.upcast(), self.path)
340-
.filter_map(|(res, remaining_index, _, prefix_info)| match res {
341-
ModuleOrTypeNs::TypeNs(type_ns) => Some((type_ns, remaining_index, prefix_info)),
342-
ModuleOrTypeNs::ModuleNs(_) => None,
343-
})
344-
.next()?;
339+
.resolve_path_in_type_ns_with_prefix_info(self.ctx.db.upcast(), self.path)?;
340+
341+
let ModuleOrTypeNs::TypeNs(resolution) = resolution else {
342+
return None;
343+
};
345344

346345
let segments = self.segments;
347346
if segments.is_empty() || matches!(self.path, Path::LangItem(..)) {

crates/hir/src/source_analyzer.rs

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,23 +1365,6 @@ pub(crate) fn resolve_hir_path_as_attr_macro(
13651365
.map(Into::into)
13661366
}
13671367

1368-
fn resolve_path_in_module_or_type_ns(
1369-
db: &dyn HirDatabase,
1370-
resolver: &Resolver,
1371-
path: &Path,
1372-
) -> Option<(ModuleOrTypeNs, Option<usize>)> {
1373-
let mut types = resolver
1374-
.resolve_path_in_type_ns(db.upcast(), path)
1375-
.map(|(ty, remaining_idx, _)| (ty, remaining_idx))
1376-
.peekable();
1377-
let (ty, _) = types.peek()?;
1378-
match ty {
1379-
ModuleOrTypeNs::ModuleNs(_) => types
1380-
.find_or_first(|(ty, _)| matches!(ty, ModuleOrTypeNs::TypeNs(TypeNs::BuiltinType(_)))),
1381-
ModuleOrTypeNs::TypeNs(_) => types.next(),
1382-
}
1383-
}
1384-
13851368
fn resolve_hir_path_(
13861369
db: &dyn HirDatabase,
13871370
resolver: &Resolver,
@@ -1404,7 +1387,7 @@ fn resolve_hir_path_(
14041387
res.map(|ty_ns| (ModuleOrTypeNs::TypeNs(ty_ns), path.segments().first()))
14051388
}
14061389
None => {
1407-
let (ty, remaining_idx) = resolve_path_in_module_or_type_ns(db, resolver, path)?;
1390+
let (ty, remaining_idx, _) = resolver.resolve_path_in_type_ns(db.upcast(), path)?;
14081391
match remaining_idx {
14091392
Some(remaining_idx) => {
14101393
if remaining_idx + 1 == path.segments().len() {
@@ -1542,7 +1525,7 @@ fn resolve_hir_path_qualifier(
15421525
res.map(|ty_ns| (ModuleOrTypeNs::TypeNs(ty_ns), path.segments().first()))
15431526
}
15441527
None => {
1545-
let (ty, remaining_idx) = resolve_path_in_module_or_type_ns(db, resolver, path)?;
1528+
let (ty, remaining_idx, _) = resolver.resolve_path_in_type_ns(db.upcast(), path)?;
15461529
match remaining_idx {
15471530
Some(remaining_idx) => {
15481531
if remaining_idx + 1 == path.segments().len() {

0 commit comments

Comments
 (0)