mirror of
https://github.com/rust-lang/rust.git
synced 2025-01-23 13:13:17 +00:00
Consider trait to be in scope for trait-impl
This commit is contained in:
parent
35db5e99f6
commit
28ca371755
@ -337,22 +337,34 @@ impl Resolver {
|
||||
pub fn traits_in_scope(&self, db: &dyn DefDatabase) -> FxHashSet<TraitId> {
|
||||
let mut traits = FxHashSet::default();
|
||||
for scope in &self.scopes {
|
||||
if let Scope::ModuleScope(m) = scope {
|
||||
if let Some(prelude) = m.def_map.prelude() {
|
||||
let prelude_def_map = prelude.def_map(db);
|
||||
traits.extend(prelude_def_map[prelude.local_id].scope.traits());
|
||||
}
|
||||
traits.extend(m.def_map[m.module_id].scope.traits());
|
||||
|
||||
// Add all traits that are in scope because of the containing DefMaps
|
||||
m.def_map.with_ancestor_maps(db, m.module_id, &mut |def_map, module| {
|
||||
if let Some(prelude) = def_map.prelude() {
|
||||
match scope {
|
||||
Scope::ModuleScope(m) => {
|
||||
if let Some(prelude) = m.def_map.prelude() {
|
||||
let prelude_def_map = prelude.def_map(db);
|
||||
traits.extend(prelude_def_map[prelude.local_id].scope.traits());
|
||||
}
|
||||
traits.extend(def_map[module].scope.traits());
|
||||
None::<()>
|
||||
});
|
||||
traits.extend(m.def_map[m.module_id].scope.traits());
|
||||
|
||||
// Add all traits that are in scope because of the containing DefMaps
|
||||
m.def_map.with_ancestor_maps(db, m.module_id, &mut |def_map, module| {
|
||||
if let Some(prelude) = def_map.prelude() {
|
||||
let prelude_def_map = prelude.def_map(db);
|
||||
traits.extend(prelude_def_map[prelude.local_id].scope.traits());
|
||||
}
|
||||
traits.extend(def_map[module].scope.traits());
|
||||
None::<()>
|
||||
});
|
||||
}
|
||||
&Scope::ImplDefScope(impl_) => {
|
||||
if let Some(target_trait) = &db.impl_data(impl_).target_trait {
|
||||
if let Some(TypeNs::TraitId(trait_)) =
|
||||
self.resolve_path_in_type_ns_fully(db, target_trait.path.mod_path())
|
||||
{
|
||||
traits.insert(trait_);
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
traits
|
||||
|
@ -578,10 +578,12 @@ impl<'a> InferenceContext<'a> {
|
||||
}
|
||||
|
||||
fn resolve_ops_try_ok(&self) -> Option<TypeAliasId> {
|
||||
// FIXME resolve via lang_item once try v2 is stable
|
||||
let path = path![core::ops::Try];
|
||||
let trait_ = self.resolver.resolve_known_trait(self.db.upcast(), &path)?;
|
||||
let trait_data = self.db.trait_data(trait_);
|
||||
trait_data
|
||||
// FIXME remove once try v2 is stable
|
||||
.associated_type_by_name(&name![Ok])
|
||||
.or_else(|| trait_data.associated_type_by_name(&name![Output]))
|
||||
}
|
||||
|
@ -3630,3 +3630,33 @@ fn test<F: FnOnce()>(f: F) {
|
||||
"#]],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn trait_in_scope_of_trait_impl() {
|
||||
check_infer(
|
||||
r#"
|
||||
mod foo {
|
||||
pub trait Foo {
|
||||
fn foo(self);
|
||||
fn bar(self) -> usize { 0 }
|
||||
}
|
||||
}
|
||||
impl foo::Foo for u32 {
|
||||
fn foo(self) {
|
||||
let _x = self.bar();
|
||||
}
|
||||
}
|
||||
"#,
|
||||
expect![[r#"
|
||||
45..49 'self': Self
|
||||
67..71 'self': Self
|
||||
82..87 '{ 0 }': usize
|
||||
84..85 '0': usize
|
||||
131..135 'self': u32
|
||||
137..173 '{ ... }': ()
|
||||
151..153 '_x': usize
|
||||
156..160 'self': u32
|
||||
156..166 'self.bar()': usize
|
||||
"#]],
|
||||
);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user