Skip to content

Commit 3bd4fe9

Browse files
committed
Remove methodowner & fix formatting
1 parent 8af1dd7 commit 3bd4fe9

File tree

4 files changed

+27
-53
lines changed

4 files changed

+27
-53
lines changed

crates/hir/src/code_model.rs

+1-38
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use hir_ty::{
3535
traits::SolutionVariables,
3636
ApplicationTy, BoundVar, CallableDefId, Canonical, DebruijnIndex, FnSig, GenericPredicate,
3737
InEnvironment, Obligation, ProjectionPredicate, ProjectionTy, Substs, TraitEnvironment, Ty,
38-
TyDefId, TyKind, TypeCtor, TyLoweringContext,
38+
TyDefId, TyKind, TypeCtor,
3939
};
4040
use rustc_hash::FxHashSet;
4141
use stdx::impl_from;
@@ -186,15 +186,6 @@ impl_from!(
186186
for ModuleDef
187187
);
188188

189-
impl From<MethodOwner> for ModuleDef {
190-
fn from(mowner: MethodOwner) -> Self {
191-
match mowner {
192-
MethodOwner::Trait(t) => t.into(),
193-
MethodOwner::Adt(t) => t.into(),
194-
}
195-
}
196-
}
197-
198189
impl From<VariantDef> for ModuleDef {
199190
fn from(var: VariantDef) -> Self {
200191
match var {
@@ -778,36 +769,8 @@ impl Function {
778769
pub fn has_body(self, db: &dyn HirDatabase) -> bool {
779770
db.function_data(self.id).has_body
780771
}
781-
782-
/// If this function is a method, the trait or type where it is declared.
783-
pub fn method_owner(self, db: &dyn HirDatabase) -> Option<MethodOwner> {
784-
match self.as_assoc_item(db).map(|assoc| assoc.container(db)) {
785-
Some(AssocItemContainer::Trait(t)) => Some(t.into()),
786-
Some(AssocItemContainer::ImplDef(imp)) => {
787-
let resolver = ModuleId::from(imp.module(db)).resolver(db.upcast());
788-
let ctx = TyLoweringContext::new(db, &resolver);
789-
let adt = Ty::from_hir(
790-
&ctx,
791-
&imp.target_trait(db).unwrap_or_else(|| imp.target_type(db)),
792-
)
793-
.as_adt()
794-
.map(|t| t.0)
795-
.unwrap();
796-
Some(Adt::from(adt).into())
797-
}
798-
None => None,
799-
}
800-
}
801772
}
802773

803-
#[derive(Debug)]
804-
pub enum MethodOwner {
805-
Trait(Trait),
806-
Adt(Adt),
807-
}
808-
809-
impl_from!(Trait, Adt for MethodOwner);
810-
811774
// Note: logically, this belongs to `hir_ty`, but we are not using it there yet.
812775
pub enum Access {
813776
Shared,

crates/hir/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ pub use crate::{
3535
code_model::{
3636
Access, Adt, AsAssocItem, AssocItem, AssocItemContainer, Callable, CallableKind, Const,
3737
Crate, CrateDependency, DefWithBody, Enum, EnumVariant, Field, FieldSource, Function,
38-
GenericDef, HasVisibility, ImplDef, Local, MacroDef, MethodOwner, Module, ModuleDef,
39-
ScopeDef, Static, Struct, Trait, Type, TypeAlias, TypeParam, Union, VariantDef, Visibility,
38+
GenericDef, HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, ScopeDef, Static,
39+
Struct, Trait, Type, TypeAlias, TypeParam, Union, VariantDef, Visibility,
4040
},
4141
has_source::HasSource,
4242
semantics::{original_range, PathResolution, Semantics, SemanticsScope},

crates/ide/src/doc_links.rs

+23-12
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
use std::iter::once;
44

55
use itertools::Itertools;
6-
use pulldown_cmark_to_cmark::{cmark_with_options, Options as CmarkOptions};
76
use pulldown_cmark::{CowStr, Event, LinkType, Options, Parser, Tag};
7+
use pulldown_cmark_to_cmark::{cmark_with_options, Options as CmarkOptions};
88
use url::Url;
99

1010
use hir::{
1111
db::{DefDatabase, HirDatabase},
12-
Adt, AsName, AssocItem, Crate, Field, HasAttrs, ItemInNs, ModuleDef, AssocItemContainer, AsAssocItem
12+
Adt, AsAssocItem, AsName, AssocItem, AssocItemContainer, Crate, Field, HasAttrs, ItemInNs,
13+
ModuleDef,
1314
};
1415
use ide_db::{
1516
defs::{classify_name, classify_name_ref, Definition},
@@ -97,18 +98,23 @@ pub fn remove_links(markdown: &str) -> String {
9798
// BUG: For Option::Some
9899
// Returns https://doc.rust-lang.org/nightly/core/prelude/v1/enum.Option.html#variant.Some
99100
// Instead of https://doc.rust-lang.org/nightly/core/option/enum.Option.html
100-
// This could be worked around by turning the `EnumVariant` into `Enum` before attempting resolution,
101-
// but it's really just working around the problem. Ideally we need to implement a slightly different
102-
// version of import map which follows the same process as rustdoc. Otherwise there'll always be some
103-
// edge cases where we select the wrong import path.
101+
//
102+
// This should cease to be a problem if RFC2988 (Stable Rustdoc URLs) is implemented
103+
// https://github.com/rust-lang/rfcs/pull/2988
104104
fn get_doc_link(db: &RootDatabase, definition: Definition) -> Option<String> {
105105
// Get the outermost definition for the moduledef. This is used to resolve the public path to the type,
106106
// then we can join the method, field, etc onto it if required.
107107
let target_def: ModuleDef = match definition {
108108
Definition::ModuleDef(moddef) => match moddef {
109-
ModuleDef::Function(f) => {
110-
f.method_owner(db).map(|mowner| mowner.into()).unwrap_or_else(|| f.clone().into())
111-
}
109+
ModuleDef::Function(f) => f
110+
.as_assoc_item(db)
111+
.and_then(|assoc| match assoc.container(db) {
112+
AssocItemContainer::Trait(t) => Some(t.into()),
113+
AssocItemContainer::ImplDef(impld) => {
114+
impld.target_ty(db).as_adt().map(|adt| adt.into())
115+
}
116+
})
117+
.unwrap_or_else(|| f.clone().into()),
112118
moddef => moddef,
113119
},
114120
Definition::Field(f) => f.parent_def(db).into(),
@@ -211,7 +217,10 @@ fn rewrite_url_link(db: &RootDatabase, def: ModuleDef, target: &str) -> Option<S
211217
}
212218

213219
/// Retrieve a link to documentation for the given symbol.
214-
pub(crate) fn external_docs(db: &RootDatabase, position: &FilePosition) -> Option<DocumentationLink> {
220+
pub(crate) fn external_docs(
221+
db: &RootDatabase,
222+
position: &FilePosition,
223+
) -> Option<DocumentationLink> {
215224
let sema = Semantics::new(db);
216225
let file = sema.parse(position.file_id).syntax().clone();
217226
let token = pick_best(file.token_at_offset(position.offset))?;
@@ -392,8 +401,10 @@ fn get_symbol_fragment(db: &dyn HirDatabase, field_or_assoc: &FieldOrAssocItem)
392401
FieldOrAssocItem::Field(field) => format!("#structfield.{}", field.name(db)),
393402
FieldOrAssocItem::AssocItem(assoc) => match assoc {
394403
AssocItem::Function(function) => {
395-
let is_trait_method =
396-
matches!(function.as_assoc_item(db).map(|assoc| assoc.container(db)), Some(AssocItemContainer::Trait(..)));
404+
let is_trait_method = matches!(
405+
function.as_assoc_item(db).map(|assoc| assoc.container(db)),
406+
Some(AssocItemContainer::Trait(..))
407+
);
397408
// This distinction may get more complicated when specialisation is available.
398409
// Rustdoc makes this decision based on whether a method 'has defaultness'.
399410
// Currently this is only the case for provided trait methods.

crates/ide/src/hover.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use test_utils::mark;
1414

1515
use crate::{
1616
display::{macro_label, ShortLabel, ToNav, TryToNav},
17-
markdown_remove::remove_markdown,
1817
doc_links::{remove_links, rewrite_links},
18+
markdown_remove::remove_markdown,
1919
markup::Markup,
2020
runnables::runnable,
2121
FileId, FilePosition, NavigationTarget, RangeInfo, Runnable,

0 commit comments

Comments
 (0)