Resolve TupleStructPat in SourceAnalyzer::resolve_path

This commit is contained in:
Lukas Wirth 2021-02-10 09:29:10 +01:00
parent 36465b34b3
commit a195711df0
2 changed files with 34 additions and 6 deletions

View file

@ -222,8 +222,9 @@ impl SourceAnalyzer {
db: &dyn HirDatabase, db: &dyn HirDatabase,
path: &ast::Path, path: &ast::Path,
) -> Option<PathResolution> { ) -> Option<PathResolution> {
let parent = || path.syntax().parent();
let mut prefer_value_ns = false; let mut prefer_value_ns = false;
if let Some(path_expr) = path.syntax().parent().and_then(ast::PathExpr::cast) { if let Some(path_expr) = parent().and_then(ast::PathExpr::cast) {
let expr_id = self.expr_id(db, &path_expr.into())?; let expr_id = self.expr_id(db, &path_expr.into())?;
let infer = self.infer.as_ref()?; let infer = self.infer.as_ref()?;
if let Some(assoc) = infer.assoc_resolutions_for_expr(expr_id) { if let Some(assoc) = infer.assoc_resolutions_for_expr(expr_id) {
@ -237,7 +238,7 @@ impl SourceAnalyzer {
prefer_value_ns = true; prefer_value_ns = true;
} }
if let Some(path_pat) = path.syntax().parent().and_then(ast::PathPat::cast) { if let Some(path_pat) = parent().and_then(ast::PathPat::cast) {
let pat_id = self.pat_id(&path_pat.into())?; let pat_id = self.pat_id(&path_pat.into())?;
if let Some(assoc) = self.infer.as_ref()?.assoc_resolutions_for_pat(pat_id) { if let Some(assoc) = self.infer.as_ref()?.assoc_resolutions_for_pat(pat_id) {
return Some(PathResolution::AssocItem(assoc.into())); return Some(PathResolution::AssocItem(assoc.into()));
@ -249,7 +250,7 @@ impl SourceAnalyzer {
} }
} }
if let Some(rec_lit) = path.syntax().parent().and_then(ast::RecordExpr::cast) { if let Some(rec_lit) = parent().and_then(ast::RecordExpr::cast) {
let expr_id = self.expr_id(db, &rec_lit.into())?; let expr_id = self.expr_id(db, &rec_lit.into())?;
if let Some(VariantId::EnumVariantId(variant)) = if let Some(VariantId::EnumVariantId(variant)) =
self.infer.as_ref()?.variant_resolution_for_expr(expr_id) self.infer.as_ref()?.variant_resolution_for_expr(expr_id)
@ -258,8 +259,12 @@ impl SourceAnalyzer {
} }
} }
if let Some(rec_pat) = path.syntax().parent().and_then(ast::RecordPat::cast) { if let Some(pat) = parent()
let pat_id = self.pat_id(&rec_pat.into())?; .and_then(ast::RecordPat::cast)
.map(ast::Pat::from)
.or_else(|| parent().and_then(ast::TupleStructPat::cast).map(ast::Pat::from))
{
let pat_id = self.pat_id(&pat)?;
if let Some(VariantId::EnumVariantId(variant)) = if let Some(VariantId::EnumVariantId(variant)) =
self.infer.as_ref()?.variant_resolution_for_pat(pat_id) self.infer.as_ref()?.variant_resolution_for_pat(pat_id)
{ {
@ -272,7 +277,7 @@ impl SourceAnalyzer {
// Case where path is a qualifier of another path, e.g. foo::bar::Baz where we // Case where path is a qualifier of another path, e.g. foo::bar::Baz where we
// trying to resolve foo::bar. // trying to resolve foo::bar.
if let Some(outer_path) = path.syntax().parent().and_then(ast::Path::cast) { if let Some(outer_path) = parent().and_then(ast::Path::cast) {
if let Some(qualifier) = outer_path.qualifier() { if let Some(qualifier) = outer_path.qualifier() {
if path == &qualifier { if path == &qualifier {
return resolve_hir_path_qualifier(db, &self.resolver, &hir_path); return resolve_hir_path_qualifier(db, &self.resolver, &hir_path);

View file

@ -1114,4 +1114,27 @@ trait Foo {
"#]], "#]],
); );
} }
#[test]
fn test_self_variant_with_payload() {
check(
r#"
enum Foo { Bar() }
impl Foo {
fn foo(self) {
match self {
Self::Bar$0() => (),
}
}
}
"#,
expect![[r#"
Bar Variant FileId(0) 11..16 11..14 Other
FileId(0) 89..92 Other
"#]],
);
}
} }