Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 366d112

Browse files
committedJan 22, 2024
Auto merge of rust-lang#120226 - matthiaskrgr:rollup-9xwx0si, r=matthiaskrgr
Rollup of 9 pull requests Successful merges: - rust-lang#118714 ( Explanation that fields are being used when deriving `(Partial)Ord` on enums) - rust-lang#119710 (Improve `let_underscore_lock`) - rust-lang#119726 (Tweak Library Integer Division Docs) - rust-lang#119746 (rustdoc: hide modals when resizing the sidebar) - rust-lang#119986 (Fix error counting) - rust-lang#120194 (Shorten `#[must_use]` Diagnostic Message for `Option::is_none`) - rust-lang#120200 (Correct the anchor of an URL in an error message) - rust-lang#120203 (Replace `#!/bin/bash` with `#!/usr/bin/env bash` in rust-installer tests) - rust-lang#120212 (Give nnethercote more reviews) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 6fff796 + 610f13d commit 366d112

File tree

30 files changed

+267
-124
lines changed

30 files changed

+267
-124
lines changed
 

‎compiler/rustc_builtin_macros/src/format.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ fn make_format_args(
529529

530530
// Only check for unused named argument names if there are no other errors to avoid causing
531531
// too much noise in output errors, such as when a named argument is entirely unused.
532-
if invalid_refs.is_empty() && ecx.dcx().err_count() == 0 {
532+
if invalid_refs.is_empty() && ecx.dcx().has_errors().is_none() {
533533
for &(index, span, used_as) in &numeric_refences_to_named_arg {
534534
let (position_sp_to_replace, position_sp_for_msg) = match used_as {
535535
Placeholder(pspan) => (span, pspan),

‎compiler/rustc_codegen_ssa/messages.ftl

+1-1
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ codegen_ssa_unsupported_arch = unsupported arch `{$arch}` for os `{$os}`
325325
326326
codegen_ssa_unsupported_link_self_contained = option `-C link-self-contained` is not supported on this target
327327
328-
codegen_ssa_use_cargo_directive = use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)
328+
codegen_ssa_use_cargo_directive = use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#rustc-link-lib)
329329
330330
codegen_ssa_version_script_write_failure = failed to write version script: {$error}
331331

‎compiler/rustc_errors/src/lib.rs

+18-22
Original file line numberDiff line numberDiff line change
@@ -421,16 +421,16 @@ pub struct DiagCtxt {
421421
struct DiagCtxtInner {
422422
flags: DiagCtxtFlags,
423423

424-
/// The number of lint errors that have been emitted.
424+
/// The number of lint errors that have been emitted, including duplicates.
425425
lint_err_count: usize,
426-
/// The number of errors that have been emitted, including duplicates.
427-
///
428-
/// This is not necessarily the count that's reported to the user once
429-
/// compilation ends.
426+
/// The number of non-lint errors that have been emitted, including duplicates.
430427
err_count: usize,
428+
429+
/// The error count shown to the user at the end.
431430
deduplicated_err_count: usize,
432-
/// The warning count, used for a recap upon finishing
431+
/// The warning count shown to the user at the end.
433432
deduplicated_warn_count: usize,
433+
434434
/// Has this diagnostic context printed any diagnostics? (I.e. has
435435
/// `self.emitter.emit_diagnostic()` been called?
436436
has_printed: bool,
@@ -927,42 +927,38 @@ impl DiagCtxt {
927927
self.struct_bug(msg).emit()
928928
}
929929

930+
/// This excludes lint errors and delayed bugs.
930931
#[inline]
931932
pub fn err_count(&self) -> usize {
932933
self.inner.borrow().err_count
933934
}
934935

936+
/// This excludes lint errors and delayed bugs.
935937
pub fn has_errors(&self) -> Option<ErrorGuaranteed> {
936938
self.inner.borrow().has_errors().then(|| {
937939
#[allow(deprecated)]
938940
ErrorGuaranteed::unchecked_claim_error_was_emitted()
939941
})
940942
}
941943

944+
/// This excludes delayed bugs. Unless absolutely necessary, prefer
945+
/// `has_errors` to this method.
942946
pub fn has_errors_or_lint_errors(&self) -> Option<ErrorGuaranteed> {
943947
let inner = self.inner.borrow();
944-
let has_errors_or_lint_errors = inner.has_errors() || inner.lint_err_count > 0;
945-
has_errors_or_lint_errors.then(|| {
946-
#[allow(deprecated)]
947-
ErrorGuaranteed::unchecked_claim_error_was_emitted()
948-
})
949-
}
950-
951-
pub fn has_errors_or_span_delayed_bugs(&self) -> Option<ErrorGuaranteed> {
952-
let inner = self.inner.borrow();
953-
let has_errors_or_span_delayed_bugs =
954-
inner.has_errors() || !inner.span_delayed_bugs.is_empty();
955-
has_errors_or_span_delayed_bugs.then(|| {
948+
let result = inner.has_errors() || inner.lint_err_count > 0;
949+
result.then(|| {
956950
#[allow(deprecated)]
957951
ErrorGuaranteed::unchecked_claim_error_was_emitted()
958952
})
959953
}
960954

961-
pub fn is_compilation_going_to_fail(&self) -> Option<ErrorGuaranteed> {
955+
/// Unless absolutely necessary, prefer `has_errors` or
956+
/// `has_errors_or_lint_errors` to this method.
957+
pub fn has_errors_or_lint_errors_or_delayed_bugs(&self) -> Option<ErrorGuaranteed> {
962958
let inner = self.inner.borrow();
963-
let will_fail =
959+
let result =
964960
inner.has_errors() || inner.lint_err_count > 0 || !inner.span_delayed_bugs.is_empty();
965-
will_fail.then(|| {
961+
result.then(|| {
966962
#[allow(deprecated)]
967963
ErrorGuaranteed::unchecked_claim_error_was_emitted()
968964
})
@@ -1162,7 +1158,7 @@ impl DiagCtxt {
11621158
let mut inner = self.inner.borrow_mut();
11631159

11641160
if loud && lint_level.is_error() {
1165-
inner.err_count += 1;
1161+
inner.lint_err_count += 1;
11661162
inner.panic_if_treat_err_as_bug();
11671163
}
11681164

‎compiler/rustc_hir_analysis/src/check/wfcheck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ where
116116
let errors = wfcx.select_all_or_error();
117117
if !errors.is_empty() {
118118
let err = infcx.err_ctxt().report_fulfillment_errors(errors);
119-
if tcx.dcx().err_count() > 0 {
119+
if tcx.dcx().has_errors().is_some() {
120120
return Err(err);
121121
} else {
122122
// HACK(oli-obk): tests/ui/specialization/min_specialization/specialize_on_type_error.rs

‎compiler/rustc_incremental/src/persist/fs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ pub fn finalize_session_directory(sess: &Session, svh: Option<Svh>) {
312312

313313
let incr_comp_session_dir: PathBuf = sess.incr_comp_session_dir().clone();
314314

315-
if let Some(_) = sess.dcx().has_errors_or_span_delayed_bugs() {
315+
if sess.dcx().has_errors_or_lint_errors_or_delayed_bugs().is_some() {
316316
// If there have been any errors during compilation, we don't want to
317317
// publish this session directory. Rather, we'll just delete it.
318318

‎compiler/rustc_incremental/src/persist/save.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ pub fn save_dep_graph(tcx: TyCtxt<'_>) {
3131
if sess.opts.incremental.is_none() {
3232
return;
3333
}
34-
// This is going to be deleted in finalize_session_directory, so let's not create it
35-
if let Some(_) = sess.dcx().has_errors_or_span_delayed_bugs() {
34+
// This is going to be deleted in finalize_session_directory, so let's not create it.
35+
if sess.dcx().has_errors_or_lint_errors_or_delayed_bugs().is_some() {
3636
return;
3737
}
3838

@@ -87,7 +87,7 @@ pub fn save_work_product_index(
8787
return;
8888
}
8989
// This is going to be deleted in finalize_session_directory, so let's not create it
90-
if let Some(_) = sess.dcx().has_errors_or_span_delayed_bugs() {
90+
if sess.dcx().has_errors_or_lint_errors().is_some() {
9191
return;
9292
}
9393

‎compiler/rustc_infer/src/infer/error_reporting/mod.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ fn escape_literal(s: &str) -> String {
117117
/// field is only populated during an in-progress typeck.
118118
/// Get an instance by calling `InferCtxt::err_ctxt` or `FnCtxt::err_ctxt`.
119119
///
120-
/// You must only create this if you intend to actually emit an error.
121-
/// This provides a lot of utility methods which should not be used
122-
/// during the happy path.
120+
/// You must only create this if you intend to actually emit an error (or
121+
/// perhaps a warning, though preferably not.) It provides a lot of utility
122+
/// methods which should not be used during the happy path.
123123
pub struct TypeErrCtxt<'a, 'tcx> {
124124
pub infcx: &'a InferCtxt<'tcx>,
125125
pub typeck_results: Option<std::cell::Ref<'a, ty::TypeckResults<'tcx>>>,
@@ -133,9 +133,10 @@ pub struct TypeErrCtxt<'a, 'tcx> {
133133

134134
impl Drop for TypeErrCtxt<'_, '_> {
135135
fn drop(&mut self) {
136-
if let Some(_) = self.dcx().has_errors_or_span_delayed_bugs() {
137-
// ok, emitted an error.
136+
if self.dcx().has_errors().is_some() {
137+
// Ok, emitted an error.
138138
} else {
139+
// Didn't emit an error; maybe it was created but not yet emitted.
139140
self.infcx
140141
.tcx
141142
.sess

‎compiler/rustc_lint/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,9 @@ lint_multiple_supertrait_upcastable = `{$ident}` is object-safe and has multiple
345345
lint_node_source = `forbid` level set here
346346
.note = {$reason}
347347
348+
lint_non_binding_let_multi_drop_fn =
349+
consider immediately dropping the value using `drop(..)` after the `let` statement
350+
348351
lint_non_binding_let_multi_suggestion =
349352
consider immediately dropping the value
350353

‎compiler/rustc_lint/src/let_underscore.rs

+39-20
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
use rustc_errors::MultiSpan;
66
use rustc_hir as hir;
77
use rustc_middle::ty;
8-
use rustc_span::Symbol;
8+
use rustc_span::{sym, Symbol};
99

1010
declare_lint! {
1111
/// The `let_underscore_drop` lint checks for statements which don't bind
@@ -105,51 +105,70 @@ const SYNC_GUARD_SYMBOLS: [Symbol; 3] = [
105105

106106
impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
107107
fn check_local(&mut self, cx: &LateContext<'_>, local: &hir::Local<'_>) {
108-
if !matches!(local.pat.kind, hir::PatKind::Wild) {
109-
return;
110-
}
111-
112108
if matches!(local.source, rustc_hir::LocalSource::AsyncFn) {
113109
return;
114110
}
115-
if let Some(init) = local.init {
116-
let init_ty = cx.typeck_results().expr_ty(init);
111+
112+
let mut top_level = true;
113+
114+
// We recursively walk through all patterns, so that we can catch cases where the lock is nested in a pattern.
115+
// For the basic `let_underscore_drop` lint, we only look at the top level, since there are many legitimate reasons
116+
// to bind a sub-pattern to an `_`, if we're only interested in the rest.
117+
// But with locks, we prefer having the chance of "false positives" over missing cases, since the effects can be
118+
// quite catastrophic.
119+
local.pat.walk_always(|pat| {
120+
let is_top_level = top_level;
121+
top_level = false;
122+
123+
if !matches!(pat.kind, hir::PatKind::Wild) {
124+
return;
125+
}
126+
127+
let ty = cx.typeck_results().pat_ty(pat);
128+
117129
// If the type has a trivial Drop implementation, then it doesn't
118130
// matter that we drop the value immediately.
119-
if !init_ty.needs_drop(cx.tcx, cx.param_env) {
131+
if !ty.needs_drop(cx.tcx, cx.param_env) {
120132
return;
121133
}
122-
let is_sync_lock = match init_ty.kind() {
134+
// Lint for patterns like `mutex.lock()`, which returns `Result<MutexGuard, _>` as well.
135+
let potential_lock_type = match ty.kind() {
136+
ty::Adt(adt, args) if cx.tcx.is_diagnostic_item(sym::Result, adt.did()) => {
137+
args.type_at(0)
138+
}
139+
_ => ty,
140+
};
141+
let is_sync_lock = match potential_lock_type.kind() {
123142
ty::Adt(adt, _) => SYNC_GUARD_SYMBOLS
124143
.iter()
125144
.any(|guard_symbol| cx.tcx.is_diagnostic_item(*guard_symbol, adt.did())),
126145
_ => false,
127146
};
128147

148+
let can_use_init = is_top_level.then_some(local.init).flatten();
149+
129150
let sub = NonBindingLetSub {
130-
suggestion: local.pat.span,
131-
multi_suggestion_start: local.span.until(init.span),
132-
multi_suggestion_end: init.span.shrink_to_hi(),
151+
suggestion: pat.span,
152+
// We can't suggest `drop()` when we're on the top level.
153+
drop_fn_start_end: can_use_init
154+
.map(|init| (local.span.until(init.span), init.span.shrink_to_hi())),
133155
is_assign_desugar: matches!(local.source, rustc_hir::LocalSource::AssignDesugar(_)),
134156
};
135157
if is_sync_lock {
136-
let mut span = MultiSpan::from_spans(vec![local.pat.span, init.span]);
158+
let mut span = MultiSpan::from_span(pat.span);
137159
span.push_span_label(
138-
local.pat.span,
160+
pat.span,
139161
"this lock is not assigned to a binding and is immediately dropped".to_string(),
140162
);
141-
span.push_span_label(
142-
init.span,
143-
"this binding will immediately drop the value assigned to it".to_string(),
144-
);
145163
cx.emit_spanned_lint(LET_UNDERSCORE_LOCK, span, NonBindingLet::SyncLock { sub });
146-
} else {
164+
// Only emit let_underscore_drop for top-level `_` patterns.
165+
} else if can_use_init.is_some() {
147166
cx.emit_spanned_lint(
148167
LET_UNDERSCORE_DROP,
149168
local.span,
150169
NonBindingLet::DropType { sub },
151170
);
152171
}
153-
}
172+
});
154173
}
155174
}

‎compiler/rustc_lint/src/lints.rs

+26-17
Original file line numberDiff line numberDiff line change
@@ -930,8 +930,7 @@ pub enum NonBindingLet {
930930

931931
pub struct NonBindingLetSub {
932932
pub suggestion: Span,
933-
pub multi_suggestion_start: Span,
934-
pub multi_suggestion_end: Span,
933+
pub drop_fn_start_end: Option<(Span, Span)>,
935934
pub is_assign_desugar: bool,
936935
}
937936

@@ -940,21 +939,31 @@ impl AddToDiagnostic for NonBindingLetSub {
940939
where
941940
F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
942941
{
943-
let prefix = if self.is_assign_desugar { "let " } else { "" };
944-
diag.span_suggestion_verbose(
945-
self.suggestion,
946-
fluent::lint_non_binding_let_suggestion,
947-
format!("{prefix}_unused"),
948-
Applicability::MachineApplicable,
949-
);
950-
diag.multipart_suggestion(
951-
fluent::lint_non_binding_let_multi_suggestion,
952-
vec![
953-
(self.multi_suggestion_start, "drop(".to_string()),
954-
(self.multi_suggestion_end, ")".to_string()),
955-
],
956-
Applicability::MachineApplicable,
957-
);
942+
let can_suggest_binding = self.drop_fn_start_end.is_some() || !self.is_assign_desugar;
943+
944+
if can_suggest_binding {
945+
let prefix = if self.is_assign_desugar { "let " } else { "" };
946+
diag.span_suggestion_verbose(
947+
self.suggestion,
948+
fluent::lint_non_binding_let_suggestion,
949+
format!("{prefix}_unused"),
950+
Applicability::MachineApplicable,
951+
);
952+
} else {
953+
diag.span_help(self.suggestion, fluent::lint_non_binding_let_suggestion);
954+
}
955+
if let Some(drop_fn_start_end) = self.drop_fn_start_end {
956+
diag.multipart_suggestion(
957+
fluent::lint_non_binding_let_multi_suggestion,
958+
vec![
959+
(drop_fn_start_end.0, "drop(".to_string()),
960+
(drop_fn_start_end.1, ")".to_string()),
961+
],
962+
Applicability::MachineApplicable,
963+
);
964+
} else {
965+
diag.help(fluent::lint_non_binding_let_multi_drop_fn);
966+
}
958967
}
959968
}
960969

‎compiler/rustc_middle/src/ty/visit.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,13 @@ pub trait TypeVisitableExt<'tcx>: TypeVisitable<TyCtxt<'tcx>> {
5555
}
5656
fn error_reported(&self) -> Result<(), ErrorGuaranteed> {
5757
if self.references_error() {
58-
if let Some(reported) = ty::tls::with(|tcx| tcx.dcx().is_compilation_going_to_fail()) {
58+
// We must include lint errors and span delayed bugs here.
59+
if let Some(reported) =
60+
ty::tls::with(|tcx| tcx.dcx().has_errors_or_lint_errors_or_delayed_bugs())
61+
{
5962
Err(reported)
6063
} else {
61-
bug!("expect tcx.sess.is_compilation_going_to_fail return `Some`");
64+
bug!("expected some kind of error in `error_reported`");
6265
}
6366
} else {
6467
Ok(())

‎compiler/rustc_query_system/src/dep_graph/graph.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ impl<D: Deps> DepGraphData<D> {
818818
None => {}
819819
}
820820

821-
if let None = qcx.dep_context().sess().dcx().has_errors_or_span_delayed_bugs() {
821+
if let None = qcx.dep_context().sess().dcx().has_errors_or_lint_errors_or_delayed_bugs() {
822822
panic!("try_mark_previous_green() - Forcing the DepNode should have set its color")
823823
}
824824

‎compiler/rustc_session/src/session.rs

+1
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ impl Session {
323323
}
324324

325325
pub fn compile_status(&self) -> Result<(), ErrorGuaranteed> {
326+
// We must include lint errors here.
326327
if let Some(reported) = self.dcx().has_errors_or_lint_errors() {
327328
let _ = self.dcx().emit_stashed_diagnostics();
328329
Err(reported)

‎library/core/src/cmp.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,8 @@ impl<T: Clone> Clone for Reverse<T> {
710710
/// [lexicographic](https://en.wikipedia.org/wiki/Lexicographic_order) ordering
711711
/// based on the top-to-bottom declaration order of the struct's members.
712712
///
713-
/// When `derive`d on enums, variants are ordered by their discriminants.
713+
/// When `derive`d on enums, variants are ordered primarily by their discriminants.
714+
/// Secondarily, they are ordered by their fields.
714715
/// By default, the discriminant is smallest for variants at the top, and
715716
/// largest for variants at the bottom. Here's an example:
716717
///
@@ -963,7 +964,8 @@ pub macro Ord($item:item) {
963964
/// [lexicographic](https://en.wikipedia.org/wiki/Lexicographic_order) ordering
964965
/// based on the top-to-bottom declaration order of the struct's members.
965966
///
966-
/// When `derive`d on enums, variants are ordered by their discriminants.
967+
/// When `derive`d on enums, variants are primarily ordered by their discriminants.
968+
/// Secondarily, they are ordered by their fields.
967969
/// By default, the discriminant is smallest for variants at the top, and
968970
/// largest for variants at the bottom. Here's an example:
969971
///

0 commit comments

Comments
 (0)
Please sign in to comment.