From 417460027e544b7f034c4d79678b5088279eeb7b Mon Sep 17 00:00:00 2001 From: bohan Date: Wed, 8 May 2024 20:24:18 +0800 Subject: [PATCH 1/7] place explicit lifetime bound after generic param --- .../src/infer/error_reporting/mod.rs | 18 ++- .../static-lifetime-tip-with-default-type.rs | 26 +++++ ...atic-lifetime-tip-with-default-type.stderr | 110 ++++++++++++++++++ 3 files changed, 151 insertions(+), 3 deletions(-) create mode 100644 tests/ui/generic-associated-types/static-lifetime-tip-with-default-type.rs create mode 100644 tests/ui/generic-associated-types/static-lifetime-tip-with-default-type.stderr diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index e0894ed31bfc3..f7ca55ccc55ae 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -63,11 +63,11 @@ use rustc_errors::{ codes::*, pluralize, struct_span_code_err, Applicability, Diag, DiagCtxt, DiagStyledString, ErrorGuaranteed, IntoDiagArg, StringPart, }; -use rustc_hir as hir; use rustc_hir::def::DefKind; use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::intravisit::Visitor; use rustc_hir::lang_items::LangItem; +use rustc_hir::{self as hir, ParamName}; use rustc_macros::extension; use rustc_middle::bug; use rustc_middle::dep_graph::DepContext; @@ -2379,7 +2379,8 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { let (type_scope, type_param_sugg_span) = match bound_kind { GenericKind::Param(param) => { let generics = self.tcx.generics_of(generic_param_scope); - let def_id = generics.type_param(param, self.tcx).def_id.expect_local(); + let type_param = generics.type_param(param, self.tcx); + let def_id = type_param.def_id.expect_local(); let scope = self.tcx.local_def_id_to_hir_id(def_id).owner.def_id; // Get the `hir::Param` to verify whether it already has any bounds. // We do this to avoid suggesting code that ends up as `T: 'a'b`, @@ -2389,7 +2390,18 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> { Some((span, open_paren_sp)) => Some((span, true, open_paren_sp)), // If `param` corresponds to `Self`, no usable suggestion span. None if generics.has_self && param.index == 0 => None, - None => Some((self.tcx.def_span(def_id).shrink_to_hi(), false, None)), + None => { + let span = if let Some(param) = + hir_generics.params.iter().find(|param| param.def_id == def_id) + && let ParamName::Plain(ident) = param.name + { + ident.span.shrink_to_hi() + } else { + let span = self.tcx.def_span(def_id); + span.shrink_to_hi() + }; + Some((span, false, None)) + } }; (scope, sugg_span) } diff --git a/tests/ui/generic-associated-types/static-lifetime-tip-with-default-type.rs b/tests/ui/generic-associated-types/static-lifetime-tip-with-default-type.rs new file mode 100644 index 0000000000000..68879a0af292a --- /dev/null +++ b/tests/ui/generic-associated-types/static-lifetime-tip-with-default-type.rs @@ -0,0 +1,26 @@ +//@ error-pattern: r#T: 'static +//@ error-pattern: r#K: 'static +//@ error-pattern: T: 'static= + +// https://github.com/rust-lang/rust/issues/124785 + +struct Foo(&'static T, &'static K); +//~^ ERROR: the parameter type `T` may not live long enough +//~| ERROR: the parameter type `K` may not live long enough + +struct Bar(&'static r#T, &'static r#K); +//~^ ERROR: the parameter type `T` may not live long enough +//~| ERROR: the parameter type `K` may not live long enough + +struct Boo(&'static T); +//~^ ERROR: the parameter type `T` may not live long enough + +struct Far(&'static T); +//~^ ERROR: the parameter type `T` may not live long enough + +struct S<'a, K: 'a = i32>(&'static K); +//~^ ERROR: lifetime parameter `'a` is never used +//~| ERROR: the parameter type `K` may not live long enough + +fn main() {} diff --git a/tests/ui/generic-associated-types/static-lifetime-tip-with-default-type.stderr b/tests/ui/generic-associated-types/static-lifetime-tip-with-default-type.stderr new file mode 100644 index 0000000000000..7d985a9013f19 --- /dev/null +++ b/tests/ui/generic-associated-types/static-lifetime-tip-with-default-type.stderr @@ -0,0 +1,110 @@ +error[E0310]: the parameter type `T` may not live long enough + --> $DIR/static-lifetime-tip-with-default-type.rs:7:24 + | +LL | struct Foo(&'static T, &'static K); + | ^^^^^^^^^^ + | | + | the parameter type `T` must be valid for the static lifetime... + | ...so that the reference type `&'static T` does not outlive the data it points at + | +help: consider adding an explicit lifetime bound + | +LL | struct Foo(&'static T, &'static K); + | +++++++++ + +error[E0310]: the parameter type `K` may not live long enough + --> $DIR/static-lifetime-tip-with-default-type.rs:7:36 + | +LL | struct Foo(&'static T, &'static K); + | ^^^^^^^^^^ + | | + | the parameter type `K` must be valid for the static lifetime... + | ...so that the reference type `&'static K` does not outlive the data it points at + | +help: consider adding an explicit lifetime bound + | +LL | struct Foo(&'static T, &'static K); + | +++++++++ + +error[E0310]: the parameter type `T` may not live long enough + --> $DIR/static-lifetime-tip-with-default-type.rs:11:28 + | +LL | struct Bar(&'static r#T, &'static r#K); + | ^^^^^^^^^^^^ + | | + | the parameter type `T` must be valid for the static lifetime... + | ...so that the reference type `&'static T` does not outlive the data it points at + | +help: consider adding an explicit lifetime bound + | +LL | struct Bar(&'static r#T, &'static r#K); + | +++++++++ + +error[E0310]: the parameter type `K` may not live long enough + --> $DIR/static-lifetime-tip-with-default-type.rs:11:42 + | +LL | struct Bar(&'static r#T, &'static r#K); + | ^^^^^^^^^^^^ + | | + | the parameter type `K` must be valid for the static lifetime... + | ...so that the reference type `&'static K` does not outlive the data it points at + | +help: consider adding an explicit lifetime bound + | +LL | struct Bar(&'static r#T, &'static r#K); + | +++++++++ + +error[E0310]: the parameter type `T` may not live long enough + --> $DIR/static-lifetime-tip-with-default-type.rs:15:20 + | +LL | struct Boo(&'static T); + | ^^^^^^^^^^ + | | + | the parameter type `T` must be valid for the static lifetime... + | ...so that the reference type `&'static T` does not outlive the data it points at + | +help: consider adding an explicit lifetime bound + | +LL | struct Boo(&'static T); + | +++++++++ + +error[E0310]: the parameter type `T` may not live long enough + --> $DIR/static-lifetime-tip-with-default-type.rs:19:8 + | +LL | = i32>(&'static T); + | ^^^^^^^^^^ + | | + | the parameter type `T` must be valid for the static lifetime... + | ...so that the reference type `&'static T` does not outlive the data it points at + | +help: consider adding an explicit lifetime bound + | +LL | struct Far $DIR/static-lifetime-tip-with-default-type.rs:22:27 + | +LL | struct S<'a, K: 'a = i32>(&'static K); + | ^^^^^^^^^^ + | | + | the parameter type `K` must be valid for the static lifetime... + | ...so that the reference type `&'static K` does not outlive the data it points at + | +help: consider adding an explicit lifetime bound + | +LL | struct S<'a, K: 'a + 'static = i32>(&'static K); + | +++++++++ + +error[E0392]: lifetime parameter `'a` is never used + --> $DIR/static-lifetime-tip-with-default-type.rs:22:10 + | +LL | struct S<'a, K: 'a = i32>(&'static K); + | ^^ unused lifetime parameter + | + = help: consider removing `'a`, referring to it in a field, or using a marker such as `PhantomData` + +error: aborting due to 8 previous errors + +Some errors have detailed explanations: E0310, E0392. +For more information about an error, try `rustc --explain E0310`. From 48d342536421b6806dcfe3c0c76718249dbea6bc Mon Sep 17 00:00:00 2001 From: Chris Denton Date: Wed, 12 Jun 2024 17:33:52 +0000 Subject: [PATCH 2/7] Remove some msys2 utils --- .github/workflows/ci.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 521f8ef0f5ae5..4cf0e5fba5378 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -95,8 +95,6 @@ jobs: path-type: inherit install: > make - dos2unix - diffutils - name: disable git crlf conversion run: git config --global core.autocrlf false From c81ffab3ec66342d542ef7de39e1ec081027f43d Mon Sep 17 00:00:00 2001 From: David Carlier Date: Wed, 12 Jun 2024 20:49:20 +0000 Subject: [PATCH 3/7] std::unix::fs::link using direct linkat call for Solaris and macOs. Since we support solaris 11 and macOs Sierra as minimum, we can get rid of the runtime overhead. --- library/std/src/sys/pal/unix/fs.rs | 21 ++------------------- 1 file changed, 2 insertions(+), 19 deletions(-) diff --git a/library/std/src/sys/pal/unix/fs.rs b/library/std/src/sys/pal/unix/fs.rs index 56a0f8e39c4aa..035c92bc84bfe 100644 --- a/library/std/src/sys/pal/unix/fs.rs +++ b/library/std/src/sys/pal/unix/fs.rs @@ -22,16 +22,12 @@ use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner}; #[cfg(any(all(target_os = "linux", target_env = "gnu"), target_vendor = "apple"))] use crate::sys::weak::syscall; -#[cfg(any(target_os = "android", target_os = "macos", target_os = "solaris"))] +#[cfg(target_os = "android")] use crate::sys::weak::weak; use libc::{c_int, mode_t}; -#[cfg(any( - target_os = "solaris", - all(target_os = "linux", target_env = "gnu"), - target_vendor = "apple", -))] +#[cfg(any(all(target_os = "linux", target_env = "gnu"), target_vendor = "apple"))] use libc::c_char; #[cfg(any( all(target_os = "linux", not(target_env = "musl")), @@ -1753,19 +1749,6 @@ pub fn link(original: &Path, link: &Path) -> io::Result<()> { // Android has `linkat` on newer versions, but we happen to know `link` // always has the correct behavior, so it's here as well. cvt(unsafe { libc::link(original.as_ptr(), link.as_ptr()) })?; - } else if #[cfg(any(target_os = "macos", target_os = "solaris"))] { - // MacOS (<=10.9) and Solaris 10 lack support for linkat while newer - // versions have it. We want to use linkat if it is available, so we use weak! - // to check. `linkat` is preferable to `link` because it gives us a flag to - // specify how symlinks should be handled. We pass 0 as the flags argument, - // meaning it shouldn't follow symlinks. - weak!(fn linkat(c_int, *const c_char, c_int, *const c_char, c_int) -> c_int); - - if let Some(f) = linkat.get() { - cvt(unsafe { f(libc::AT_FDCWD, original.as_ptr(), libc::AT_FDCWD, link.as_ptr(), 0) })?; - } else { - cvt(unsafe { libc::link(original.as_ptr(), link.as_ptr()) })?; - }; } else { // Where we can, use `linkat` instead of `link`; see the comment above // this one for details on why. From 12432130a37e1637690df3dc70c31ecb866e3183 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Thu, 13 Jun 2024 11:42:52 +1000 Subject: [PATCH 4/7] Remove some unnecessary crate dependencies. --- Cargo.lock | 1 - compiler/rustc_data_structures/Cargo.toml | 6 +++++- compiler/rustc_infer/Cargo.toml | 1 - 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e5ea9fee8a6d8..10ca6b0b4bd8e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4205,7 +4205,6 @@ dependencies = [ "rustc_middle", "rustc_span", "rustc_target", - "rustc_type_ir", "smallvec", "tracing", ] diff --git a/compiler/rustc_data_structures/Cargo.toml b/compiler/rustc_data_structures/Cargo.toml index 6876046a58380..ff0a94f8e9b27 100644 --- a/compiler/rustc_data_structures/Cargo.toml +++ b/compiler/rustc_data_structures/Cargo.toml @@ -12,7 +12,6 @@ elsa = "=1.7.1" ena = "0.14.3" indexmap = { version = "2.0.0" } jobserver_crate = { version = "0.1.28", package = "jobserver" } -libc = "0.2" measureme = "11" rustc-hash = "1.1.0" rustc-rayon = { version = "0.5.0", optional = true } @@ -41,6 +40,11 @@ features = [ "Win32_System_Threading", ] +[target.'cfg(unix)'.dependencies] +# tidy-alphabetical-start +libc = "0.2" +# tidy-alphabetical-end + [target.'cfg(not(target_arch = "wasm32"))'.dependencies] # tidy-alphabetical-start memmap2 = "0.2.1" diff --git a/compiler/rustc_infer/Cargo.toml b/compiler/rustc_infer/Cargo.toml index 5136ab79a0f84..c1565a7d40fd5 100644 --- a/compiler/rustc_infer/Cargo.toml +++ b/compiler/rustc_infer/Cargo.toml @@ -18,7 +18,6 @@ rustc_macros = { path = "../rustc_macros" } rustc_middle = { path = "../rustc_middle" } rustc_span = { path = "../rustc_span" } rustc_target = { path = "../rustc_target" } -rustc_type_ir = { path = "../rustc_type_ir" } smallvec = { version = "1.8.1", features = ["union", "may_dangle"] } tracing = "0.1" # tidy-alphabetical-end From 9314831b456b3f17c0849173436531d7d92b2231 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 13 Jun 2024 11:30:23 +0200 Subject: [PATCH 5/7] Implement `CompletedProcess::assert_stdout_contains` and improve error output --- src/tools/run-make-support/src/command.rs | 10 ++++++++-- src/tools/run-make-support/src/lib.rs | 12 ++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/tools/run-make-support/src/command.rs b/src/tools/run-make-support/src/command.rs index d689dc2f979a7..5ee58e4e7a951 100644 --- a/src/tools/run-make-support/src/command.rs +++ b/src/tools/run-make-support/src/command.rs @@ -6,7 +6,7 @@ use std::path::Path; use std::process::{Command as StdCommand, ExitStatus, Output, Stdio}; use crate::drop_bomb::DropBomb; -use crate::{assert_not_contains, handle_failed_output}; +use crate::{assert_contains, assert_not_contains, handle_failed_output}; /// This is a custom command wrapper that simplifies working with commands and makes it easier to /// ensure that we check the exit status of executed processes. @@ -167,6 +167,12 @@ impl CompletedProcess { self } + #[track_caller] + pub fn assert_stdout_contains>(self, needle: S) -> Self { + assert_contains(&self.stdout_utf8(), needle.as_ref()); + self + } + #[track_caller] pub fn assert_stdout_not_contains>(&self, needle: S) -> &Self { assert_not_contains(&self.stdout_utf8(), needle.as_ref()); @@ -182,7 +188,7 @@ impl CompletedProcess { #[track_caller] pub fn assert_stderr_contains>(&self, needle: S) -> &Self { - assert!(self.stderr_utf8().contains(needle.as_ref())); + assert_contains(&self.stderr_utf8(), needle.as_ref()); self } diff --git a/src/tools/run-make-support/src/lib.rs b/src/tools/run-make-support/src/lib.rs index 0238255a53f3a..8bb6a8d51d0db 100644 --- a/src/tools/run-make-support/src/lib.rs +++ b/src/tools/run-make-support/src/lib.rs @@ -315,6 +315,18 @@ pub fn read_dir(dir: impl AsRef, callback: F) { } } +/// Check that `haystack` contains `needle`. Panic otherwise. +#[track_caller] +pub fn assert_contains(haystack: &str, needle: &str) { + if !haystack.contains(needle) { + eprintln!("=== HAYSTACK ==="); + eprintln!("{}", haystack); + eprintln!("=== NEEDLE ==="); + eprintln!("{}", needle); + panic!("needle was not found in haystack"); + } +} + /// Check that `haystack` does not contain `needle`. Panic otherwise. #[track_caller] pub fn assert_not_contains(haystack: &str, needle: &str) { From 96f9fe5488e74f8b0f75c993ffbcca2c14ba9d01 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 13 Jun 2024 11:21:25 +0200 Subject: [PATCH 6/7] Migrate `run-make/allow-non-lint-warnings-cmdline` to `rmake.rs` --- src/tools/tidy/src/allowed_run_make_makefiles.txt | 1 - .../allow-non-lint-warnings-cmdline/Makefile | 12 ------------ .../allow-non-lint-warnings-cmdline/rmake.rs | 8 ++++++++ 3 files changed, 8 insertions(+), 13 deletions(-) delete mode 100644 tests/run-make/allow-non-lint-warnings-cmdline/Makefile create mode 100644 tests/run-make/allow-non-lint-warnings-cmdline/rmake.rs diff --git a/src/tools/tidy/src/allowed_run_make_makefiles.txt b/src/tools/tidy/src/allowed_run_make_makefiles.txt index 37da5d9c88d90..280420d022d66 100644 --- a/src/tools/tidy/src/allowed_run_make_makefiles.txt +++ b/src/tools/tidy/src/allowed_run_make_makefiles.txt @@ -1,5 +1,4 @@ run-make/allocator-shim-circular-deps/Makefile -run-make/allow-non-lint-warnings-cmdline/Makefile run-make/archive-duplicate-names/Makefile run-make/atomic-lock-free/Makefile run-make/branch-protection-check-IBT/Makefile diff --git a/tests/run-make/allow-non-lint-warnings-cmdline/Makefile b/tests/run-make/allow-non-lint-warnings-cmdline/Makefile deleted file mode 100644 index 78b9a7b989895..0000000000000 --- a/tests/run-make/allow-non-lint-warnings-cmdline/Makefile +++ /dev/null @@ -1,12 +0,0 @@ -# ignore-cross-compile -include ../tools.mk - -# Test that -A warnings makes the 'empty trait list for derive' warning go away -OUT=$(shell $(RUSTC) foo.rs -A warnings 2>&1 | grep "warning" ) - -all: foo - test -z '$(OUT)' - -# This is just to make sure the above command actually succeeds -foo: - $(RUSTC) foo.rs -A warnings diff --git a/tests/run-make/allow-non-lint-warnings-cmdline/rmake.rs b/tests/run-make/allow-non-lint-warnings-cmdline/rmake.rs new file mode 100644 index 0000000000000..3866ed3f5be51 --- /dev/null +++ b/tests/run-make/allow-non-lint-warnings-cmdline/rmake.rs @@ -0,0 +1,8 @@ +// Test that -A warnings makes the 'empty trait list for derive' warning go away. + +use run_make_support::rustc; + +fn main() { + let output = rustc().input("foo.rs").arg("-Awarnings").run(); + output.assert_stderr_not_contains("warning"); +} From eca8d209d9c75ed0ba7ce434d62d9460d42271f5 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 13 Jun 2024 12:52:05 +0200 Subject: [PATCH 7/7] Make `run-make/allow-non-lint-warnings-cmdline` into a ui test --- tests/run-make/allow-non-lint-warnings-cmdline/rmake.rs | 8 -------- .../foo.rs => ui/allow-non-lint-warnings.rs} | 3 +++ 2 files changed, 3 insertions(+), 8 deletions(-) delete mode 100644 tests/run-make/allow-non-lint-warnings-cmdline/rmake.rs rename tests/{run-make/allow-non-lint-warnings-cmdline/foo.rs => ui/allow-non-lint-warnings.rs} (60%) diff --git a/tests/run-make/allow-non-lint-warnings-cmdline/rmake.rs b/tests/run-make/allow-non-lint-warnings-cmdline/rmake.rs deleted file mode 100644 index 3866ed3f5be51..0000000000000 --- a/tests/run-make/allow-non-lint-warnings-cmdline/rmake.rs +++ /dev/null @@ -1,8 +0,0 @@ -// Test that -A warnings makes the 'empty trait list for derive' warning go away. - -use run_make_support::rustc; - -fn main() { - let output = rustc().input("foo.rs").arg("-Awarnings").run(); - output.assert_stderr_not_contains("warning"); -} diff --git a/tests/run-make/allow-non-lint-warnings-cmdline/foo.rs b/tests/ui/allow-non-lint-warnings.rs similarity index 60% rename from tests/run-make/allow-non-lint-warnings-cmdline/foo.rs rename to tests/ui/allow-non-lint-warnings.rs index 02e8ccabf7921..f8f5a78ebff26 100644 --- a/tests/run-make/allow-non-lint-warnings-cmdline/foo.rs +++ b/tests/ui/allow-non-lint-warnings.rs @@ -1,3 +1,6 @@ +//@ compile-flags: -Awarnings +//@ check-pass + #[derive()] #[derive(Copy, Clone)] pub struct Foo;