Skip to content
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

refactor: Use Config::emit_diagnostic to emit warnings #12531

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion crates/xtask-bump-check/src/xtask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ fn get_base_commit<'a>(
let upstream_ref = upstream_branches[0].get();
if upstream_branches.len() > 1 {
let name = upstream_ref.name().expect("name is valid UTF-8");
let _ = config.shell().warn(format!(
let _ = config.emit_diagnostic(format!(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should xtasks still use shell()?

"multiple `{UPSTREAM_BRANCH}` found, picking {name}"
));
}
Expand Down
8 changes: 4 additions & 4 deletions src/bin/cargo/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ fn expand_aliases(
match (exec, aliased_cmd) {
(Some(_), Ok(Some(_))) => {
// User alias conflicts with a built-in subcommand
config.shell().warn(format!(
config.emit_diagnostic(format!(
"user-defined alias `{}` is ignored, because it is shadowed by a built-in command",
cmd,
))?;
Expand Down Expand Up @@ -296,7 +296,7 @@ To pass the arguments to the subcommand, remove `--`",
// a hard error.
if super::builtin_aliases_execs(cmd).is_none() {
if let Some(path) = super::find_external_subcommand(config, cmd) {
config.shell().warn(format!(
config.emit_diagnostic(format!(
"\
user-defined alias `{}` is shadowing an external subcommand found at: `{}`
This was previously accepted but is being phased out; it will become a hard error in a future release.
Expand All @@ -310,7 +310,7 @@ For more information, see issue #10049 <https://github.com/rust-lang/cargo/issue
if config.cli_unstable().script {
return Ok((args, GlobalArgs::default()));
} else {
config.shell().warn(format_args!(
config.emit_diagnostic(format_args!(
"\
user-defined alias `{cmd}` has the appearance of a manfiest-command
This was previously accepted but will be phased out when `-Zscript` is stabilized.
Expand Down Expand Up @@ -445,7 +445,7 @@ impl Exec {
Self::Manifest(cmd) => {
let ext_path = super::find_external_subcommand(config, &cmd);
if !config.cli_unstable().script && ext_path.is_some() {
config.shell().warn(format_args!(
config.emit_diagnostic(format_args!(
"\
external subcommand `{cmd}` has the appearance of a manfiest-command
This was previously accepted but will be phased out when `-Zscript` is stabilized.
Expand Down
2 changes: 1 addition & 1 deletion src/bin/cargo/commands/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {

let version = match args.get_one::<String>("format-version") {
None => {
config.shell().warn(
config.emit_diagnostic(
"please specify `--format-version` flag explicitly \
to avoid compatibility problems",
)?;
Expand Down
18 changes: 5 additions & 13 deletions src/bin/cargo/commands/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,10 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
return Ok(());
}
let prefix = if args.flag("no-indent") {
config
.shell()
.warn("the --no-indent flag has been changed to --prefix=none")?;
config.emit_diagnostic("the --no-indent flag has been changed to --prefix=none")?;
"none"
} else if args.flag("prefix-depth") {
config
.shell()
.warn("the --prefix-depth flag has been changed to --prefix=depth")?;
config.emit_diagnostic("the --prefix-depth flag has been changed to --prefix=depth")?;
"depth"
} else {
args.get_one::<String>("prefix").unwrap().as_str()
Expand All @@ -123,17 +119,15 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {

let no_dedupe = args.flag("no-dedupe") || args.flag("all");
if args.flag("all") {
config.shell().warn(
config.emit_diagnostic(
"The `cargo tree` --all flag has been changed to --no-dedupe, \
and may be removed in a future version.\n\
If you are looking to display all workspace members, use the --workspace flag.",
)?;
}

let targets = if args.flag("all-targets") {
config
.shell()
.warn("the --all-targets flag has been changed to --target=all")?;
config.emit_diagnostic("the --all-targets flag has been changed to --target=all")?;
vec!["all".to_string()]
} else {
args._values_of("target")
Expand Down Expand Up @@ -228,9 +222,7 @@ fn parse_edge_kinds(config: &Config, args: &ArgMatches) -> CargoResult<(HashSet<
);

if args.flag("no-dev-dependencies") {
config
.shell()
.warn("the --no-dev-dependencies flag has changed to -e=no-dev")?;
config.emit_diagnostic("the --no-dev-dependencies flag has changed to -e=no-dev")?;
kinds.push("no-dev");
}

Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/compiler/build_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl BuildConfig {
let cfg = config.build_config()?;
let requested_kinds = CompileKind::from_requested_targets(config, requested_targets)?;
if jobs.is_some() && config.jobserver_from_env().is_some() {
config.shell().warn(
config.emit_diagnostic(
"a `-j` argument was passed to Cargo but Cargo is \
also configured with an external jobserver in \
its environment, ignoring the `-j` parameter",
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/compiler/build_context/target_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ impl TargetInfo {
continue;
}
if !reached_fixed_point {
config.shell().warn("non-trivial mutual dependency between target-specific configuration and RUSTFLAGS")?;
config.emit_diagnostic("non-trivial mutual dependency between target-specific configuration and RUSTFLAGS")?;
}

return Ok(TargetInfo {
Expand Down
6 changes: 3 additions & 3 deletions src/cargo/core/compiler/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
suggestion: &str|
-> CargoResult<()> {
if unit.target.name() == other_unit.target.name() {
self.bcx.config.shell().warn(format!(
self.bcx.config.emit_diagnostic(format!(
"output filename collision.\n\
{}\
The targets should have unique names.\n\
Expand All @@ -474,7 +474,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
suggestion
))
} else {
self.bcx.config.shell().warn(format!(
self.bcx.config.emit_diagnostic(format!(
"output filename collision.\n\
{}\
The output filenames should be unique.\n\
Expand Down Expand Up @@ -560,7 +560,7 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
}
if let Some(ref export_path) = output.export_path {
if let Some(other_unit) = output_collisions.insert(export_path.clone(), unit) {
self.bcx.config.shell().warn(format!(
self.bcx.config.emit_diagnostic(format!(
"`--out-dir` filename collision.\n\
{}\
The exported filenames should be unique.\n\
Expand Down
6 changes: 3 additions & 3 deletions src/cargo/core/compiler/future_incompat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl OnDiskReports {
crate::display_warning_with_error(
"failed to write on-disk future incompatible report",
&e,
&mut ws.config().shell(),
ws.config(),
);
}

Expand Down Expand Up @@ -395,7 +395,7 @@ pub fn save_and_display_report(
crate::display_warning_with_error(
"failed to read future-incompat config from disk",
&e,
&mut bcx.config.shell(),
bcx.config,
);
true
}
Expand Down Expand Up @@ -434,7 +434,7 @@ pub fn save_and_display_report(
let package_vers: Vec<_> = package_ids.iter().map(|pid| pid.to_string()).collect();

if should_display_message || bcx.build_config.future_incompat_report {
drop(bcx.config.shell().warn(&format!(
drop(bcx.config.emit_diagnostic(&format!(
"the following packages contain code that will be rejected by a future \
version of Rust: {}",
package_vers.join(", ")
Expand Down
22 changes: 11 additions & 11 deletions src/cargo/core/compiler/job_queue/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ use crate::core::compiler::future_incompat::{
self, FutureBreakageItem, FutureIncompatReportPackage,
};
use crate::core::resolver::ResolveBehavior;
use crate::core::{PackageId, Shell, TargetKind};
use crate::core::{PackageId, TargetKind};
use crate::util::diagnostic_server::{self, DiagnosticPrinter};
use crate::util::errors::AlreadyPrintedError;
use crate::util::machine_message::{self, Message as _};
Expand Down Expand Up @@ -619,7 +619,7 @@ impl<'cfg> DrainState<'cfg> {
}
}
Message::Warning { id, warning } => {
cx.bcx.config.shell().warn(warning)?;
cx.bcx.config.emit_diagnostic(warning)?;
self.bump_warning_count(id, true, false);
}
Message::WarningCount {
Expand Down Expand Up @@ -745,7 +745,7 @@ impl<'cfg> DrainState<'cfg> {
loop {
if errors.count == 0 || cx.bcx.build_config.keep_going {
if let Err(e) = self.spawn_work_if_possible(cx, jobserver_helper, scope) {
self.handle_error(&mut cx.bcx.config.shell(), &mut errors, e);
self.handle_error(cx.bcx.config, &mut errors, e);
}
}

Expand All @@ -762,7 +762,7 @@ impl<'cfg> DrainState<'cfg> {
// to the jobserver itself.
for event in self.wait_for_events() {
if let Err(event_err) = self.handle_event(cx, plan, event) {
self.handle_error(&mut cx.bcx.config.shell(), &mut errors, event_err);
self.handle_error(cx.bcx.config, &mut errors, event_err);
}
}
}
Expand All @@ -788,7 +788,7 @@ impl<'cfg> DrainState<'cfg> {

let time_elapsed = util::elapsed(cx.bcx.config.creation_time().elapsed());
if let Err(e) = self.timings.finished(cx, &errors.to_error()) {
self.handle_error(&mut cx.bcx.config.shell(), &mut errors, e);
self.handle_error(cx.bcx.config, &mut errors, e);
}
if cx.bcx.build_config.emit_json() {
let mut shell = cx.bcx.config.shell();
Expand All @@ -797,7 +797,7 @@ impl<'cfg> DrainState<'cfg> {
}
.to_json_string();
if let Err(e) = writeln!(shell.out(), "{}", msg) {
self.handle_error(&mut shell, &mut errors, e);
self.handle_error(cx.bcx.config, &mut errors, e);
}
}

Expand Down Expand Up @@ -828,15 +828,15 @@ impl<'cfg> DrainState<'cfg> {

fn handle_error(
&self,
shell: &mut Shell,
config: &Config,
err_state: &mut ErrorsDuringDrain,
new_err: impl Into<ErrorToHandle>,
) {
let new_err = new_err.into();
if new_err.print_always || err_state.count == 0 {
crate::display_error(&new_err.error, shell);
crate::display_error(&new_err.error, &mut config.shell());
if err_state.count == 0 && !self.active.is_empty() {
let _ = shell.warn("build failed, waiting for other jobs to finish...");
let _ = config.emit_diagnostic("build failed, waiting for other jobs to finish...");
}
err_state.count += 1;
} else {
Expand Down Expand Up @@ -953,7 +953,7 @@ impl<'cfg> DrainState<'cfg> {
}

for warning in output.warnings.iter() {
bcx.config.shell().warn(warning)?;
bcx.config.emit_diagnostic(warning)?;
}

if msg.is_some() {
Expand Down Expand Up @@ -1058,7 +1058,7 @@ impl<'cfg> DrainState<'cfg> {
}
// Errors are ignored here because it is tricky to handle them
// correctly, and they aren't important.
let _ = config.shell().warn(message);
let _ = config.emit_diagnostic(message);
}

fn finish(
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1272,7 +1272,7 @@ fn build_deps_args(cmd: &mut ProcessBuilder, cx: &Context<'_, '_>, unit: &Unit)
if let Some(dep) = deps.iter().find(|dep| {
!dep.unit.mode.is_doc() && dep.unit.target.is_lib() && !dep.unit.artifact.is_true()
}) {
bcx.config.shell().warn(format!(
bcx.config.emit_diagnostic(format!(
"The package `{}` \
provides no linkable target. The compiler might raise an error while compiling \
`{}`. Consider adding 'dylib' or 'rlib' to key `crate-type` in `{}`'s \
Expand Down
3 changes: 1 addition & 2 deletions src/cargo/core/compiler/standard_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ pub fn resolve_std<'cfg>(
) -> CargoResult<(PackageSet<'cfg>, Resolve, ResolvedFeatures)> {
if build_config.build_plan {
ws.config()
.shell()
.warn("-Zbuild-std does not currently fully support --build-plan")?;
.emit_diagnostic("-Zbuild-std does not currently fully support --build-plan")?;
}

let src_path = detect_sysroot_src_path(target_data)?;
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ impl<'cfg> PackageSet<'cfg> {
.map(|artifact| artifact.is_lib())
.unwrap_or(true)
}) {
ws.config().shell().warn(&format!(
ws.config().emit_diagnostic(&format!(
"{} ignoring invalid dependency `{}` which is missing a lib target",
pkg_id,
dep.name_in_toml(),
Expand Down
14 changes: 7 additions & 7 deletions src/cargo/core/profiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
use crate::core::compiler::{CompileKind, CompileTarget, Unit};
use crate::core::dependency::Artifact;
use crate::core::resolver::features::FeaturesFor;
use crate::core::{PackageId, PackageIdSpec, Resolve, Shell, Target, Workspace};
use crate::core::{PackageId, PackageIdSpec, Resolve, Target, Workspace};
use crate::util::interning::InternedString;
use crate::util::toml::{
ProfilePackageSpec, StringOrBool, TomlDebugInfo, TomlProfile, TomlProfiles,
Expand Down Expand Up @@ -341,7 +341,7 @@ impl Profiles {
pub fn validate_packages(
&self,
profiles: Option<&TomlProfiles>,
shell: &mut Shell,
config: &Config,
resolve: &Resolve,
) -> CargoResult<()> {
for (name, profile) in &self.by_name {
Expand All @@ -364,7 +364,7 @@ impl Profiles {
// iterates over the manifest profiles only.
if let Some(profiles) = profiles {
if let Some(toml_profile) = profiles.get(name) {
validate_packages_unmatched(shell, resolve, name, toml_profile, &found)?;
validate_packages_unmatched(config, resolve, name, toml_profile, &found)?;
}
}
}
Expand Down Expand Up @@ -1225,7 +1225,7 @@ fn get_config_profile(ws: &Workspace<'_>, name: &str) -> CargoResult<Option<Toml
)
})?;
for warning in warnings {
ws.config().shell().warn(warning)?;
ws.config().emit_diagnostic(warning)?;
}
Ok(Some(profile.val))
}
Expand Down Expand Up @@ -1291,7 +1291,7 @@ fn validate_packages_unique(
///
/// This helps check for typos and mistakes.
fn validate_packages_unmatched(
shell: &mut Shell,
config: &Config,
resolve: &Resolve,
name: &str,
toml: &TomlProfile,
Expand Down Expand Up @@ -1325,12 +1325,12 @@ fn validate_packages_unmatched(
.collect();
if name_matches.is_empty() {
let suggestion = closest_msg(&spec.name(), resolve.iter(), |p| p.name().as_str());
shell.warn(format!(
config.emit_diagnostic(format!(
"profile package spec `{}` in profile `{}` did not match any packages{}",
spec, name, suggestion
))?;
} else {
shell.warn(format!(
config.emit_diagnostic(format!(
"profile package spec `{}` in profile `{}` \
has a version or URL that does not match any of the packages: {}",
spec,
Expand Down
6 changes: 3 additions & 3 deletions src/cargo/core/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ impl<'cfg> PackageRegistry<'cfg> {
);

if dep.features().len() != 0 || !dep.uses_default_features() {
self.source_config.config().shell().warn(format!(
self.source_config.config().emit_diagnostic(format!(
"patch for `{}` uses the features mechanism. \
default-features and features will not take effect because the patch dependency does not support this mechanism",
dep.package_name()
Expand Down Expand Up @@ -546,7 +546,7 @@ https://doc.rust-lang.org/cargo/reference/overriding-dependencies.html
dep.package_name(),
boilerplate
);
self.source_config.config().shell().warn(&msg)?;
self.source_config.config().emit_diagnostic(&msg)?;
return Ok(());
}

Expand All @@ -559,7 +559,7 @@ https://doc.rust-lang.org/cargo/reference/overriding-dependencies.html
dep.package_name(),
boilerplate
);
self.source_config.config().shell().warn(&msg)?;
self.source_config.config().emit_diagnostic(&msg)?;
return Ok(());
}

Expand Down
Loading