Skip to content

Commit 70350b1

Browse files
authored
Merge branch 'rust-lang:master' into cached-submodules
2 parents 7755f01 + 2d4fa13 commit 70350b1

File tree

8 files changed

+147
-23
lines changed

8 files changed

+147
-23
lines changed

src/cargo/core/compiler/mod.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,11 +1095,14 @@ fn add_error_format_and_color(build_runner: &BuildRunner<'_, '_>, cmd: &mut Proc
10951095
cmd.arg("--error-format=json");
10961096
let mut json = String::from("--json=diagnostic-rendered-ansi,artifacts,future-incompat");
10971097

1098-
match build_runner.bcx.build_config.message_format {
1099-
MessageFormat::Short | MessageFormat::Json { short: true, .. } => {
1100-
json.push_str(",diagnostic-short");
1101-
}
1102-
_ => {}
1098+
if let MessageFormat::Short | MessageFormat::Json { short: true, .. } =
1099+
build_runner.bcx.build_config.message_format
1100+
{
1101+
json.push_str(",diagnostic-short");
1102+
} else if build_runner.bcx.gctx.shell().err_unicode()
1103+
&& build_runner.bcx.gctx.cli_unstable().rustc_unicode
1104+
{
1105+
json.push_str(",diagnostic-unicode");
11031106
}
11041107

11051108
if enable_timings {

src/cargo/core/features.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,7 @@ unstable_cli_options!(
881881
public_dependency: bool = ("Respect a dependency's `public` field in Cargo.toml to control public/private dependencies"),
882882
publish_timeout: bool = ("Enable the `publish.timeout` key in .cargo/config.toml file"),
883883
root_dir: Option<PathBuf> = ("Set the root directory relative to which paths are printed (defaults to workspace root)"),
884+
rustc_unicode: bool = ("Enable `rustc`'s unicode error format in Cargo's error messages"),
884885
rustdoc_depinfo: bool = ("Use dep-info files in rustdoc rebuild detection"),
885886
rustdoc_map: bool = ("Allow passing external documentation mappings to rustdoc"),
886887
rustdoc_scrape_examples: bool = ("Allows Rustdoc to scrape code examples from reverse-dependencies"),
@@ -1411,6 +1412,7 @@ impl CliUnstable {
14111412
"trim-paths" => self.trim_paths = parse_empty(k, v)?,
14121413
"publish-timeout" => self.publish_timeout = parse_empty(k, v)?,
14131414
"root-dir" => self.root_dir = v.map(|v| v.into()),
1415+
"rustc-unicode" => self.rustc_unicode = parse_empty(k, v)?,
14141416
"rustdoc-depinfo" => self.rustdoc_depinfo = parse_empty(k, v)?,
14151417
"rustdoc-map" => self.rustdoc_map = parse_empty(k, v)?,
14161418
"rustdoc-scrape-examples" => self.rustdoc_scrape_examples = parse_empty(k, v)?,

src/cargo/core/shell.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::fmt;
22
use std::io::IsTerminal;
33
use std::io::prelude::*;
44

5+
use annotate_snippets::renderer::DecorStyle;
56
use annotate_snippets::{Renderer, Report};
67
use anstream::AutoStream;
78
use anstyle::Style;
@@ -57,6 +58,7 @@ impl Shell {
5758
stdout_unicode: supports_unicode(&std::io::stdout()),
5859
stderr_unicode: supports_unicode(&std::io::stderr()),
5960
stderr_term_integration: supports_term_integration(&std::io::stderr()),
61+
unstable_flags_rustc_unicode: false,
6062
},
6163
verbosity: Verbosity::Verbose,
6264
needs_clear: false,
@@ -380,6 +382,27 @@ impl Shell {
380382
Some(url)
381383
}
382384

385+
fn unstable_flags_rustc_unicode(&self) -> bool {
386+
match &self.output {
387+
ShellOut::Write(_) => false,
388+
ShellOut::Stream {
389+
unstable_flags_rustc_unicode,
390+
..
391+
} => *unstable_flags_rustc_unicode,
392+
}
393+
}
394+
395+
pub(crate) fn set_unstable_flags_rustc_unicode(&mut self, yes: bool) -> CargoResult<()> {
396+
if let ShellOut::Stream {
397+
unstable_flags_rustc_unicode,
398+
..
399+
} = &mut self.output
400+
{
401+
*unstable_flags_rustc_unicode = yes;
402+
}
403+
Ok(())
404+
}
405+
383406
/// Prints a message to stderr and translates ANSI escape code into console colors.
384407
pub fn print_ansi_stderr(&mut self, message: &[u8]) -> CargoResult<()> {
385408
if self.needs_clear {
@@ -419,7 +442,15 @@ impl Shell {
419442
.err_width()
420443
.diagnostic_terminal_width()
421444
.unwrap_or(annotate_snippets::renderer::DEFAULT_TERM_WIDTH);
422-
let rendered = Renderer::styled().term_width(term_width).render(report);
445+
let decor_style = if self.err_unicode() && self.unstable_flags_rustc_unicode() {
446+
DecorStyle::Unicode
447+
} else {
448+
DecorStyle::Ascii
449+
};
450+
let rendered = Renderer::styled()
451+
.term_width(term_width)
452+
.decor_style(decor_style)
453+
.render(report);
423454
self.err().write_all(rendered.as_bytes())?;
424455
self.err().write_all(b"\n")?;
425456
Ok(())
@@ -446,6 +477,7 @@ enum ShellOut {
446477
stdout_unicode: bool,
447478
stderr_unicode: bool,
448479
stderr_term_integration: bool,
480+
unstable_flags_rustc_unicode: bool,
449481
},
450482
}
451483

src/cargo/util/context/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,9 @@ impl GlobalContext {
11741174
let cli_target_dir = target_dir.as_ref().map(|dir| Filesystem::new(dir.clone()));
11751175
self.target_dir = cli_target_dir;
11761176

1177+
self.shell()
1178+
.set_unstable_flags_rustc_unicode(self.unstable_flags.rustc_unicode)?;
1179+
11771180
Ok(())
11781181
}
11791182

src/doc/src/reference/unstable.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ Each new feature described below should explain how to use it.
116116
* [unit-graph](#unit-graph) --- Emits JSON for Cargo's internal graph structure.
117117
* [`cargo rustc --print`](#rustc---print) --- Calls rustc with `--print` to display information from rustc.
118118
* [Build analysis](#build-analysis) --- Record and persist detailed build metrics across runs, with new commands to query past builds.
119+
* [`rustc-unicode`](#rustc-unicode) --- Enables `rustc`'s unicode error format in Cargo's error messages
119120
* Configuration
120121
* [config-include](#config-include) --- Adds the ability for config files to include other files.
121122
* [`cargo config`](#cargo-config) --- Adds a new subcommand for viewing config files.
@@ -2028,6 +2029,12 @@ cargo +nightly build --compile-time-deps -Z unstable-options
20282029
cargo +nightly check --compile-time-deps --all-targets -Z unstable-options
20292030
```
20302031

2032+
# `rustc-unicode`
2033+
* Tracking Issue: [rust#148607](https://github.com/rust-lang/rust/issues/148607)
2034+
2035+
Enable `rustc`'s unicode error format in Cargo's error messages
2036+
2037+
20312038
# Stabilized and removed features
20322039

20332040
## Compile progress

tests/testsuite/cargo/z_help/stdout.term.svg

Lines changed: 19 additions & 17 deletions
Loading

tests/testsuite/lints/mod.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,3 +323,42 @@ workspace = true
323323
"#]])
324324
.run();
325325
}
326+
327+
#[cargo_test(nightly, reason = "-Zrustc-unicode is unstable")]
328+
fn unicode_report() {
329+
let p = project()
330+
.file(
331+
"Cargo.toml",
332+
r#"
333+
cargo-features = ["test-dummy-unstable"]
334+
335+
[package]
336+
name = "foo"
337+
version = "0.0.1"
338+
edition = "2015"
339+
authors = []
340+
im-a-teapot = true
341+
342+
[lints.cargo]
343+
im_a_teapot = { level = "warn", priority = 10 }
344+
"#,
345+
)
346+
.file("src/lib.rs", "")
347+
.build();
348+
349+
p.cargo("check -Zcargo-lints -Zrustc-unicode")
350+
.masquerade_as_nightly_cargo(&["cargo-lints", "rustc-unicode", "test-dummy-unstable"])
351+
.with_stderr_data(str![[r#"
352+
[WARNING] `im_a_teapot` is specified
353+
╭▸ Cargo.toml:9:1
354+
355+
9 │ im-a-teapot = true
356+
│ ━━━━━━━━━━━━━━━━━━
357+
358+
╰ [NOTE] `cargo::im_a_teapot` is set to `warn` in `[lints]`
359+
[CHECKING] foo v0.0.1 ([ROOT]/foo)
360+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
361+
362+
"#]])
363+
.run();
364+
}

tests/testsuite/message_format.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,42 @@ failures:
162162
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in [ELAPSED]s
163163
164164
165+
"#]])
166+
.run();
167+
}
168+
169+
#[cargo_test(nightly, reason = "-Zrustc-unicode is unstable")]
170+
fn cargo_passes_unicode_output() {
171+
let foo = project()
172+
.file(
173+
"Cargo.toml",
174+
r#"
175+
[package]
176+
name = "foo"
177+
version = "0.0.1"
178+
edition = "2015"
179+
"#,
180+
)
181+
.file(
182+
"src/lib.rs",
183+
"\
184+
mod tests {
185+
#[test]
186+
fn t1() {
187+
use std::io;
188+
}
189+
}
190+
",
191+
)
192+
.build();
193+
194+
foo.cargo("check -v -Zrustc-unicode")
195+
.masquerade_as_nightly_cargo(&["rustc-unicode"])
196+
.with_stderr_data(str![[r#"
197+
[CHECKING] foo v0.0.1 ([ROOT]/foo)
198+
[RUNNING] `rustc [..] --json=diagnostic-rendered-ansi,artifacts,future-incompat,diagnostic-unicode [..]`
199+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
200+
165201
"#]])
166202
.run();
167203
}

0 commit comments

Comments
 (0)