Skip to content

Commit 47aeac6

Browse files
committed
Auto merge of #90337 - matthiaskrgr:rollup-azkr158, r=matthiaskrgr
Rollup of 3 pull requests Successful merges: - #90154 (rustdoc: Remove `GetDefId`) - #90232 (rustdoc: Use TTF based font instead of OTF for CJK glyphs to improve readability) - #90278 (rustdoc: use better highlighting for *const, *mut, and &mut) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents a9b2bfb + 96c0e71 commit 47aeac6

23 files changed

+153
-142
lines changed

library/core/src/str/traits.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ unsafe impl SliceIndex<str> for ops::Range<usize> {
234234
/// Implements substring slicing with syntax `&self[.. end]` or `&mut
235235
/// self[.. end]`.
236236
///
237-
/// Returns a slice of the given string from the byte range [`0`, `end`).
237+
/// Returns a slice of the given string from the byte range \[0, `end`).
238238
/// Equivalent to `&self[0 .. end]` or `&mut self[0 .. end]`.
239239
///
240240
/// This operation is *O*(1).
@@ -304,9 +304,8 @@ unsafe impl SliceIndex<str> for ops::RangeTo<usize> {
304304
/// Implements substring slicing with syntax `&self[begin ..]` or `&mut
305305
/// self[begin ..]`.
306306
///
307-
/// Returns a slice of the given string from the byte range [`begin`,
308-
/// `len`). Equivalent to `&self[begin .. len]` or `&mut self[begin ..
309-
/// len]`.
307+
/// Returns a slice of the given string from the byte range \[`begin`, `len`).
308+
/// Equivalent to `&self[begin .. len]` or `&mut self[begin .. len]`.
310309
///
311310
/// This operation is *O*(1).
312311
///
@@ -433,7 +432,7 @@ unsafe impl SliceIndex<str> for ops::RangeInclusive<usize> {
433432
/// Implements substring slicing with syntax `&self[..= end]` or `&mut
434433
/// self[..= end]`.
435434
///
436-
/// Returns a slice of the given string from the byte range [0, `end`].
435+
/// Returns a slice of the given string from the byte range \[0, `end`\].
437436
/// Equivalent to `&self [0 .. end + 1]`, except if `end` has the maximum
438437
/// value for `usize`.
439438
///

library/std/src/sys_common/wtf8.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ impl Wtf8 {
686686
}
687687
}
688688

689-
/// Returns a slice of the given string for the byte range [`begin`..`end`).
689+
/// Returns a slice of the given string for the byte range \[`begin`..`end`).
690690
///
691691
/// # Panics
692692
///

src/librustdoc/clean/inline.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ use rustc_middle::ty::{self, TyCtxt};
1414
use rustc_span::hygiene::MacroKind;
1515
use rustc_span::symbol::{kw, sym, Symbol};
1616

17-
use crate::clean::{
18-
self, utils, Attributes, AttributesExt, GetDefId, ItemId, NestedAttributesExt, Type,
19-
};
17+
use crate::clean::{self, utils, Attributes, AttributesExt, ItemId, NestedAttributesExt, Type};
2018
use crate::core::DocContext;
2119
use crate::formats::item_type::ItemType;
2220

@@ -325,7 +323,7 @@ fn merge_attrs(
325323
}
326324
}
327325

328-
/// Builds a specific implementation of a type. The `did` could be a type method or trait method.
326+
/// Inline an `impl`, inherent or of a trait. The `did` must be for an `impl`.
329327
crate fn build_impl(
330328
cx: &mut DocContext<'_>,
331329
parent_module: impl Into<Option<DefId>>,
@@ -376,7 +374,7 @@ crate fn build_impl(
376374
// Only inline impl if the implementing type is
377375
// reachable in rustdoc generated documentation
378376
if !did.is_local() {
379-
if let Some(did) = for_.def_id() {
377+
if let Some(did) = for_.def_id(&cx.cache) {
380378
if !cx.cache.access_levels.is_public(did) {
381379
return;
382380
}
@@ -464,7 +462,7 @@ crate fn build_impl(
464462
}
465463

466464
while let Some(ty) = stack.pop() {
467-
if let Some(did) = ty.def_id() {
465+
if let Some(did) = ty.def_id(&cx.cache) {
468466
if tcx.get_attrs(did).lists(sym::doc).has_word(sym::hidden) {
469467
return;
470468
}
@@ -481,7 +479,11 @@ crate fn build_impl(
481479
let (merged_attrs, cfg) = merge_attrs(cx, parent_module.into(), load_attrs(cx, did), attrs);
482480
trace!("merged_attrs={:?}", merged_attrs);
483481

484-
trace!("build_impl: impl {:?} for {:?}", trait_.as_ref().map(|t| t.def_id()), for_.def_id());
482+
trace!(
483+
"build_impl: impl {:?} for {:?}",
484+
trait_.as_ref().map(|t| t.def_id()),
485+
for_.def_id(&cx.cache)
486+
);
485487
ret.push(clean::Item::from_def_id_and_attrs_and_parts(
486488
did,
487489
None,

src/librustdoc/clean/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ impl<'tcx> Clean<Type> for ty::ProjectionTy<'tcx> {
383383
let self_type = self.self_ty().clean(cx);
384384
Type::QPath {
385385
name: cx.tcx.associated_item(self.item_def_id).ident.name,
386-
self_def_id: self_type.def_id(),
386+
self_def_id: self_type.def_id(&cx.cache),
387387
self_type: box self_type,
388388
trait_,
389389
}
@@ -1883,7 +1883,7 @@ fn clean_impl(impl_: &hir::Impl<'_>, hir_id: hir::HirId, cx: &mut DocContext<'_>
18831883
}
18841884

18851885
let for_ = impl_.self_ty.clean(cx);
1886-
let type_alias = for_.def_id().and_then(|did| match tcx.def_kind(did) {
1886+
let type_alias = for_.def_id(&cx.cache).and_then(|did| match tcx.def_kind(did) {
18871887
DefKind::TyAlias => Some(tcx.type_of(did).clean(cx)),
18881888
_ => None,
18891889
});

src/librustdoc/clean/types.rs

+21-56
Original file line numberDiff line numberDiff line change
@@ -1370,17 +1370,10 @@ crate enum FnRetTy {
13701370
DefaultReturn,
13711371
}
13721372

1373-
impl GetDefId for FnRetTy {
1374-
fn def_id(&self) -> Option<DefId> {
1375-
match *self {
1376-
Return(ref ty) => ty.def_id(),
1377-
DefaultReturn => None,
1378-
}
1379-
}
1380-
1381-
fn def_id_full(&self, cache: &Cache) -> Option<DefId> {
1382-
match *self {
1383-
Return(ref ty) => ty.def_id_full(cache),
1373+
impl FnRetTy {
1374+
crate fn as_return(&self) -> Option<&Type> {
1375+
match self {
1376+
Return(ret) => Some(ret),
13841377
DefaultReturn => None,
13851378
}
13861379
}
@@ -1458,34 +1451,6 @@ crate enum Type {
14581451
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
14591452
rustc_data_structures::static_assert_size!(Type, 72);
14601453

1461-
crate trait GetDefId {
1462-
/// Use this method to get the [`DefId`] of a [`clean`] AST node.
1463-
/// This will return [`None`] when called on a primitive [`clean::Type`].
1464-
/// Use [`Self::def_id_full`] if you want to include primitives.
1465-
///
1466-
/// [`clean`]: crate::clean
1467-
/// [`clean::Type`]: crate::clean::Type
1468-
// FIXME: get rid of this function and always use `def_id_full`
1469-
fn def_id(&self) -> Option<DefId>;
1470-
1471-
/// Use this method to get the [DefId] of a [clean] AST node, including [PrimitiveType]s.
1472-
///
1473-
/// See [`Self::def_id`] for more.
1474-
///
1475-
/// [clean]: crate::clean
1476-
fn def_id_full(&self, cache: &Cache) -> Option<DefId>;
1477-
}
1478-
1479-
impl<T: GetDefId> GetDefId for Option<T> {
1480-
fn def_id(&self) -> Option<DefId> {
1481-
self.as_ref().and_then(|d| d.def_id())
1482-
}
1483-
1484-
fn def_id_full(&self, cache: &Cache) -> Option<DefId> {
1485-
self.as_ref().and_then(|d| d.def_id_full(cache))
1486-
}
1487-
}
1488-
14891454
impl Type {
14901455
crate fn primitive_type(&self) -> Option<PrimitiveType> {
14911456
match *self {
@@ -1564,17 +1529,27 @@ impl Type {
15641529
QPath { ref self_type, .. } => return self_type.inner_def_id(cache),
15651530
Generic(_) | Infer | ImplTrait(_) => return None,
15661531
};
1567-
cache.and_then(|c| Primitive(t).def_id_full(c))
1532+
cache.and_then(|c| Primitive(t).def_id(c))
15681533
}
1569-
}
15701534

1571-
impl GetDefId for Type {
1572-
fn def_id(&self) -> Option<DefId> {
1573-
self.inner_def_id(None)
1535+
/// Use this method to get the [DefId] of a [clean] AST node, including [PrimitiveType]s.
1536+
///
1537+
/// See [`Self::def_id_no_primitives`] for more.
1538+
///
1539+
/// [clean]: crate::clean
1540+
crate fn def_id(&self, cache: &Cache) -> Option<DefId> {
1541+
self.inner_def_id(Some(cache))
15741542
}
15751543

1576-
fn def_id_full(&self, cache: &Cache) -> Option<DefId> {
1577-
self.inner_def_id(Some(cache))
1544+
/// Use this method to get the [`DefId`] of a [`clean`] AST node.
1545+
/// This will return [`None`] when called on a primitive [`clean::Type`].
1546+
/// Use [`Self::def_id`] if you want to include primitives.
1547+
///
1548+
/// [`clean`]: crate::clean
1549+
/// [`clean::Type`]: crate::clean::Type
1550+
// FIXME: get rid of this function and always use `def_id`
1551+
crate fn def_id_no_primitives(&self) -> Option<DefId> {
1552+
self.inner_def_id(None)
15781553
}
15791554
}
15801555

@@ -2092,16 +2067,6 @@ crate struct Typedef {
20922067
crate item_type: Option<Type>,
20932068
}
20942069

2095-
impl GetDefId for Typedef {
2096-
fn def_id(&self) -> Option<DefId> {
2097-
self.type_.def_id()
2098-
}
2099-
2100-
fn def_id_full(&self, cache: &Cache) -> Option<DefId> {
2101-
self.type_.def_id_full(cache)
2102-
}
2103-
}
2104-
21052070
#[derive(Clone, Debug)]
21062071
crate struct OpaqueTy {
21072072
crate bounds: Vec<GenericBound>,

src/librustdoc/formats/cache.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_middle::middle::privacy::AccessLevels;
66
use rustc_middle::ty::TyCtxt;
77
use rustc_span::symbol::sym;
88

9-
use crate::clean::{self, GetDefId, ItemId, PrimitiveType};
9+
use crate::clean::{self, ItemId, PrimitiveType};
1010
use crate::config::RenderOptions;
1111
use crate::fold::DocFolder;
1212
use crate::formats::item_type::ItemType;
@@ -206,7 +206,9 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
206206
|| i.trait_
207207
.as_ref()
208208
.map_or(false, |t| self.cache.masked_crates.contains(&t.def_id().krate))
209-
|| i.for_.def_id().map_or(false, |d| self.cache.masked_crates.contains(&d.krate))
209+
|| i.for_
210+
.def_id(self.cache)
211+
.map_or(false, |d| self.cache.masked_crates.contains(&d.krate))
210212
{
211213
return None;
212214
}
@@ -454,7 +456,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
454456

455457
if let Some(generics) = i.trait_.as_ref().and_then(|t| t.generics()) {
456458
for bound in generics {
457-
if let Some(did) = bound.def_id() {
459+
if let Some(did) = bound.def_id(self.cache) {
458460
dids.insert(did);
459461
}
460462
}

src/librustdoc/html/highlight.rs

+21-6
Original file line numberDiff line numberDiff line change
@@ -469,22 +469,37 @@ impl<'a> Classifier<'a> {
469469
// Assume that '&' or '*' is the reference or dereference operator
470470
// or a reference or pointer type. Unless, of course, it looks like
471471
// a logical and or a multiplication operator: `&&` or `* `.
472-
TokenKind::Star => match self.peek() {
473-
Some(TokenKind::Whitespace) => Class::Op,
472+
TokenKind::Star => match self.tokens.peek() {
473+
Some((TokenKind::Whitespace, _)) => Class::Op,
474+
Some((TokenKind::Ident, "mut")) => {
475+
self.next();
476+
sink(Highlight::Token { text: "*mut", class: Some(Class::RefKeyWord) });
477+
return;
478+
}
479+
Some((TokenKind::Ident, "const")) => {
480+
self.next();
481+
sink(Highlight::Token { text: "*const", class: Some(Class::RefKeyWord) });
482+
return;
483+
}
474484
_ => Class::RefKeyWord,
475485
},
476-
TokenKind::And => match lookahead {
477-
Some(TokenKind::And) => {
486+
TokenKind::And => match self.tokens.peek() {
487+
Some((TokenKind::And, _)) => {
478488
self.next();
479489
sink(Highlight::Token { text: "&&", class: Some(Class::Op) });
480490
return;
481491
}
482-
Some(TokenKind::Eq) => {
492+
Some((TokenKind::Eq, _)) => {
483493
self.next();
484494
sink(Highlight::Token { text: "&=", class: Some(Class::Op) });
485495
return;
486496
}
487-
Some(TokenKind::Whitespace) => Class::Op,
497+
Some((TokenKind::Whitespace, _)) => Class::Op,
498+
Some((TokenKind::Ident, "mut")) => {
499+
self.next();
500+
sink(Highlight::Token { text: "&mut", class: Some(Class::RefKeyWord) });
501+
return;
502+
}
488503
_ => Class::RefKeyWord,
489504
},
490505

src/librustdoc/html/highlight/fixtures/sample.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
<span class="attribute">#[<span class="ident">cfg</span>(<span class="ident">target_os</span> <span class="op">=</span> <span class="string">&quot;linux&quot;</span>)]</span>
1616
<span class="kw">fn</span> <span class="ident">main</span>() -&gt; () {
1717
<span class="kw">let</span> <span class="ident">foo</span> <span class="op">=</span> <span class="bool-val">true</span> <span class="op">&amp;&amp;</span> <span class="bool-val">false</span> <span class="op">|</span><span class="op">|</span> <span class="bool-val">true</span>;
18-
<span class="kw">let</span> <span class="kw">_</span>: <span class="kw-2">*</span><span class="kw">const</span> () <span class="op">=</span> <span class="number">0</span>;
18+
<span class="kw">let</span> <span class="kw">_</span>: <span class="kw-2">*const</span> () <span class="op">=</span> <span class="number">0</span>;
1919
<span class="kw">let</span> <span class="kw">_</span> <span class="op">=</span> <span class="kw-2">&amp;</span><span class="ident">foo</span>;
2020
<span class="kw">let</span> <span class="kw">_</span> <span class="op">=</span> <span class="op">&amp;&amp;</span><span class="ident">foo</span>;
2121
<span class="kw">let</span> <span class="kw">_</span> <span class="op">=</span> <span class="kw-2">*</span><span class="ident">foo</span>;
22-
<span class="macro">mac!</span>(<span class="ident">foo</span>, <span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">bar</span>);
22+
<span class="macro">mac!</span>(<span class="ident">foo</span>, <span class="kw-2">&amp;mut</span> <span class="ident">bar</span>);
2323
<span class="macro">assert!</span>(<span class="self">self</span>.<span class="ident">length</span> <span class="op">&lt;</span> <span class="ident">N</span> <span class="op">&amp;&amp;</span> <span class="ident">index</span> <span class="op">&lt;</span><span class="op">=</span> <span class="self">self</span>.<span class="ident">length</span>);
2424
<span class="ident">::std::env::var</span>(<span class="string">&quot;gateau&quot;</span>).<span class="ident">is_ok</span>();
2525
<span class="attribute">#[<span class="ident">rustfmt::skip</span>]</span>

src/librustdoc/html/render/cache.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ use rustc_span::symbol::Symbol;
77
use serde::ser::{Serialize, SerializeStruct, Serializer};
88

99
use crate::clean;
10-
use crate::clean::types::{
11-
FnDecl, FnRetTy, GenericBound, Generics, GetDefId, Type, WherePredicate,
12-
};
10+
use crate::clean::types::{FnDecl, FnRetTy, GenericBound, Generics, Type, WherePredicate};
1311
use crate::formats::cache::Cache;
1412
use crate::formats::item_type::ItemType;
1513
use crate::html::markdown::short_markdown_summary;
@@ -278,7 +276,7 @@ crate fn get_real_types<'tcx>(
278276
res: &mut FxHashSet<(Type, ItemType)>,
279277
) -> usize {
280278
fn insert(res: &mut FxHashSet<(Type, ItemType)>, tcx: TyCtxt<'_>, ty: Type) -> usize {
281-
if let Some(kind) = ty.def_id().map(|did| tcx.def_kind(did).into()) {
279+
if let Some(kind) = ty.def_id_no_primitives().map(|did| tcx.def_kind(did).into()) {
282280
res.insert((ty, kind));
283281
1
284282
} else if ty.is_primitive() {
@@ -298,7 +296,9 @@ crate fn get_real_types<'tcx>(
298296

299297
if let Type::Generic(arg_s) = *arg {
300298
if let Some(where_pred) = generics.where_predicates.iter().find(|g| match g {
301-
WherePredicate::BoundPredicate { ty, .. } => ty.def_id() == arg.def_id(),
299+
WherePredicate::BoundPredicate { ty, .. } => {
300+
ty.def_id_no_primitives() == arg.def_id_no_primitives()
301+
}
302302
_ => false,
303303
}) {
304304
let bounds = where_pred.get_bounds().unwrap_or_else(|| &[]);
@@ -365,7 +365,8 @@ crate fn get_all_types<'tcx>(
365365
if !args.is_empty() {
366366
all_types.extend(args);
367367
} else {
368-
if let Some(kind) = arg.type_.def_id().map(|did| tcx.def_kind(did).into()) {
368+
if let Some(kind) = arg.type_.def_id_no_primitives().map(|did| tcx.def_kind(did).into())
369+
{
369370
all_types.insert((arg.type_.clone(), kind));
370371
}
371372
}
@@ -376,7 +377,9 @@ crate fn get_all_types<'tcx>(
376377
let mut ret = FxHashSet::default();
377378
get_real_types(generics, return_type, tcx, 0, &mut ret);
378379
if ret.is_empty() {
379-
if let Some(kind) = return_type.def_id().map(|did| tcx.def_kind(did).into()) {
380+
if let Some(kind) =
381+
return_type.def_id_no_primitives().map(|did| tcx.def_kind(did).into())
382+
{
380383
ret.insert((return_type.clone(), kind));
381384
}
382385
}

0 commit comments

Comments
 (0)