10000 Rollup of 6 pull requests by Dylan-DPC · Pull Request #98347 · rust-lang/rust · GitHub
[go: up one dir, main page]

Skip to content

Rollup of 6 pull requests #98347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
c7b6e1d
lub: don't bail out due to empty binders
lcnr Jun 8, 2022
0667b00
update tests + add future compat test
lcnr Jun 8, 2022
3f12fa7
Add support for macro in "jump to def" feature
GuillaumeGomez Nov 26, 2021
dda980d
Rename ContextInfo into HrefContext
GuillaumeGomez Jun 18, 2022
810254b
Improve code readability and documentation
GuillaumeGomez Jun 18, 2022
f4db07e
Add test for macro support in "jump to def" feature
GuillaumeGomez Nov 26, 2021
987c731
Integrate `generate_macro_def_id_path` into `href_with_root_path`
GuillaumeGomez Jun 20, 2022
eb6141e
Support setting file accessed/modified timestamps
joshtriplett Jun 19, 2022
585767d
Add alias `File::set_modified` as shorthand
joshtriplett Jun 20, 2022
beb2f36
Fix panic by checking if `CStore` has the crate data we want before a…
GuillaumeGomez Jun 20, 2022
e900a35
Give name if anonymous region appears in impl signature
compiler-errors Jun 21, 2022
31476e7
Add a full regression test for #73727
JohnTitor Jun 21, 2022
5ed1495
This comment is out dated and misleading
spastorino Jun 21, 2022
d502e83
Rollup merge of #91264 - GuillaumeGomez:macro-jump-to-def, r=jsha
Dylan-DPC Jun 21, 2022
a6de3f6
Rollup merge of #97867 - lcnr:lub-binder, r=oli-obk
Dylan-DPC Jun 21, 2022
b5a2a80
Rollup merge of #98184 - compiler-errors:elided-lifetime-in-impl-nll,…
Dylan-DPC Jun 21, 2022
223d7f8
Rollup merge of #98246 - joshtriplett:times, r=m-ou-se
Dylan-DPC Jun 21, 2022
2b1ce20
Rollup merge of #98334 - JohnTitor:issue-73727, r=compiler-errors
Dylan-DPC Jun 21, 2022
9bf095d
Rollup merge of #98344 - spastorino:remove-misleading-comment, r=oli-obk
Dylan-DPC Jun 21, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Integrate generate_macro_def_id_path into href_with_root_path
  • Loading branch information
GuillaumeGomez committed Jun 20, 2022
commit 987c73158e2120ef75b4b7fc46dcd88a621106d8
71 changes: 70 additions & 1 deletion src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
use std::borrow::Cow;
use std::cell::Cell;
use std::fmt;
use std::iter;
use std::iter::{self, once};

use rustc_ast as ast;
use rustc_attr::{ConstStability, StabilityLevel};
use rustc_data_structures::captures::Captures;
use rustc_data_structures::fx::FxHashSet;
use rustc_hir as hir;
use rustc_hir::def::DefKind;
use rustc_hir::def_id::DefId;
use rustc_metadata::creader::{CStore, LoadedMacro};
use rustc_middle::ty;
use rustc_middle::ty::DefIdTree;
use rustc_middle::ty::TyCtxt;
Expand Down Expand Up @@ -557,6 +559,71 @@ pub(crate) fn join_with_double_colon(syms: &[Symbol]) -> String {
s
}

/// This function is to get the external macro path because they are not in the cache used in
/// `href_with_root_path`.
fn generate_macro_def_id_path(
def_id: DefId,
cx: &Context<'_>,
root_path: Option<&str>,
) -> (String, ItemType, Vec<Symbol>) {
let tcx = cx.shared.tcx;
let crate_name = tcx.crate_name(def_id.krate).to_string();
let cache = cx.cache();

let fqp: Vec<Symbol> = tcx
.def_path(def_id)
.data
.into_iter()
.filter_map(|elem| {
// extern blocks (and a few others things) have an empty name.
match elem.data.get_opt_name() {
Some(s) if !s.is_empty() => Some(s),
_ => None,
}
})
.collect();
let relative = fqp.iter().map(|elem| elem.to_string());
// Check to see if it is a macro 2.0 or built-in macro.
// More information in <https://rust-lang.github.io/rfcs/1584-macros.html>.
let is_macro_2 = match CStore::from_tcx(tcx).load_macro_untracked(def_id, tcx.sess) {
LoadedMacro::MacroDef(def, _) => {
// If `ast_def.macro_rules` is `true`, then it's not a macro 2.0.
matches!(&def.kind, ast::ItemKind::MacroDef(ast_def) if !ast_def.macro_rules)
}
_ => false,
};

let mut path = if is_macro_2 {
once(crate_name.clone()).chain(relative).collect()
} else {
vec![crate_name.clone(), relative.last().unwrap()]
};
if path.len() < 2 {
// The minimum we can have is the crate name followed by the macro name. If shorter, then
// it means that that `relative` was empty, which is an error.
panic!("macro path cannot be empty!");
}

if let Some(last) = path.last_mut() {
*last = format!("macro.{}.html", last);
}

let url = match cache.extern_locations[&def_id.krate] {
ExternalLocation::Remote(ref s) => {
// `ExternalLocation::Remote` always end with a `/`.
format!("{}{}", s, path.join("/"))
}
ExternalLocation::Local => {
// `root_path` always end with a `/`.
format!("{}{}/{}", root_path.unwrap_or(""), crate_name, path.join("/"))
}
ExternalLocation::Unknown => {
panic!("crate {} not in cache when linkifying macros", crate_name)
}
};
(url, ItemType::Macro, fqp)
}

