rustdoc/passes/
collect_intra_doc_links.rs

1//! This module implements [RFC 1946]: Intra-rustdoc-links
2//!
3//! [RFC 1946]: https://github.com/rust-lang/rfcs/blob/master/text/1946-intra-rustdoc-links.md
4
5use std::borrow::Cow;
6use std::fmt::Display;
7use std::mem;
8use std::ops::Range;
9
10use pulldown_cmark::LinkType;
11use rustc_ast::util::comments::may_have_doc_links;
12use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
13use rustc_data_structures::intern::Interned;
14use rustc_errors::{Applicability, Diag, DiagMessage};
15use rustc_hir::def::Namespace::*;
16use rustc_hir::def::{DefKind, Namespace, PerNS};
17use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LOCAL_CRATE};
18use rustc_hir::{Mutability, Safety};
19use rustc_middle::ty::{Ty, TyCtxt};
20use rustc_middle::{bug, span_bug, ty};
21use rustc_resolve::rustdoc::{
22    MalformedGenerics, has_primitive_or_keyword_docs, prepare_to_doc_link_resolution,
23    source_span_for_markdown_range, strip_generics_from_path,
24};
25use rustc_session::config::CrateType;
26use rustc_session::lint::Lint;
27use rustc_span::BytePos;
28use rustc_span::hygiene::MacroKind;
29use rustc_span::symbol::{Ident, Symbol, sym};
30use smallvec::{SmallVec, smallvec};
31use tracing::{debug, info, instrument, trace};
32
33use crate::clean::utils::find_nearest_parent_module;
34use crate::clean::{self, Crate, Item, ItemId, ItemLink, PrimitiveType};
35use crate::core::DocContext;
36use crate::html::markdown::{MarkdownLink, MarkdownLinkRange, markdown_links};
37use crate::lint::{BROKEN_INTRA_DOC_LINKS, PRIVATE_INTRA_DOC_LINKS};
38use crate::passes::Pass;
39use crate::visit::DocVisitor;
40
41pub(crate) const COLLECT_INTRA_DOC_LINKS: Pass =
42    Pass { name: "collect-intra-doc-links", run: None, description: "resolves intra-doc links" };
43
44pub(crate) fn collect_intra_doc_links<'a, 'tcx>(
45    krate: Crate,
46    cx: &'a mut DocContext<'tcx>,
47) -> (Crate, LinkCollector<'a, 'tcx>) {
48    let mut collector = LinkCollector {
49        cx,
50        visited_links: FxHashMap::default(),
51        ambiguous_links: FxIndexMap::default(),
52    };
53    collector.visit_crate(&krate);
54    (krate, collector)
55}
56
57fn filter_assoc_items_by_name_and_namespace(
58    tcx: TyCtxt<'_>,
59    assoc_items_of: DefId,
60    ident: Ident,
61    ns: Namespace,
62) -> impl Iterator<Item = &ty::AssocItem> {
63    tcx.associated_items(assoc_items_of).filter_by_name_unhygienic(ident.name).filter(move |item| {
64        item.namespace() == ns && tcx.hygienic_eq(ident, item.ident(tcx), assoc_items_of)
65    })
66}
67
68#[derive(Copy, Clone, Debug, Hash, PartialEq)]
69pub(crate) enum Res {
70    Def(DefKind, DefId),
71    Primitive(PrimitiveType),
72}
73
74type ResolveRes = rustc_hir::def::Res<rustc_ast::NodeId>;
75
76impl Res {
77    fn descr(self) -> &'static str {
78        match self {
79            Res::Def(kind, id) => ResolveRes::Def(kind, id).descr(),
80            Res::Primitive(_) => "primitive type",
81        }
82    }
83
84    fn article(self) -> &'static str {
85        match self {
86            Res::Def(kind, id) => ResolveRes::Def(kind, id).article(),
87            Res::Primitive(_) => "a",
88        }
89    }
90
91    fn name(self, tcx: TyCtxt<'_>) -> Symbol {
92        match self {
93            Res::Def(_, id) => tcx.item_name(id),
94            Res::Primitive(prim) => prim.as_sym(),
95        }
96    }
97
98    fn def_id(self, tcx: TyCtxt<'_>) -> Option<DefId> {
99        match self {
100            Res::Def(_, id) => Some(id),
101            Res::Primitive(prim) => PrimitiveType::primitive_locations(tcx).get(&prim).copied(),
102        }
103    }
104
105    fn from_def_id(tcx: TyCtxt<'_>, def_id: DefId) -> Res {
106        Res::Def(tcx.def_kind(def_id), def_id)
107    }
108
109    /// Used for error reporting.
110    fn disambiguator_suggestion(self) -> Suggestion {
111        let kind = match self {
112            Res::Primitive(_) => return Suggestion::Prefix("prim"),
113            Res::Def(kind, _) => kind,
114        };
115
116        let prefix = match kind {
117            DefKind::Fn | DefKind::AssocFn => return Suggestion::Function,
118            DefKind::Macro(MacroKind::Bang) => return Suggestion::Macro,
119
120            DefKind::Macro(MacroKind::Derive) => "derive",
121            DefKind::Struct => "struct",
122            DefKind::Enum => "enum",
123            DefKind::Trait => "trait",
124            DefKind::Union => "union",
125            DefKind::Mod => "mod",
126            DefKind::Const | DefKind::ConstParam | DefKind::AssocConst | DefKind::AnonConst => {
127                "const"
128            }
129            DefKind::Static { .. } => "static",
130            DefKind::Field => "field",
131            DefKind::Variant | DefKind::Ctor(..) => "variant",
132            // Now handle things that don't have a specific disambiguator
133            _ => match kind
134                .ns()
135                .expect("tried to calculate a disambiguator for a def without a namespace?")
136            {
137                Namespace::TypeNS => "type",
138                Namespace::ValueNS => "value",
139                Namespace::MacroNS => "macro",
140            },
141        };
142
143        Suggestion::Prefix(prefix)
144    }
145}
146
147impl TryFrom<ResolveRes> for Res {
148    type Error = ();
149
150    fn try_from(res: ResolveRes) -> Result<Self, ()> {
151        use rustc_hir::def::Res::*;
152        match res {
153            Def(kind, id) => Ok(Res::Def(kind, id)),
154            PrimTy(prim) => Ok(Res::Primitive(PrimitiveType::from_hir(prim))),
155            // e.g. `#[derive]`
156            ToolMod | NonMacroAttr(..) | Err => Result::Err(()),
157            other => bug!("unrecognized res {other:?}"),
158        }
159    }
160}
161
162/// The link failed to resolve. [`resolution_failure`] should look to see if there's
163/// a more helpful error that can be given.
164#[derive(Debug)]
165struct UnresolvedPath<'a> {
166    /// Item on which the link is resolved, used for resolving `Self`.
167    item_id: DefId,
168    /// The scope the link was resolved in.
169    module_id: DefId,
170    /// If part of the link resolved, this has the `Res`.
171    ///
172    /// In `[std::io::Error::x]`, `std::io::Error` would be a partial resolution.
173    partial_res: Option<Res>,
174    /// The remaining unresolved path segments.
175    ///
176    /// In `[std::io::Error::x]`, `x` would be unresolved.
177    unresolved: Cow<'a, str>,
178}
179
180#[derive(Debug)]
181enum ResolutionFailure<'a> {
182    /// This resolved, but with the wrong namespace.
183    WrongNamespace {
184        /// What the link resolved to.
185        res: Res,
186        /// The expected namespace for the resolution, determined from the link's disambiguator.
187        ///
188        /// E.g., for `[fn@Result]` this is [`Namespace::ValueNS`],
189        /// even though `Result`'s actual namespace is [`Namespace::TypeNS`].
190        expected_ns: Namespace,
191    },
192    NotResolved(UnresolvedPath<'a>),
193}
194
195#[derive(Clone, Debug, Hash, PartialEq, Eq)]
196pub(crate) enum UrlFragment {
197    Item(DefId),
198    /// A part of a page that isn't a rust item.
199    ///
200    /// Eg: `[Vector Examples](std::vec::Vec#examples)`
201    UserWritten(String),
202}
203
204impl UrlFragment {
205    /// Render the fragment, including the leading `#`.
206    pub(crate) fn render(&self, s: &mut String, tcx: TyCtxt<'_>) {
207        s.push('#');
208        match self {
209            &UrlFragment::Item(def_id) => {
210                let kind = match tcx.def_kind(def_id) {
211                    DefKind::AssocFn => {
212                        if tcx.defaultness(def_id).has_value() {
213                            "method."
214                        } else {
215                            "tymethod."
216                        }
217                    }
218                    DefKind::AssocConst => "associatedconstant.",
219                    DefKind::AssocTy => "associatedtype.",
220                    DefKind::Variant => "variant.",
221                    DefKind::Field => {
222                        let parent_id = tcx.parent(def_id);
223                        if tcx.def_kind(parent_id) == DefKind::Variant {
224                            s.push_str("variant.");
225                            s.push_str(tcx.item_name(parent_id).as_str());
226                            ".field."
227                        } else {
228                            "structfield."
229                        }
230                    }
231                    kind => bug!("unexpected associated item kind: {kind:?}"),
232                };
233                s.push_str(kind);
234                s.push_str(tcx.item_name(def_id).as_str());
235            }
236            UrlFragment::UserWritten(raw) => s.push_str(raw),
237        }
238    }
239}
240
241#[derive(Clone, Debug, Hash, PartialEq, Eq)]
242pub(crate) struct ResolutionInfo {
243    item_id: DefId,
244    module_id: DefId,
245    dis: Option<Disambiguator>,
246    path_str: Box<str>,
247    extra_fragment: Option<String>,
248}
249
250#[derive(Clone)]
251pub(crate) struct DiagnosticInfo<'a> {
252    item: &'a Item,
253    dox: &'a str,
254    ori_link: &'a str,
255    link_range: MarkdownLinkRange,
256}
257
258pub(crate) struct OwnedDiagnosticInfo {
259    item: Item,
260    dox: String,
261    ori_link: String,
262    link_range: MarkdownLinkRange,
263}
264
265impl From<DiagnosticInfo<'_>> for OwnedDiagnosticInfo {
266    fn from(f: DiagnosticInfo<'_>) -> Self {
267        Self {
268            item: f.item.clone(),
269            dox: f.dox.to_string(),
270            ori_link: f.ori_link.to_string(),
271            link_range: f.link_range.clone(),
272        }
273    }
274}
275
276impl OwnedDiagnosticInfo {
277    pub(crate) fn as_info(&self) -> DiagnosticInfo<'_> {
278        DiagnosticInfo {
279            item: &self.item,
280            ori_link: &self.ori_link,
281            dox: &self.dox,
282            link_range: self.link_range.clone(),
283        }
284    }
285}
286
287pub(crate) struct LinkCollector<'a, 'tcx> {
288    pub(crate) cx: &'a mut DocContext<'tcx>,
289    /// Cache the resolved links so we can avoid resolving (and emitting errors for) the same link.
290    /// The link will be `None` if it could not be resolved (i.e. the error was cached).
291    pub(crate) visited_links: FxHashMap<ResolutionInfo, Option<(Res, Option<UrlFragment>)>>,
292    /// According to `rustc_resolve`, these links are ambiguous.
293    ///
294    /// However, we cannot link to an item that has been stripped from the documentation. If all
295    /// but one of the "possibilities" are stripped, then there is no real ambiguity. To determine
296    /// if an ambiguity is real, we delay resolving them until after `Cache::populate`, then filter
297    /// every item that doesn't have a cached path.
298    ///
299    /// We could get correct results by simply delaying everything. This would have fewer happy
300    /// codepaths, but we want to distinguish different kinds of error conditions, and this is easy
301    /// to do by resolving links as soon as possible.
302    pub(crate) ambiguous_links: FxIndexMap<(ItemId, String), Vec<AmbiguousLinks>>,
303}
304
305pub(crate) struct AmbiguousLinks {
306    link_text: Box<str>,
307    diag_info: OwnedDiagnosticInfo,
308    resolved: Vec<(Res, Option<UrlFragment>)>,
309}
310
311impl<'tcx> LinkCollector<'_, 'tcx> {
312    /// Given a full link, parse it as an [enum struct variant].
313    ///
314    /// In particular, this will return an error whenever there aren't three
315    /// full path segments left in the link.
316    ///
317    /// [enum struct variant]: rustc_hir::VariantData::Struct
318    fn variant_field<'path>(
319        &self,
320        path_str: &'path str,
321        item_id: DefId,
322        module_id: DefId,
323    ) -> Result<(Res, DefId), UnresolvedPath<'path>> {
324        let tcx = self.cx.tcx;
325        let no_res = || UnresolvedPath {
326            item_id,
327            module_id,
328            partial_res: None,
329            unresolved: path_str.into(),
330        };
331
332        debug!("looking for enum variant {path_str}");
333        let mut split = path_str.rsplitn(3, "::");
334        let variant_field_name = Symbol::intern(split.next().unwrap());
335        // We're not sure this is a variant at all, so use the full string.
336        // If there's no second component, the link looks like `[path]`.
337        // So there's no partial res and we should say the whole link failed to resolve.
338        let variant_name = Symbol::intern(split.next().ok_or_else(no_res)?);
339
340        // If there's no third component, we saw `[a::b]` before and it failed to resolve.
341        // So there's no partial res.
342        let path = split.next().ok_or_else(no_res)?;
343        let ty_res = self.resolve_path(path, TypeNS, item_id, module_id).ok_or_else(no_res)?;
344
345        match ty_res {
346            Res::Def(DefKind::Enum, did) => match tcx.type_of(did).instantiate_identity().kind() {
347                ty::Adt(def, _) if def.is_enum() => {
348                    if let Some(variant) = def.variants().iter().find(|v| v.name == variant_name)
349                        && let Some(field) =
350                            variant.fields.iter().find(|f| f.name == variant_field_name)
351                    {
352                        Ok((ty_res, field.did))
353                    } else {
354                        Err(UnresolvedPath {
355                            item_id,
356                            module_id,
357                            partial_res: Some(Res::Def(DefKind::Enum, def.did())),
358                            unresolved: variant_field_name.to_string().into(),
359                        })
360                    }
361                }
362                _ => unreachable!(),
363            },
364            _ => Err(UnresolvedPath {
365                item_id,
366                module_id,
367                partial_res: Some(ty_res),
368                unresolved: variant_name.to_string().into(),
369            }),
370        }
371    }
372
373    /// Given a primitive type, try to resolve an associated item.
374    fn resolve_primitive_associated_item(
375        &self,
376        prim_ty: PrimitiveType,
377        ns: Namespace,
378        item_name: Symbol,
379    ) -> Vec<(Res, DefId)> {
380        let tcx = self.cx.tcx;
381
382        prim_ty
383            .impls(tcx)
384            .flat_map(|impl_| {
385                filter_assoc_items_by_name_and_namespace(
386                    tcx,
387                    impl_,
388                    Ident::with_dummy_span(item_name),
389                    ns,
390                )
391                .map(|item| (Res::Primitive(prim_ty), item.def_id))
392            })
393            .collect::<Vec<_>>()
394    }
395
396    fn resolve_self_ty(&self, path_str: &str, ns: Namespace, item_id: DefId) -> Option<Res> {
397        if ns != TypeNS || path_str != "Self" {
398            return None;
399        }
400
401        let tcx = self.cx.tcx;
402        let self_id = match tcx.def_kind(item_id) {
403            def_kind @ (DefKind::AssocFn
404            | DefKind::AssocConst
405            | DefKind::AssocTy
406            | DefKind::Variant
407            | DefKind::Field) => {
408                let parent_def_id = tcx.parent(item_id);
409                if def_kind == DefKind::Field && tcx.def_kind(parent_def_id) == DefKind::Variant {
410                    tcx.parent(parent_def_id)
411                } else {
412                    parent_def_id
413                }
414            }
415            _ => item_id,
416        };
417
418        match tcx.def_kind(self_id) {
419            DefKind::Impl { .. } => self.def_id_to_res(self_id),
420            DefKind::Use => None,
421            def_kind => Some(Res::Def(def_kind, self_id)),
422        }
423    }
424
425    /// Convenience wrapper around `doc_link_resolutions`.
426    ///
427    /// This also handles resolving `true` and `false` as booleans.
428    /// NOTE: `doc_link_resolutions` knows only about paths, not about types.
429    /// Associated items will never be resolved by this function.
430    fn resolve_path(
431        &self,
432        path_str: &str,
433        ns: Namespace,
434        item_id: DefId,
435        module_id: DefId,
436    ) -> Option<Res> {
437        if let res @ Some(..) = self.resolve_self_ty(path_str, ns, item_id) {
438            return res;
439        }
440
441        // Resolver doesn't know about true, false, and types that aren't paths (e.g. `()`).
442        let result = self
443            .cx
444            .tcx
445            .doc_link_resolutions(module_id)
446            .get(&(Symbol::intern(path_str), ns))
447            .copied()
448            // NOTE: do not remove this panic! Missing links should be recorded as `Res::Err`; if
449            // `doc_link_resolutions` is missing a `path_str`, that means that there are valid links
450            // that are being missed. To fix the ICE, change
451            // `rustc_resolve::rustdoc::attrs_to_preprocessed_links` to cache the link.
452            .unwrap_or_else(|| {
453                span_bug!(
454                    self.cx.tcx.def_span(item_id),
455                    "no resolution for {path_str:?} {ns:?} {module_id:?}",
456                )
457            })
458            .and_then(|res| res.try_into().ok())
459            .or_else(|| resolve_primitive(path_str, ns));
460        debug!("{path_str} resolved to {result:?} in namespace {ns:?}");
461        result
462    }
463
464    /// Resolves a string as a path within a particular namespace. Returns an
465    /// optional URL fragment in the case of variants and methods.
466    fn resolve<'path>(
467        &mut self,
468        path_str: &'path str,
469        ns: Namespace,
470        disambiguator: Option<Disambiguator>,
471        item_id: DefId,
472        module_id: DefId,
473    ) -> Result<Vec<(Res, Option<DefId>)>, UnresolvedPath<'path>> {
474        if let Some(res) = self.resolve_path(path_str, ns, item_id, module_id) {
475            return Ok(match res {
476                Res::Def(
477                    DefKind::AssocFn | DefKind::AssocConst | DefKind::AssocTy | DefKind::Variant,
478                    def_id,
479                ) => {
480                    vec![(Res::from_def_id(self.cx.tcx, self.cx.tcx.parent(def_id)), Some(def_id))]
481                }
482                _ => vec![(res, None)],
483            });
484        } else if ns == MacroNS {
485            return Err(UnresolvedPath {
486                item_id,
487                module_id,
488                partial_res: None,
489                unresolved: path_str.into(),
490            });
491        }
492
493        // Try looking for methods and associated items.
494        // NB: `path_root` could be empty when resolving in the root namespace (e.g. `::std`).
495        let (path_root, item_str) = match path_str.rsplit_once("::") {
496            Some(res @ (_path_root, item_str)) if !item_str.is_empty() => res,
497            _ => {
498                // If there's no `::`, or the `::` is at the end (e.g. `String::`) it's not an
499                // associated item. So we can be sure that `rustc_resolve` was accurate when it
500                // said it wasn't resolved.
501                debug!("`::` missing or at end, assuming {path_str} was not in scope");
502                return Err(UnresolvedPath {
503                    item_id,
504                    module_id,
505                    partial_res: None,
506                    unresolved: path_str.into(),
507                });
508            }
509        };
510        let item_name = Symbol::intern(item_str);
511
512        // FIXME(#83862): this arbitrarily gives precedence to primitives over modules to support
513        // links to primitives when `#[rustc_doc_primitive]` is present. It should give an ambiguity
514        // error instead and special case *only* modules with `#[rustc_doc_primitive]`, not all
515        // primitives.
516        match resolve_primitive(path_root, TypeNS)
517            .or_else(|| self.resolve_path(path_root, TypeNS, item_id, module_id))
518            .map(|ty_res| {
519                self.resolve_associated_item(ty_res, item_name, ns, disambiguator, module_id)
520                    .into_iter()
521                    .map(|(res, def_id)| (res, Some(def_id)))
522                    .collect::<Vec<_>>()
523            }) {
524            Some(r) if !r.is_empty() => Ok(r),
525            _ => {
526                if ns == Namespace::ValueNS {
527                    self.variant_field(path_str, item_id, module_id)
528                        .map(|(res, def_id)| vec![(res, Some(def_id))])
529                } else {
530                    Err(UnresolvedPath {
531                        item_id,
532                        module_id,
533                        partial_res: None,
534                        unresolved: path_root.into(),
535                    })
536                }
537            }
538        }
539    }
540
541    /// Convert a DefId to a Res, where possible.
542    ///
543    /// This is used for resolving type aliases.
544    fn def_id_to_res(&self, ty_id: DefId) -> Option<Res> {
545        use PrimitiveType::*;
546        Some(match *self.cx.tcx.type_of(ty_id).instantiate_identity().kind() {
547            ty::Bool => Res::Primitive(Bool),
548            ty::Char => Res::Primitive(Char),
549            ty::Int(ity) => Res::Primitive(ity.into()),
550            ty::Uint(uty) => Res::Primitive(uty.into()),
551            ty::Float(fty) => Res::Primitive(fty.into()),
552            ty::Str => Res::Primitive(Str),
553            ty::Tuple(tys) if tys.is_empty() => Res::Primitive(Unit),
554            ty::Tuple(_) => Res::Primitive(Tuple),
555            ty::Pat(..) => Res::Primitive(Pat),
556            ty::Array(..) => Res::Primitive(Array),
557            ty::Slice(_) => Res::Primitive(Slice),
558            ty::RawPtr(_, _) => Res::Primitive(RawPointer),
559            ty::Ref(..) => Res::Primitive(Reference),
560            ty::FnDef(..) => panic!("type alias to a function definition"),
561            ty::FnPtr(..) => Res::Primitive(Fn),
562            ty::Never => Res::Primitive(Never),
563            ty::Adt(ty::AdtDef(Interned(&ty::AdtDefData { did, .. }, _)), _) | ty::Foreign(did) => {
564                Res::from_def_id(self.cx.tcx, did)
565            }
566            ty::Alias(..)
567            | ty::Closure(..)
568            | ty::CoroutineClosure(..)
569            | ty::Coroutine(..)
570            | ty::CoroutineWitness(..)
571            | ty::Dynamic(..)
572            | ty::UnsafeBinder(_)
573            | ty::Param(_)
574            | ty::Bound(..)
575            | ty::Placeholder(_)
576            | ty::Infer(_)
577            | ty::Error(_) => return None,
578        })
579    }
580
581    /// Convert a PrimitiveType to a Ty, where possible.
582    ///
583    /// This is used for resolving trait impls for primitives
584    fn primitive_type_to_ty(&mut self, prim: PrimitiveType) -> Option<Ty<'tcx>> {
585        use PrimitiveType::*;
586        let tcx = self.cx.tcx;
587
588        // FIXME: Only simple types are supported here, see if we can support
589        // other types such as Tuple, Array, Slice, etc.
590        // See https://github.com/rust-lang/rust/issues/90703#issuecomment-1004263455
591        Some(match prim {
592            Bool => tcx.types.bool,
593            Str => tcx.types.str_,
594            Char => tcx.types.char,
595            Never => tcx.types.never,
596            I8 => tcx.types.i8,
597            I16 => tcx.types.i16,
598            I32 => tcx.types.i32,
599            I64 => tcx.types.i64,
600            I128 => tcx.types.i128,
601            Isize => tcx.types.isize,
602            F16 => tcx.types.f16,
603            F32 => tcx.types.f32,
604            F64 => tcx.types.f64,
605            F128 => tcx.types.f128,
606            U8 => tcx.types.u8,
607            U16 => tcx.types.u16,
608            U32 => tcx.types.u32,
609            U64 => tcx.types.u64,
610            U128 => tcx.types.u128,
611            Usize => tcx.types.usize,
612            _ => return None,
613        })
614    }
615
616    /// Resolve an associated item, returning its containing page's `Res`
617    /// and the fragment targeting the associated item on its page.
618    fn resolve_associated_item(
619        &mut self,
620        root_res: Res,
621        item_name: Symbol,
622        ns: Namespace,
623        disambiguator: Option<Disambiguator>,
624        module_id: DefId,
625    ) -> Vec<(Res, DefId)> {
626        let tcx = self.cx.tcx;
627
628        match root_res {
629            Res::Primitive(prim) => {
630                let items = self.resolve_primitive_associated_item(prim, ns, item_name);
631                if !items.is_empty() {
632                    items
633                // Inherent associated items take precedence over items that come from trait impls.
634                } else {
635                    self.primitive_type_to_ty(prim)
636                        .map(|ty| {
637                            resolve_associated_trait_item(ty, module_id, item_name, ns, self.cx)
638                                .iter()
639                                .map(|item| (root_res, item.def_id))
640                                .collect::<Vec<_>>()
641                        })
642                        .unwrap_or_default()
643                }
644            }
645            Res::Def(DefKind::TyAlias, did) => {
646                // Resolve the link on the type the alias points to.
647                // FIXME: if the associated item is defined directly on the type alias,
648                // it will show up on its documentation page, we should link there instead.
649                let Some(res) = self.def_id_to_res(did) else { return Vec::new() };
650                self.resolve_associated_item(res, item_name, ns, disambiguator, module_id)
651            }
652            Res::Def(
653                def_kind @ (DefKind::Struct | DefKind::Union | DefKind::Enum | DefKind::ForeignTy),
654                did,
655            ) => {
656                debug!("looking for associated item named {item_name} for item {did:?}");
657                // Checks if item_name is a variant of the `SomeItem` enum
658                if ns == TypeNS && def_kind == DefKind::Enum {
659                    match tcx.type_of(did).instantiate_identity().kind() {
660                        ty::Adt(adt_def, _) => {
661                            for variant in adt_def.variants() {
662                                if variant.name == item_name {
663                                    return vec![(root_res, variant.def_id)];
664                                }
665                            }
666                        }
667                        _ => unreachable!(),
668                    }
669                }
670
671                let search_for_field = || {
672                    let (DefKind::Struct | DefKind::Union) = def_kind else { return vec![] };
673                    debug!("looking for fields named {item_name} for {did:?}");
674                    // FIXME: this doesn't really belong in `associated_item` (maybe `variant_field` is better?)
675                    // NOTE: it's different from variant_field because it only resolves struct fields,
676                    // not variant fields (2 path segments, not 3).
677                    //
678                    // We need to handle struct (and union) fields in this code because
679                    // syntactically their paths are identical to associated item paths:
680                    // `module::Type::field` and `module::Type::Assoc`.
681                    //
682                    // On the other hand, variant fields can't be mistaken for associated
683                    // items because they look like this: `module::Type::Variant::field`.
684                    //
685                    // Variants themselves don't need to be handled here, even though
686                    // they also look like associated items (`module::Type::Variant`),
687                    // because they are real Rust syntax (unlike the intra-doc links
688                    // field syntax) and are handled by the compiler's resolver.
689                    let ty::Adt(def, _) = tcx.type_of(did).instantiate_identity().kind() else {
690                        unreachable!()
691                    };
692                    def.non_enum_variant()
693                        .fields
694                        .iter()
695                        .filter(|field| field.name == item_name)
696                        .map(|field| (root_res, field.did))
697                        .collect::<Vec<_>>()
698                };
699
700                if let Some(Disambiguator::Kind(DefKind::Field)) = disambiguator {
701                    return search_for_field();
702                }
703
704                // Checks if item_name belongs to `impl SomeItem`
705                let mut assoc_items: Vec<_> = tcx
706                    .inherent_impls(did)
707                    .iter()
708                    .flat_map(|&imp| {
709                        filter_assoc_items_by_name_and_namespace(
710                            tcx,
711                            imp,
712                            Ident::with_dummy_span(item_name),
713                            ns,
714                        )
715                    })
716                    .map(|item| (root_res, item.def_id))
717                    .collect();
718
719                if assoc_items.is_empty() {
720                    // Check if item_name belongs to `impl SomeTrait for SomeItem`
721                    // FIXME(#74563): This gives precedence to `impl SomeItem`:
722                    // Although having both would be ambiguous, use impl version for compatibility's sake.
723                    // To handle that properly resolve() would have to support
724                    // something like [`ambi_fn`](<SomeStruct as SomeTrait>::ambi_fn)
725                    assoc_items = resolve_associated_trait_item(
726                        tcx.type_of(did).instantiate_identity(),
727                        module_id,
728                        item_name,
729                        ns,
730                        self.cx,
731                    )
732                    .into_iter()
733                    .map(|item| (root_res, item.def_id))
734                    .collect::<Vec<_>>();
735                }
736
737                debug!("got associated item {assoc_items:?}");
738
739                if !assoc_items.is_empty() {
740                    return assoc_items;
741                }
742
743                if ns != Namespace::ValueNS {
744                    return Vec::new();
745                }
746
747                search_for_field()
748            }
749            Res::Def(DefKind::Trait, did) => filter_assoc_items_by_name_and_namespace(
750                tcx,
751                did,
752                Ident::with_dummy_span(item_name),
753                ns,
754            )
755            .map(|item| {
756                let res = Res::Def(item.as_def_kind(), item.def_id);
757                (res, item.def_id)
758            })
759            .collect::<Vec<_>>(),
760            _ => Vec::new(),
761        }
762    }
763}
764
765fn full_res(tcx: TyCtxt<'_>, (base, assoc_item): (Res, Option<DefId>)) -> Res {
766    assoc_item.map_or(base, |def_id| Res::from_def_id(tcx, def_id))
767}
768
769/// Look to see if a resolved item has an associated item named `item_name`.
770///
771/// Given `[std::io::Error::source]`, where `source` is unresolved, this would
772/// find `std::error::Error::source` and return
773/// `<io::Error as error::Error>::source`.
774fn resolve_associated_trait_item<'a>(
775    ty: Ty<'a>,
776    module: DefId,
777    item_name: Symbol,
778    ns: Namespace,
779    cx: &mut DocContext<'a>,
780) -> Vec<ty::AssocItem> {
781    // FIXME: this should also consider blanket impls (`impl<T> X for T`). Unfortunately
782    // `get_auto_trait_and_blanket_impls` is broken because the caching behavior is wrong. In the
783    // meantime, just don't look for these blanket impls.
784
785    // Next consider explicit impls: `impl MyTrait for MyType`
786    // Give precedence to inherent impls.
787    let traits = trait_impls_for(cx, ty, module);
788    let tcx = cx.tcx;
789    debug!("considering traits {traits:?}");
790    let candidates = traits
791        .iter()
792        .flat_map(|&(impl_, trait_)| {
793            filter_assoc_items_by_name_and_namespace(
794                tcx,
795                trait_,
796                Ident::with_dummy_span(item_name),
797                ns,
798            )
799            .map(move |trait_assoc| {
800                trait_assoc_to_impl_assoc_item(tcx, impl_, trait_assoc.def_id)
801                    .unwrap_or(*trait_assoc)
802            })
803        })
804        .collect::<Vec<_>>();
805    // FIXME(#74563): warn about ambiguity
806    debug!("the candidates were {candidates:?}");
807    candidates
808}
809
810/// Find the associated item in the impl `impl_id` that corresponds to the
811/// trait associated item `trait_assoc_id`.
812///
813/// This function returns `None` if no associated item was found in the impl.
814/// This can occur when the trait associated item has a default value that is
815/// not overridden in the impl.
816///
817/// This is just a wrapper around [`TyCtxt::impl_item_implementor_ids()`] and
818/// [`TyCtxt::associated_item()`] (with some helpful logging added).
819#[instrument(level = "debug", skip(tcx), ret)]
820fn trait_assoc_to_impl_assoc_item<'tcx>(
821    tcx: TyCtxt<'tcx>,
822    impl_id: DefId,
823    trait_assoc_id: DefId,
824) -> Option<ty::AssocItem> {
825    let trait_to_impl_assoc_map = tcx.impl_item_implementor_ids(impl_id);
826    debug!(?trait_to_impl_assoc_map);
827    let impl_assoc_id = *trait_to_impl_assoc_map.get(&trait_assoc_id)?;
828    debug!(?impl_assoc_id);
829    Some(tcx.associated_item(impl_assoc_id))
830}
831
832/// Given a type, return all trait impls in scope in `module` for that type.
833/// Returns a set of pairs of `(impl_id, trait_id)`.
834///
835/// NOTE: this cannot be a query because more traits could be available when more crates are compiled!
836/// So it is not stable to serialize cross-crate.
837#[instrument(level = "debug", skip(cx))]
838fn trait_impls_for<'a>(
839    cx: &mut DocContext<'a>,
840    ty: Ty<'a>,
841    module: DefId,
842) -> FxIndexSet<(DefId, DefId)> {
843    let tcx = cx.tcx;
844    let mut impls = FxIndexSet::default();
845
846    for &trait_ in tcx.doc_link_traits_in_scope(module) {
847        tcx.for_each_relevant_impl(trait_, ty, |impl_| {
848            let trait_ref = tcx.impl_trait_ref(impl_).expect("this is not an inherent impl");
849            // Check if these are the same type.
850            let impl_type = trait_ref.skip_binder().self_ty();
851            trace!(
852                "comparing type {impl_type} with kind {kind:?} against type {ty:?}",
853                kind = impl_type.kind(),
854            );
855            // Fast path: if this is a primitive simple `==` will work
856            // NOTE: the `match` is necessary; see #92662.
857            // this allows us to ignore generics because the user input
858            // may not include the generic placeholders
859            // e.g. this allows us to match Foo (user comment) with Foo<T> (actual type)
860            let saw_impl = impl_type == ty
861                || match (impl_type.kind(), ty.kind()) {
862                    (ty::Adt(impl_def, _), ty::Adt(ty_def, _)) => {
863                        debug!("impl def_id: {:?}, ty def_id: {:?}", impl_def.did(), ty_def.did());
864                        impl_def.did() == ty_def.did()
865                    }
866                    _ => false,
867                };
868
869            if saw_impl {
870                impls.insert((impl_, trait_));
871            }
872        });
873    }
874
875    impls
876}
877
878/// Check for resolve collisions between a trait and its derive.
879///
880/// These are common and we should just resolve to the trait in that case.
881fn is_derive_trait_collision<T>(ns: &PerNS<Result<Vec<(Res, T)>, ResolutionFailure<'_>>>) -> bool {
882    if let (Ok(type_ns), Ok(macro_ns)) = (&ns.type_ns, &ns.macro_ns) {
883        type_ns.iter().any(|(res, _)| matches!(res, Res::Def(DefKind::Trait, _)))
884            && macro_ns
885                .iter()
886                .any(|(res, _)| matches!(res, Res::Def(DefKind::Macro(MacroKind::Derive), _)))
887    } else {
888        false
889    }
890}
891
892impl DocVisitor<'_> for LinkCollector<'_, '_> {
893    fn visit_item(&mut self, item: &Item) {
894        self.resolve_links(item);
895        self.visit_item_recur(item)
896    }
897}
898
899enum PreprocessingError {
900    /// User error: `[std#x#y]` is not valid
901    MultipleAnchors,
902    Disambiguator(MarkdownLinkRange, String),
903    MalformedGenerics(MalformedGenerics, String),
904}
905
906impl PreprocessingError {
907    fn report(&self, cx: &DocContext<'_>, diag_info: DiagnosticInfo<'_>) {
908        match self {
909            PreprocessingError::MultipleAnchors => report_multiple_anchors(cx, diag_info),
910            PreprocessingError::Disambiguator(range, msg) => {
911                disambiguator_error(cx, diag_info, range.clone(), msg.clone())
912            }
913            PreprocessingError::MalformedGenerics(err, path_str) => {
914                report_malformed_generics(cx, diag_info, *err, path_str)
915            }
916        }
917    }
918}
919
920#[derive(Clone)]
921struct PreprocessingInfo {
922    path_str: Box<str>,
923    disambiguator: Option<Disambiguator>,
924    extra_fragment: Option<String>,
925    link_text: Box<str>,
926}
927
928// Not a typedef to avoid leaking several private structures from this module.
929pub(crate) struct PreprocessedMarkdownLink(
930    Result<PreprocessingInfo, PreprocessingError>,
931    MarkdownLink,
932);
933
934/// Returns:
935/// - `None` if the link should be ignored.
936/// - `Some(Err)` if the link should emit an error
937/// - `Some(Ok)` if the link is valid
938///
939/// `link_buffer` is needed for lifetime reasons; it will always be overwritten and the contents ignored.
940fn preprocess_link(
941    ori_link: &MarkdownLink,
942    dox: &str,
943) -> Option<Result<PreprocessingInfo, PreprocessingError>> {
944    // certain link kinds cannot have their path be urls,
945    // so they should not be ignored, no matter how much they look like urls.
946    // e.g. [https://example.com/] is not a link to example.com.
947    let can_be_url = !matches!(
948        ori_link.kind,
949        LinkType::ShortcutUnknown | LinkType::CollapsedUnknown | LinkType::ReferenceUnknown
950    );
951
952    // [] is mostly likely not supposed to be a link
953    if ori_link.link.is_empty() {
954        return None;
955    }
956
957    // Bail early for real links.
958    if can_be_url && ori_link.link.contains('/') {
959        return None;
960    }
961
962    let stripped = ori_link.link.replace('`', "");
963    let mut parts = stripped.split('#');
964
965    let link = parts.next().unwrap();
966    let link = link.trim();
967    if link.is_empty() {
968        // This is an anchor to an element of the current page, nothing to do in here!
969        return None;
970    }
971    let extra_fragment = parts.next();
972    if parts.next().is_some() {
973        // A valid link can't have multiple #'s
974        return Some(Err(PreprocessingError::MultipleAnchors));
975    }
976
977    // Parse and strip the disambiguator from the link, if present.
978    let (disambiguator, path_str, link_text) = match Disambiguator::from_str(link) {
979        Ok(Some((d, path, link_text))) => (Some(d), path.trim(), link_text.trim()),
980        Ok(None) => (None, link, link),
981        Err((err_msg, relative_range)) => {
982            // Only report error if we would not have ignored this link. See issue #83859.
983            if !(can_be_url && should_ignore_link_with_disambiguators(link)) {
984                let disambiguator_range = match range_between_backticks(&ori_link.range, dox) {
985                    MarkdownLinkRange::Destination(no_backticks_range) => {
986                        MarkdownLinkRange::Destination(
987                            (no_backticks_range.start + relative_range.start)
988                                ..(no_backticks_range.start + relative_range.end),
989                        )
990                    }
991                    mdlr @ MarkdownLinkRange::WholeLink(_) => mdlr,
992                };
993                return Some(Err(PreprocessingError::Disambiguator(disambiguator_range, err_msg)));
994            } else {
995                return None;
996            }
997        }
998    };
999
1000    // If there's no backticks, be lenient and revert to the old behavior.
1001    // This is to prevent churn by linting on stuff that isn't meant to be a link.
1002    // only shortcut links have simple enough syntax that they
1003    // are likely to be written accidentally, collapsed and reference links
1004    // need 4 metachars, and reference links will not usually use
1005    // backticks in the reference name.
1006    // therefore, only shortcut syntax gets the lenient behavior.
1007    //
1008    // here's a truth table for how link kinds that cannot be urls are handled:
1009    //
1010    // |-------------------------------------------------------|
1011    // |              |  is shortcut link  | not shortcut link |
1012    // |--------------|--------------------|-------------------|
1013    // | has backtick |    never ignore    |    never ignore   |
1014    // | no backtick  | ignore if url-like |    never ignore   |
1015    // |-------------------------------------------------------|
1016    let ignore_urllike =
1017        can_be_url || (ori_link.kind == LinkType::ShortcutUnknown && !ori_link.link.contains('`'));
1018    if ignore_urllike && should_ignore_link(path_str) {
1019        return None;
1020    }
1021
1022    // Strip generics from the path.
1023    let path_str = match strip_generics_from_path(path_str) {
1024        Ok(path) => path,
1025        Err(err) => {
1026            debug!("link has malformed generics: {path_str}");
1027            return Some(Err(PreprocessingError::MalformedGenerics(err, path_str.to_owned())));
1028        }
1029    };
1030
1031    // Sanity check to make sure we don't have any angle brackets after stripping generics.
1032    assert!(!path_str.contains(['<', '>'].as_slice()));
1033
1034    // The link is not an intra-doc link if it still contains spaces after stripping generics.
1035    if path_str.contains(' ') {
1036        return None;
1037    }
1038
1039    Some(Ok(PreprocessingInfo {
1040        path_str,
1041        disambiguator,
1042        extra_fragment: extra_fragment.map(|frag| frag.to_owned()),
1043        link_text: Box::<str>::from(link_text),
1044    }))
1045}
1046
1047fn preprocessed_markdown_links(s: &str) -> Vec<PreprocessedMarkdownLink> {
1048    markdown_links(s, |link| {
1049        preprocess_link(&link, s).map(|pp_link| PreprocessedMarkdownLink(pp_link, link))
1050    })
1051}
1052
1053impl LinkCollector<'_, '_> {
1054    #[instrument(level = "debug", skip_all)]
1055    fn resolve_links(&mut self, item: &Item) {
1056        if !self.cx.render_options.document_private
1057            && let Some(def_id) = item.item_id.as_def_id()
1058            && let Some(def_id) = def_id.as_local()
1059            && !self.cx.tcx.effective_visibilities(()).is_exported(def_id)
1060            && !has_primitive_or_keyword_docs(&item.attrs.other_attrs)
1061        {
1062            // Skip link resolution for non-exported items.
1063            return;
1064        }
1065
1066        // We want to resolve in the lexical scope of the documentation.
1067        // In the presence of re-exports, this is not the same as the module of the item.
1068        // Rather than merging all documentation into one, resolve it one attribute at a time
1069        // so we know which module it came from.
1070        for (item_id, doc) in prepare_to_doc_link_resolution(&item.attrs.doc_strings) {
1071            if !may_have_doc_links(&doc) {
1072                continue;
1073            }
1074            debug!("combined_docs={doc}");
1075            // NOTE: if there are links that start in one crate and end in another, this will not resolve them.
1076            // This is a degenerate case and it's not supported by rustdoc.
1077            let item_id = item_id.unwrap_or_else(|| item.item_id.expect_def_id());
1078            let module_id = match self.cx.tcx.def_kind(item_id) {
1079                DefKind::Mod if item.inner_docs(self.cx.tcx) => item_id,
1080                _ => find_nearest_parent_module(self.cx.tcx, item_id).unwrap(),
1081            };
1082            for md_link in preprocessed_markdown_links(&doc) {
1083                let link = self.resolve_link(&doc, item, item_id, module_id, &md_link);
1084                if let Some(link) = link {
1085                    self.cx.cache.intra_doc_links.entry(item.item_id).or_default().insert(link);
1086                }
1087            }
1088        }
1089    }
1090
1091    pub(crate) fn save_link(&mut self, item_id: ItemId, link: ItemLink) {
1092        self.cx.cache.intra_doc_links.entry(item_id).or_default().insert(link);
1093    }
1094
1095    /// This is the entry point for resolving an intra-doc link.
1096    ///
1097    /// FIXME(jynelson): this is way too many arguments
1098    fn resolve_link(
1099        &mut self,
1100        dox: &String,
1101        item: &Item,
1102        item_id: DefId,
1103        module_id: DefId,
1104        PreprocessedMarkdownLink(pp_link, ori_link): &PreprocessedMarkdownLink,
1105    ) -> Option<ItemLink> {
1106        trace!("considering link '{}'", ori_link.link);
1107
1108        let diag_info = DiagnosticInfo {
1109            item,
1110            dox,
1111            ori_link: &ori_link.link,
1112            link_range: ori_link.range.clone(),
1113        };
1114        let PreprocessingInfo { path_str, disambiguator, extra_fragment, link_text } =
1115            pp_link.as_ref().map_err(|err| err.report(self.cx, diag_info.clone())).ok()?;
1116        let disambiguator = *disambiguator;
1117
1118        let mut resolved = self.resolve_with_disambiguator_cached(
1119            ResolutionInfo {
1120                item_id,
1121                module_id,
1122                dis: disambiguator,
1123                path_str: path_str.clone(),
1124                extra_fragment: extra_fragment.clone(),
1125            },
1126            diag_info.clone(), // this struct should really be Copy, but Range is not :(
1127            // For reference-style links we want to report only one error so unsuccessful
1128            // resolutions are cached, for other links we want to report an error every
1129            // time so they are not cached.
1130            matches!(ori_link.kind, LinkType::Reference | LinkType::Shortcut),
1131        )?;
1132
1133        if resolved.len() > 1 {
1134            let links = AmbiguousLinks {
1135                link_text: link_text.clone(),
1136                diag_info: diag_info.into(),
1137                resolved,
1138            };
1139
1140            self.ambiguous_links
1141                .entry((item.item_id, path_str.to_string()))
1142                .or_default()
1143                .push(links);
1144            None
1145        } else if let Some((res, fragment)) = resolved.pop() {
1146            self.compute_link(res, fragment, path_str, disambiguator, diag_info, link_text)
1147        } else {
1148            None
1149        }
1150    }
1151
1152    /// Returns `true` if a link could be generated from the given intra-doc information.
1153    ///
1154    /// This is a very light version of `format::href_with_root_path` since we're only interested
1155    /// about whether we can generate a link to an item or not.
1156    ///
1157    /// * If `original_did` is local, then we check if the item is reexported or public.
1158    /// * If `original_did` is not local, then we check if the crate it comes from is a direct
1159    ///   public dependency.
1160    fn validate_link(&self, original_did: DefId) -> bool {
1161        let tcx = self.cx.tcx;
1162        let def_kind = tcx.def_kind(original_did);
1163        let did = match def_kind {
1164            DefKind::AssocTy | DefKind::AssocFn | DefKind::AssocConst | DefKind::Variant => {
1165                // documented on their parent's page
1166                tcx.parent(original_did)
1167            }
1168            // If this a constructor, we get the parent (either a struct or a variant) and then
1169            // generate the link for this item.
1170            DefKind::Ctor(..) => return self.validate_link(tcx.parent(original_did)),
1171            DefKind::ExternCrate => {
1172                // Link to the crate itself, not the `extern crate` item.
1173                if let Some(local_did) = original_did.as_local() {
1174                    tcx.extern_mod_stmt_cnum(local_did).unwrap_or(LOCAL_CRATE).as_def_id()
1175                } else {
1176                    original_did
1177                }
1178            }
1179            _ => original_did,
1180        };
1181
1182        let cache = &self.cx.cache;
1183        if !original_did.is_local()
1184            && !cache.effective_visibilities.is_directly_public(tcx, did)
1185            && !cache.document_private
1186            && !cache.primitive_locations.values().any(|&id| id == did)
1187        {
1188            return false;
1189        }
1190
1191        cache.paths.get(&did).is_some()
1192            || cache.external_paths.contains_key(&did)
1193            || !did.is_local()
1194    }
1195
1196    #[allow(rustc::potential_query_instability)]
1197    pub(crate) fn resolve_ambiguities(&mut self) {
1198        let mut ambiguous_links = mem::take(&mut self.ambiguous_links);
1199        for ((item_id, path_str), info_items) in ambiguous_links.iter_mut() {
1200            for info in info_items {
1201                info.resolved.retain(|(res, _)| match res {
1202                    Res::Def(_, def_id) => self.validate_link(*def_id),
1203                    // Primitive types are always valid.
1204                    Res::Primitive(_) => true,
1205                });
1206                let diag_info = info.diag_info.as_info();
1207                match info.resolved.len() {
1208                    1 => {
1209                        let (res, fragment) = info.resolved.pop().unwrap();
1210                        if let Some(link) = self.compute_link(
1211                            res,
1212                            fragment,
1213                            path_str,
1214                            None,
1215                            diag_info,
1216                            &info.link_text,
1217                        ) {
1218                            self.save_link(*item_id, link);
1219                        }
1220                    }
1221                    0 => {
1222                        report_diagnostic(
1223                            self.cx.tcx,
1224                            BROKEN_INTRA_DOC_LINKS,
1225                            format!("all items matching `{path_str}` are private or doc(hidden)"),
1226                            &diag_info,
1227                            |diag, sp, _| {
1228                                if let Some(sp) = sp {
1229                                    diag.span_label(sp, "unresolved link");
1230                                } else {
1231                                    diag.note("unresolved link");
1232                                }
1233                            },
1234                        );
1235                    }
1236                    _ => {
1237                        let candidates = info
1238                            .resolved
1239                            .iter()
1240                            .map(|(res, fragment)| {
1241                                let def_id = if let Some(UrlFragment::Item(def_id)) = fragment {
1242                                    Some(*def_id)
1243                                } else {
1244                                    None
1245                                };
1246                                (*res, def_id)
1247                            })
1248                            .collect::<Vec<_>>();
1249                        ambiguity_error(self.cx, &diag_info, path_str, &candidates, true);
1250                    }
1251                }
1252            }
1253        }
1254    }
1255
1256    fn compute_link(
1257        &mut self,
1258        mut res: Res,
1259        fragment: Option<UrlFragment>,
1260        path_str: &str,
1261        disambiguator: Option<Disambiguator>,
1262        diag_info: DiagnosticInfo<'_>,
1263        link_text: &Box<str>,
1264    ) -> Option<ItemLink> {
1265        // Check for a primitive which might conflict with a module
1266        // Report the ambiguity and require that the user specify which one they meant.
1267        // FIXME: could there ever be a primitive not in the type namespace?
1268        if matches!(
1269            disambiguator,
1270            None | Some(Disambiguator::Namespace(Namespace::TypeNS) | Disambiguator::Primitive)
1271        ) && !matches!(res, Res::Primitive(_))
1272            && let Some(prim) = resolve_primitive(path_str, TypeNS)
1273        {
1274            // `prim@char`
1275            if matches!(disambiguator, Some(Disambiguator::Primitive)) {
1276                res = prim;
1277            } else {
1278                // `[char]` when a `char` module is in scope
1279                let candidates = &[(res, res.def_id(self.cx.tcx)), (prim, None)];
1280                ambiguity_error(self.cx, &diag_info, path_str, candidates, true);
1281                return None;
1282            }
1283        }
1284
1285        match res {
1286            Res::Primitive(_) => {
1287                if let Some(UrlFragment::Item(id)) = fragment {
1288                    // We're actually resolving an associated item of a primitive, so we need to
1289                    // verify the disambiguator (if any) matches the type of the associated item.
1290                    // This case should really follow the same flow as the `Res::Def` branch below,
1291                    // but attempting to add a call to `clean::register_res` causes an ICE. @jyn514
1292                    // thinks `register_res` is only needed for cross-crate re-exports, but Rust
1293                    // doesn't allow statements like `use str::trim;`, making this a (hopefully)
1294                    // valid omission. See https://github.com/rust-lang/rust/pull/80660#discussion_r551585677
1295                    // for discussion on the matter.
1296                    let kind = self.cx.tcx.def_kind(id);
1297                    self.verify_disambiguator(path_str, kind, id, disambiguator, &diag_info)?;
1298                } else {
1299                    match disambiguator {
1300                        Some(Disambiguator::Primitive | Disambiguator::Namespace(_)) | None => {}
1301                        Some(other) => {
1302                            self.report_disambiguator_mismatch(path_str, other, res, &diag_info);
1303                            return None;
1304                        }
1305                    }
1306                }
1307
1308                res.def_id(self.cx.tcx).map(|page_id| ItemLink {
1309                    link: Box::<str>::from(diag_info.ori_link),
1310                    link_text: link_text.clone(),
1311                    page_id,
1312                    fragment,
1313                })
1314            }
1315            Res::Def(kind, id) => {
1316                let (kind_for_dis, id_for_dis) = if let Some(UrlFragment::Item(id)) = fragment {
1317                    (self.cx.tcx.def_kind(id), id)
1318                } else {
1319                    (kind, id)
1320                };
1321                self.verify_disambiguator(
1322                    path_str,
1323                    kind_for_dis,
1324                    id_for_dis,
1325                    disambiguator,
1326                    &diag_info,
1327                )?;
1328
1329                let page_id = clean::register_res(self.cx, rustc_hir::def::Res::Def(kind, id));
1330                Some(ItemLink {
1331                    link: Box::<str>::from(diag_info.ori_link),
1332                    link_text: link_text.clone(),
1333                    page_id,
1334                    fragment,
1335                })
1336            }
1337        }
1338    }
1339
1340    fn verify_disambiguator(
1341        &self,
1342        path_str: &str,
1343        kind: DefKind,
1344        id: DefId,
1345        disambiguator: Option<Disambiguator>,
1346        diag_info: &DiagnosticInfo<'_>,
1347    ) -> Option<()> {
1348        debug!("intra-doc link to {path_str} resolved to {:?}", (kind, id));
1349
1350        // Disallow e.g. linking to enums with `struct@`
1351        debug!("saw kind {kind:?} with disambiguator {disambiguator:?}");
1352        match (kind, disambiguator) {
1353                | (DefKind::Const | DefKind::ConstParam | DefKind::AssocConst | DefKind::AnonConst, Some(Disambiguator::Kind(DefKind::Const)))
1354                // NOTE: this allows 'method' to mean both normal functions and associated functions
1355                // This can't cause ambiguity because both are in the same namespace.
1356                | (DefKind::Fn | DefKind::AssocFn, Some(Disambiguator::Kind(DefKind::Fn)))
1357                // These are namespaces; allow anything in the namespace to match
1358                | (_, Some(Disambiguator::Namespace(_)))
1359                // If no disambiguator given, allow anything
1360                | (_, None)
1361                // All of these are valid, so do nothing
1362                => {}
1363                (actual, Some(Disambiguator::Kind(expected))) if actual == expected => {}
1364                (_, Some(specified @ Disambiguator::Kind(_) | specified @ Disambiguator::Primitive)) => {
1365                    self.report_disambiguator_mismatch(path_str, specified, Res::Def(kind, id), diag_info);
1366                    return None;
1367                }
1368            }
1369
1370        // item can be non-local e.g. when using `#[rustc_doc_primitive = "pointer"]`
1371        if let Some(dst_id) = id.as_local()
1372            && let Some(src_id) = diag_info.item.item_id.expect_def_id().as_local()
1373            && self.cx.tcx.effective_visibilities(()).is_exported(src_id)
1374            && !self.cx.tcx.effective_visibilities(()).is_exported(dst_id)
1375        {
1376            privacy_error(self.cx, diag_info, path_str);
1377        }
1378
1379        Some(())
1380    }
1381
1382    fn report_disambiguator_mismatch(
1383        &self,
1384        path_str: &str,
1385        specified: Disambiguator,
1386        resolved: Res,
1387        diag_info: &DiagnosticInfo<'_>,
1388    ) {
1389        // The resolved item did not match the disambiguator; give a better error than 'not found'
1390        let msg = format!("incompatible link kind for `{path_str}`");
1391        let callback = |diag: &mut Diag<'_, ()>, sp: Option<rustc_span::Span>, link_range| {
1392            let note = format!(
1393                "this link resolved to {} {}, which is not {} {}",
1394                resolved.article(),
1395                resolved.descr(),
1396                specified.article(),
1397                specified.descr(),
1398            );
1399            if let Some(sp) = sp {
1400                diag.span_label(sp, note);
1401            } else {
1402                diag.note(note);
1403            }
1404            suggest_disambiguator(resolved, diag, path_str, link_range, sp, diag_info);
1405        };
1406        report_diagnostic(self.cx.tcx, BROKEN_INTRA_DOC_LINKS, msg, diag_info, callback);
1407    }
1408
1409    fn report_rawptr_assoc_feature_gate(
1410        &self,
1411        dox: &str,
1412        ori_link: &MarkdownLinkRange,
1413        item: &Item,
1414    ) {
1415        let span = match source_span_for_markdown_range(
1416            self.cx.tcx,
1417            dox,
1418            ori_link.inner_range(),
1419            &item.attrs.doc_strings,
1420        ) {
1421            Some((sp, _)) => sp,
1422            None => item.attr_span(self.cx.tcx),
1423        };
1424        rustc_session::parse::feature_err(
1425            self.cx.tcx.sess,
1426            sym::intra_doc_pointers,
1427            span,
1428            "linking to associated items of raw pointers is experimental",
1429        )
1430        .with_note("rustdoc does not allow disambiguating between `*const` and `*mut`, and pointers are unstable until it does")
1431        .emit();
1432    }
1433
1434    fn resolve_with_disambiguator_cached(
1435        &mut self,
1436        key: ResolutionInfo,
1437        diag: DiagnosticInfo<'_>,
1438        // If errors are cached then they are only reported on first occurrence
1439        // which we want in some cases but not in others.
1440        cache_errors: bool,
1441    ) -> Option<Vec<(Res, Option<UrlFragment>)>> {
1442        if let Some(res) = self.visited_links.get(&key)
1443            && (res.is_some() || cache_errors)
1444        {
1445            return res.clone().map(|r| vec![r]);
1446        }
1447
1448        let mut candidates = self.resolve_with_disambiguator(&key, diag.clone());
1449
1450        // FIXME: it would be nice to check that the feature gate was enabled in the original crate, not just ignore it altogether.
1451        // However I'm not sure how to check that across crates.
1452        if let Some(candidate) = candidates.first()
1453            && candidate.0 == Res::Primitive(PrimitiveType::RawPointer)
1454            && key.path_str.contains("::")
1455        // We only want to check this if this is an associated item.
1456        {
1457            if key.item_id.is_local() && !self.cx.tcx.features().intra_doc_pointers() {
1458                self.report_rawptr_assoc_feature_gate(diag.dox, &diag.link_range, diag.item);
1459                return None;
1460            } else {
1461                candidates = vec![*candidate];
1462            }
1463        }
1464
1465        // If there are multiple items with the same "kind" (for example, both "associated types")
1466        // and after removing duplicated kinds, only one remains, the `ambiguity_error` function
1467        // won't emit an error. So at this point, we can just take the first candidate as it was
1468        // the first retrieved and use it to generate the link.
1469        if let [candidate, _candidate2, ..] = *candidates
1470            && !ambiguity_error(self.cx, &diag, &key.path_str, &candidates, false)
1471        {
1472            candidates = vec![candidate];
1473        }
1474
1475        let mut out = Vec::with_capacity(candidates.len());
1476        for (res, def_id) in candidates {
1477            let fragment = match (&key.extra_fragment, def_id) {
1478                (Some(_), Some(def_id)) => {
1479                    report_anchor_conflict(self.cx, diag, def_id);
1480                    return None;
1481                }
1482                (Some(u_frag), None) => Some(UrlFragment::UserWritten(u_frag.clone())),
1483                (None, Some(def_id)) => Some(UrlFragment::Item(def_id)),
1484                (None, None) => None,
1485            };
1486            out.push((res, fragment));
1487        }
1488        if let [r] = out.as_slice() {
1489            self.visited_links.insert(key, Some(r.clone()));
1490        } else if cache_errors {
1491            self.visited_links.insert(key, None);
1492        }
1493        Some(out)
1494    }
1495
1496    /// After parsing the disambiguator, resolve the main part of the link.
1497    fn resolve_with_disambiguator(
1498        &mut self,
1499        key: &ResolutionInfo,
1500        diag: DiagnosticInfo<'_>,
1501    ) -> Vec<(Res, Option<DefId>)> {
1502        let disambiguator = key.dis;
1503        let path_str = &key.path_str;
1504        let item_id = key.item_id;
1505        let module_id = key.module_id;
1506
1507        match disambiguator.map(Disambiguator::ns) {
1508            Some(expected_ns) => {
1509                match self.resolve(path_str, expected_ns, disambiguator, item_id, module_id) {
1510                    Ok(candidates) => candidates,
1511                    Err(err) => {
1512                        // We only looked in one namespace. Try to give a better error if possible.
1513                        // FIXME: really it should be `resolution_failure` that does this, not `resolve_with_disambiguator`.
1514                        // See https://github.com/rust-lang/rust/pull/76955#discussion_r493953382 for a good approach.
1515                        let mut err = ResolutionFailure::NotResolved(err);
1516                        for other_ns in [TypeNS, ValueNS, MacroNS] {
1517                            if other_ns != expected_ns
1518                                && let Ok(&[res, ..]) = self
1519                                    .resolve(path_str, other_ns, None, item_id, module_id)
1520                                    .as_deref()
1521                            {
1522                                err = ResolutionFailure::WrongNamespace {
1523                                    res: full_res(self.cx.tcx, res),
1524                                    expected_ns,
1525                                };
1526                                break;
1527                            }
1528                        }
1529                        resolution_failure(self, diag, path_str, disambiguator, smallvec![err]);
1530                        vec![]
1531                    }
1532                }
1533            }
1534            None => {
1535                // Try everything!
1536                let mut candidate = |ns| {
1537                    self.resolve(path_str, ns, None, item_id, module_id)
1538                        .map_err(ResolutionFailure::NotResolved)
1539                };
1540
1541                let candidates = PerNS {
1542                    macro_ns: candidate(MacroNS),
1543                    type_ns: candidate(TypeNS),
1544                    value_ns: candidate(ValueNS).and_then(|v_res| {
1545                        for (res, _) in v_res.iter() {
1546                            // Constructors are picked up in the type namespace.
1547                            if let Res::Def(DefKind::Ctor(..), _) = res {
1548                                return Err(ResolutionFailure::WrongNamespace {
1549                                    res: *res,
1550                                    expected_ns: TypeNS,
1551                                });
1552                            }
1553                        }
1554                        Ok(v_res)
1555                    }),
1556                };
1557
1558                let len = candidates
1559                    .iter()
1560                    .fold(0, |acc, res| if let Ok(res) = res { acc + res.len() } else { acc });
1561
1562                if len == 0 {
1563                    resolution_failure(
1564                        self,
1565                        diag,
1566                        path_str,
1567                        disambiguator,
1568                        candidates.into_iter().filter_map(|res| res.err()).collect(),
1569                    );
1570                    vec![]
1571                } else if len == 1 {
1572                    candidates.into_iter().filter_map(|res| res.ok()).flatten().collect::<Vec<_>>()
1573                } else {
1574                    let has_derive_trait_collision = is_derive_trait_collision(&candidates);
1575                    if len == 2 && has_derive_trait_collision {
1576                        candidates.type_ns.unwrap()
1577                    } else {
1578                        // If we're reporting an ambiguity, don't mention the namespaces that failed
1579                        let mut candidates = candidates.map(|candidate| candidate.ok());
1580                        // If there a collision between a trait and a derive, we ignore the derive.
1581                        if has_derive_trait_collision {
1582                            candidates.macro_ns = None;
1583                        }
1584                        candidates.into_iter().flatten().flatten().collect::<Vec<_>>()
1585                    }
1586                }
1587            }
1588        }
1589    }
1590}
1591
1592/// Get the section of a link between the backticks,
1593/// or the whole link if there aren't any backticks.
1594///
1595/// For example:
1596///
1597/// ```text
1598/// [`Foo`]
1599///   ^^^
1600/// ```
1601///
1602/// This function does nothing if `ori_link.range` is a `MarkdownLinkRange::WholeLink`.
1603fn range_between_backticks(ori_link_range: &MarkdownLinkRange, dox: &str) -> MarkdownLinkRange {
1604    let range = match ori_link_range {
1605        mdlr @ MarkdownLinkRange::WholeLink(_) => return mdlr.clone(),
1606        MarkdownLinkRange::Destination(inner) => inner.clone(),
1607    };
1608    let ori_link_text = &dox[range.clone()];
1609    let after_first_backtick_group = ori_link_text.bytes().position(|b| b != b'`').unwrap_or(0);
1610    let before_second_backtick_group = ori_link_text
1611        .bytes()
1612        .skip(after_first_backtick_group)
1613        .position(|b| b == b'`')
1614        .unwrap_or(ori_link_text.len());
1615    MarkdownLinkRange::Destination(
1616        (range.start + after_first_backtick_group)..(range.start + before_second_backtick_group),
1617    )
1618}
1619
1620/// Returns true if we should ignore `link` due to it being unlikely
1621/// that it is an intra-doc link. `link` should still have disambiguators
1622/// if there were any.
1623///
1624/// The difference between this and [`should_ignore_link()`] is that this
1625/// check should only be used on links that still have disambiguators.
1626fn should_ignore_link_with_disambiguators(link: &str) -> bool {
1627    link.contains(|ch: char| !(ch.is_alphanumeric() || ":_<>, !*&;@()".contains(ch)))
1628}
1629
1630/// Returns true if we should ignore `path_str` due to it being unlikely
1631/// that it is an intra-doc link.
1632fn should_ignore_link(path_str: &str) -> bool {
1633    path_str.contains(|ch: char| !(ch.is_alphanumeric() || ":_<>, !*&;".contains(ch)))
1634}
1635
1636#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
1637/// Disambiguators for a link.
1638enum Disambiguator {
1639    /// `prim@`
1640    ///
1641    /// This is buggy, see <https://github.com/rust-lang/rust/pull/77875#discussion_r503583103>
1642    Primitive,
1643    /// `struct@` or `f()`
1644    Kind(DefKind),
1645    /// `type@`
1646    Namespace(Namespace),
1647}
1648
1649impl Disambiguator {
1650    /// Given a link, parse and return `(disambiguator, path_str, link_text)`.
1651    ///
1652    /// This returns `Ok(Some(...))` if a disambiguator was found,
1653    /// `Ok(None)` if no disambiguator was found, or `Err(...)`
1654    /// if there was a problem with the disambiguator.
1655    fn from_str(link: &str) -> Result<Option<(Self, &str, &str)>, (String, Range<usize>)> {
1656        use Disambiguator::{Kind, Namespace as NS, Primitive};
1657
1658        let suffixes = [
1659            // If you update this list, please also update the relevant rustdoc book section!
1660            ("!()", DefKind::Macro(MacroKind::Bang)),
1661            ("!{}", DefKind::Macro(MacroKind::Bang)),
1662            ("![]", DefKind::Macro(MacroKind::Bang)),
1663            ("()", DefKind::Fn),
1664            ("!", DefKind::Macro(MacroKind::Bang)),
1665        ];
1666
1667        if let Some(idx) = link.find('@') {
1668            let (prefix, rest) = link.split_at(idx);
1669            let d = match prefix {
1670                // If you update this list, please also update the relevant rustdoc book section!
1671                "struct" => Kind(DefKind::Struct),
1672                "enum" => Kind(DefKind::Enum),
1673                "trait" => Kind(DefKind::Trait),
1674                "union" => Kind(DefKind::Union),
1675                "module" | "mod" => Kind(DefKind::Mod),
1676                "const" | "constant" => Kind(DefKind::Const),
1677                "static" => Kind(DefKind::Static {
1678                    mutability: Mutability::Not,
1679                    nested: false,
1680                    safety: Safety::Safe,
1681                }),
1682                "function" | "fn" | "method" => Kind(DefKind::Fn),
1683                "derive" => Kind(DefKind::Macro(MacroKind::Derive)),
1684                "field" => Kind(DefKind::Field),
1685                "variant" => Kind(DefKind::Variant),
1686                "type" => NS(Namespace::TypeNS),
1687                "value" => NS(Namespace::ValueNS),
1688                "macro" => NS(Namespace::MacroNS),
1689                "prim" | "primitive" => Primitive,
1690                _ => return Err((format!("unknown disambiguator `{prefix}`"), 0..idx)),
1691            };
1692
1693            for (suffix, kind) in suffixes {
1694                if let Some(path_str) = rest.strip_suffix(suffix) {
1695                    if d.ns() != Kind(kind).ns() {
1696                        return Err((
1697                            format!("unmatched disambiguator `{prefix}` and suffix `{suffix}`"),
1698                            0..idx,
1699                        ));
1700                    } else if path_str.len() > 1 {
1701                        // path_str != "@"
1702                        return Ok(Some((d, &path_str[1..], &rest[1..])));
1703                    }
1704                }
1705            }
1706
1707            Ok(Some((d, &rest[1..], &rest[1..])))
1708        } else {
1709            for (suffix, kind) in suffixes {
1710                // Avoid turning `!` or `()` into an empty string
1711                if let Some(path_str) = link.strip_suffix(suffix)
1712                    && !path_str.is_empty()
1713                {
1714                    return Ok(Some((Kind(kind), path_str, link)));
1715                }
1716            }
1717            Ok(None)
1718        }
1719    }
1720
1721    fn ns(self) -> Namespace {
1722        match self {
1723            Self::Namespace(n) => n,
1724            // for purposes of link resolution, fields are in the value namespace.
1725            Self::Kind(DefKind::Field) => ValueNS,
1726            Self::Kind(k) => {
1727                k.ns().expect("only DefKinds with a valid namespace can be disambiguators")
1728            }
1729            Self::Primitive => TypeNS,
1730        }
1731    }
1732
1733    fn article(self) -> &'static str {
1734        match self {
1735            Self::Namespace(_) => panic!("article() doesn't make sense for namespaces"),
1736            Self::Kind(k) => k.article(),
1737            Self::Primitive => "a",
1738        }
1739    }
1740
1741    fn descr(self) -> &'static str {
1742        match self {
1743            Self::Namespace(n) => n.descr(),
1744            // HACK(jynelson): the source of `DefKind::descr` only uses the DefId for
1745            // printing "module" vs "crate" so using the wrong ID is not a huge problem
1746            Self::Kind(k) => k.descr(CRATE_DEF_ID.to_def_id()),
1747            Self::Primitive => "builtin type",
1748        }
1749    }
1750}
1751
1752/// A suggestion to show in a diagnostic.
1753enum Suggestion {
1754    /// `struct@`
1755    Prefix(&'static str),
1756    /// `f()`
1757    Function,
1758    /// `m!`
1759    Macro,
1760}
1761
1762impl Suggestion {
1763    fn descr(&self) -> Cow<'static, str> {
1764        match self {
1765            Self::Prefix(x) => format!("prefix with `{x}@`").into(),
1766            Self::Function => "add parentheses".into(),
1767            Self::Macro => "add an exclamation mark".into(),
1768        }
1769    }
1770
1771    fn as_help(&self, path_str: &str) -> String {
1772        // FIXME: if this is an implied shortcut link, it's bad style to suggest `@`
1773        match self {
1774            Self::Prefix(prefix) => format!("{prefix}@{path_str}"),
1775            Self::Function => format!("{path_str}()"),
1776            Self::Macro => format!("{path_str}!"),
1777        }
1778    }
1779
1780    fn as_help_span(
1781        &self,
1782        ori_link: &str,
1783        sp: rustc_span::Span,
1784    ) -> Vec<(rustc_span::Span, String)> {
1785        let inner_sp = match ori_link.find('(') {
1786            Some(index) if index != 0 && ori_link.as_bytes()[index - 1] == b'\\' => {
1787                sp.with_hi(sp.lo() + BytePos((index - 1) as _))
1788            }
1789            Some(index) => sp.with_hi(sp.lo() + BytePos(index as _)),
1790            None => sp,
1791        };
1792        let inner_sp = match ori_link.find('!') {
1793            Some(index) if index != 0 && ori_link.as_bytes()[index - 1] == b'\\' => {
1794                sp.with_hi(sp.lo() + BytePos((index - 1) as _))
1795            }
1796            Some(index) => inner_sp.with_hi(inner_sp.lo() + BytePos(index as _)),
1797            None => inner_sp,
1798        };
1799        let inner_sp = match ori_link.find('@') {
1800            Some(index) if index != 0 && ori_link.as_bytes()[index - 1] == b'\\' => {
1801                sp.with_hi(sp.lo() + BytePos((index - 1) as _))
1802            }
1803            Some(index) => inner_sp.with_lo(inner_sp.lo() + BytePos(index as u32 + 1)),
1804            None => inner_sp,
1805        };
1806        match self {
1807            Self::Prefix(prefix) => {
1808                // FIXME: if this is an implied shortcut link, it's bad style to suggest `@`
1809                let mut sugg = vec![(sp.with_hi(inner_sp.lo()), format!("{prefix}@"))];
1810                if sp.hi() != inner_sp.hi() {
1811                    sugg.push((inner_sp.shrink_to_hi().with_hi(sp.hi()), String::new()));
1812                }
1813                sugg
1814            }
1815            Self::Function => {
1816                let mut sugg = vec![(inner_sp.shrink_to_hi().with_hi(sp.hi()), "()".to_string())];
1817                if sp.lo() != inner_sp.lo() {
1818                    sugg.push((inner_sp.shrink_to_lo().with_lo(sp.lo()), String::new()));
1819                }
1820                sugg
1821            }
1822            Self::Macro => {
1823                let mut sugg = vec![(inner_sp.shrink_to_hi(), "!".to_string())];
1824                if sp.lo() != inner_sp.lo() {
1825                    sugg.push((inner_sp.shrink_to_lo().with_lo(sp.lo()), String::new()));
1826                }
1827                sugg
1828            }
1829        }
1830    }
1831}
1832
1833/// Reports a diagnostic for an intra-doc link.
1834///
1835/// If no link range is provided, or the source span of the link cannot be determined, the span of
1836/// the entire documentation block is used for the lint. If a range is provided but the span
1837/// calculation fails, a note is added to the diagnostic pointing to the link in the markdown.
1838///
1839/// The `decorate` callback is invoked in all cases to allow further customization of the
1840/// diagnostic before emission. If the span of the link was able to be determined, the second
1841/// parameter of the callback will contain it, and the primary span of the diagnostic will be set
1842/// to it.
1843fn report_diagnostic(
1844    tcx: TyCtxt<'_>,
1845    lint: &'static Lint,
1846    msg: impl Into<DiagMessage> + Display,
1847    DiagnosticInfo { item, ori_link: _, dox, link_range }: &DiagnosticInfo<'_>,
1848    decorate: impl FnOnce(&mut Diag<'_, ()>, Option<rustc_span::Span>, MarkdownLinkRange),
1849) {
1850    let Some(hir_id) = DocContext::as_local_hir_id(tcx, item.item_id) else {
1851        // If non-local, no need to check anything.
1852        info!("ignoring warning from parent crate: {msg}");
1853        return;
1854    };
1855
1856    let sp = item.attr_span(tcx);
1857
1858    tcx.node_span_lint(lint, hir_id, sp, |lint| {
1859        lint.primary_message(msg);
1860
1861        let (span, link_range) = match link_range {
1862            MarkdownLinkRange::Destination(md_range) => {
1863                let mut md_range = md_range.clone();
1864                let sp =
1865                    source_span_for_markdown_range(tcx, dox, &md_range, &item.attrs.doc_strings)
1866                        .map(|(mut sp, _)| {
1867                            while dox.as_bytes().get(md_range.start) == Some(&b' ')
1868                                || dox.as_bytes().get(md_range.start) == Some(&b'`')
1869                            {
1870                                md_range.start += 1;
1871                                sp = sp.with_lo(sp.lo() + BytePos(1));
1872                            }
1873                            while dox.as_bytes().get(md_range.end - 1) == Some(&b' ')
1874                                || dox.as_bytes().get(md_range.end - 1) == Some(&b'`')
1875                            {
1876                                md_range.end -= 1;
1877                                sp = sp.with_hi(sp.hi() - BytePos(1));
1878                            }
1879                            sp
1880                        });
1881                (sp, MarkdownLinkRange::Destination(md_range))
1882            }
1883            MarkdownLinkRange::WholeLink(md_range) => (
1884                source_span_for_markdown_range(tcx, dox, md_range, &item.attrs.doc_strings)
1885                    .map(|(sp, _)| sp),
1886                link_range.clone(),
1887            ),
1888        };
1889
1890        if let Some(sp) = span {
1891            lint.span(sp);
1892        } else {
1893            // blah blah blah\nblah\nblah [blah] blah blah\nblah blah
1894            //                       ^     ~~~~
1895            //                       |     link_range
1896            //                       last_new_line_offset
1897            let md_range = link_range.inner_range().clone();
1898            let last_new_line_offset = dox[..md_range.start].rfind('\n').map_or(0, |n| n + 1);
1899            let line = dox[last_new_line_offset..].lines().next().unwrap_or("");
1900
1901            // Print the line containing the `md_range` and manually mark it with '^'s.
1902            lint.note(format!(
1903                "the link appears in this line:\n\n{line}\n\
1904                     {indicator: <before$}{indicator:^<found$}",
1905                indicator = "",
1906                before = md_range.start - last_new_line_offset,
1907                found = md_range.len(),
1908            ));
1909        }
1910
1911        decorate(lint, span, link_range);
1912    });
1913}
1914
1915/// Reports a link that failed to resolve.
1916///
1917/// This also tries to resolve any intermediate path segments that weren't
1918/// handled earlier. For example, if passed `Item::Crate(std)` and `path_str`
1919/// `std::io::Error::x`, this will resolve `std::io::Error`.
1920fn resolution_failure(
1921    collector: &mut LinkCollector<'_, '_>,
1922    diag_info: DiagnosticInfo<'_>,
1923    path_str: &str,
1924    disambiguator: Option<Disambiguator>,
1925    kinds: SmallVec<[ResolutionFailure<'_>; 3]>,
1926) {
1927    let tcx = collector.cx.tcx;
1928    report_diagnostic(
1929        tcx,
1930        BROKEN_INTRA_DOC_LINKS,
1931        format!("unresolved link to `{path_str}`"),
1932        &diag_info,
1933        |diag, sp, link_range| {
1934            let item = |res: Res| format!("the {} `{}`", res.descr(), res.name(tcx));
1935            let assoc_item_not_allowed = |res: Res| {
1936                let name = res.name(tcx);
1937                format!(
1938                    "`{name}` is {} {}, not a module or type, and cannot have associated items",
1939                    res.article(),
1940                    res.descr()
1941                )
1942            };
1943            // ignore duplicates
1944            let mut variants_seen = SmallVec::<[_; 3]>::new();
1945            for mut failure in kinds {
1946                let variant = mem::discriminant(&failure);
1947                if variants_seen.contains(&variant) {
1948                    continue;
1949                }
1950                variants_seen.push(variant);
1951
1952                if let ResolutionFailure::NotResolved(UnresolvedPath {
1953                    item_id,
1954                    module_id,
1955                    partial_res,
1956                    unresolved,
1957                }) = &mut failure
1958                {
1959                    use DefKind::*;
1960
1961                    let item_id = *item_id;
1962                    let module_id = *module_id;
1963
1964                    // Check if _any_ parent of the path gets resolved.
1965                    // If so, report it and say the first which failed; if not, say the first path segment didn't resolve.
1966                    let mut name = path_str;
1967                    'outer: loop {
1968                        // FIXME(jynelson): this might conflict with my `Self` fix in #76467
1969                        let Some((start, end)) = name.rsplit_once("::") else {
1970                            // avoid bug that marked [Quux::Z] as missing Z, not Quux
1971                            if partial_res.is_none() {
1972                                *unresolved = name.into();
1973                            }
1974                            break;
1975                        };
1976                        name = start;
1977                        for ns in [TypeNS, ValueNS, MacroNS] {
1978                            if let Ok(v_res) =
1979                                collector.resolve(start, ns, None, item_id, module_id)
1980                            {
1981                                debug!("found partial_res={v_res:?}");
1982                                if let Some(&res) = v_res.first() {
1983                                    *partial_res = Some(full_res(tcx, res));
1984                                    *unresolved = end.into();
1985                                    break 'outer;
1986                                }
1987                            }
1988                        }
1989                        *unresolved = end.into();
1990                    }
1991
1992                    let last_found_module = match *partial_res {
1993                        Some(Res::Def(DefKind::Mod, id)) => Some(id),
1994                        None => Some(module_id),
1995                        _ => None,
1996                    };
1997                    // See if this was a module: `[path]` or `[std::io::nope]`
1998                    if let Some(module) = last_found_module {
1999                        let note = if partial_res.is_some() {
2000                            // Part of the link resolved; e.g. `std::io::nonexistent`
2001                            let module_name = tcx.item_name(module);
2002                            format!("no item named `{unresolved}` in module `{module_name}`")
2003                        } else {
2004                            // None of the link resolved; e.g. `Notimported`
2005                            format!("no item named `{unresolved}` in scope")
2006                        };
2007                        if let Some(span) = sp {
2008                            diag.span_label(span, note);
2009                        } else {
2010                            diag.note(note);
2011                        }
2012
2013                        if !path_str.contains("::") {
2014                            if disambiguator.is_none_or(|d| d.ns() == MacroNS)
2015                                && collector
2016                                    .cx
2017                                    .tcx
2018                                    .resolutions(())
2019                                    .all_macro_rules
2020                                    .contains(&Symbol::intern(path_str))
2021                            {
2022                                diag.note(format!(
2023                                    "`macro_rules` named `{path_str}` exists in this crate, \
2024                                     but it is not in scope at this link's location"
2025                                ));
2026                            } else {
2027                                // If the link has `::` in it, assume it was meant to be an
2028                                // intra-doc link. Otherwise, the `[]` might be unrelated.
2029                                diag.help(
2030                                    "to escape `[` and `]` characters, \
2031                                           add '\\' before them like `\\[` or `\\]`",
2032                                );
2033                            }
2034                        }
2035
2036                        continue;
2037                    }
2038
2039                    // Otherwise, it must be an associated item or variant
2040                    let res = partial_res.expect("None case was handled by `last_found_module`");
2041                    let kind_did = match res {
2042                        Res::Def(kind, did) => Some((kind, did)),
2043                        Res::Primitive(_) => None,
2044                    };
2045                    let is_struct_variant = |did| {
2046                        if let ty::Adt(def, _) = tcx.type_of(did).instantiate_identity().kind()
2047                            && def.is_enum()
2048                            && let Some(variant) =
2049                                def.variants().iter().find(|v| v.name == res.name(tcx))
2050                        {
2051                            // ctor is `None` if variant is a struct
2052                            variant.ctor.is_none()
2053                        } else {
2054                            false
2055                        }
2056                    };
2057                    let path_description = if let Some((kind, did)) = kind_did {
2058                        match kind {
2059                            Mod | ForeignMod => "inner item",
2060                            Struct => "field or associated item",
2061                            Enum | Union => "variant or associated item",
2062                            Variant if is_struct_variant(did) => {
2063                                let variant = res.name(tcx);
2064                                let note = format!("variant `{variant}` has no such field");
2065                                if let Some(span) = sp {
2066                                    diag.span_label(span, note);
2067                                } else {
2068                                    diag.note(note);
2069                                }
2070                                return;
2071                            }
2072                            Variant
2073                            | Field
2074                            | Closure
2075                            | AssocTy
2076                            | AssocConst
2077                            | AssocFn
2078                            | Fn
2079                            | Macro(_)
2080                            | Const
2081                            | ConstParam
2082                            | ExternCrate
2083                            | Use
2084                            | LifetimeParam
2085                            | Ctor(_, _)
2086                            | AnonConst
2087                            | InlineConst => {
2088                                let note = assoc_item_not_allowed(res);
2089                                if let Some(span) = sp {
2090                                    diag.span_label(span, note);
2091                                } else {
2092                                    diag.note(note);
2093                                }
2094                                return;
2095                            }
2096                            Trait
2097                            | TyAlias
2098                            | ForeignTy
2099                            | OpaqueTy
2100                            | TraitAlias
2101                            | TyParam
2102                            | Static { .. } => "associated item",
2103                            Impl { .. } | GlobalAsm | SyntheticCoroutineBody => {
2104                                unreachable!("not a path")
2105                            }
2106                        }
2107                    } else {
2108                        "associated item"
2109                    };
2110                    let name = res.name(tcx);
2111                    let note = format!(
2112                        "the {res} `{name}` has no {disamb_res} named `{unresolved}`",
2113                        res = res.descr(),
2114                        disamb_res = disambiguator.map_or(path_description, |d| d.descr()),
2115                    );
2116                    if let Some(span) = sp {
2117                        diag.span_label(span, note);
2118                    } else {
2119                        diag.note(note);
2120                    }
2121
2122                    continue;
2123                }
2124                let note = match failure {
2125                    ResolutionFailure::NotResolved { .. } => unreachable!("handled above"),
2126                    ResolutionFailure::WrongNamespace { res, expected_ns } => {
2127                        suggest_disambiguator(
2128                            res,
2129                            diag,
2130                            path_str,
2131                            link_range.clone(),
2132                            sp,
2133                            &diag_info,
2134                        );
2135
2136                        if let Some(disambiguator) = disambiguator
2137                            && !matches!(disambiguator, Disambiguator::Namespace(..))
2138                        {
2139                            format!(
2140                                "this link resolves to {}, which is not {} {}",
2141                                item(res),
2142                                disambiguator.article(),
2143                                disambiguator.descr()
2144                            )
2145                        } else {
2146                            format!(
2147                                "this link resolves to {}, which is not in the {} namespace",
2148                                item(res),
2149                                expected_ns.descr()
2150                            )
2151                        }
2152                    }
2153                };
2154                if let Some(span) = sp {
2155                    diag.span_label(span, note);
2156                } else {
2157                    diag.note(note);
2158                }
2159            }
2160        },
2161    );
2162}
2163
2164fn report_multiple_anchors(cx: &DocContext<'_>, diag_info: DiagnosticInfo<'_>) {
2165    let msg = format!("`{}` contains multiple anchors", diag_info.ori_link);
2166    anchor_failure(cx, diag_info, msg, 1)
2167}
2168
2169fn report_anchor_conflict(cx: &DocContext<'_>, diag_info: DiagnosticInfo<'_>, def_id: DefId) {
2170    let (link, kind) = (diag_info.ori_link, Res::from_def_id(cx.tcx, def_id).descr());
2171    let msg = format!("`{link}` contains an anchor, but links to {kind}s are already anchored");
2172    anchor_failure(cx, diag_info, msg, 0)
2173}
2174
2175/// Report an anchor failure.
2176fn anchor_failure(
2177    cx: &DocContext<'_>,
2178    diag_info: DiagnosticInfo<'_>,
2179    msg: String,
2180    anchor_idx: usize,
2181) {
2182    report_diagnostic(cx.tcx, BROKEN_INTRA_DOC_LINKS, msg, &diag_info, |diag, sp, _link_range| {
2183        if let Some(mut sp) = sp {
2184            if let Some((fragment_offset, _)) =
2185                diag_info.ori_link.char_indices().filter(|(_, x)| *x == '#').nth(anchor_idx)
2186            {
2187                sp = sp.with_lo(sp.lo() + BytePos(fragment_offset as _));
2188            }
2189            diag.span_label(sp, "invalid anchor");
2190        }
2191    });
2192}
2193
2194/// Report an error in the link disambiguator.
2195fn disambiguator_error(
2196    cx: &DocContext<'_>,
2197    mut diag_info: DiagnosticInfo<'_>,
2198    disambiguator_range: MarkdownLinkRange,
2199    msg: impl Into<DiagMessage> + Display,
2200) {
2201    diag_info.link_range = disambiguator_range;
2202    report_diagnostic(cx.tcx, BROKEN_INTRA_DOC_LINKS, msg, &diag_info, |diag, _sp, _link_range| {
2203        let msg = format!(
2204            "see {}/rustdoc/write-documentation/linking-to-items-by-name.html#namespaces-and-disambiguators for more info about disambiguators",
2205            crate::DOC_RUST_LANG_ORG_VERSION
2206        );
2207        diag.note(msg);
2208    });
2209}
2210
2211fn report_malformed_generics(
2212    cx: &DocContext<'_>,
2213    diag_info: DiagnosticInfo<'_>,
2214    err: MalformedGenerics,
2215    path_str: &str,
2216) {
2217    report_diagnostic(
2218        cx.tcx,
2219        BROKEN_INTRA_DOC_LINKS,
2220        format!("unresolved link to `{path_str}`"),
2221        &diag_info,
2222        |diag, sp, _link_range| {
2223            let note = match err {
2224                MalformedGenerics::UnbalancedAngleBrackets => "unbalanced angle brackets",
2225                MalformedGenerics::MissingType => "missing type for generic parameters",
2226                MalformedGenerics::HasFullyQualifiedSyntax => {
2227                    diag.note(
2228                        "see https://github.com/rust-lang/rust/issues/74563 for more information",
2229                    );
2230                    "fully-qualified syntax is unsupported"
2231                }
2232                MalformedGenerics::InvalidPathSeparator => "has invalid path separator",
2233                MalformedGenerics::TooManyAngleBrackets => "too many angle brackets",
2234                MalformedGenerics::EmptyAngleBrackets => "empty angle brackets",
2235            };
2236            if let Some(span) = sp {
2237                diag.span_label(span, note);
2238            } else {
2239                diag.note(note);
2240            }
2241        },
2242    );
2243}
2244
2245/// Report an ambiguity error, where there were multiple possible resolutions.
2246///
2247/// If all `candidates` have the same kind, it's not possible to disambiguate so in this case,
2248/// the function won't emit an error and will return `false`. Otherwise, it'll emit the error and
2249/// return `true`.
2250fn ambiguity_error(
2251    cx: &DocContext<'_>,
2252    diag_info: &DiagnosticInfo<'_>,
2253    path_str: &str,
2254    candidates: &[(Res, Option<DefId>)],
2255    emit_error: bool,
2256) -> bool {
2257    let mut descrs = FxHashSet::default();
2258    // proc macro can exist in multiple namespaces at once, so we need to compare `DefIds`
2259    //  to remove the candidate in the fn namespace.
2260    let mut possible_proc_macro_id = None;
2261    let is_proc_macro_crate = cx.tcx.crate_types() == [CrateType::ProcMacro];
2262    let mut kinds = candidates
2263        .iter()
2264        .map(|(res, def_id)| {
2265            let r =
2266                if let Some(def_id) = def_id { Res::from_def_id(cx.tcx, *def_id) } else { *res };
2267            if is_proc_macro_crate && let Res::Def(DefKind::Macro(_), id) = r {
2268                possible_proc_macro_id = Some(id);
2269            }
2270            r
2271        })
2272        .collect::<Vec<_>>();
2273    // In order to properly dedup proc macros, we have to do it in two passes:
2274    // 1. Completing the full traversal to find the possible duplicate in the macro namespace,
2275    // 2. Another full traversal to eliminate the candidate in the fn namespace.
2276    //
2277    // Thus, we have to do an iteration after collection is finished.
2278    //
2279    // As an optimization, we only deduplicate if we're in a proc-macro crate,
2280    // and only if we already found something that looks like a proc macro.
2281    if is_proc_macro_crate && let Some(macro_id) = possible_proc_macro_id {
2282        kinds.retain(|res| !matches!(res, Res::Def(DefKind::Fn, fn_id) if macro_id == *fn_id));
2283    }
2284
2285    kinds.retain(|res| descrs.insert(res.descr()));
2286
2287    if descrs.len() == 1 {
2288        // There is no way for users to disambiguate at this point, so better return the first
2289        // candidate and not show a warning.
2290        return false;
2291    } else if !emit_error {
2292        return true;
2293    }
2294
2295    let mut msg = format!("`{path_str}` is ");
2296    match kinds.as_slice() {
2297        [res1, res2] => {
2298            msg += &format!(
2299                "both {} {} and {} {}",
2300                res1.article(),
2301                res1.descr(),
2302                res2.article(),
2303                res2.descr()
2304            );
2305        }
2306        _ => {
2307            let mut kinds = kinds.iter().peekable();
2308            while let Some(res) = kinds.next() {
2309                if kinds.peek().is_some() {
2310                    msg += &format!("{} {}, ", res.article(), res.descr());
2311                } else {
2312                    msg += &format!("and {} {}", res.article(), res.descr());
2313                }
2314            }
2315        }
2316    }
2317
2318    report_diagnostic(cx.tcx, BROKEN_INTRA_DOC_LINKS, msg, diag_info, |diag, sp, link_range| {
2319        if let Some(sp) = sp {
2320            diag.span_label(sp, "ambiguous link");
2321        } else {
2322            diag.note("ambiguous link");
2323        }
2324
2325        for res in kinds {
2326            suggest_disambiguator(res, diag, path_str, link_range.clone(), sp, diag_info);
2327        }
2328    });
2329    true
2330}
2331
2332/// In case of an ambiguity or mismatched disambiguator, suggest the correct
2333/// disambiguator.
2334fn suggest_disambiguator(
2335    res: Res,
2336    diag: &mut Diag<'_, ()>,
2337    path_str: &str,
2338    link_range: MarkdownLinkRange,
2339    sp: Option<rustc_span::Span>,
2340    diag_info: &DiagnosticInfo<'_>,
2341) {
2342    let suggestion = res.disambiguator_suggestion();
2343    let help = format!("to link to the {}, {}", res.descr(), suggestion.descr());
2344
2345    let ori_link = match link_range {
2346        MarkdownLinkRange::Destination(range) => Some(&diag_info.dox[range]),
2347        MarkdownLinkRange::WholeLink(_) => None,
2348    };
2349
2350    if let (Some(sp), Some(ori_link)) = (sp, ori_link) {
2351        let mut spans = suggestion.as_help_span(ori_link, sp);
2352        if spans.len() > 1 {
2353            diag.multipart_suggestion(help, spans, Applicability::MaybeIncorrect);
2354        } else {
2355            let (sp, suggestion_text) = spans.pop().unwrap();
2356            diag.span_suggestion_verbose(sp, help, suggestion_text, Applicability::MaybeIncorrect);
2357        }
2358    } else {
2359        diag.help(format!("{help}: {}", suggestion.as_help(path_str)));
2360    }
2361}
2362
2363/// Report a link from a public item to a private one.
2364fn privacy_error(cx: &DocContext<'_>, diag_info: &DiagnosticInfo<'_>, path_str: &str) {
2365    let sym;
2366    let item_name = match diag_info.item.name {
2367        Some(name) => {
2368            sym = name;
2369            sym.as_str()
2370        }
2371        None => "<unknown>",
2372    };
2373    let msg = format!("public documentation for `{item_name}` links to private item `{path_str}`");
2374
2375    report_diagnostic(cx.tcx, PRIVATE_INTRA_DOC_LINKS, msg, diag_info, |diag, sp, _link_range| {
2376        if let Some(sp) = sp {
2377            diag.span_label(sp, "this item is private");
2378        }
2379
2380        let note_msg = if cx.render_options.document_private {
2381            "this link resolves only because you passed `--document-private-items`, but will break without"
2382        } else {
2383            "this link will resolve properly if you pass `--document-private-items`"
2384        };
2385        diag.note(note_msg);
2386    });
2387}
2388
2389/// Resolve a primitive type or value.
2390fn resolve_primitive(path_str: &str, ns: Namespace) -> Option<Res> {
2391    if ns != TypeNS {
2392        return None;
2393    }
2394    use PrimitiveType::*;
2395    let prim = match path_str {
2396        "isize" => Isize,
2397        "i8" => I8,
2398        "i16" => I16,
2399        "i32" => I32,
2400        "i64" => I64,
2401        "i128" => I128,
2402        "usize" => Usize,
2403        "u8" => U8,
2404        "u16" => U16,
2405        "u32" => U32,
2406        "u64" => U64,
2407        "u128" => U128,
2408        "f16" => F16,
2409        "f32" => F32,
2410        "f64" => F64,
2411        "f128" => F128,
2412        "char" => Char,
2413        "bool" | "true" | "false" => Bool,
2414        "str" | "&str" => Str,
2415        // See #80181 for why these don't have symbols associated.
2416        "slice" => Slice,
2417        "array" => Array,
2418        "tuple" => Tuple,
2419        "unit" => Unit,
2420        "pointer" | "*const" | "*mut" => RawPointer,
2421        "reference" | "&" | "&mut" => Reference,
2422        "fn" => Fn,
2423        "never" | "!" => Never,
2424        _ => return None,
2425    };
2426    debug!("resolved primitives {prim:?}");
2427    Some(Res::Primitive(prim))
2428}