Skip to content

Commit e1ff77d

Browse files
committed
Auto merge of #156736 - GuillaumeGomez:primitive-assoc-methods, r=fmease
Fix jump to def link generation on primitive type associated methods Fixes #156707. Interestingly enough, the inference fails on primitive type, so instead I go around a bit by generating a `PrimitiveType` and then tweak a bit the `href` generation. r? @fmease
2 parents dd8b2d6 + 0c7388c commit e1ff77d

3 files changed

Lines changed: 71 additions & 16 deletions

File tree

src/librustdoc/clean/types.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc_index::IndexVec;
2020
use rustc_metadata::rendered_const;
2121
use rustc_middle::span_bug;
2222
use rustc_middle::ty::fast_reject::SimplifiedType;
23-
use rustc_middle::ty::{self, TyCtxt, Visibility};
23+
use rustc_middle::ty::{self, Ty, TyCtxt, Visibility};
2424
use rustc_resolve::rustdoc::{
2525
DocFragment, add_doc_fragment, attrs_to_doc_fragments, inner_docs, span_of_fragments,
2626
};
@@ -1758,6 +1758,40 @@ impl PrimitiveType {
17581758
}
17591759
}
17601760

1761+
pub(crate) fn from_ty(ty: Ty<'_>) -> Option<Self> {
1762+
match ty.kind() {
1763+
ty::Array(..) => Some(Self::Array),
1764+
ty::Bool => Some(Self::Bool),
1765+
ty::Char => Some(Self::Char),
1766+
ty::FnDef(..) | ty::FnPtr(..) => Some(Self::Fn),
1767+
ty::Int(int) => Some(Self::from(*int)),
1768+
ty::Uint(uint) => Some(Self::from(*uint)),
1769+
ty::Float(float) => Some(Self::from(*float)),
1770+
ty::Never => Some(Self::Never),
1771+
ty::Pat(..) => Some(Self::Pat),
1772+
ty::RawPtr(..) => Some(Self::RawPointer),
1773+
ty::Ref(..) => Some(Self::Reference),
1774+
ty::Slice(..) => Some(Self::Slice),
1775+
ty::Str => Some(Self::Str),
1776+
ty::Tuple(elems) if elems.is_empty() => Some(Self::Unit),
1777+
ty::Tuple(_) => Some(Self::Tuple),
1778+
ty::Adt(..)
1779+
| ty::Alias(..)
1780+
| ty::Bound(..)
1781+
| ty::Closure(..)
1782+
| ty::Coroutine(..)
1783+
| ty::CoroutineClosure(..)
1784+
| ty::CoroutineWitness(..)
1785+
| ty::Dynamic(..)
1786+
| ty::Error(..)
1787+
| ty::Foreign(..)
1788+
| ty::Infer(..)
1789+
| ty::Param(..)
1790+
| ty::Placeholder(..)
1791+
| ty::UnsafeBinder(..) => None,
1792+
}
1793+
}
1794+
17611795
pub(crate) fn simplified_types() -> &'static SimplifiedTypes {
17621796
use PrimitiveType::*;
17631797
use ty::{FloatTy, IntTy, UintTy};

src/librustdoc/html/format.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
1010
use std::cmp::Ordering;
1111
use std::fmt::{self, Display, Write};
12-
use std::iter::{self, once};
13-
use std::slice;
12+
use std::{iter, slice};
1413

1514
use itertools::{Either, Itertools};
1615
use rustc_abi::ExternAbi;
@@ -434,27 +433,33 @@ fn generate_item_def_id_path(
434433

435434
let tcx = cx.tcx();
436435
let crate_name = tcx.crate_name(def_id.krate);
436+
let mut prim = None;
437437

438438
// No need to try to infer the actual parent item if it's not an associated item from the `impl`
439439
// block.
440440
if def_id != original_def_id && matches!(tcx.def_kind(def_id), DefKind::Impl { .. }) {
441441
let infcx = tcx.infer_ctxt().build(TypingMode::non_body_analysis());
442-
def_id = infcx
442+
let ty = tcx.type_of(def_id);
443+
let ty = infcx
443444
.at(&ObligationCause::dummy(), tcx.param_env(def_id))
444-
.query_normalize(ty::Binder::dummy(
445-
tcx.type_of(def_id).instantiate_identity().skip_norm_wip(),
446-
))
447-
.map(|resolved| infcx.resolve_vars_if_possible(resolved.value))
448-
.ok()
449-
.and_then(|normalized| normalized.skip_binder().ty_adt_def())
450-
.map(|adt| adt.did())
451-
.unwrap_or(def_id);
445+
.query_normalize(ty::Binder::dummy(ty.instantiate_identity().skip_norm_wip()))
446+
.map(|resolved| infcx.resolve_vars_if_possible(resolved.value).skip_binder())
447+
.unwrap_or(ty.skip_binder());
448+
if let Some(new_def_id) = ty.ty_adt_def().map(|adt| adt.did()) {
449+
def_id = new_def_id;
450+
} else {
451+
prim = PrimitiveType::from_ty(ty);
452+
}
452453
}
453454

454-
let relative = clean::inline::item_relative_path(tcx, def_id);
455-
let fqp: Vec<Symbol> = once(crate_name).chain(relative).collect();
456-
457-
let shortty = ItemType::from_def_id(def_id, tcx);
455+
let mut fqp = vec![crate_name];
456+
let shortty = if let Some(prim) = prim {
457+
fqp.push(prim.as_sym());
458+
ItemType::Primitive
459+
} else {
460+
fqp.append(&mut clean::inline::item_relative_path(tcx, def_id));
461+
ItemType::from_def_id(def_id, tcx)
462+
};
458463
let module_fqp = to_module_fqp(shortty, &fqp);
459464

460465
let (parts, is_absolute) = url_parts(cx.cache(), def_id, module_fqp, &cx.current)?;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Checks that links to primitive types methods work.
2+
// Regression test for <https://github.com/rust-lang/rust/issues/156707>.
3+
4+
// ignore-tidy-linelength
5+
//@ compile-flags: -Zunstable-options --generate-link-to-definition
6+
7+
#![crate_name = "foo"]
8+
9+
//@ has 'src/foo/prim-method.rs.html'
10+
11+
fn scope() {
12+
//@ has - '//a[@href="{{channel}}/core/primitive.usize.html#method.saturating_add"]' 'saturating_add'
13+
let _ = 0usize.saturating_add(1);
14+
//@ has - '//a[@href="{{channel}}/core/primitive.bool.html#method.then_some"]' 'then_some'
15+
let _ = false.then_some(());
16+
}

0 commit comments

Comments
 (0)