Unnamed repository; edit this file 'description' to name the repository.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
//! Attributes & documentation for hir types.

use cfg::CfgExpr;
use either::Either;
use hir_def::{
    AssocItemId, AttrDefId, FieldId, GenericDefId, ItemContainerId, LifetimeParamId, ModuleDefId,
    TraitId, TypeOrConstParamId,
    attrs::{AttrFlags, Docs, IsInnerDoc},
    expr_store::path::Path,
    item_scope::ItemInNs,
    per_ns::Namespace,
    resolver::{HasResolver, Resolver, TypeNs},
};
use hir_expand::{
    mod_path::{ModPath, PathKind},
    name::Name,
};
use hir_ty::{
    db::HirDatabase,
    method_resolution::{
        self, CandidateId, MethodError, MethodResolutionContext, MethodResolutionUnstableFeatures,
    },
    next_solver::{DbInterner, TypingMode, infer::DbInternerInferExt},
};
use intern::Symbol;
use stdx::never;

use crate::{
    Adt, AsAssocItem, AssocItem, BuiltinType, Const, ConstParam, DocLinkDef, Enum, ExternCrateDecl,
    Field, Function, GenericParam, HasCrate, Impl, LangItem, LifetimeParam, Macro, Module,
    ModuleDef, Static, Struct, Trait, Type, TypeAlias, TypeParam, Union, Variant, VariantDef,
};

#[derive(Debug, Clone, Copy)]
pub enum AttrsOwner {
    AttrDef(AttrDefId),
    Field(FieldId),
    LifetimeParam(LifetimeParamId),
    TypeOrConstParam(TypeOrConstParamId),
    /// Things that do not have attributes. Used for builtin derives.
    Dummy,
}

impl AttrsOwner {
    #[inline]
    fn attr_def(&self) -> Option<AttrDefId> {
        match self {
            AttrsOwner::AttrDef(it) => Some(*it),
            _ => None,
        }
    }
}

#[derive(Debug, Clone)]
pub struct AttrsWithOwner {
    pub(crate) attrs: AttrFlags,
    owner: AttrsOwner,
}

impl AttrsWithOwner {
    fn new(db: &dyn HirDatabase, owner: AttrDefId) -> Self {
        Self { attrs: AttrFlags::query(db, owner), owner: AttrsOwner::AttrDef(owner) }
    }

    fn new_field(db: &dyn HirDatabase, owner: FieldId) -> Self {
        Self { attrs: AttrFlags::query_field(db, owner), owner: AttrsOwner::Field(owner) }
    }

    fn new_lifetime_param(db: &dyn HirDatabase, owner: LifetimeParamId) -> Self {
        Self {
            attrs: AttrFlags::query_lifetime_param(db, owner),
            owner: AttrsOwner::LifetimeParam(owner),
        }
    }
    fn new_type_or_const_param(db: &dyn HirDatabase, owner: TypeOrConstParamId) -> Self {
        Self {
            attrs: AttrFlags::query_type_or_const_param(db, owner),
            owner: AttrsOwner::TypeOrConstParam(owner),
        }
    }

    #[inline]
    pub fn is_unstable(&self) -> bool {
        self.attrs.contains(AttrFlags::IS_UNSTABLE)
    }

    #[inline]
    pub fn is_macro_export(&self) -> bool {
        self.attrs.contains(AttrFlags::IS_MACRO_EXPORT)
    }

    #[inline]
    pub fn is_doc_notable_trait(&self) -> bool {
        self.attrs.contains(AttrFlags::IS_DOC_NOTABLE_TRAIT)
    }

    #[inline]
    pub fn is_doc_hidden(&self) -> bool {
        self.attrs.contains(AttrFlags::IS_DOC_HIDDEN)
    }

    #[inline]
    pub fn is_deprecated(&self) -> bool {
        self.attrs.contains(AttrFlags::IS_DEPRECATED)
    }

    #[inline]
    pub fn is_non_exhaustive(&self) -> bool {
        self.attrs.contains(AttrFlags::NON_EXHAUSTIVE)
    }

    #[inline]
    pub fn is_test(&self) -> bool {
        self.attrs.contains(AttrFlags::IS_TEST)
    }

    #[inline]
    pub fn lang(&self, db: &dyn HirDatabase) -> Option<LangItem> {
        self.owner
            .attr_def()
            .and_then(|owner| self.attrs.lang_item_with_attrs(db, owner))
            .and_then(|lang| LangItem::from_symbol(&lang))
    }