pub(crate) fn href_with_root_path(
did: DefId,
cx: &Context<'_>,
Expand Down Expand Up @@ -612,6 +679,8 @@ pub(crate) fn href_with_root_path(
ExternalLocation::Unknown => return Err(HrefError::DocumentationNotBuilt),
},
)
} else if matches!(def_kind, DefKind::Macro(_)) {
return Ok(generate_macro_def_id_path(did, cx, root_path));
} else {
return Err(HrefError::NotInExternalCache);
}
Expand Down
69 changes: 2 additions & 67 deletions src/librustdoc/html/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,15 @@
//!
//! Use the `render_with_highlighting` to highlight some rust code.

use crate::clean::{ExternalLocation, PrimitiveType};
use crate::clean::PrimitiveType;
use crate::html::escape::Escape;
use crate::html::render::Context;

use std::collections::VecDeque;
use std::fmt::{Display, Write};
use std::iter::once;

use rustc_ast as ast;
use rustc_data_structures::fx::FxHashMap;
use rustc_hir::def_id::DefId;
use rustc_lexer::{LiteralKind, TokenKind};
use rustc_metadata::creader::{CStore, LoadedMacro};
use rustc_span::edition::Edition;
use rustc_span::symbol::Symbol;
use rustc_span::{BytePos, Span, DUMMY_SP};
Expand Down Expand Up @@ -784,17 +780,8 @@ fn string_without_closing_tag<T: Display>(
.map(|s| format!("{}{}", href_context.root_path, s)),
LinkFromSrc::External(def_id) => {
format::href_with_root_path(*def_id, context, Some(href_context.root_path))
.map(|(url, _, _)| url)
.or_else(|e| {
if e == format::HrefError::NotInExternalCache
&& matches!(klass, Class::Macro(_))
{
Ok(generate_macro_def_id_path(href_context, *def_id))
} else {
Err(e)
}
})
.ok()
.map(|(url, _, _)| url)
}
LinkFromSrc::Primitive(prim) => format::href_with_root_path(
PrimitiveType::primitive_locations(context.tcx())[prim],
Expand All @@ -814,57 +801,5 @@ fn string_without_closing_tag<T: Display>(
Some("</span>")
}

/// This function is to get the external macro path because they are not in the cache used in
/// `href_with_root_path`.
fn generate_macro_def_id_path(href_context: &HrefContext<'_, '_, '_>, def_id: DefId) -> String {
let tcx = href_context.context.shared.tcx;
let crate_name = tcx.crate_name(def_id.krate).to_string();
let cache = href_context.context.cache();

let relative = tcx.def_path(def_id).data.into_iter().filter_map(|elem| {
// extern blocks have an empty name
let s = elem.data.to_string();
if !s.is_empty() { Some(s) } else { None }
});
// Check to see if it is a macro 2.0 or built-in macro.
// More information in <https://rust-lang.github.io/rfcs/1584-macros.html>.
let is_macro_2 = match CStore::from_tcx(tcx).load_macro_untracked(def_id, tcx.sess) {
LoadedMacro::MacroDef(def, _) => {
// If `ast_def.macro_rules` is `true`, then it's not a macro 2.0.
matches!(&def.kind, ast::ItemKind::MacroDef(ast_def) if !ast_def.macro_rules)
}
_ => false,
};

let mut path = if is_macro_2 {
once(crate_name.clone()).chain(relative).collect()
} else {
vec![crate_name.clone(), relative.last().unwrap()]
};
if path.len() < 2 {
// The minimum we can have is the crate name followed by the macro name. If shorter, then
// it means that that `relative` was empty, which is an error.
panic!("macro path cannot be empty!");
}

if let Some(last) = path.last_mut() {
*last = format!("macro.{}.html", last);
}

match cache.extern_locations[&def_id.krate] {
ExternalLocation::Remote(ref s) => {
// `ExternalLocation::Remote` always end with a `/`.
format!("{}{}", s, path.join("/"))
}
ExternalLocation::Local => {
// `href_context.root_path` always end with a `/`.
format!("{}{}/{}", href_context.root_path, crate_name, path.join("/"))
}
ExternalLocation::Unknown => {
panic!("crate {} not in cache when linkifying macros", crate_name)
}
}
}

#[cfg(test)]
mod tests;
0