Relax the suspicious_double_ref_op lint (rhbz2225471)

Enable the profiler runtime for native hosts (rhbz2213875)
This commit is contained in:
Josh Stone 2023-07-26 16:16:43 -07:00
parent 0ad12dddea
commit 2ee658390a
3 changed files with 350 additions and 1 deletions

View File

@ -0,0 +1,142 @@
From f2fd2d01f96b50b039402c9ab4278230687f7922 Mon Sep 17 00:00:00 2001
From: Josh Stone <jistone@redhat.com>
Date: Tue, 25 Jul 2023 13:11:50 -0700
Subject: [PATCH] Allow using external builds of the compiler-rt profile lib
This changes the bootstrap config `target.*.profiler` from a plain bool
to also allow a string, which will be used as a path to the pre-built
profiling runtime for that target. Then `profiler_builtins/build.rs`
reads that in a `LLVM_PROFILER_RT_LIB` environment variable.
---
config.example.toml | 6 ++++--
library/profiler_builtins/build.rs | 6 ++++++
src/bootstrap/compile.rs | 4 ++++
src/bootstrap/config.rs | 30 ++++++++++++++++++++++++------
4 files changed, 38 insertions(+), 8 deletions(-)
diff --git a/config.example.toml b/config.example.toml
index d0eaa9fd7ffa..e0e991e679af 100644
--- a/config.example.toml
+++ b/config.example.toml
@@ -745,8 +745,10 @@ changelog-seen = 2
# This option will override the same option under [build] section.
#sanitizers = build.sanitizers (bool)
-# Build the profiler runtime for this target(required when compiling with options that depend
-# on this runtime, such as `-C profile-generate` or `-C instrument-coverage`).
+# When true, build the profiler runtime for this target(required when compiling
+# with options that depend on this runtime, such as `-C profile-generate` or
+# `-C instrument-coverage`). This may also be given a path to an existing build
+# of the profiling runtime library from LLVM's compiler-rt.
# This option will override the same option under [build] section.
#profiler = build.profiler (bool)
diff --git a/library/profiler_builtins/build.rs b/library/profiler_builtins/build.rs
index 1b1f11798d74..d14d0b82229a 100644
--- a/library/profiler_builtins/build.rs
+++ b/library/profiler_builtins/build.rs
@@ -6,6 +6,12 @@
use std::path::Path;
fn main() {
+ println!("cargo:rerun-if-env-changed=LLVM_PROFILER_RT_LIB");
+ if let Ok(rt) = env::var("LLVM_PROFILER_RT_LIB") {
+ println!("cargo:rustc-link-lib=static:+verbatim={rt}");
+ return;
+ }
+
let target = env::var("TARGET").expect("TARGET was not set");
let cfg = &mut cc::Build::new();
diff --git a/src/bootstrap/compile.rs b/src/bootstrap/compile.rs
index 33addb90da37..1d8b3c6e5435 100644
--- a/src/bootstrap/compile.rs
+++ b/src/bootstrap/compile.rs
@@ -305,6 +305,10 @@ pub fn std_cargo(builder: &Builder<'_>, target: TargetSelection, stage: u32, car
cargo.env("MACOSX_DEPLOYMENT_TARGET", target);
}
+ if let Some(path) = builder.config.profiler_path(target) {
+ cargo.env("LLVM_PROFILER_RT_LIB", path);
+ }
+
// Determine if we're going to compile in optimized C intrinsics to
// the `compiler-builtins` crate. These intrinsics live in LLVM's
// `compiler-rt` repository, but our `src/llvm-project` submodule isn't
diff --git a/src/bootstrap/config.rs b/src/bootstrap/config.rs
index e192cda9a9a7..a4803db0a470 100644
--- a/src/bootstrap/config.rs
+++ b/src/bootstrap/config.rs
@@ -467,7 +467,7 @@ pub struct Target {
pub linker: Option<PathBuf>,
pub ndk: Option<PathBuf>,
pub sanitizers: Option<bool>,
- pub profiler: Option<bool>,
+ pub profiler: Option<StringOrBool>,
pub rpath: Option<bool>,
pub crt_static: Option<bool>,
pub musl_root: Option<PathBuf>,
@@ -796,9 +796,9 @@ struct Dist {
}
}
-#[derive(Debug, Deserialize)]
+#[derive(Clone, Debug, Deserialize)]
#[serde(untagged)]
-enum StringOrBool {
+pub enum StringOrBool {
String(String),
Bool(bool),
}
@@ -809,6 +809,12 @@ fn default() -> StringOrBool {
}
}
+impl StringOrBool {
+ fn is_string_or_true(&self) -> bool {
+ matches!(self, Self::String(_) | Self::Bool(true))
+ }
+}
+
define_config! {
/// TOML representation of how the Rust build is configured.
struct Rust {
@@ -880,7 +886,7 @@ struct TomlTarget {
llvm_libunwind: Option<String> = "llvm-libunwind",
android_ndk: Option<String> = "android-ndk",
sanitizers: Option<bool> = "sanitizers",
- profiler: Option<bool> = "profiler",
+ profiler: Option<StringOrBool> = "profiler",
rpath: Option<bool> = "rpath",
crt_static: Option<bool> = "crt-static",
musl_root: Option<String> = "musl-root",
@@ -1744,12 +1750,24 @@ pub fn any_sanitizers_enabled(&self) -> bool {
self.target_config.values().any(|t| t.sanitizers == Some(true)) || self.sanitizers
}
+ pub fn profiler_path(&self, target: TargetSelection) -> Option<&str> {
+ match self.target_config.get(&target)?.profiler.as_ref()? {
+ StringOrBool::String(s) => Some(s),
+ StringOrBool::Bool(_) => None,
+ }
+ }
+
pub fn profiler_enabled(&self, target: TargetSelection) -> bool {
- self.target_config.get(&target).map(|t| t.profiler).flatten().unwrap_or(self.profiler)
+ self.target_config
+ .get(&target)
+ .and_then(|t| t.profiler.as_ref())
+ .map(StringOrBool::is_string_or_true)
+ .unwrap_or(self.profiler)
}
pub fn any_profiler_enabled(&self) -> bool {
- self.target_config.values().any(|t| t.profiler == Some(true)) || self.profiler
+ self.target_config.values().any(|t| matches!(&t.profiler, Some(p) if p.is_string_or_true()))
+ || self.profiler
}
pub fn rpath_enabled(&self, target: TargetSelection) -> bool {
--
2.41.0

View File

@ -0,0 +1,185 @@
From abb7c31ab038f38e33057062ae8b66b4e3cd699c Mon Sep 17 00:00:00 2001
From: Guillaume Gomez <guillaume1.gomez@gmail.com>
Date: Thu, 15 Jun 2023 22:04:55 +0200
Subject: [PATCH] Rollup merge of #112517 - fee1-dead-contrib:sus-op-no-borrow,
r=compiler-errors
`suspicious_double_ref_op`: don't lint on `.borrow()`
closes #112489
(cherry picked from commit db7d8374c1b6f1e2e8297f43e6a2cbffeff21882)
---
compiler/rustc_lint/messages.ftl | 12 ++--
compiler/rustc_lint/src/lints.rs | 12 ++--
compiler/rustc_lint/src/noop_method_call.rs | 62 +++++++++++----------
tests/ui/lint/issue-112489.rs | 17 ++++++
4 files changed, 64 insertions(+), 39 deletions(-)
create mode 100644 tests/ui/lint/issue-112489.rs
diff --git a/compiler/rustc_lint/messages.ftl b/compiler/rustc_lint/messages.ftl
index d34a3afcba53..0fa67cdb391f 100644
--- a/compiler/rustc_lint/messages.ftl
+++ b/compiler/rustc_lint/messages.ftl
@@ -463,13 +463,11 @@ lint_requested_level = requested on the command line with `{$level} {$lint_name}
lint_supertrait_as_deref_target = `{$t}` implements `Deref` with supertrait `{$target_principal}` as target
.label = target type is set here
-lint_suspicious_double_ref_op =
- using `.{$call}()` on a double reference, which returns `{$ty}` instead of {$op ->
- *[should_not_happen] [{$op}]
- [deref] dereferencing
- [borrow] borrowing
- [clone] cloning
- } the inner type
+lint_suspicious_double_ref_clone =
+ using `.clone()` on a double reference, which returns `{$ty}` instead of cloning the inner type
+
+lint_suspicious_double_ref_deref =
+ using `.deref()` on a double reference, which returns `{$ty}` instead of dereferencing the inner type
lint_trivial_untranslatable_diag = diagnostic with static strings only
diff --git a/compiler/rustc_lint/src/lints.rs b/compiler/rustc_lint/src/lints.rs
index de1c2be28757..d96723a68eb6 100644
--- a/compiler/rustc_lint/src/lints.rs
+++ b/compiler/rustc_lint/src/lints.rs
@@ -1188,11 +1188,15 @@ pub struct NoopMethodCallDiag<'a> {
}
#[derive(LintDiagnostic)]
-#[diag(lint_suspicious_double_ref_op)]
-pub struct SuspiciousDoubleRefDiag<'a> {
- pub call: Symbol,
+#[diag(lint_suspicious_double_ref_deref)]
+pub struct SuspiciousDoubleRefDerefDiag<'a> {
+ pub ty: Ty<'a>,
+}
+
+#[derive(LintDiagnostic)]
+#[diag(lint_suspicious_double_ref_clone)]
+pub struct SuspiciousDoubleRefCloneDiag<'a> {
pub ty: Ty<'a>,
- pub op: &'static str,
}
// pass_by_value.rs
diff --git a/compiler/rustc_lint/src/noop_method_call.rs b/compiler/rustc_lint/src/noop_method_call.rs
index d054966459d8..d56c35bb677a 100644
--- a/compiler/rustc_lint/src/noop_method_call.rs
+++ b/compiler/rustc_lint/src/noop_method_call.rs
@@ -1,5 +1,7 @@
use crate::context::LintContext;
-use crate::lints::{NoopMethodCallDiag, SuspiciousDoubleRefDiag};
+use crate::lints::{
+ NoopMethodCallDiag, SuspiciousDoubleRefCloneDiag, SuspiciousDoubleRefDerefDiag,
+};
use crate::LateContext;
use crate::LateLintPass;
use rustc_hir::def::DefKind;
@@ -76,22 +78,22 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
// We only care about method calls corresponding to the `Clone`, `Deref` and `Borrow`
// traits and ignore any other method call.
- let did = match cx.typeck_results().type_dependent_def(expr.hir_id) {
- // Verify we are dealing with a method/associated function.
- Some((DefKind::AssocFn, did)) => match cx.tcx.trait_of_item(did) {
- // Check that we're dealing with a trait method for one of the traits we care about.
- Some(trait_id)
- if matches!(
- cx.tcx.get_diagnostic_name(trait_id),
- Some(sym::Borrow | sym::Clone | sym::Deref)
- ) =>
- {
- did
- }
- _ => return,
- },
- _ => return,
+
+ let Some((DefKind::AssocFn, did)) =
+ cx.typeck_results().type_dependent_def(expr.hir_id)
+ else {
+ return;
+ };
+
+ let Some(trait_id) = cx.tcx.trait_of_item(did) else { return };
+
+ if !matches!(
+ cx.tcx.get_diagnostic_name(trait_id),
+ Some(sym::Borrow | sym::Clone | sym::Deref)
+ ) {
+ return;
};
+
let substs = cx
.tcx
.normalize_erasing_regions(cx.param_env, cx.typeck_results().node_substs(expr.hir_id));
@@ -102,13 +104,6 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
// (Re)check that it implements the noop diagnostic.
let Some(name) = cx.tcx.get_diagnostic_name(i.def_id()) else { return };
- let op = match name {
- sym::noop_method_borrow => "borrow",
- sym::noop_method_clone => "clone",
- sym::noop_method_deref => "deref",
- _ => return,
- };
-
let receiver_ty = cx.typeck_results().expr_ty(receiver);
let expr_ty = cx.typeck_results().expr_ty_adjusted(expr);
let arg_adjustments = cx.typeck_results().expr_adjustments(receiver);
@@ -129,11 +124,22 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
NoopMethodCallDiag { method: call.ident.name, receiver_ty, label: span },
);
} else {
- cx.emit_spanned_lint(
- SUSPICIOUS_DOUBLE_REF_OP,
- span,
- SuspiciousDoubleRefDiag { call: call.ident.name, ty: expr_ty, op },
- )
+ match name {
+ // If `type_of(x) == T` and `x.borrow()` is used to get `&T`,
+ // then that should be allowed
+ sym::noop_method_borrow => return,
+ sym::noop_method_clone => cx.emit_spanned_lint(
+ SUSPICIOUS_DOUBLE_REF_OP,
+ span,
+ SuspiciousDoubleRefCloneDiag { ty: expr_ty },
+ ),
+ sym::noop_method_deref => cx.emit_spanned_lint(
+ SUSPICIOUS_DOUBLE_REF_OP,
+ span,
+ SuspiciousDoubleRefDerefDiag { ty: expr_ty },
+ ),
+ _ => return,
+ }
}
}
}
diff --git a/tests/ui/lint/issue-112489.rs b/tests/ui/lint/issue-112489.rs
new file mode 100644
index 000000000000..559edf0e4f23
--- /dev/null
+++ b/tests/ui/lint/issue-112489.rs
@@ -0,0 +1,17 @@
+// check-pass
+use std::borrow::Borrow;
+
+struct S;
+
+trait T: Sized {
+ fn foo(self) {}
+}
+
+impl T for S {}
+impl T for &S {}
+
+fn main() {
+ let s = S;
+ s.borrow().foo();
+ s.foo();
+}
--
2.41.0

View File

@ -88,7 +88,7 @@
Name: rust
Version: 1.71.0
Release: 1%{?dist}
Release: 2%{?dist}
Summary: The Rust Programming Language
License: (ASL 2.0 or MIT) and (BSD and MIT)
# ^ written as: (rust itself) and (bundled libraries)
@ -122,6 +122,14 @@ Patch4: 0001-Revert-Fix-x-test-lint-docs-when-download-rustc-is-e.patch
# https://github.com/rust-lang/rust/pull/110906#issuecomment-1629832675
Patch5: 0001-Revert-fix-bug-etc-bash_complettion-src-etc-.-to-avo.patch
# (c9s) rhbz2225471: relax the suspicious_double_ref_op lint
# https://github.com/rust-lang/rust/pull/112517
Patch6: 0001-Rollup-merge-of-112517-fee1-dead-contrib-sus-op-no-b.patch
# Enable the profiler runtime for native hosts
# https://github.com/rust-lang/rust/pull/114069
Patch7: 0001-Allow-using-external-builds-of-the-compiler-rt-profi.patch
### RHEL-specific patches below ###
# Simple rpm macros for rust-toolset (as opposed to full rust-packaging)
@ -335,6 +343,9 @@ find '%{buildroot}%{rustlibdir}'/wasm*/lib -type f -regex '.*\\.\\(a\\|rlib\\)'
%{nil}
%endif
# For profiler_builtins
BuildRequires: compiler-rt
# This component was removed as of Rust 1.69.0.
# https://github.com/rust-lang/rust/pull/101841
Obsoletes: %{name}-analysis < 1.69.0~
@ -594,6 +605,8 @@ test -f '%{local_rust_root}/bin/rustc'
%patch -P3 -p1
%patch -P4 -p1
%patch -P5 -p1
%patch -P6 -p1
%patch -P7 -p1
%if %with disabled_libssh2
%patch -P100 -p1
@ -749,6 +762,10 @@ end}
end}
%endif
# The exact profiler path is version dependent, and uses LLVM-specific
# arch names in the filename, but this find is good enough for now...
PROFILER=$(find %{_libdir}/clang -type f -name 'libclang_rt.profile-*.a')
%configure --disable-option-checking \
--libdir=%{common_libdir} \
--build=%{rust_triple} --host=%{rust_triple} --target=%{rust_triple} \
@ -757,6 +774,7 @@ end}
--set target.%{rust_triple}.cxx=%{__cxx} \
--set target.%{rust_triple}.ar=%{__ar} \
--set target.%{rust_triple}.ranlib=%{__ranlib} \
${PROFILER:+--set target.%{rust_triple}.profiler="$PROFILER"} \
%{?mingw_target_config} \
%{?wasm_target_config} \
--python=%{__python3} \
@ -1068,6 +1086,10 @@ end}
%changelog
* Wed Jul 26 2023 Josh Stone <jistone@redhat.com> - 1.71.0-2
- Relax the suspicious_double_ref_op lint (rhbz2225471)
- Enable the profiler runtime for native hosts (rhbz2213875)
* Thu Jul 20 2023 Josh Stone <jistone@redhat.com> - 1.71.0-1
- Update to 1.71.0.