    #[inline]
    pub fn doc_aliases<'db>(&self, db: &'db dyn HirDatabase) -> &'db [Symbol] {
        let owner = match self.owner {
            AttrsOwner::AttrDef(it) => Either::Left(it),
            AttrsOwner::Field(it) => Either::Right(it),
            AttrsOwner::LifetimeParam(_) | AttrsOwner::TypeOrConstParam(_) | AttrsOwner::Dummy => {
                return &[];
            }
        };
        self.attrs.doc_aliases(db, owner)
    }

    #[inline]
    pub fn cfgs<'db>(&self, db: &'db dyn HirDatabase) -> Option<&'db CfgExpr> {
        let owner = match self.owner {
            AttrsOwner::AttrDef(it) => Either::Left(it),
            AttrsOwner::Field(it) => Either::Right(it),
            AttrsOwner::LifetimeParam(_) | AttrsOwner::TypeOrConstParam(_) | AttrsOwner::Dummy => {
                return None;
            }
        };
        self.attrs.cfgs(db, owner)
    }

    #[inline]
    pub fn hir_docs<'db>(&self, db: &'db dyn HirDatabase) -> Option<&'db Docs> {
        match self.owner {
            AttrsOwner::AttrDef(it) => AttrFlags::docs(db, it).as_deref(),
            AttrsOwner::Field(it) => AttrFlags::field_docs(db, it),
            AttrsOwner::LifetimeParam(_) | AttrsOwner::TypeOrConstParam(_) | AttrsOwner::Dummy => {
                None
            }
        }
    }
}

pub trait HasAttrs: Sized {
    #[inline]
    fn attrs(self, db: &dyn HirDatabase) -> AttrsWithOwner {
        match self.attr_id(db) {
            AttrsOwner::AttrDef(it) => AttrsWithOwner::new(db, it),
            AttrsOwner::Field(it) => AttrsWithOwner::new_field(db, it),
            AttrsOwner::LifetimeParam(it) => AttrsWithOwner::new_lifetime_param(db, it),
            AttrsOwner::TypeOrConstParam(it) => AttrsWithOwner::new_type_or_const_param(db, it),
            AttrsOwner::Dummy => {
                AttrsWithOwner { attrs: AttrFlags::empty(), owner: AttrsOwner::Dummy }
            }
        }
    }

    #[doc(hidden)]
    fn attr_id(self, db: &dyn HirDatabase) -> AttrsOwner;

    #[inline]
    fn hir_docs(self, db: &dyn HirDatabase) -> Option<&Docs> {
        match self.attr_id(db) {
            AttrsOwner::AttrDef(it) => AttrFlags::docs(db, it).as_deref(),
            AttrsOwner::Field(it) => AttrFlags::field_docs(db, it),
            AttrsOwner::LifetimeParam(_) | AttrsOwner::TypeOrConstParam(_) | AttrsOwner::Dummy => {
                None
            }
        }
    }
}

macro_rules! impl_has_attrs {
    ($(($def:ident, $def_id:ident),)*) => {$(
        impl HasAttrs for $def {
            #[inline]
            fn attr_id(self, _db: &dyn HirDatabase) -> AttrsOwner {
                AttrsOwner::AttrDef(AttrDefId::$def_id(self.into()))
            }
        }
    )*};
}

impl_has_attrs![
    (Variant, EnumVariantId),
    (Static, StaticId),
    (Const, ConstId),
    (Trait, TraitId),
    (TypeAlias, TypeAliasId),
    (Macro, MacroId),
    (Adt, AdtId),
    (ExternCrateDecl, ExternCrateId),
];

impl HasAttrs for Function {
    fn attr_id(self, _db: &dyn HirDatabase) -> AttrsOwner {
        match self.id {
            crate::AnyFunctionId::FunctionId(id) => AttrsOwner::AttrDef(id.into()),
            crate::AnyFunctionId::BuiltinDeriveImplMethod { .. } => AttrsOwner::Dummy,
        }
    }
}

impl HasAttrs for Impl {
    fn attr_id(self, _db: &dyn HirDatabase) -> AttrsOwner {
        match self.id {
            hir_ty::next_solver::AnyImplId::ImplId(id) => AttrsOwner::AttrDef(id.into()),
            hir_ty::next_solver::AnyImplId::BuiltinDeriveImplId(..) => AttrsOwner::Dummy,
        }
    }
}

