Skip to content

Commit 78f2104

Browse files
committed
Auto merge of #139912 - matthiaskrgr:rollup-va0rqvk, r=matthiaskrgr
Rollup of 9 pull requests Successful merges: - #139647 (Add unstable parsing of `--extern foo::bar=libbar.rlib` command line options) - #139823 (Fix some bootstrap papercuts) - #139867 (Fix some tidy paper cuts) - #139871 (Fix wrong "move keyword" suggestion for async gen block) - #139876 (Make CodeStats' type_sizes public) - #139880 (Don't compute name of associated item if it's an RPITIT) - #139884 (Update books) - #139886 (`borrowck_graphviz_*` attribute tweaks) - #139893 (Add test for issue 125668) r? `@ghost` `@rustbot` modify labels: rollup
2 parents c6aad02 + 0039c7d commit 78f2104

File tree

27 files changed

+395
-74
lines changed

27 files changed

+395
-74
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -3376,10 +3376,15 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
33763376

33773377
let (sugg_span, suggestion) = match tcx.sess.source_map().span_to_snippet(args_span) {
33783378
Ok(string) => {
3379-
let coro_prefix = if string.starts_with("async") {
3380-
// `async` is 5 chars long. Not using `.len()` to avoid the cast from `usize`
3381-
// to `u32`.
3382-
Some(5)
3379+
let coro_prefix = if let Some(sub) = string.strip_prefix("async") {
3380+
let trimmed_sub = sub.trim_end();
3381+
if trimmed_sub.ends_with("gen") {
3382+
// `async` is 5 chars long.
3383+
Some((trimmed_sub.len() + 5) as _)
3384+
} else {
3385+
// `async` is 5 chars long.
3386+
Some(5)
3387+
}
33833388
} else if string.starts_with("gen") {
33843389
// `gen` is 3 chars long
33853390
Some(3)

compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
204204
.iter()
205205
.flat_map(|trait_def_id| tcx.associated_items(*trait_def_id).in_definition_order())
206206
.filter_map(|item| {
207-
(!item.is_impl_trait_in_trait() && item.as_tag() == assoc_tag)
208-
.then_some(item.name())
207+
(!item.is_impl_trait_in_trait() && item.as_tag() == assoc_tag).then(|| item.name())
209208
})
210209
.collect();
211210

compiler/rustc_mir_dataflow/src/framework/graphviz.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ impl RustcMirAttrs {
122122
})
123123
} else if attr.has_name(sym::borrowck_graphviz_format) {
124124
Self::set_field(&mut ret.formatter, tcx, &attr, |s| match s {
125-
sym::gen_kill | sym::two_phase => Ok(s),
125+
sym::two_phase => Ok(s),
126126
_ => {
127127
tcx.dcx().emit_err(UnknownFormatter { span: attr.span() });
128128
Err(())

compiler/rustc_session/src/code_stats.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub struct TypeSizeInfo {
7272

7373
#[derive(Default)]
7474
pub struct CodeStats {
75-
type_sizes: Lock<FxHashSet<TypeSizeInfo>>,
75+
pub type_sizes: Lock<FxHashSet<TypeSizeInfo>>,
7676
}
7777

7878
impl CodeStats {

compiler/rustc_session/src/config.rs

+5-34
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::str::{self, FromStr};
1414
use std::sync::LazyLock;
1515
use std::{cmp, fmt, fs, iter};
1616

17+
use externs::{ExternOpt, split_extern_opt};
1718
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
1819
use rustc_data_structures::stable_hasher::{StableOrd, ToStableHashKey};
1920
use rustc_errors::emitter::HumanReadableErrorType;
@@ -39,6 +40,7 @@ use crate::utils::CanonicalizedPath;
3940
use crate::{EarlyDiagCtxt, HashStableContext, Session, filesearch, lint};
4041

4142
mod cfg;
43+
mod externs;
4244
mod native_libs;
4345
pub mod sigpipe;
4446

@@ -2205,44 +2207,13 @@ pub fn parse_externs(
22052207
matches: &getopts::Matches,
22062208
unstable_opts: &UnstableOptions,
22072209
) -> Externs {
2208-
fn is_ascii_ident(string: &str) -> bool {
2209-
let mut chars = string.chars();
2210-
if let Some(start) = chars.next()
2211-
&& (start.is_ascii_alphabetic() || start == '_')
2212-
{
2213-
chars.all(|char| char.is_ascii_alphanumeric() || char == '_')
2214-
} else {
2215-
false
2216-
}
2217-
}
2218-
22192210
let is_unstable_enabled = unstable_opts.unstable_options;
22202211
let mut externs: BTreeMap<String, ExternEntry> = BTreeMap::new();
22212212
for arg in matches.opt_strs("extern") {
2222-
let (name, path) = match arg.split_once('=') {
2223-
None => (arg, None),
2224-
Some((name, path)) => (name.to_string(), Some(Path::new(path))),
2225-
};
2226-
let (options, name) = match name.split_once(':') {
2227-
None => (None, name),
2228-
Some((opts, name)) => (Some(opts), name.to_string()),
2229-
};
2230-
2231-
if !is_ascii_ident(&name) {
2232-
let mut error = early_dcx.early_struct_fatal(format!(
2233-
"crate name `{name}` passed to `--extern` is not a valid ASCII identifier"
2234-
));
2235-
let adjusted_name = name.replace('-', "_");
2236-
if is_ascii_ident(&adjusted_name) {
2237-
#[allow(rustc::diagnostic_outside_of_impl)] // FIXME
2238-
error.help(format!(
2239-
"consider replacing the dashes with underscores: `{adjusted_name}`"
2240-
));
2241-
}
2242-
error.emit();
2243-
}
2213+
let ExternOpt { crate_name: name, path, options } =
2214+
split_extern_opt(early_dcx, unstable_opts, &arg).unwrap_or_else(|e| e.emit());
22442215

2245-
let path = path.map(|p| CanonicalizedPath::new(p));
2216+
let path = path.map(|p| CanonicalizedPath::new(p.as_path()));
22462217

22472218
let entry = externs.entry(name.to_owned());
22482219

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//! This module contains code to help parse and manipulate `--extern` arguments.
2+
3+
use std::path::PathBuf;
4+
5+
use rustc_errors::{Diag, FatalAbort};
6+
7+
use super::UnstableOptions;
8+
use crate::EarlyDiagCtxt;
9+
10+
#[cfg(test)]
11+
mod tests;
12+
13+
/// Represents the pieces of an `--extern` argument.
14+
pub(crate) struct ExternOpt {
15+
pub(crate) crate_name: String,
16+
pub(crate) path: Option<PathBuf>,
17+
pub(crate) options: Option<String>,
18+
}
19+
20+
/// Breaks out the major components of an `--extern` argument.
21+
///
22+
/// The options field will be a string containing comma-separated options that will need further
23+
/// parsing and processing.
24+
pub(crate) fn split_extern_opt<'a>(
25+
early_dcx: &'a EarlyDiagCtxt,
26+
unstable_opts: &UnstableOptions,
27+
extern_opt: &str,
28+
) -> Result<ExternOpt, Diag<'a, FatalAbort>> {
29+
let (name, path) = match extern_opt.split_once('=') {
30+
None => (extern_opt.to_string(), None),
31+
Some((name, path)) => (name.to_string(), Some(PathBuf::from(path))),
32+
};
33+
let (options, crate_name) = match name.split_once(':') {
34+
None => (None, name),
35+
Some((opts, crate_name)) => {
36+
if unstable_opts.namespaced_crates && crate_name.starts_with(':') {
37+
// If the name starts with `:`, we know this was actually something like `foo::bar` and
38+
// not a set of options. We can just use the original name as the crate name.
39+
(None, name)
40+
} else {
41+
(Some(opts.to_string()), crate_name.to_string())
42+
}
43+
}
44+
};
45+
46+
if !valid_crate_name(&crate_name, unstable_opts) {
47+
let mut error = early_dcx.early_struct_fatal(format!(
48+
"crate name `{crate_name}` passed to `--extern` is not a valid ASCII identifier"
49+
));
50+
let adjusted_name = crate_name.replace('-', "_");
51+
if is_ascii_ident(&adjusted_name) {
52+
#[allow(rustc::diagnostic_outside_of_impl)] // FIXME
53+
error
54+
.help(format!("consider replacing the dashes with underscores: `{adjusted_name}`"));
55+
}
56+
return Err(error);
57+
}
58+
59+
Ok(ExternOpt { crate_name, path, options })
60+
}
61+
62+
fn valid_crate_name(name: &str, unstable_opts: &UnstableOptions) -> bool {
63+
match name.split_once("::") {
64+
Some((a, b)) if unstable_opts.namespaced_crates => is_ascii_ident(a) && is_ascii_ident(b),
65+
Some(_) => false,
66+
None => is_ascii_ident(name),
67+
}
68+
}
69+
70+
fn is_ascii_ident(string: &str) -> bool {
71+
let mut chars = string.chars();
72+
if let Some(start) = chars.next()
73+
&& (start.is_ascii_alphabetic() || start == '_')
74+
{
75+
chars.all(|char| char.is_ascii_alphanumeric() || char == '_')
76+
} else {
77+
false
78+
}
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
use std::path::PathBuf;
2+
3+
use super::split_extern_opt;
4+
use crate::EarlyDiagCtxt;
5+
use crate::config::UnstableOptions;
6+
7+
/// Verifies split_extern_opt handles the supported cases.
8+
#[test]
9+
fn test_split_extern_opt() {
10+
let early_dcx = EarlyDiagCtxt::new(<_>::default());
11+
let unstable_opts = &UnstableOptions::default();
12+
13+
let extern_opt =
14+
split_extern_opt(&early_dcx, unstable_opts, "priv,noprelude:foo=libbar.rlib").unwrap();
15+
assert_eq!(extern_opt.crate_name, "foo");
16+
assert_eq!(extern_opt.path, Some(PathBuf::from("libbar.rlib")));
17+
assert_eq!(extern_opt.options, Some("priv,noprelude".to_string()));
18+
19+
let extern_opt = split_extern_opt(&early_dcx, unstable_opts, "priv,noprelude:foo").unwrap();
20+
assert_eq!(extern_opt.crate_name, "foo");
21+
assert_eq!(extern_opt.path, None);
22+
assert_eq!(extern_opt.options, Some("priv,noprelude".to_string()));
23+
24+
let extern_opt = split_extern_opt(&early_dcx, unstable_opts, "foo=libbar.rlib").unwrap();
25+
assert_eq!(extern_opt.crate_name, "foo");
26+
assert_eq!(extern_opt.path, Some(PathBuf::from("libbar.rlib")));
27+
assert_eq!(extern_opt.options, None);
28+
29+
let extern_opt = split_extern_opt(&early_dcx, unstable_opts, "foo").unwrap();
30+
assert_eq!(extern_opt.crate_name, "foo");
31+
assert_eq!(extern_opt.path, None);
32+
assert_eq!(extern_opt.options, None);
33+
}
34+
35+
/// Tests some invalid cases for split_extern_opt.
36+
#[test]
37+
fn test_split_extern_opt_invalid() {
38+
let early_dcx = EarlyDiagCtxt::new(<_>::default());
39+
let unstable_opts = &UnstableOptions::default();
40+
41+
// too many `:`s
42+
let result = split_extern_opt(&early_dcx, unstable_opts, "priv:noprelude:foo=libbar.rlib");
43+
assert!(result.is_err());
44+
let _ = result.map_err(|e| e.cancel());
45+
46+
// can't nest externs without the unstable flag
47+
let result = split_extern_opt(&early_dcx, unstable_opts, "noprelude:foo::bar=libbar.rlib");
48+
assert!(result.is_err());
49+
let _ = result.map_err(|e| e.cancel());
50+
}
51+
52+
/// Tests some cases for split_extern_opt with nested crates like `foo::bar`.
53+
#[test]
54+
fn test_split_extern_opt_nested() {
55+
let early_dcx = EarlyDiagCtxt::new(<_>::default());
56+
let unstable_opts = &UnstableOptions { namespaced_crates: true, ..Default::default() };
57+
58+
let extern_opt =
59+
split_extern_opt(&early_dcx, unstable_opts, "priv,noprelude:foo::bar=libbar.rlib").unwrap();
60+
assert_eq!(extern_opt.crate_name, "foo::bar");
61+
assert_eq!(extern_opt.path, Some(PathBuf::from("libbar.rlib")));
62+
assert_eq!(extern_opt.options, Some("priv,noprelude".to_string()));
63+
64+
let extern_opt =
65+
split_extern_opt(&early_dcx, unstable_opts, "priv,noprelude:foo::bar").unwrap();
66+
assert_eq!(extern_opt.crate_name, "foo::bar");
67+
assert_eq!(extern_opt.path, None);
68+
assert_eq!(extern_opt.options, Some("priv,noprelude".to_string()));
69+
70+
let extern_opt = split_extern_opt(&early_dcx, unstable_opts, "foo::bar=libbar.rlib").unwrap();
71+
assert_eq!(extern_opt.crate_name, "foo::bar");
72+
assert_eq!(extern_opt.path, Some(PathBuf::from("libbar.rlib")));
73+
assert_eq!(extern_opt.options, None);
74+
75+
let extern_opt = split_extern_opt(&early_dcx, unstable_opts, "foo::bar").unwrap();
76+
assert_eq!(extern_opt.crate_name, "foo::bar");
77+
assert_eq!(extern_opt.path, None);
78+
assert_eq!(extern_opt.options, None);
79+
}
80+
81+
/// Tests some invalid cases for split_extern_opt with nested crates like `foo::bar`.
82+
#[test]
83+
fn test_split_extern_opt_nested_invalid() {
84+
let early_dcx = EarlyDiagCtxt::new(<_>::default());
85+
let unstable_opts = &UnstableOptions { namespaced_crates: true, ..Default::default() };
86+
87+
// crates can only be nested one deep.
88+
let result =
89+
split_extern_opt(&early_dcx, unstable_opts, "priv,noprelude:foo::bar::baz=libbar.rlib");
90+
assert!(result.is_err());
91+
let _ = result.map_err(|e| e.cancel());
92+
}

compiler/rustc_session/src/options.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2334,6 +2334,8 @@ options! {
23342334
"the size at which the `large_assignments` lint starts to be emitted"),
23352335
mutable_noalias: bool = (true, parse_bool, [TRACKED],
23362336
"emit noalias metadata for mutable references (default: yes)"),
2337+
namespaced_crates: bool = (false, parse_bool, [TRACKED],
2338+
"allow crates to be namespaced by other crates (default: no)"),
23372339
next_solver: NextSolverConfig = (NextSolverConfig::default(), parse_next_solver_config, [TRACKED],
23382340
"enable and configure the next generation trait solver used by rustc"),
23392341
nll_facts: bool = (false, parse_bool, [UNTRACKED],

compiler/rustc_span/src/symbol.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1068,7 +1068,6 @@ symbols! {
10681068
ge,
10691069
gen_blocks,
10701070
gen_future,
1071-
gen_kill,
10721071
generator_clone,
10731072
generators,
10741073
generic_arg_infer,

src/bootstrap/src/core/build_steps/format.rs

+24-13
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,19 @@ fn update_rustfmt_version(build: &Builder<'_>) {
8181
let Some((version, stamp_file)) = get_rustfmt_version(build) else {
8282
return;
8383
};
84-
t!(std::fs::write(stamp_file.path(), version))
84+
85+
t!(stamp_file.add_stamp(version).write());
8586
}
8687

87-
/// Returns the Rust files modified between the `merge-base` of HEAD and
88-
/// rust-lang/master and what is now on the disk. Does not include removed files.
88+
/// Returns the Rust files modified between the last merge commit and what is now on the disk.
89+
/// Does not include removed files.
8990
///
9091
/// Returns `None` if all files should be formatted.
9192
fn get_modified_rs_files(build: &Builder<'_>) -> Result<Option<Vec<String>>, String> {
93+
// In CI `get_git_modified_files` returns something different to normal environment.
94+
// This shouldn't be called in CI anyway.
95+
assert!(!build.config.is_running_on_ci);
96+
9297
if !verify_rustfmt_version(build) {
9398
return Ok(None);
9499
}
@@ -103,7 +108,7 @@ struct RustfmtConfig {
103108

104109
// Prints output describing a collection of paths, with lines such as "formatted modified file
105110
// foo/bar/baz" or "skipped 20 untracked files".
106-
fn print_paths(build: &Builder<'_>, verb: &str, adjective: Option<&str>, paths: &[String]) {
111+
fn print_paths(verb: &str, adjective: Option<&str>, paths: &[String]) {
107112
let len = paths.len();
108113
let adjective =
109114
if let Some(adjective) = adjective { format!("{adjective} ") } else { String::new() };
@@ -114,9 +119,6 @@ fn print_paths(build: &Builder<'_>, verb: &str, adjective: Option<&str>, paths:
114119
} else {
115120
println!("fmt: {verb} {len} {adjective}files");
116121
}
117-
if len > 1000 && !build.config.is_running_on_ci {
118-
println!("hint: if this number seems too high, try running `git fetch origin master`");
119-
}
120122
}
121123

122124
pub fn format(build: &Builder<'_>, check: bool, all: bool, paths: &[PathBuf]) {
@@ -189,7 +191,7 @@ pub fn format(build: &Builder<'_>, check: bool, all: bool, paths: &[PathBuf]) {
189191
)
190192
.map(|x| x.to_string())
191193
.collect();
192-
print_paths(build, "skipped", Some("untracked"), &untracked_paths);
194+
print_paths("skipped", Some("untracked"), &untracked_paths);
193195

194196
for untracked_path in untracked_paths {
195197
// The leading `/` makes it an exact match against the
@@ -212,7 +214,13 @@ pub fn format(build: &Builder<'_>, check: bool, all: bool, paths: &[PathBuf]) {
212214
override_builder.add(&format!("/{file}")).expect(&file);
213215
}
214216
}
215-
Ok(None) => {}
217+
Ok(None) => {
218+
// NOTE: `Ok(None)` signifies that we need to format all files.
219+
// The tricky part here is that if `override_builder` isn't given any white
220+
// list files (i.e. files to be formatted, added without leading `!`), it
221+
// will instead look for *all* files. So, by doing nothing here, we are
222+
// actually making it so we format all files.
223+
}
216224
Err(err) => {
217225
eprintln!("fmt warning: Something went wrong running git commands:");
218226
eprintln!("fmt warning: {err}");
@@ -318,7 +326,7 @@ pub fn format(build: &Builder<'_>, check: bool, all: bool, paths: &[PathBuf]) {
318326
});
319327
let mut paths = formatted_paths.into_inner().unwrap();
320328
paths.sort();
321-
print_paths(build, if check { "checked" } else { "formatted" }, adjective, &paths);
329+
print_paths(if check { "checked" } else { "formatted" }, adjective, &paths);
322330

323331
drop(tx);
324332

@@ -328,7 +336,10 @@ pub fn format(build: &Builder<'_>, check: bool, all: bool, paths: &[PathBuf]) {
328336
crate::exit!(1);
329337
}
330338

331-
if !check {
332-
update_rustfmt_version(build);
333-
}
339+
// Update `build/.rustfmt-stamp`, allowing this code to ignore files which have not been changed
340+
// since last merge.
341+
//
342+
// NOTE: Because of the exit above, this is only reachable if formatting / format checking
343+
// succeeded. So we are not commiting the version if formatting was not good.
344+
update_rustfmt_version(build);
334345
}

0 commit comments

Comments
 (0)