Skip to content

Commit 7fda174

Browse files
Support self upvar in upvars_mentioned()
1 parent 5ef268e commit 7fda174

1 file changed

Lines changed: 29 additions & 3 deletions

File tree

crates/hir-ty/src/upvars.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,9 @@ fn resolve_maybe_upvar<'db>(
250250
path: &Path,
251251
) {
252252
if let Path::BarePath(mod_path) = path
253-
&& matches!(mod_path.kind, PathKind::Plain)
254-
&& mod_path.segments().len() == 1
253+
&& matches!(mod_path.kind, PathKind::Plain | PathKind::SELF)
254+
// `self` is length zero.
255+
&& mod_path.segments().len() <= 1
255256
{
256257
// Could be a variable.
257258
let guard = resolver.update_to_inner_scope(db, owner, expr);
@@ -269,7 +270,9 @@ fn resolve_maybe_upvar<'db>(
269270
#[cfg(test)]
270271
mod tests {
271272
use expect_test::{Expect, expect};
272-
use hir_def::{DefWithBodyId, ModuleDefId, expr_store::Body, nameres::crate_def_map};
273+
use hir_def::{
274+
AssocItemId, DefWithBodyId, ModuleDefId, expr_store::Body, nameres::crate_def_map,
275+
};
273276
use itertools::Itertools;
274277
use span::Edition;
275278
use test_fixture::WithFixture;
@@ -288,6 +291,14 @@ mod tests {
288291
ModuleDefId::FunctionId(func) => Some(func),
289292
_ => None,
290293
})
294+
.chain(def_map.modules().flat_map(|(_, module)| {
295+
module.scope.impls().flat_map(|impl_| &*impl_.impl_items(&db).items).filter_map(
296+
|&(_, item)| match item {
297+
AssocItemId::FunctionId(it) => Some(it),
298+
_ => None,
299+
},
300+
)
301+
}))
291302
.exactly_one()
292303
.unwrap_or_else(|_| panic!("expected one function"));
293304
let (body, source_map) = Body::with_source_map(&db, func.into());
@@ -387,4 +398,19 @@ fn foo() {
387398
49..110: a, b"#]],
388399
);
389400
}
401+
402+
#[test]
403+
fn self_upvar() {
404+
check(
405+
r#"
406+
struct Foo(i32);
407+
impl Foo {
408+
fn foo(&self) {
409+
|| self.0;
410+
}
411+
}
412+
"#,
413+
expect!["56..65: self"],
414+
);
415+
}
390416
}

0 commit comments

Comments
 (0)