macro_rules! impl_has_attrs_enum {
    ($($variant:ident),* for $enum:ident) => {$(
        impl HasAttrs for $variant {
            #[inline]
            fn attr_id(self, db: &dyn HirDatabase) -> AttrsOwner {
                $enum::$variant(self).attr_id(db)
            }
        }
    )*};
}

impl_has_attrs_enum![Struct, Union, Enum for Adt];
impl_has_attrs_enum![TypeParam, ConstParam, LifetimeParam for GenericParam];

impl HasAttrs for Module {
    #[inline]
    fn attr_id(self, _: &dyn HirDatabase) -> AttrsOwner {
        AttrsOwner::AttrDef(AttrDefId::ModuleId(self.id))
    }
}

impl HasAttrs for GenericParam {
    #[inline]
    fn attr_id(self, _db: &dyn HirDatabase) -> AttrsOwner {
        match self {
            GenericParam::TypeParam(it) => AttrsOwner::TypeOrConstParam(it.merge().into()),
            GenericParam::ConstParam(it) => AttrsOwner::TypeOrConstParam(it.merge().into()),
            GenericParam::LifetimeParam(it) => AttrsOwner::LifetimeParam(it.into()),
        }
    }
}

impl HasAttrs for AssocItem {
    #[inline]
    fn attr_id(self, db: &dyn HirDatabase) -> AttrsOwner {
        match self {
            AssocItem::Function(it) => it.attr_id(db),
            AssocItem::Const(it) => it.attr_id(db),
            AssocItem::TypeAlias(it) => it.attr_id(db),
        }
    }
}

impl HasAttrs for crate::Crate {
    #[inline]
    fn attr_id(self, db: &dyn HirDatabase) -> AttrsOwner {
        self.root_module(db).attr_id(db)
    }
}

impl HasAttrs for Field {
    #[inline]
    fn attr_id(self, _db: &dyn HirDatabase) -> AttrsOwner {
        AttrsOwner::Field(self.into())
    }
}

/// Resolves the item `link` points to in the scope of `def`.
pub fn resolve_doc_path_on(
    db: &dyn HirDatabase,
    def: impl HasAttrs + Copy,
    link: &str,
    ns: Option<Namespace>,
    is_inner_doc: IsInnerDoc,
) -> Option<DocLinkDef> {
    resolve_doc_path_on_(db, link, def.attr_id(db), ns, is_inner_doc)
}

fn resolve_doc_path_on_(
    db: &dyn HirDatabase,
    link: &str,
    attr_id: AttrsOwner,
    ns: Option<Namespace>,
    is_inner_doc: IsInnerDoc,
) -> Option<DocLinkDef> {
    let resolver = match attr_id {
        AttrsOwner::AttrDef(AttrDefId::ModuleId(it)) => {
            if is_inner_doc.yes() {
                it.resolver(db)
            } else if let Some(parent) = Module::from(it).parent(db) {
                parent.id.resolver(db)
            } else {
                it.resolver(db)
            }
        }
        AttrsOwner::AttrDef(AttrDefId::AdtId(it)) => it.resolver(db),
        AttrsOwner::AttrDef(AttrDefId::FunctionId(it)) => it.resolver(db),
        AttrsOwner::AttrDef(AttrDefId::EnumVariantId(it)) => it.resolver(db),
        AttrsOwner::AttrDef(AttrDefId::StaticId(it)) => it.resolver(db),
        AttrsOwner::AttrDef(AttrDefId::ConstId(it)) => it.resolver(db),
        AttrsOwner::AttrDef(AttrDefId::TraitId(it)) => it.resolver(db),
        AttrsOwner::AttrDef(AttrDefId::TypeAliasId(it)) => it.resolver(db),
        AttrsOwner::AttrDef(AttrDefId::ImplId(it)) => it.resolver(db),
        AttrsOwner::AttrDef(AttrDefId::ExternBlockId(it)) => it.resolver(db),
        AttrsOwner::AttrDef(AttrDefId::UseId(it)) => it.resolver(db),
        AttrsOwner::AttrDef(AttrDefId::MacroId(it)) => it.resolver(db),
        AttrsOwner::AttrDef(AttrDefId::ExternCrateId(it)) => it.resolver(db),
        AttrsOwner::Field(it) => it.parent.resolver(db),
        AttrsOwner::LifetimeParam(_) | AttrsOwner::TypeOrConstParam(_) | AttrsOwner::Dummy => {
            return None;
        }
    };

    let mut modpath = doc_modpath_from_str(link)?;

    let resolved = resolver.resolve_module_path_in_items(db, &modpath);
    if resolved.is_none() {
        let last_name = modpath.pop_segment()?;
        resolve_assoc_or_field(db, resolver, modpath, last_name, ns)
    } else {
        let def = match ns {
            Some(Namespace::Types) => resolved.take_types(),
            Some(Namespace::Values) => resolved.take_values(),
            Some(Namespace::Macros) => resolved.take_macros().map(ModuleDefId::MacroId),
            None => resolved.iter_items().next().map(|(it, _)| match it {
                ItemInNs::Types(it) => it,
                ItemInNs::Values(it) => it,
                ItemInNs::Macros(it) => ModuleDefId::MacroId(it),
            }),
        };
        Some(DocLinkDef::ModuleDef(def?.into()))
    }
}

