Skip to content

Commit 9353538

Browse files
committed
Auto merge of rust-lang#101318 - GuillaumeGomez:rollup-qsr51z4, r=GuillaumeGomez
Rollup of 9 pull requests Successful merges: - rust-lang#97739 (Uplift the `let_underscore` lints from clippy into rustc.) - rust-lang#99583 (Add additional methods to the Demand type) - rust-lang#100147 (optimization of access level table construction) - rust-lang#100552 (rustc_target: Add a compatibility layer to separate internal and user-facing linker flavors) - rust-lang#100827 (Simplify MIR opt tests) - rust-lang#101166 (Generate error index with mdbook instead of raw HTML pages) - rust-lang#101294 (Fix rust-lang#100844 rebase accident) - rust-lang#101298 (rustdoc: remove unused CSS `#main-content > .since`) - rust-lang#101304 (Add autolabels for `A-query-system`) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents e21d771 + 138121a commit 9353538

File tree

104 files changed

+2743
-2275
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+2743
-2275
lines changed

Cargo.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -1259,7 +1259,7 @@ dependencies = [
12591259
name = "error_index_generator"
12601260
version = "0.0.0"
12611261
dependencies = [
1262-
"rustdoc",
1262+
"mdbook",
12631263
]
12641264

12651265
[[package]]

compiler/rustc_codegen_ssa/src/back/link.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -1173,13 +1173,6 @@ pub fn linker_and_flavor(sess: &Session) -> (PathBuf, LinkerFlavor) {
11731173
// only the linker flavor is known; use the default linker for the selected flavor
11741174
(None, Some(flavor)) => Some((
11751175
PathBuf::from(match flavor {
1176-
LinkerFlavor::Em => {
1177-
if cfg!(windows) {
1178-
"emcc.bat"
1179-
} else {
1180-
"emcc"
1181-
}
1182-
}
11831176
LinkerFlavor::Gcc => {
11841177
if cfg!(any(target_os = "solaris", target_os = "illumos")) {
11851178
// On historical Solaris systems, "cc" may have
@@ -1194,11 +1187,17 @@ pub fn linker_and_flavor(sess: &Session) -> (PathBuf, LinkerFlavor) {
11941187
}
11951188
}
11961189
LinkerFlavor::Ld => "ld",
1197-
LinkerFlavor::Msvc => "link.exe",
11981190
LinkerFlavor::Lld(_) => "lld",
1199-
LinkerFlavor::PtxLinker => "rust-ptx-linker",
1200-
LinkerFlavor::BpfLinker => "bpf-linker",
1201-
LinkerFlavor::L4Bender => "l4-bender",
1191+
LinkerFlavor::Msvc => "link.exe",
1192+
LinkerFlavor::EmCc => {
1193+
if cfg!(windows) {
1194+
"emcc.bat"
1195+
} else {
1196+
"emcc"
1197+
}
1198+
}
1199+
LinkerFlavor::Bpf => "bpf-linker",
1200+
LinkerFlavor::Ptx => "rust-ptx-linker",
12021201
}),
12031202
flavor,
12041203
)),
@@ -1208,7 +1207,7 @@ pub fn linker_and_flavor(sess: &Session) -> (PathBuf, LinkerFlavor) {
12081207
});
12091208

12101209
let flavor = if stem == "emcc" {
1211-
LinkerFlavor::Em
1210+
LinkerFlavor::EmCc
12121211
} else if stem == "gcc"
12131212
|| stem.ends_with("-gcc")
12141213
|| stem == "clang"
@@ -1236,7 +1235,8 @@ pub fn linker_and_flavor(sess: &Session) -> (PathBuf, LinkerFlavor) {
12361235

12371236
// linker and linker flavor specified via command line have precedence over what the target
12381237
// specification specifies
1239-
if let Some(ret) = infer_from(sess, sess.opts.cg.linker.clone(), sess.opts.cg.linker_flavor) {
1238+
let linker_flavor = sess.opts.cg.linker_flavor.map(LinkerFlavor::from_cli);
1239+
if let Some(ret) = infer_from(sess, sess.opts.cg.linker.clone(), linker_flavor) {
12401240
return ret;
12411241
}
12421242

@@ -2113,11 +2113,11 @@ fn add_order_independent_options(
21132113
});
21142114
}
21152115

2116-
if flavor == LinkerFlavor::PtxLinker {
2116+
if flavor == LinkerFlavor::Ptx {
21172117
// Provide the linker with fallback to internal `target-cpu`.
21182118
cmd.arg("--fallback-arch");
21192119
cmd.arg(&codegen_results.crate_info.target_cpu);
2120-
} else if flavor == LinkerFlavor::BpfLinker {
2120+
} else if flavor == LinkerFlavor::Bpf {
21212121
cmd.arg("--cpu");
21222122
cmd.arg(&codegen_results.crate_info.target_cpu);
21232123
cmd.arg("--cpu-features");

compiler/rustc_codegen_ssa/src/back/linker.rs

+9-12
Original file line numberDiff line numberDiff line change
@@ -126,29 +126,26 @@ pub fn get_linker<'a>(
126126
// to the linker args construction.
127127
assert!(cmd.get_args().is_empty() || sess.target.vendor == "uwp");
128128
match flavor {
129-
LinkerFlavor::Lld(LldFlavor::Link) | LinkerFlavor::Msvc => {
130-
Box::new(MsvcLinker { cmd, sess }) as Box<dyn Linker>
131-
}
132-
LinkerFlavor::Em => Box::new(EmLinker { cmd, sess }) as Box<dyn Linker>,
133129
LinkerFlavor::Gcc => {
134130
Box::new(GccLinker { cmd, sess, target_cpu, hinted_static: false, is_ld: false })
135131
as Box<dyn Linker>
136132
}
137-
133+
LinkerFlavor::Ld if sess.target.os == "l4re" => {
134+
Box::new(L4Bender::new(cmd, sess)) as Box<dyn Linker>
135+
}
138136
LinkerFlavor::Lld(LldFlavor::Ld)
139137
| LinkerFlavor::Lld(LldFlavor::Ld64)
140138
| LinkerFlavor::Ld => {
141139
Box::new(GccLinker { cmd, sess, target_cpu, hinted_static: false, is_ld: true })
142140
as Box<dyn Linker>
143141
}
144-
142+
LinkerFlavor::Lld(LldFlavor::Link) | LinkerFlavor::Msvc => {
143+
Box::new(MsvcLinker { cmd, sess }) as Box<dyn Linker>
144+
}
145145
LinkerFlavor::Lld(LldFlavor::Wasm) => Box::new(WasmLd::new(cmd, sess)) as Box<dyn Linker>,
146-
147-
LinkerFlavor::PtxLinker => Box::new(PtxLinker { cmd, sess }) as Box<dyn Linker>,
148-
149-
LinkerFlavor::BpfLinker => Box::new(BpfLinker { cmd, sess }) as Box<dyn Linker>,
150-
151-
LinkerFlavor::L4Bender => Box::new(L4Bender::new(cmd, sess)) as Box<dyn Linker>,
146+
LinkerFlavor::EmCc => Box::new(EmLinker { cmd, sess }) as Box<dyn Linker>,
147+
LinkerFlavor::Bpf => Box::new(BpfLinker { cmd, sess }) as Box<dyn Linker>,
148+
LinkerFlavor::Ptx => Box::new(PtxLinker { cmd, sess }) as Box<dyn Linker>,
152149
}
153150
}
154151

compiler/rustc_error_messages/locales/en-US/privacy.ftl

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ privacy_in_public_interface = {$vis_descr} {$kind} `{$descr}` in public interfac
1111
.label = can't leak {$vis_descr} {$kind}
1212
.visibility_label = `{$descr}` declared as {$vis_descr}
1313
14+
privacy_report_access_level = {$descr}
15+
1416
privacy_from_private_dep_in_public_interface =
1517
{$kind} `{$descr}` from private dependency '{$krate}' in public interface
1618

compiler/rustc_feature/src/builtin_attrs.rs

+1
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,7 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
758758
// Internal attributes, Testing:
759759
// ==========================================================================
760760

761+
rustc_attr!(TEST, rustc_access_level, Normal, template!(Word), WarnFollowing),
761762
rustc_attr!(TEST, rustc_outlives, Normal, template!(Word), WarnFollowing),
762763
rustc_attr!(TEST, rustc_capture_analysis, Normal, template!(Word), WarnFollowing),
763764
rustc_attr!(TEST, rustc_insignificant_dtor, Normal, template!(Word), WarnFollowing),

compiler/rustc_interface/src/tests.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ use rustc_session::{build_session, getopts, DiagnosticOutput, Session};
2121
use rustc_span::edition::{Edition, DEFAULT_EDITION};
2222
use rustc_span::symbol::sym;
2323
use rustc_span::SourceFileHashAlgorithm;
24-
use rustc_target::spec::{CodeModel, LinkerFlavor, MergeFunctions, PanicStrategy};
25-
use rustc_target::spec::{
26-
RelocModel, RelroLevel, SanitizerSet, SplitDebuginfo, StackProtector, TlsModel,
27-
};
24+
use rustc_target::spec::{CodeModel, LinkerFlavorCli, MergeFunctions, PanicStrategy, RelocModel};
25+
use rustc_target::spec::{RelroLevel, SanitizerSet, SplitDebuginfo, StackProtector, TlsModel};
2826

2927
use std::collections::{BTreeMap, BTreeSet};
3028
use std::iter::FromIterator;
@@ -552,7 +550,7 @@ fn test_codegen_options_tracking_hash() {
552550
untracked!(link_args, vec![String::from("abc"), String::from("def")]);
553551
untracked!(link_self_contained, Some(true));
554552
untracked!(linker, Some(PathBuf::from("linker")));
555-
untracked!(linker_flavor, Some(LinkerFlavor::Gcc));
553+
untracked!(linker_flavor, Some(LinkerFlavorCli::Gcc));
556554
untracked!(no_stack_check, true);
557555
untracked!(remark, Passes::Some(vec![String::from("pass1"), String::from("pass2")]));
558556
untracked!(rpath, true);
+175
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
use crate::{LateContext, LateLintPass, LintContext};
2+
use rustc_errors::{Applicability, LintDiagnosticBuilder, MultiSpan};
3+
use rustc_hir as hir;
4+
use rustc_middle::ty;
5+
use rustc_span::Symbol;
6+
7+
declare_lint! {
8+
/// The `let_underscore_drop` lint checks for statements which don't bind
9+
/// an expression which has a non-trivial Drop implementation to anything,
10+
/// causing the expression to be dropped immediately instead of at end of
11+
/// scope.
12+
///
13+
/// ### Example
14+
/// ```
15+
/// struct SomeStruct;
16+
/// impl Drop for SomeStruct {
17+
/// fn drop(&mut self) {
18+
/// println!("Dropping SomeStruct");
19+
/// }
20+
/// }
21+
///
22+
/// fn main() {
23+
/// #[warn(let_underscore_drop)]
24+
/// // SomeStuct is dropped immediately instead of at end of scope,
25+
/// // so "Dropping SomeStruct" is printed before "end of main".
26+
/// // The order of prints would be reversed if SomeStruct was bound to
27+
/// // a name (such as "_foo").
28+
/// let _ = SomeStruct;
29+
/// println!("end of main");
30+
/// }
31+
/// ```
32+
///
33+
/// {{produces}}
34+
///
35+
/// ### Explanation
36+
///
37+
/// Statements which assign an expression to an underscore causes the
38+
/// expression to immediately drop instead of extending the expression's
39+
/// lifetime to the end of the scope. This is usually unintended,
40+
/// especially for types like `MutexGuard`, which are typically used to
41+
/// lock a mutex for the duration of an entire scope.
42+
///
43+
/// If you want to extend the expression's lifetime to the end of the scope,
44+
/// assign an underscore-prefixed name (such as `_foo`) to the expression.
45+
/// If you do actually want to drop the expression immediately, then
46+
/// calling `std::mem::drop` on the expression is clearer and helps convey
47+
/// intent.
48+
pub LET_UNDERSCORE_DROP,
49+
Allow,
50+
"non-binding let on a type that implements `Drop`"
51+
}
52+
53+
declare_lint! {
54+
/// The `let_underscore_lock` lint checks for statements which don't bind
55+
/// a mutex to anything, causing the lock to be released immediately instead
56+
/// of at end of scope, which is typically incorrect.
57+
///
58+
/// ### Example
59+
/// ```compile_fail
60+
/// use std::sync::{Arc, Mutex};
61+
/// use std::thread;
62+
/// let data = Arc::new(Mutex::new(0));
63+
///
64+
/// thread::spawn(move || {
65+
/// // The lock is immediately released instead of at the end of the
66+
/// // scope, which is probably not intended.
67+
/// let _ = data.lock().unwrap();
68+
/// println!("doing some work");
69+
/// let mut lock = data.lock().unwrap();
70+
/// *lock += 1;
71+
/// });
72+
/// ```
73+
///
74+
/// {{produces}}
75+
///
76+
/// ### Explanation
77+
///
78+
/// Statements which assign an expression to an underscore causes the
79+
/// expression to immediately drop instead of extending the expression's
80+
/// lifetime to the end of the scope. This is usually unintended,
81+
/// especially for types like `MutexGuard`, which are typically used to
82+
/// lock a mutex for the duration of an entire scope.
83+
///
84+
/// If you want to extend the expression's lifetime to the end of the scope,
85+
/// assign an underscore-prefixed name (such as `_foo`) to the expression.
86+
/// If you do actually want to drop the expression immediately, then
87+
/// calling `std::mem::drop` on the expression is clearer and helps convey
88+
/// intent.
89+
pub LET_UNDERSCORE_LOCK,
90+
Deny,
91+
"non-binding let on a synchronization lock"
92+
}
93+
94+
declare_lint_pass!(LetUnderscore => [LET_UNDERSCORE_DROP, LET_UNDERSCORE_LOCK]);
95+
96+
const SYNC_GUARD_SYMBOLS: [Symbol; 3] = [
97+
rustc_span::sym::MutexGuard,
98+
rustc_span::sym::RwLockReadGuard,
99+
rustc_span::sym::RwLockWriteGuard,
100+
];
101+
102+
impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
103+
fn check_local(&mut self, cx: &LateContext<'_>, local: &hir::Local<'_>) {
104+
if !matches!(local.pat.kind, hir::PatKind::Wild) {
105+
return;
106+
}
107+
if let Some(init) = local.init {
108+
let init_ty = cx.typeck_results().expr_ty(init);
109+
// If the type has a trivial Drop implementation, then it doesn't
110+
// matter that we drop the value immediately.
111+
if !init_ty.needs_drop(cx.tcx, cx.param_env) {
112+
return;
113+
}
114+
let is_sync_lock = match init_ty.kind() {
115+
ty::Adt(adt, _) => SYNC_GUARD_SYMBOLS
116+
.iter()
117+
.any(|guard_symbol| cx.tcx.is_diagnostic_item(*guard_symbol, adt.did())),
118+
_ => false,
119+
};
120+
121+
if is_sync_lock {
122+
let mut span = MultiSpan::from_spans(vec![local.pat.span, init.span]);
123+
span.push_span_label(
124+
local.pat.span,
125+
"this lock is not assigned to a binding and is immediately dropped".to_string(),
126+
);
127+
span.push_span_label(
128+
init.span,
129+
"this binding will immediately drop the value assigned to it".to_string(),
130+
);
131+
cx.struct_span_lint(LET_UNDERSCORE_LOCK, span, |lint| {
132+
build_and_emit_lint(
133+
lint,
134+
local,
135+
init.span,
136+
"non-binding let on a synchronization lock",
137+
)
138+
})
139+
} else {
140+
cx.struct_span_lint(LET_UNDERSCORE_DROP, local.span, |lint| {
141+
build_and_emit_lint(
142+
lint,
143+
local,
144+
init.span,
145+
"non-binding let on a type that implements `Drop`",
146+
);
147+
})
148+
}
149+
}
150+
}
151+
}
152+
153+
fn build_and_emit_lint(
154+
lint: LintDiagnosticBuilder<'_, ()>,
155+
local: &hir::Local<'_>,
156+
init_span: rustc_span::Span,
157+
msg: &str,
158+
) {
159+
lint.build(msg)
160+
.span_suggestion_verbose(
161+
local.pat.span,
162+
"consider binding to an unused variable to avoid immediately dropping the value",
163+
"_unused",
164+
Applicability::MachineApplicable,
165+
)
166+
.multipart_suggestion(
167+
"consider immediately dropping the value",
168+
vec![
169+
(local.span.until(init_span), "drop(".to_string()),
170+
(init_span.shrink_to_hi(), ")".to_string()),
171+
],
172+
Applicability::MachineApplicable,
173+
)
174+
.emit();
175+
}

compiler/rustc_lint/src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ mod expect;
5555
pub mod hidden_unicode_codepoints;
5656
mod internal;
5757
mod late;
58+
mod let_underscore;
5859
mod levels;
5960
mod methods;
6061
mod non_ascii_idents;
@@ -86,6 +87,7 @@ use builtin::*;
8687
use enum_intrinsics_non_enums::EnumIntrinsicsNonEnums;
8788
use hidden_unicode_codepoints::*;
8889
use internal::*;
90+
use let_underscore::*;
8991
use methods::*;
9092
use non_ascii_idents::*;
9193
use non_fmt_panic::NonPanicFmt;
@@ -189,6 +191,7 @@ macro_rules! late_lint_mod_passes {
189191
VariantSizeDifferences: VariantSizeDifferences,
190192
BoxPointers: BoxPointers,
191193
PathStatements: PathStatements,
194+
LetUnderscore: LetUnderscore,
192195
// Depends on referenced function signatures in expressions
193196
UnusedResults: UnusedResults,
194197
NonUpperCaseGlobals: NonUpperCaseGlobals,
@@ -315,6 +318,8 @@ fn register_builtins(store: &mut LintStore, no_interleave_lints: bool) {
315318
REDUNDANT_SEMICOLONS
316319
);
317320

321+
add_lint_group!("let_underscore", LET_UNDERSCORE_DROP, LET_UNDERSCORE_LOCK);
322+
318323
add_lint_group!(
319324
"rust_2018_idioms",
320325
BARE_TRAIT_OBJECTS,

0 commit comments

Comments
 (0)