Skip to content

Commit 3f42b2e

Browse files
committed
Handle Self in values and patterns
I.e. - `Self(x)` or `Self` in tuple/unit struct impls - `Self::Variant(x)` or `Self::Variant` in enum impls - the same in patterns Fixes #4454.
1 parent d51c1f6 commit 3f42b2e

File tree

6 files changed

+197
-19
lines changed

6 files changed

+197
-19
lines changed

crates/ra_hir/src/source_analyzer.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ pub(crate) fn resolve_hir_path(
417417
ValueNs::StaticId(it) => PathResolution::Def(Static::from(it).into()),
418418
ValueNs::StructId(it) => PathResolution::Def(Struct::from(it).into()),
419419
ValueNs::EnumVariantId(it) => PathResolution::Def(EnumVariant::from(it).into()),
420+
ValueNs::ImplSelf(impl_id) => PathResolution::SelfType(impl_id.into()),
420421
};
421422
Some(res)
422423
});

crates/ra_hir_def/src/resolver.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ pub enum ResolveValueResult {
8686

8787
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
8888
pub enum ValueNs {
89+
ImplSelf(ImplId),
8990
LocalBinding(PatId),
9091
FunctionId(FunctionId),
9192
ConstId(ConstId),
@@ -291,19 +292,26 @@ impl Resolver {
291292
}
292293
Scope::GenericParams { .. } => continue,
293294

294-
Scope::ImplDefScope(impl_) if n_segments > 1 => {
295+
Scope::ImplDefScope(impl_) => {
295296
if first_name == &name![Self] {
296-
let ty = TypeNs::SelfType(*impl_);
297-
return Some(ResolveValueResult::Partial(ty, 1));
297+
if n_segments > 1 {
298+
let ty = TypeNs::SelfType(*impl_);
299+
return Some(ResolveValueResult::Partial(ty, 1));
300+
} else {
301+
return Some(ResolveValueResult::ValueNs(ValueNs::ImplSelf(*impl_)));
302+
}
298303
}
299304
}
300-
Scope::AdtScope(adt) if n_segments > 1 => {
305+
Scope::AdtScope(adt) => {
306+
if n_segments == 1 {
307+
// bare `Self` doesn't work in the value namespace in a struct/enum definition
308+
continue;
309+
}
301310
if first_name == &name![Self] {
302311
let ty = TypeNs::AdtSelfType(*adt);
303312
return Some(ResolveValueResult::Partial(ty, 1));
304313
}
305314
}
306-
Scope::ImplDefScope(_) | Scope::AdtScope(_) => continue,
307315

308316
Scope::ModuleScope(m) => {
309317
let (module_def, idx) = m.crate_def_map.resolve_path(

crates/ra_hir_ty/src/infer.rs

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ use hir_def::{
2828
path::{path, Path},
2929
resolver::{HasResolver, Resolver, TypeNs},
3030
type_ref::{Mutability, TypeRef},
31-
AdtId, AssocItemId, DefWithBodyId, FieldId, FunctionId, TraitId, TypeAliasId, VariantId,
31+
AdtId, AssocItemId, DefWithBodyId, EnumVariantId, FieldId, FunctionId, TraitId, TypeAliasId,
32+
VariantId,
3233
};
3334
use hir_expand::{diagnostics::DiagnosticSink, name::name};
3435
use ra_arena::map::ArenaMap;
@@ -438,43 +439,95 @@ impl<'a> InferenceContext<'a> {
438439
let ctx = crate::lower::TyLoweringContext::new(self.db, &self.resolver);
439440
// FIXME: this should resolve assoc items as well, see this example:
440441
// https://play.rust-lang.org/?gist=087992e9e22495446c01c0d4e2d69521
441-
return match resolver.resolve_path_in_type_ns_fully(self.db.upcast(), path.mod_path()) {
442-
Some(TypeNs::AdtId(AdtId::StructId(strukt))) => {
442+
let (resolution, unresolved) =
443+
match resolver.resolve_path_in_type_ns(self.db.upcast(), path.mod_path()) {
444+
Some(it) => it,
445+
None => return (Ty::Unknown, None),
446+
};
447+
return match resolution {
448+
TypeNs::AdtId(AdtId::StructId(strukt)) => {
443449
let substs = Ty::substs_from_path(&ctx, path, strukt.into());
444450
let ty = self.db.ty(strukt.into());
445451
let ty = self.insert_type_vars(ty.subst(&substs));
446-
(ty, Some(strukt.into()))
452+
forbid_unresolved_segments((ty, Some(strukt.into())), unresolved)
447453
}
448-
Some(TypeNs::EnumVariantId(var)) => {
454+
TypeNs::EnumVariantId(var) => {
449455
let substs = Ty::substs_from_path(&ctx, path, var.into());
450456
let ty = self.db.ty(var.parent.into());
451457
let ty = self.insert_type_vars(ty.subst(&substs));
452-
(ty, Some(var.into()))
458+
forbid_unresolved_segments((ty, Some(var.into())), unresolved)
453459
}
454-
Some(TypeNs::SelfType(impl_id)) => {
460+
TypeNs::SelfType(impl_id) => {
455461
let generics = crate::utils::generics(self.db.upcast(), impl_id.into());
456462
let substs = Substs::type_params_for_generics(&generics);
457463
let ty = self.db.impl_self_ty(impl_id).subst(&substs);
458-
let variant = ty_variant(&ty);
459-
(ty, variant)
464+
match unresolved {
465+
None => {
466+
let variant = ty_variant(&ty);
467+
(ty, variant)
468+
}
469+
Some(1) => {
470+
let segment = path.mod_path().segments.last().unwrap();
471+
// this could be an enum variant or associated type
472+
if let Some((AdtId::EnumId(enum_id), _)) = ty.as_adt() {
473+
let enum_data = self.db.enum_data(enum_id);
474+
if let Some(local_id) = enum_data.variant(segment) {
475+
let variant = EnumVariantId { parent: enum_id, local_id };
476+
return (ty, Some(variant.into()));
477+
}
478+
}
479+
// FIXME potentially resolve assoc type
480+
(Ty::Unknown, None)
481+
}
482+
Some(_) => {
483+
// FIXME diagnostic
484+
(Ty::Unknown, None)
485+
}
486+
}
460487
}
461-
Some(TypeNs::TypeAliasId(it)) => {
488+
TypeNs::TypeAliasId(it) => {
462489
let substs = Substs::build_for_def(self.db, it)
463490
.fill(std::iter::repeat_with(|| self.table.new_type_var()))
464491
.build();
465492
let ty = self.db.ty(it.into()).subst(&substs);
466493
let variant = ty_variant(&ty);
467-
(ty, variant)
494+
forbid_unresolved_segments((ty, variant), unresolved)
495+
}
496+
TypeNs::AdtSelfType(_) => {
497+
// FIXME this could happen in array size expressions, once we're checking them
498+
(Ty::Unknown, None)
499+
}
500+
TypeNs::GenericParam(_) => {
501+
// FIXME potentially resolve assoc type
502+
(Ty::Unknown, None)
503+
}
504+
TypeNs::AdtId(AdtId::EnumId(_))
505+
| TypeNs::AdtId(AdtId::UnionId(_))
506+
| TypeNs::BuiltinType(_)
507+
| TypeNs::TraitId(_) => {
508+
// FIXME diagnostic
509+
(Ty::Unknown, None)
468510
}
469-
Some(_) | None => (Ty::Unknown, None),
470511
};
471512

513+
fn forbid_unresolved_segments(
514+
result: (Ty, Option<VariantId>),
515+
unresolved: Option<usize>,
516+
) -> (Ty, Option<VariantId>) {
517+
if unresolved.is_none() {
518+
result
519+
} else {
520+
// FIXME diagnostic
521+
(Ty::Unknown, None)
522+
}
523+
}
524+
472525
fn ty_variant(ty: &Ty) -> Option<VariantId> {
473526
ty.as_adt().and_then(|(adt_id, _)| match adt_id {
474527
AdtId::StructId(s) => Some(VariantId::StructId(s)),
475528
AdtId::UnionId(u) => Some(VariantId::UnionId(u)),
476529
AdtId::EnumId(_) => {
477-
// Error E0071, expected struct, variant or union type, found enum `Foo`
530+
// FIXME Error E0071, expected struct, variant or union type, found enum `Foo`
478531
None
479532
}
480533
})

crates/ra_hir_ty/src/infer/path.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::iter;
55
use hir_def::{
66
path::{Path, PathSegment},
77
resolver::{ResolveValueResult, Resolver, TypeNs, ValueNs},
8-
AssocContainerId, AssocItemId, Lookup,
8+
AdtId, AssocContainerId, AssocItemId, EnumVariantId, Lookup,
99
};
1010
use hir_expand::name::Name;
1111

@@ -77,6 +77,18 @@ impl<'a> InferenceContext<'a> {
7777

7878
it.into()
7979
}
80+
ValueNs::ImplSelf(impl_id) => {
81+
let generics = crate::utils::generics(self.db.upcast(), impl_id.into());
82+
let substs = Substs::type_params_for_generics(&generics);
83+
let ty = self.db.impl_self_ty(impl_id).subst(&substs);
84+
if let Some((AdtId::StructId(struct_id), _)) = ty.as_adt() {
85+
let ty = self.db.value_ty(struct_id.into()).subst(&substs);
86+
return Some(ty);
87+
} else {
88+
// FIXME: diagnostic, invalid Self reference
89+
return None;
90+
}
91+
}
8092
};
8193

8294
let ty = self.db.value_ty(typable);
@@ -199,6 +211,10 @@ impl<'a> InferenceContext<'a> {
199211
return None;
200212
}
201213

214+
if let Some(result) = self.resolve_enum_variant_on_ty(&ty, name, id) {
215+
return Some(result);
216+
}
217+
202218
let canonical_ty = self.canonicalizer().canonicalize_ty(ty.clone());
203219
let krate = self.resolver.krate()?;
204220
let traits_in_scope = self.resolver.traits_in_scope(self.db.upcast());
@@ -250,4 +266,21 @@ impl<'a> InferenceContext<'a> {
250266
},
251267
)
252268
}
269+
270+
fn resolve_enum_variant_on_ty(
271+
&mut self,
272+
ty: &Ty,
273+
name: &Name,
274+
id: ExprOrPatId,
275+
) -> Option<(ValueNs, Option<Substs>)> {
276+
let (enum_id, subst) = match ty.as_adt() {
277+
Some((AdtId::EnumId(e), subst)) => (e, subst),
278+
_ => return None,
279+
};
280+
let enum_data = self.db.enum_data(enum_id);
281+
let local_id = enum_data.variant(name)?;
282+
let variant = EnumVariantId { parent: enum_id, local_id };
283+
self.write_variant_resolution(id, variant.into());
284+
Some((ValueNs::EnumVariantId(variant), Some(subst.clone())))
285+
}
253286
}

crates/ra_hir_ty/src/tests/patterns.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,45 @@ fn test() {
368368
);
369369
}
370370

371+
#[test]
372+
fn enum_variant_through_self_in_pattern() {
373+
assert_snapshot!(
374+
infer(r#"
375+
enum E {
376+
A { x: usize },
377+
B(usize),
378+
C
379+
}
380+
381+
impl E {
382+
fn test() {
383+
match (loop {}) {
384+
Self::A { x } => { x; },
385+
Self::B(x) => { x; },
386+
Self::C => {},
387+
};
388+
}
389+
}
390+
"#),
391+
@r###"
392+
76..218 '{ ... }': ()
393+
86..211 'match ... }': ()
394+
93..100 'loop {}': !
395+
98..100 '{}': ()
396+
116..129 'Self::A { x }': E
397+
126..127 'x': usize
398+
133..139 '{ x; }': ()
399+
135..136 'x': usize
400+
153..163 'Self::B(x)': E
401+
161..162 'x': usize
402+
167..173 '{ x; }': ()
403+
169..170 'x': usize
404+
187..194 'Self::C': E
405+
198..200 '{}': ()
406+
"###
407+
);
408+
}
409+
371410
#[test]
372411
fn infer_generics_in_patterns() {
373412
assert_snapshot!(

crates/ra_hir_ty/src/tests/simple.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,50 @@ impl S {
575575
);
576576
}
577577

578+
#[test]
579+
fn infer_self_as_path() {
580+
assert_snapshot!(
581+
infer(r#"
582+
struct S1;
583+
struct S2(isize);
584+
enum E {
585+
V1,
586+
V2(u32),
587+
}
588+
589+
impl S1 {
590+
fn test() {
591+
Self;
592+
}
593+
}
594+
impl S2 {
595+
fn test() {
596+
Self(1);
597+
}
598+
}
599+
impl E {
600+
fn test() {
601+
Self::V1;
602+
Self::V2(1);
603+
}
604+
}
605+
"#),
606+
@r###"
607+
87..108 '{ ... }': ()
608+
97..101 'Self': S1
609+
135..159 '{ ... }': ()
610+
145..149 'Self': S2(isize) -> S2
611+
145..152 'Self(1)': S2
612+
150..151 '1': isize
613+
185..231 '{ ... }': ()
614+
195..203 'Self::V1': E
615+
213..221 'Self::V2': V2(u32) -> E
616+
213..224 'Self::V2(1)': E
617+
222..223 '1': u32
618+
"###
619+
);
620+
}
621+
578622
#[test]
579623
fn infer_binary_op() {
580624
assert_snapshot!(

0 commit comments

Comments
 (0)