fn resolve_assoc_or_field(
    db: &dyn HirDatabase,
    resolver: Resolver<'_>,
    path: ModPath,
    name: Name,
    ns: Option<Namespace>,
) -> Option<DocLinkDef> {
    let path = Path::from_known_path_with_no_generic(path);
    let base_def = resolver.resolve_path_in_type_ns_fully(db, &path)?;

    let handle_trait = |id: TraitId| {
        // Doc paths in this context may only resolve to an item of this trait
        // (i.e. no items of its supertraits), so we need to handle them here
        // independently of others.
        id.trait_items(db).items.iter().find(|it| it.0 == name).map(|(_, assoc_id)| {
            let def = match *assoc_id {
                AssocItemId::FunctionId(it) => ModuleDef::Function(it.into()),
                AssocItemId::ConstId(it) => ModuleDef::Const(it.into()),
                AssocItemId::TypeAliasId(it) => ModuleDef::TypeAlias(it.into()),
            };
            DocLinkDef::ModuleDef(def)
        })
    };
    let ty = match base_def {
        TypeNs::SelfType(id) => Impl::from(id).self_ty(db),
        TypeNs::GenericParam(param) => {
            let generic_params = db.generic_params(param.parent());
            if generic_params[param.local_id()].is_trait_self() {
                // `Self::assoc` in traits should refer to the trait itself.
                let parent_trait = |container| match container {
                    ItemContainerId::TraitId(trait_) => handle_trait(trait_),
                    _ => {
                        never!("container {container:?} should be a trait");
                        None
                    }
                };
                return match param.parent() {
                    GenericDefId::TraitId(trait_) => handle_trait(trait_),
                    GenericDefId::ConstId(it) => parent_trait(it.loc(db).container),
                    GenericDefId::FunctionId(it) => parent_trait(it.loc(db).container),
                    GenericDefId::TypeAliasId(it) => parent_trait(it.loc(db).container),
                    _ => {
                        never!("type param {param:?} should belong to a trait");
                        None
                    }
                };
            }

            // Even if this generic parameter has some trait bounds, rustdoc doesn't
            // resolve `name` to trait items.
            return None;
        }
        TypeNs::AdtId(id) | TypeNs::AdtSelfType(id) => Adt::from(id).ty(db),
        TypeNs::EnumVariantId(id) => {
            // Enum variants don't have path candidates.
            let variant = Variant::from(id);
            return resolve_field(db, variant.into(), name, ns);
        }
        TypeNs::TypeAliasId(id) => {
            let alias = TypeAlias::from(id);
            if alias.as_assoc_item(db).is_some() {
                // We don't normalize associated type aliases, so we have nothing to
                // resolve `name` to.
                return None;
            }
            alias.ty(db)
        }
        TypeNs::BuiltinType(id) => BuiltinType::from(id).ty(db),
        TypeNs::TraitId(id) => return handle_trait(id),
        TypeNs::ModuleId(_) => {
            return None;
        }
    };

    // Resolve inherent items first, then trait items, then fields.
    if let Some(assoc_item_def) = resolve_assoc_item(db, &ty, &name, ns) {
        return Some(assoc_item_def);
    }

    if let Some(impl_trait_item_def) = resolve_impl_trait_item(db, resolver, &ty, &name, ns) {
        return Some(impl_trait_item_def);
    }

    let variant_def = match ty.as_adt()? {
        Adt::Struct(it) => it.into(),
        Adt::Union(it) => it.into(),
        Adt::Enum(enum_) => {
            // Can happen on `Self::Variant` (otherwise would be fully resolved by the resolver).
            return enum_
                .id
                .enum_variants(db)
                .variant(&name)
                .map(|variant| DocLinkDef::ModuleDef(ModuleDef::Variant(variant.into())));
        }
    };
    resolve_field(db, variant_def, name, ns)
}

