Skip to content

Commit 6fd7a6d

Browse files
committed
Auto merge of rust-lang#85156 - GuillaumeGomez:rollup-8u4h34g, r=GuillaumeGomez
Rollup of 4 pull requests Successful merges: - rust-lang#84465 (rustdoc: Implement `is_primitive` in terms of `primitive_type()`) - rust-lang#85118 (Use an SVG image for clipboard instead of unicode character) - rust-lang#85148 (Fix source code line number display and make it clickable again) - rust-lang#85152 (Adjust target search algorithm for rustlib path) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 266f452 + 6ec1de7 commit 6fd7a6d

File tree

21 files changed

+173
-104
lines changed

21 files changed

+173
-104
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1576,7 +1576,7 @@ fn add_rpath_args(
15761576
let target_triple = sess.opts.target_triple.triple();
15771577
let mut get_install_prefix_lib_path = || {
15781578
let install_prefix = option_env!("CFG_PREFIX").expect("CFG_PREFIX");
1579-
let tlib = filesearch::relative_target_lib_path(&sess.sysroot, target_triple);
1579+
let tlib = rustc_target::target_rustlib_path(&sess.sysroot, target_triple).join("lib");
15801580
let mut path = PathBuf::from(install_prefix);
15811581
path.push(&tlib);
15821582

compiler/rustc_interface/src/util.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -423,8 +423,7 @@ pub fn get_codegen_sysroot(
423423
.iter()
424424
.chain(sysroot_candidates.iter())
425425
.map(|sysroot| {
426-
let libdir = filesearch::relative_target_lib_path(&sysroot, &target);
427-
sysroot.join(libdir).with_file_name("codegen-backends")
426+
filesearch::make_target_lib_path(&sysroot, &target).with_file_name("codegen-backends")
428427
})
429428
.find(|f| {
430429
info!("codegen backend candidate: {}", f.display());

compiler/rustc_session/src/filesearch.rs

+15-53
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
pub use self::FileMatch::*;
22

3-
use std::borrow::Cow;
43
use std::env;
54
use std::fs;
65
use std::path::{Path, PathBuf};
@@ -91,26 +90,21 @@ impl<'a> FileSearch<'a> {
9190

9291
// Returns a list of directories where target-specific tool binaries are located.
9392
pub fn get_tools_search_paths(&self, self_contained: bool) -> Vec<PathBuf> {
94-
let mut p = PathBuf::from(self.sysroot);
95-
p.push(find_libdir(self.sysroot).as_ref());
96-
p.push(RUST_LIB_DIR);
97-
p.push(&self.triple);
98-
p.push("bin");
93+
let rustlib_path = rustc_target::target_rustlib_path(self.sysroot, &self.triple);
94+
let p = std::array::IntoIter::new([
95+
Path::new(&self.sysroot),
96+
Path::new(&rustlib_path),
97+
Path::new("bin"),
98+
])
99+
.collect::<PathBuf>();
99100
if self_contained { vec![p.clone(), p.join("self-contained")] } else { vec![p] }
100101
}
101102
}
102103

103-
pub fn relative_target_lib_path(sysroot: &Path, target_triple: &str) -> PathBuf {
104-
let mut p = PathBuf::from(find_libdir(sysroot).as_ref());
105-
assert!(p.is_relative());
106-
p.push(RUST_LIB_DIR);
107-
p.push(target_triple);
108-
p.push("lib");
109-
p
110-
}
111-
112104
pub fn make_target_lib_path(sysroot: &Path, target_triple: &str) -> PathBuf {
113-
sysroot.join(&relative_target_lib_path(sysroot, target_triple))
105+
let rustlib_path = rustc_target::target_rustlib_path(sysroot, target_triple);
106+
std::array::IntoIter::new([sysroot, Path::new(&rustlib_path), Path::new("lib")])
107+
.collect::<PathBuf>()
114108
}
115109

116110
// This function checks if sysroot is found using env::args().next(), and if it
@@ -157,11 +151,13 @@ pub fn get_or_default_sysroot() -> PathBuf {
157151
return None;
158152
}
159153

154+
// Pop off `bin/rustc`, obtaining the suspected sysroot.
160155
p.pop();
161156
p.pop();
162-
let mut libdir = PathBuf::from(&p);
163-
libdir.push(find_libdir(&p).as_ref());
164-
if libdir.exists() { Some(p) } else { None }
157+
// Look for the target rustlib directory in the suspected sysroot.
158+
let mut rustlib_path = rustc_target::target_rustlib_path(&p, "dummy");
159+
rustlib_path.pop(); // pop off the dummy target.
160+
if rustlib_path.exists() { Some(p) } else { None }
165161
}
166162
None => None,
167163
}
@@ -171,37 +167,3 @@ pub fn get_or_default_sysroot() -> PathBuf {
171167
// use env::current_exe() to imply sysroot.
172168
from_env_args_next().unwrap_or_else(from_current_exe)
173169
}
174-
175-
// The name of the directory rustc expects libraries to be located.
176-
fn find_libdir(sysroot: &Path) -> Cow<'static, str> {
177-
// FIXME: This is a quick hack to make the rustc binary able to locate
178-
// Rust libraries in Linux environments where libraries might be installed
179-
// to lib64/lib32. This would be more foolproof by basing the sysroot off
180-
// of the directory where `librustc_driver` is located, rather than
181-
// where the rustc binary is.
182-
// If --libdir is set during configuration to the value other than
183-
// "lib" (i.e., non-default), this value is used (see issue #16552).
184-
185-
#[cfg(target_pointer_width = "64")]
186-
const PRIMARY_LIB_DIR: &str = "lib64";
187-
188-
#[cfg(target_pointer_width = "32")]
189-
const PRIMARY_LIB_DIR: &str = "lib32";
190-
191-
const SECONDARY_LIB_DIR: &str = "lib";
192-
193-
match option_env!("CFG_LIBDIR_RELATIVE") {
194-
None | Some("lib") => {
195-
if sysroot.join(PRIMARY_LIB_DIR).join(RUST_LIB_DIR).exists() {
196-
PRIMARY_LIB_DIR.into()
197-
} else {
198-
SECONDARY_LIB_DIR.into()
199-
}
200-
}
201-
Some(libdir) => libdir.into(),
202-
}
203-
}
204-
205-
// The name of rustc's own place to organize libraries.
206-
// Used to be "rustc", now the default is "rustlib"
207-
const RUST_LIB_DIR: &str = "rustlib";

compiler/rustc_target/src/lib.rs

+51
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#![feature(associated_type_bounds)]
1616
#![feature(exhaustive_patterns)]
1717

18+
use std::path::{Path, PathBuf};
19+
1820
#[macro_use]
1921
extern crate rustc_macros;
2022

@@ -29,3 +31,52 @@ pub mod spec;
2931
/// This is a hack to allow using the `HashStable_Generic` derive macro
3032
/// instead of implementing everything in `rustc_middle`.
3133
pub trait HashStableContext {}
34+
35+
/// The name of rustc's own place to organize libraries.
36+
///
37+
/// Used to be `rustc`, now the default is `rustlib`.
38+
const RUST_LIB_DIR: &str = "rustlib";
39+
40+
/// Returns a `rustlib` path for this particular target, relative to the provided sysroot.
41+
///
42+
/// For example: `target_sysroot_path("/usr", "x86_64-unknown-linux-gnu")` =>
43+
/// `"lib*/rustlib/x86_64-unknown-linux-gnu"`.
44+
pub fn target_rustlib_path(sysroot: &Path, target_triple: &str) -> PathBuf {
45+
let libdir = find_libdir(sysroot);
46+
std::array::IntoIter::new([
47+
Path::new(libdir.as_ref()),
48+
Path::new(RUST_LIB_DIR),
49+
Path::new(target_triple),
50+
])
51+
.collect::<PathBuf>()
52+
}
53+
54+
/// The name of the directory rustc expects libraries to be located.
55+
fn find_libdir(sysroot: &Path) -> std::borrow::Cow<'static, str> {
56+
// FIXME: This is a quick hack to make the rustc binary able to locate
57+
// Rust libraries in Linux environments where libraries might be installed
58+
// to lib64/lib32. This would be more foolproof by basing the sysroot off
59+
// of the directory where `librustc_driver` is located, rather than
60+
// where the rustc binary is.
61+
// If --libdir is set during configuration to the value other than
62+
// "lib" (i.e., non-default), this value is used (see issue #16552).
63+
64+
#[cfg(target_pointer_width = "64")]
65+
const PRIMARY_LIB_DIR: &str = "lib64";
66+
67+
#[cfg(target_pointer_width = "32")]
68+
const PRIMARY_LIB_DIR: &str = "lib32";
69+
70+
const SECONDARY_LIB_DIR: &str = "lib";
71+
72+
match option_env!("CFG_LIBDIR_RELATIVE") {
73+
None | Some("lib") => {
74+
if sysroot.join(PRIMARY_LIB_DIR).join(RUST_LIB_DIR).exists() {
75+
PRIMARY_LIB_DIR.into()
76+
} else {
77+
SECONDARY_LIB_DIR.into()
78+
}
79+
}
80+
Some(libdir) => libdir.into(),
81+
}
82+
}

compiler/rustc_target/src/spec/mod.rs

+15-10
Original file line numberDiff line numberDiff line change
@@ -1897,15 +1897,15 @@ impl Target {
18971897
Ok(base)
18981898
}
18991899

1900-
/// Search RUST_TARGET_PATH for a JSON file specifying the given target
1901-
/// triple. If none is found, look for a file called `target.json` inside
1902-
/// the sysroot under the target-triple's `rustlib` directory.
1903-
/// Note that it could also just be a bare filename already, so also
1904-
/// check for that. If one of the hardcoded targets we know about, just
1905-
/// return it directly.
1900+
/// Search for a JSON file specifying the given target triple.
19061901
///
1907-
/// The error string could come from any of the APIs called, including
1908-
/// filesystem access and JSON decoding.
1902+
/// If none is found in `$RUST_TARGET_PATH`, look for a file called `target.json` inside the
1903+
/// sysroot under the target-triple's `rustlib` directory. Note that it could also just be a
1904+
/// bare filename already, so also check for that. If one of the hardcoded targets we know
1905+
/// about, just return it directly.
1906+
///
1907+
/// The error string could come from any of the APIs called, including filesystem access and
1908+
/// JSON decoding.
19091909
pub fn search(target_triple: &TargetTriple, sysroot: &PathBuf) -> Result<Target, String> {
19101910
use rustc_serialize::json;
19111911
use std::env;
@@ -1942,8 +1942,13 @@ impl Target {
19421942

19431943
// Additionally look in the sysroot under `lib/rustlib/<triple>/target.json`
19441944
// as a fallback.
1945-
let p =
1946-
sysroot.join("lib").join("rustlib").join(&target_triple).join("target.json");
1945+
let rustlib_path = crate::target_rustlib_path(&sysroot, &target_triple);
1946+
let p = std::array::IntoIter::new([
1947+
Path::new(sysroot),
1948+
Path::new(&rustlib_path),
1949+
Path::new("target.json"),
1950+
])
1951+
.collect::<PathBuf>();
19471952
if p.is_file() {
19481953
return load_file(&p);
19491954
}

src/librustdoc/clean/types.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -1606,7 +1606,6 @@ impl Type {
16061606
}
16071607
}
16081608
RawPointer(..) => Some(PrimitiveType::RawPointer),
1609-
BorrowedRef { type_: box Generic(..), .. } => Some(PrimitiveType::Reference),
16101609
BareFunction(..) => Some(PrimitiveType::Fn),
16111610
Never => Some(PrimitiveType::Never),
16121611
_ => None,
@@ -1665,13 +1664,7 @@ impl Type {
16651664
}
16661665

16671666
crate fn is_primitive(&self) -> bool {
1668-
match self {
1669-
Self::Primitive(_) => true,
1670-
Self::BorrowedRef { ref type_, .. } | Self::RawPointer(_, ref type_) => {
1671-
type_.is_primitive()
1672-
}
1673-
_ => false,
1674-
}
1667+
self.primitive_type().is_some()
16751668
}
16761669

16771670
crate fn projection(&self) -> Option<(&Type, DefId, Symbol)> {

src/librustdoc/html/highlight.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ crate fn render_with_highlighting(
2424
playground_button: Option<&str>,
2525
tooltip: Option<(Option<Edition>, &str)>,
2626
edition: Edition,
27+
extra_content: Option<Buffer>,
2728
) {
2829
debug!("highlighting: ================\n{}\n==============", src);
2930
if let Some((edition_info, class)) = tooltip {
@@ -39,13 +40,21 @@ crate fn render_with_highlighting(
3940
);
4041
}
4142

42-
write_header(out, class);
43+
write_header(out, class, extra_content);
4344
write_code(out, &src, edition);
4445
write_footer(out, playground_button);
4546
}
4647

47-
fn write_header(out: &mut Buffer, class: Option<&str>) {
48-
writeln!(out, "<div class=\"example-wrap\"><pre class=\"rust {}\">", class.unwrap_or_default());
48+
fn write_header(out: &mut Buffer, class: Option<&str>, extra_content: Option<Buffer>) {
49+
write!(out, "<div class=\"example-wrap\">");
50+
if let Some(extra) = extra_content {
51+
out.push_buffer(extra);
52+
}
53+
if let Some(class) = class {
54+
writeln!(out, "<pre class=\"rust {}\">", class);
55+
} else {
56+
writeln!(out, "<pre class=\"rust\">");
57+
}
4958
}
5059

5160
fn write_code(out: &mut Buffer, src: &str, edition: Edition) {

src/librustdoc/html/layout.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,20 @@ crate struct Page<'a> {
3434
crate static_extra_scripts: &'a [&'a str],
3535
}
3636

37+
impl<'a> Page<'a> {
38+
crate fn get_static_root_path(&self) -> &str {
39+
self.static_root_path.unwrap_or(self.root_path)
40+
}
41+
}
42+
3743
crate fn render<T: Print, S: Print>(
3844
layout: &Layout,
3945
page: &Page<'_>,
4046
sidebar: S,
4147
t: T,
4248
style_files: &[StylePath],
4349
) -> String {
44-
let static_root_path = page.static_root_path.unwrap_or(page.root_path);
50+
let static_root_path = page.get_static_root_path();
4551
format!(
4652
"<!DOCTYPE html>\
4753
<html lang=\"en\">\

src/librustdoc/html/markdown.rs

+1
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
315315
playground_button.as_deref(),
316316
tooltip,
317317
edition,
318+
None,
318319
);
319320
Some(Event::Html(s.into_inner().into()))
320321
}

src/librustdoc/html/render/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ impl<'tcx> Context<'tcx> {
215215
&self.shared.layout,
216216
&page,
217217
|buf: &mut _| print_sidebar(self, it, buf),
218-
|buf: &mut _| print_item(self, it, buf),
218+
|buf: &mut _| print_item(self, it, buf, &page),
219219
&self.shared.style_files,
220220
)
221221
} else {

src/librustdoc/html/render/print_item.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ use crate::formats::{AssocItemRender, Impl, RenderMode};
2222
use crate::html::escape::Escape;
2323
use crate::html::format::{print_abi_with_space, print_where_clause, Buffer, PrintWithSpace};
2424
use crate::html::highlight;
25+
use crate::html::layout::Page;
2526
use crate::html::markdown::MarkdownSummaryLine;
2627

27-
pub(super) fn print_item(cx: &Context<'_>, item: &clean::Item, buf: &mut Buffer) {
28+
pub(super) fn print_item(cx: &Context<'_>, item: &clean::Item, buf: &mut Buffer, page: &Page<'_>) {
2829
debug_assert!(!item.is_stripped());
2930
// Write the breadcrumb trail header for the top
3031
buf.write_str("<h1 class=\"fqn\"><span class=\"in-band\">");
@@ -74,7 +75,16 @@ pub(super) fn print_item(cx: &Context<'_>, item: &clean::Item, buf: &mut Buffer)
7475
}
7576
}
7677
write!(buf, "<a class=\"{}\" href=\"\">{}</a>", item.type_(), item.name.as_ref().unwrap());
77-
write!(buf, "<button id=\"copy-path\" onclick=\"copy_path(this)\">⎘</button>");
78+
write!(
79+
buf,
80+
"<button id=\"copy-path\" onclick=\"copy_path(this)\">\
81+
<img src=\"{static_root_path}clipboard{suffix}.svg\" \
82+
width=\"19\" height=\"18\" \
83+
alt=\"Copy item import\">\
84+
</button>",
85+
static_root_path = page.get_static_root_path(),
86+
suffix = page.resource_suffix,
87+
);
7888

7989
buf.write_str("</span>"); // in-band
8090
buf.write_str("<span class=\"out-of-band\">");
@@ -1016,6 +1026,7 @@ fn item_macro(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Mac
10161026
None,
10171027
None,
10181028
it.span(cx.tcx()).inner().edition(),
1029+
None,
10191030
);
10201031
});
10211032
document(w, cx, it, None)

src/librustdoc/html/render/write_shared.rs

+1
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ pub(super) fn write_shared(
207207
}
208208
write_toolchain("brush.svg", static_files::BRUSH_SVG)?;
209209
write_toolchain("wheel.svg", static_files::WHEEL_SVG)?;
210+
write_toolchain("clipboard.svg", static_files::CLIPBOARD_SVG)?;
210211
write_toolchain("down-arrow.svg", static_files::DOWN_ARROW_SVG)?;
211212

212213
let mut themes: Vec<&String> = themes.iter().collect();

src/librustdoc/html/sources.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -169,16 +169,17 @@ where
169169
/// adding line numbers to the left-hand side.
170170
fn print_src(buf: &mut Buffer, s: &str, edition: Edition) {
171171
let lines = s.lines().count();
172+
let mut line_numbers = Buffer::empty_from(buf);
172173
let mut cols = 0;
173174
let mut tmp = lines;
174175
while tmp > 0 {
175176
cols += 1;
176177
tmp /= 10;
177178
}
178-
buf.write_str("<pre class=\"line-numbers\">");
179+
line_numbers.write_str("<pre class=\"line-numbers\">");
179180
for i in 1..=lines {
180-
writeln!(buf, "<span id=\"{0}\">{0:1$}</span>", i, cols);
181+
writeln!(line_numbers, "<span id=\"{0}\">{0:1$}</span>", i, cols);
181182
}
182-
buf.write_str("</pre>");
183-
highlight::render_with_highlighting(s, buf, None, None, None, edition);
183+
line_numbers.write_str("</pre>");
184+
highlight::render_with_highlighting(s, buf, None, None, None, edition, Some(line_numbers));
184185
}
+1
Loading

0 commit comments

Comments
 (0)