Skip to content

Rollup of 9 pull requests #98447

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

Merged
merged 24 commits into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
f7d12b4
Session object: Decouple e_flags from FileFlags
mkroening Jun 1, 2022
37fd294
rustc_target: Remove some redundant target properties
petrochenkov Jun 17, 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
beb2f36
Fix panic by checking if `CStore` has the crate data we want before a…
GuillaumeGomez Jun 20, 2022
d15fed7
Improve suggestion for calling closure on type mismatch
compiler-errors Jun 20, 2022
94477e3
Fixup missing renames from `#[main]` to `#[rustc_main]`
Enselic Jun 22, 2022
36ccdbe
Remove (transitive) reliance on sorting by DefId in pretty-printer
Aaron1011 May 11, 2022
04b75a7
Update tendril
ehuss Jun 22, 2022
9730221
Remove excess rib while resolving closures
WaffleLapkin Jun 23, 2022
21625e5
Session object: Set OS/ABI
mkroening Jun 1, 2022
774e814
Fix BTreeSet's range API panic message, document
tnballo Jun 23, 2022
97f4d7b
Rollup merge of #91264 - GuillaumeGomez:macro-jump-to-def, r=jsha
JohnTitor Jun 24, 2022
2c6feb5
Rollup merge of #96955 - Aaron1011:pretty-print-sort, r=petrochenkov
JohnTitor Jun 24, 2022
0af99c9
Rollup merge of #97633 - mkroening:object-osabi, r=petrochenkov
JohnTitor Jun 24, 2022
6580d7e
Rollup merge of #98039 - tnballo:master, r=thomcc
JohnTitor Jun 24, 2022
33eb3c0
Rollup merge of #98214 - petrochenkov:islike, r=compiler-errors
JohnTitor Jun 24, 2022
964fc41
Rollup merge of #98280 - compiler-errors:better-call-closure-on-type-…
JohnTitor Jun 24, 2022
f3078d0
Rollup merge of #98394 - Enselic:fixup-rustc_main-renames, r=petroche…
JohnTitor Jun 24, 2022
d26b03c
Rollup merge of #98411 - ehuss:update-tendril, r=Mark-Simulacrum
JohnTitor Jun 24, 2022
5e98e55
Rollup merge of #98419 - WaffleLapkin:remove_excess_rib, r=compiler-e…
JohnTitor Jun 24, 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;