fn resolve_assoc_item<'db>(
    db: &'db dyn HirDatabase,
    ty: &Type<'db>,
    name: &Name,
    ns: Option<Namespace>,
) -> Option<DocLinkDef> {
    ty.iterate_assoc_items(db, move |assoc_item| {
        if assoc_item.name(db)? != *name {
            return None;
        }
        as_module_def_if_namespace_matches(assoc_item, ns)
    })
}

fn resolve_impl_trait_item<'db>(
    db: &'db dyn HirDatabase,
    resolver: Resolver<'_>,
    ty: &Type<'db>,
    name: &Name,
    ns: Option<Namespace>,
) -> Option<DocLinkDef> {
    let krate = ty.krate(db);
    let environment = crate::param_env_from_resolver(db, &resolver);
    let traits_in_scope = resolver.traits_in_scope(db);

    // `ty.iterate_path_candidates()` require a scope, which is not available when resolving
    // attributes here. Use path resolution directly instead.
    //
    // FIXME: resolve type aliases (which are not yielded by iterate_path_candidates)
    let interner = DbInterner::new_with(db, environment.krate);
    let infcx = interner.infer_ctxt().build(TypingMode::PostAnalysis);
    let unstable_features =
        MethodResolutionUnstableFeatures::from_def_map(resolver.top_level_def_map());
    let ctx = MethodResolutionContext {
        infcx: &infcx,
        resolver: &resolver,
        param_env: environment.param_env,
        traits_in_scope: &traits_in_scope,
        edition: krate.edition(db),
        unstable_features: &unstable_features,
    };
    let resolution = ctx.probe_for_name(method_resolution::Mode::Path, name.clone(), ty.ty);
    let resolution = match resolution {
        Ok(resolution) => resolution.item,
        Err(MethodError::PrivateMatch(resolution)) => resolution.item,
        _ => return None,
    };
    let resolution = match resolution {
        CandidateId::FunctionId(id) => AssocItem::Function(id.into()),
        CandidateId::ConstId(id) => AssocItem::Const(id.into()),
    };
    as_module_def_if_namespace_matches(resolution, ns)
}

fn resolve_field(
    db: &dyn HirDatabase,
    def: VariantDef,
    name: Name,
    ns: Option<Namespace>,
) -> Option<DocLinkDef> {
    if let Some(Namespace::Types | Namespace::Macros) = ns {
        return None;
    }
    def.fields(db).into_iter().find(|f| f.name(db) == name).map(DocLinkDef::Field)
}

fn as_module_def_if_namespace_matches(
    assoc_item: AssocItem,
    ns: Option<Namespace>,
) -> Option<DocLinkDef> {
    let (def, expected_ns) = match assoc_item {
        AssocItem::Function(it) => (ModuleDef::Function(it), Namespace::Values),
        AssocItem::Const(it) => (ModuleDef::Const(it), Namespace::Values),
        AssocItem::TypeAlias(it) => (ModuleDef::TypeAlias(it), Namespace::Types),
    };

    (ns.unwrap_or(expected_ns) == expected_ns).then_some(DocLinkDef::ModuleDef(def))
}

fn doc_modpath_from_str(link: &str) -> Option<ModPath> {
    // FIXME: this is not how we should get a mod path here.
    let try_get_modpath = |link: &str| {
        let mut parts = link.split("::");
        let mut first_segment = None;
        let kind = match parts.next()? {
            "" => PathKind::Abs,
            "crate" => PathKind::Crate,
            "self" => PathKind::SELF,
            "super" => {
                let mut deg = 1;
                for segment in parts.by_ref() {
                    if segment == "super" {
                        deg += 1;
                    } else {
                        first_segment = Some(segment);
                        break;
                    }
                }
                PathKind::Super(deg)
            }
            segment => {
                first_segment = Some(segment);
                PathKind::Plain
            }
        };
        let parts = first_segment.into_iter().chain(parts).map(|segment| match segment.parse() {
            Ok(idx) => Name::new_tuple_field(idx),
            Err(_) => Name::new_root(segment.split_once('<').map_or(segment, |it| it.0)),
        });
        Some(ModPath::from_segments(kind, parts))
    };
    try_get_modpath(link)
}