From a0fcc8ebc0793afd66be06db3c338b0b4d43fe3c Mon Sep 17 00:00:00 2001 From: riverbl <94326797+riverbl@users.noreply.github.com> Date: Sun, 7 Jan 2024 13:58:39 +0000 Subject: [PATCH 01/16] Add `display` method to `OsStr` Add `display` method to `OsStr` for lossy display of an `OsStr` which may contain invalid unicode. Invalid Unicode sequences are replaced with `U+FFFD REPLACEMENT CHARACTER`. This change also makes the `std::ffi::os_str` module public. --- library/std/src/ffi/mod.rs | 4 +- library/std/src/ffi/os_str.rs | 67 +++++++++++++++++++++- library/std/src/path.rs | 10 ++-- tests/rustdoc-js-std/osstring-to-string.js | 2 +- 4 files changed, 73 insertions(+), 10 deletions(-) diff --git a/library/std/src/ffi/mod.rs b/library/std/src/ffi/mod.rs index 97e78d1778677..1622308d9f56b 100644 --- a/library/std/src/ffi/mod.rs +++ b/library/std/src/ffi/mod.rs @@ -162,6 +162,7 @@ pub use core::ffi::FromBytesUntilNulError; pub use core::ffi::{CStr, FromBytesWithNulError}; #[stable(feature = "rust1", since = "1.0.0")] +#[doc(inline)] pub use self::os_str::{OsStr, OsString}; #[stable(feature = "core_ffi_c", since = "1.64.0")] @@ -181,4 +182,5 @@ pub use core::ffi::c_void; )] pub use core::ffi::{VaList, VaListImpl}; -mod os_str; +#[unstable(feature = "os_str_display", issue = "120048")] +pub mod os_str; diff --git a/library/std/src/ffi/os_str.rs b/library/std/src/ffi/os_str.rs index 81973182148ef..cd954f9e802f4 100644 --- a/library/std/src/ffi/os_str.rs +++ b/library/std/src/ffi/os_str.rs @@ -1,3 +1,5 @@ +//! The [`OsStr`] and [`OsString`] types and associated utilities. + #[cfg(test)] mod tests; @@ -1173,6 +1175,32 @@ impl OsStr { pub fn eq_ignore_ascii_case>(&self, other: S) -> bool { self.inner.eq_ignore_ascii_case(&other.as_ref().inner) } + + /// Returns an object that implements [`Display`] for safely printing an + /// [`OsStr`] that may contain non-Unicode data. This may perform lossy + /// conversion, depending on the platform. If you would like an + /// implementation which escapes the [`OsStr`] please use [`Debug`] + /// instead. + /// + /// [`Display`]: fmt::Display + /// [`Debug`]: fmt::Debug + /// + /// # Examples + /// + /// ``` + /// #![feature(os_str_display)] + /// use std::ffi::OsStr; + /// + /// let s = OsStr::new("Hello, world!"); + /// println!("{}", s.display()); + /// ``` + #[unstable(feature = "os_str_display", issue = "120048")] + #[must_use = "this does not display the `OsStr`; \ + it returns an object that can be displayed"] + #[inline] + pub fn display(&self) -> Display<'_> { + Display { os_str: self } + } } #[stable(feature = "box_from_os_str", since = "1.17.0")] @@ -1467,9 +1495,42 @@ impl fmt::Debug for OsStr { } } -impl OsStr { - pub(crate) fn display(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Display::fmt(&self.inner, formatter) +/// Helper struct for safely printing an [`OsStr`] with [`format!`] and `{}`. +/// +/// An [`OsStr`] might contain non-Unicode data. This `struct` implements the +/// [`Display`] trait in a way that mitigates that. It is created by the +/// [`display`](OsStr::display) method on [`OsStr`]. This may perform lossy +/// conversion, depending on the platform. If you would like an implementation +/// which escapes the [`OsStr`] please use [`Debug`] instead. +/// +/// # Examples +/// +/// ``` +/// #![feature(os_str_display)] +/// use std::ffi::OsStr; +/// +/// let s = OsStr::new("Hello, world!"); +/// println!("{}", s.display()); +/// ``` +/// +/// [`Display`]: fmt::Display +/// [`format!`]: crate::format +#[unstable(feature = "os_str_display", issue = "120048")] +pub struct Display<'a> { + os_str: &'a OsStr, +} + +#[unstable(feature = "os_str_display", issue = "120048")] +impl fmt::Debug for Display<'_> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Debug::fmt(&self.os_str, f) + } +} + +#[unstable(feature = "os_str_display", issue = "120048")] +impl fmt::Display for Display<'_> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Display::fmt(&self.os_str.inner, f) } } diff --git a/library/std/src/path.rs b/library/std/src/path.rs index 60562f64c90d9..20cac9f2a3c22 100644 --- a/library/std/src/path.rs +++ b/library/std/src/path.rs @@ -84,7 +84,7 @@ use crate::rc::Rc; use crate::str::FromStr; use crate::sync::Arc; -use crate::ffi::{OsStr, OsString}; +use crate::ffi::{os_str, OsStr, OsString}; use crate::sys; use crate::sys::path::{is_sep_byte, is_verbatim_sep, parse_prefix, MAIN_SEP_STR}; @@ -2725,7 +2725,7 @@ impl Path { it returns an object that can be displayed"] #[inline] pub fn display(&self) -> Display<'_> { - Display { path: self } + Display { inner: self.inner.display() } } /// Queries the file system to get information about a file, directory, etc. @@ -3032,20 +3032,20 @@ impl fmt::Debug for Path { /// [`format!`]: crate::format #[stable(feature = "rust1", since = "1.0.0")] pub struct Display<'a> { - path: &'a Path, + inner: os_str::Display<'a>, } #[stable(feature = "rust1", since = "1.0.0")] impl fmt::Debug for Display<'_> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Debug::fmt(&self.path, f) + fmt::Debug::fmt(&self.inner, f) } } #[stable(feature = "rust1", since = "1.0.0")] impl fmt::Display for Display<'_> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - self.path.inner.display(f) + fmt::Display::fmt(&self.inner, f) } } diff --git a/tests/rustdoc-js-std/osstring-to-string.js b/tests/rustdoc-js-std/osstring-to-string.js index 17bb602a502af..3fdc0b9f24a3c 100644 --- a/tests/rustdoc-js-std/osstring-to-string.js +++ b/tests/rustdoc-js-std/osstring-to-string.js @@ -4,6 +4,6 @@ const EXPECTED = { 'query': 'OsString -> String', 'others': [ - { 'path': 'std::ffi::OsString', 'name': 'into_string' }, + { 'path': 'std::ffi::os_str::OsString', 'name': 'into_string' }, ] }; From e060274e55a5b09fe6ace6dfe72ca0cca663b711 Mon Sep 17 00:00:00 2001 From: Martin Nordholts Date: Fri, 9 Feb 2024 05:48:24 +0100 Subject: [PATCH 02/16] tests: Fix typo unix_sigpipe-error.rs -> unix_sigpipe-sig_ign.rs There is no error expected. It's simply the "regular" test for sig_ign. So rename it. --- .../{unix_sigpipe-error.rs => unix_sigpipe-sig_ign.rs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/ui/attributes/unix_sigpipe/{unix_sigpipe-error.rs => unix_sigpipe-sig_ign.rs} (100%) diff --git a/tests/ui/attributes/unix_sigpipe/unix_sigpipe-error.rs b/tests/ui/attributes/unix_sigpipe/unix_sigpipe-sig_ign.rs similarity index 100% rename from tests/ui/attributes/unix_sigpipe/unix_sigpipe-error.rs rename to tests/ui/attributes/unix_sigpipe/unix_sigpipe-sig_ign.rs From a1cb3dba840fd56a7f9a0c90346a1fcddc641f9c Mon Sep 17 00:00:00 2001 From: Martin Nordholts Date: Fri, 9 Feb 2024 06:44:56 +0100 Subject: [PATCH 03/16] tests: Rename unix_sigpipe.rs to unix_sigpipe-bare.rs for clarity The test is for the "bare" variant of the attribute that looks like this: #[unix_sigpipe] which is not allowed, because it must look like this: #[unix_sigpipe = "sig_ign"] --- .../unix_sigpipe/{unix_sigpipe.rs => unix_sigpipe-bare.rs} | 0 .../{unix_sigpipe.stderr => unix_sigpipe-bare.stderr} | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename tests/ui/attributes/unix_sigpipe/{unix_sigpipe.rs => unix_sigpipe-bare.rs} (100%) rename tests/ui/attributes/unix_sigpipe/{unix_sigpipe.stderr => unix_sigpipe-bare.stderr} (83%) diff --git a/tests/ui/attributes/unix_sigpipe/unix_sigpipe.rs b/tests/ui/attributes/unix_sigpipe/unix_sigpipe-bare.rs similarity index 100% rename from tests/ui/attributes/unix_sigpipe/unix_sigpipe.rs rename to tests/ui/attributes/unix_sigpipe/unix_sigpipe-bare.rs diff --git a/tests/ui/attributes/unix_sigpipe/unix_sigpipe.stderr b/tests/ui/attributes/unix_sigpipe/unix_sigpipe-bare.stderr similarity index 83% rename from tests/ui/attributes/unix_sigpipe/unix_sigpipe.stderr rename to tests/ui/attributes/unix_sigpipe/unix_sigpipe-bare.stderr index b18ec9abc3745..56218ed499ece 100644 --- a/tests/ui/attributes/unix_sigpipe/unix_sigpipe.stderr +++ b/tests/ui/attributes/unix_sigpipe/unix_sigpipe-bare.stderr @@ -1,5 +1,5 @@ error: valid values for `#[unix_sigpipe = "..."]` are `inherit`, `sig_ign`, or `sig_dfl` - --> $DIR/unix_sigpipe.rs:3:1 + --> $DIR/unix_sigpipe-bare.rs:3:1 | LL | #[unix_sigpipe] | ^^^^^^^^^^^^^^^ From d14f15862d2ac7111f70efe82882fb3575167a53 Mon Sep 17 00:00:00 2001 From: Martin Nordholts Date: Fri, 9 Feb 2024 08:47:47 +0100 Subject: [PATCH 04/16] tests: Combine unix_sigpipe-not-used.rs and unix_sigpipe-only-feature.rs The only difference between the files is the presence/absence of #![feature(unix_sigpipe)] attribute. Avoid duplication by using revisions instead. --- .../unix_sigpipe/unix_sigpipe-not-used.rs | 3 +++ .../unix_sigpipe/unix_sigpipe-only-feature.rs | 13 ------------- 2 files changed, 3 insertions(+), 13 deletions(-) delete mode 100644 tests/ui/attributes/unix_sigpipe/unix_sigpipe-only-feature.rs diff --git a/tests/ui/attributes/unix_sigpipe/unix_sigpipe-not-used.rs b/tests/ui/attributes/unix_sigpipe/unix_sigpipe-not-used.rs index 778e06cb3effe..b0044f5e91999 100644 --- a/tests/ui/attributes/unix_sigpipe/unix_sigpipe-not-used.rs +++ b/tests/ui/attributes/unix_sigpipe/unix_sigpipe-not-used.rs @@ -1,6 +1,9 @@ +//@ revisions: with_feature without_feature //@ run-pass //@ aux-build:sigpipe-utils.rs +#![cfg_attr(with_feature, feature(unix_sigpipe))] + fn main() { extern crate sigpipe_utils; diff --git a/tests/ui/attributes/unix_sigpipe/unix_sigpipe-only-feature.rs b/tests/ui/attributes/unix_sigpipe/unix_sigpipe-only-feature.rs deleted file mode 100644 index 6bbe4a8d0d688..0000000000000 --- a/tests/ui/attributes/unix_sigpipe/unix_sigpipe-only-feature.rs +++ /dev/null @@ -1,13 +0,0 @@ -//@ run-pass -//@ aux-build:sigpipe-utils.rs - -#![feature(unix_sigpipe)] - -fn main() { - extern crate sigpipe_utils; - - // Only #![feature(unix_sigpipe)] is enabled, not #[unix_sigpipe = "..."]. - // This shall not change any behavior, so we still expect SIGPIPE to be - // ignored - sigpipe_utils::assert_sigpipe_handler(sigpipe_utils::SignalHandler::Ignore); -} From 948b1d68abdf6ccde608831401dfe35af4a8cb04 Mon Sep 17 00:00:00 2001 From: Martin Nordholts Date: Fri, 9 Feb 2024 07:57:27 +0100 Subject: [PATCH 05/16] tests: Add unix_sigpipe-different-duplicates.rs test variant To make sure that #[unix_sigpipe = "x"] #[unix_sigpipe = "y"] behaves like #[unix_sigpipe = "x"] #[unix_sigpipe = "x"] --- .../unix_sigpipe-different-duplicates.rs | 5 +++++ .../unix_sigpipe-different-duplicates.stderr | 14 ++++++++++++++ .../unix_sigpipe/unix_sigpipe-duplicates.rs | 2 +- .../unix_sigpipe/unix_sigpipe-duplicates.stderr | 2 +- 4 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 tests/ui/attributes/unix_sigpipe/unix_sigpipe-different-duplicates.rs create mode 100644 tests/ui/attributes/unix_sigpipe/unix_sigpipe-different-duplicates.stderr diff --git a/tests/ui/attributes/unix_sigpipe/unix_sigpipe-different-duplicates.rs b/tests/ui/attributes/unix_sigpipe/unix_sigpipe-different-duplicates.rs new file mode 100644 index 0000000000000..294cb38526bd0 --- /dev/null +++ b/tests/ui/attributes/unix_sigpipe/unix_sigpipe-different-duplicates.rs @@ -0,0 +1,5 @@ +#![feature(unix_sigpipe)] + +#[unix_sigpipe = "sig_ign"] +#[unix_sigpipe = "inherit"] //~ error: multiple `unix_sigpipe` attributes +fn main() {} diff --git a/tests/ui/attributes/unix_sigpipe/unix_sigpipe-different-duplicates.stderr b/tests/ui/attributes/unix_sigpipe/unix_sigpipe-different-duplicates.stderr new file mode 100644 index 0000000000000..c2a3b9f45f9ac --- /dev/null +++ b/tests/ui/attributes/unix_sigpipe/unix_sigpipe-different-duplicates.stderr @@ -0,0 +1,14 @@ +error: multiple `unix_sigpipe` attributes + --> $DIR/unix_sigpipe-different-duplicates.rs:4:1 + | +LL | #[unix_sigpipe = "inherit"] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove this attribute + | +note: attribute also specified here + --> $DIR/unix_sigpipe-different-duplicates.rs:3:1 + | +LL | #[unix_sigpipe = "sig_ign"] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 1 previous error + diff --git a/tests/ui/attributes/unix_sigpipe/unix_sigpipe-duplicates.rs b/tests/ui/attributes/unix_sigpipe/unix_sigpipe-duplicates.rs index 294cb38526bd0..eccb23021b6b8 100644 --- a/tests/ui/attributes/unix_sigpipe/unix_sigpipe-duplicates.rs +++ b/tests/ui/attributes/unix_sigpipe/unix_sigpipe-duplicates.rs @@ -1,5 +1,5 @@ #![feature(unix_sigpipe)] -#[unix_sigpipe = "sig_ign"] +#[unix_sigpipe = "inherit"] #[unix_sigpipe = "inherit"] //~ error: multiple `unix_sigpipe` attributes fn main() {} diff --git a/tests/ui/attributes/unix_sigpipe/unix_sigpipe-duplicates.stderr b/tests/ui/attributes/unix_sigpipe/unix_sigpipe-duplicates.stderr index 931aae96b0ff7..c86e54a1e532f 100644 --- a/tests/ui/attributes/unix_sigpipe/unix_sigpipe-duplicates.stderr +++ b/tests/ui/attributes/unix_sigpipe/unix_sigpipe-duplicates.stderr @@ -7,7 +7,7 @@ LL | #[unix_sigpipe = "inherit"] note: attribute also specified here --> $DIR/unix_sigpipe-duplicates.rs:3:1 | -LL | #[unix_sigpipe = "sig_ign"] +LL | #[unix_sigpipe = "inherit"] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: aborting due to 1 previous error From 0da1b71053e03ed0c49e9c589045bbb39b70d45d Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Sat, 24 Feb 2024 16:47:29 -0700 Subject: [PATCH 06/16] Add test case for primitive links in alias js --- .../type-alias/primitive-local-link-121106.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 tests/rustdoc/type-alias/primitive-local-link-121106.rs diff --git a/tests/rustdoc/type-alias/primitive-local-link-121106.rs b/tests/rustdoc/type-alias/primitive-local-link-121106.rs new file mode 100644 index 0000000000000..c11d35d86464d --- /dev/null +++ b/tests/rustdoc/type-alias/primitive-local-link-121106.rs @@ -0,0 +1,19 @@ +#![crate_name = "foo"] + +#![feature(rustc_attrs)] + +// @has foo/primitive.i32.html '//h1' 'Primitive Type i32' +// @has foo/index.html '//a/@href' '../foo/index.html' +#[rustc_doc_primitive = "i32"] +mod i32 {} + +// @has foo/struct.Node.html '//a/@href' 'primitive.i32.html' +pub struct Node; + +impl Node { + pub fn edge(&self) -> i32 { 0 } +} + +// @!has foo/type.Alias.html '//a/@href' 'primitive.i32.html' +// @hasraw 'type.impl/foo/struct.Node.js' 'href=\"foo/primitive.i32.html\"' +pub type Alias = Node; From 3bcef2dc1b145ebe32d317e331579fd4c20e0bd8 Mon Sep 17 00:00:00 2001 From: yukang Date: Sat, 17 Feb 2024 17:54:06 +0800 Subject: [PATCH 07/16] Fix issues in suggesting importing extern crate paths --- compiler/rustc_resolve/src/diagnostics.rs | 12 +++++++++++- .../import-alias-issue-121168-extern.rs | 1 + .../import-alias-issue-121168.edition2015.stderr | 14 ++++++++++++++ .../import-alias-issue-121168.edition2018.stderr | 16 ++++++++++++++++ .../import-alias-issue-121168.edition2021.stderr | 16 ++++++++++++++++ tests/ui/imports/import-alias-issue-121168.rs | 14 ++++++++++++++ 6 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 tests/ui/imports/auxiliary/import-alias-issue-121168-extern.rs create mode 100644 tests/ui/imports/import-alias-issue-121168.edition2015.stderr create mode 100644 tests/ui/imports/import-alias-issue-121168.edition2018.stderr create mode 100644 tests/ui/imports/import-alias-issue-121168.edition2021.stderr create mode 100644 tests/ui/imports/import-alias-issue-121168.rs diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index d64a3b43aad31..0b545ce6a893f 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -1290,10 +1290,20 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { let mut path_segments = path_segments.clone(); path_segments.push(ast::PathSegment::from_ident(ident)); + let alias_import = if let NameBindingKind::Import { import, .. } = + name_binding.kind + && let ImportKind::ExternCrate { source: Some(_), .. } = import.kind + && import.parent_scope.expansion == parent_scope.expansion + { + true + } else { + false + }; + let is_extern_crate_that_also_appears_in_prelude = name_binding.is_extern_crate() && lookup_ident.span.at_least_rust_2018(); - if !is_extern_crate_that_also_appears_in_prelude { + if !is_extern_crate_that_also_appears_in_prelude || alias_import { // add the module to the lookup if seen_modules.insert(module.def_id()) { if via_import { &mut worklist_via_import } else { &mut worklist } diff --git a/tests/ui/imports/auxiliary/import-alias-issue-121168-extern.rs b/tests/ui/imports/auxiliary/import-alias-issue-121168-extern.rs new file mode 100644 index 0000000000000..a665eb8eb96aa --- /dev/null +++ b/tests/ui/imports/auxiliary/import-alias-issue-121168-extern.rs @@ -0,0 +1 @@ +pub struct Foo(pub core::ptr::NonNull); diff --git a/tests/ui/imports/import-alias-issue-121168.edition2015.stderr b/tests/ui/imports/import-alias-issue-121168.edition2015.stderr new file mode 100644 index 0000000000000..47001fc1a5291 --- /dev/null +++ b/tests/ui/imports/import-alias-issue-121168.edition2015.stderr @@ -0,0 +1,14 @@ +error[E0412]: cannot find type `Foo` in this scope + --> $DIR/import-alias-issue-121168.rs:11:12 + | +LL | let _: Foo = todo!(); + | ^^^ not found in this scope + | +help: consider importing this struct + | +LL + use nice_crate_name::Foo; + | + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0412`. diff --git a/tests/ui/imports/import-alias-issue-121168.edition2018.stderr b/tests/ui/imports/import-alias-issue-121168.edition2018.stderr new file mode 100644 index 0000000000000..b61a0e3edd524 --- /dev/null +++ b/tests/ui/imports/import-alias-issue-121168.edition2018.stderr @@ -0,0 +1,16 @@ +error[E0412]: cannot find type `Foo` in this scope + --> $DIR/import-alias-issue-121168.rs:11:12 + | +LL | let _: Foo = todo!(); + | ^^^ not found in this scope + | +help: consider importing one of these items + | +LL + use crate::nice_crate_name::Foo; + | +LL + use import_alias_issue_121168_extern::Foo; + | + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0412`. diff --git a/tests/ui/imports/import-alias-issue-121168.edition2021.stderr b/tests/ui/imports/import-alias-issue-121168.edition2021.stderr new file mode 100644 index 0000000000000..b61a0e3edd524 --- /dev/null +++ b/tests/ui/imports/import-alias-issue-121168.edition2021.stderr @@ -0,0 +1,16 @@ +error[E0412]: cannot find type `Foo` in this scope + --> $DIR/import-alias-issue-121168.rs:11:12 + | +LL | let _: Foo = todo!(); + | ^^^ not found in this scope + | +help: consider importing one of these items + | +LL + use crate::nice_crate_name::Foo; + | +LL + use import_alias_issue_121168_extern::Foo; + | + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0412`. diff --git a/tests/ui/imports/import-alias-issue-121168.rs b/tests/ui/imports/import-alias-issue-121168.rs new file mode 100644 index 0000000000000..826a6765b0399 --- /dev/null +++ b/tests/ui/imports/import-alias-issue-121168.rs @@ -0,0 +1,14 @@ +//@ revisions: edition2015 edition2018 edition2021 +//@ [edition2015] edition:2015 +//@ [edition2018] edition:2018 +//@ [edition2021] edition:2021 +//@ compile-flags: --extern import_alias_issue_121168_extern +//@ aux-build: import-alias-issue-121168-extern.rs + +extern crate import_alias_issue_121168_extern as nice_crate_name; + +fn use_foo_from_another_crate_without_importing_it_first() { + let _: Foo = todo!(); //~ ERROR cannot find type `Foo` in this scope +} + +fn main() {} From 03a10a917ad27ff26e821e1bf9527d5f8bbeaf1e Mon Sep 17 00:00:00 2001 From: surechen Date: Tue, 27 Feb 2024 10:45:19 +0800 Subject: [PATCH 08/16] Changing some attributes to only_local. Modified according to https://github.com/rust-lang/compiler-team/issues/505. By running test cases, I found that modifying the attribute's only_local tag sometimes causes some unintuitive error reports, so I tend to split it into multiple PRs and edit a small number of attributes each time to prevent too many changes at once. Prevent possible subsequent difficulties in locating problems. --- compiler/rustc_feature/src/builtin_attrs.rs | 63 ++++++++++++++------- 1 file changed, 41 insertions(+), 22 deletions(-) diff --git a/compiler/rustc_feature/src/builtin_attrs.rs b/compiler/rustc_feature/src/builtin_attrs.rs index 682363ed19d87..1fb061f911474 100644 --- a/compiler/rustc_feature/src/builtin_attrs.rs +++ b/compiler/rustc_feature/src/builtin_attrs.rs @@ -390,20 +390,21 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[ // Entry point: gated!(unix_sigpipe, Normal, template!(Word, NameValueStr: "inherit|sig_ign|sig_dfl"), ErrorFollowing, experimental!(unix_sigpipe)), - ungated!(start, Normal, template!(Word), WarnFollowing), - ungated!(no_start, CrateLevel, template!(Word), WarnFollowing), - ungated!(no_main, CrateLevel, template!(Word), WarnFollowing), + ungated!(start, Normal, template!(Word), WarnFollowing, @only_local: true), + ungated!(no_start, CrateLevel, template!(Word), WarnFollowing, @only_local: true), + ungated!(no_main, CrateLevel, template!(Word), WarnFollowing, @only_local: true), // Modules, prelude, and resolution: - ungated!(path, Normal, template!(NameValueStr: "file"), FutureWarnFollowing), - ungated!(no_std, CrateLevel, template!(Word), WarnFollowing), - ungated!(no_implicit_prelude, Normal, template!(Word), WarnFollowing), + ungated!(path, Normal, template!(NameValueStr: "file"), FutureWarnFollowing, @only_local: true), + ungated!(no_std, CrateLevel, template!(Word), WarnFollowing, @only_local: true), + ungated!(no_implicit_prelude, Normal, template!(Word), WarnFollowing, @only_local: true), ungated!(non_exhaustive, Normal, template!(Word), WarnFollowing), // Runtime ungated!( windows_subsystem, CrateLevel, - template!(NameValueStr: "windows|console"), FutureWarnFollowing + template!(NameValueStr: "windows|console"), FutureWarnFollowing, + @only_local: true ), ungated!(panic_handler, Normal, template!(Word), WarnFollowing), // RFC 2070 @@ -416,13 +417,17 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[ DuplicatesOk, @only_local: true, ), ungated!(track_caller, Normal, template!(Word), WarnFollowing), - ungated!(instruction_set, Normal, template!(List: "set"), ErrorPreceding), + ungated!(instruction_set, Normal, template!(List: "set"), ErrorPreceding, @only_local: true), gated!( no_sanitize, Normal, template!(List: "address, kcfi, memory, thread"), DuplicatesOk, - experimental!(no_sanitize) + @only_local: true, experimental!(no_sanitize) + ), + gated!( + coverage, Normal, template!(Word, List: "on|off"), + WarnFollowing, @only_local: true, + coverage_attribute, experimental!(coverage) ), - gated!(coverage, Normal, template!(Word, List: "on|off"), WarnFollowing, coverage_attribute, experimental!(coverage)), ungated!( doc, Normal, template!(List: "hidden|inline|...", NameValueStr: "string"), DuplicatesOk @@ -431,7 +436,8 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[ // Debugging ungated!( debugger_visualizer, Normal, - template!(List: r#"natvis_file = "...", gdb_script_file = "...""#), DuplicatesOk + template!(List: r#"natvis_file = "...", gdb_script_file = "...""#), + DuplicatesOk, @only_local: true ), // ========================================================================== @@ -455,26 +461,35 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[ marker_trait_attr, experimental!(marker) ), gated!( - thread_local, Normal, template!(Word), WarnFollowing, + thread_local, Normal, template!(Word), WarnFollowing, @only_local: true, "`#[thread_local]` is an experimental feature, and does not currently handle destructors", ), - gated!(no_core, CrateLevel, template!(Word), WarnFollowing, experimental!(no_core)), + gated!( + no_core, CrateLevel, template!(Word), WarnFollowing, + @only_local: true, experimental!(no_core) + ), // RFC 2412 gated!( - optimize, Normal, template!(List: "size|speed"), ErrorPreceding, optimize_attribute, - experimental!(optimize), + optimize, Normal, template!(List: "size|speed"), ErrorPreceding, + @only_local: true, optimize_attribute, experimental!(optimize) ), - gated!(ffi_pure, Normal, template!(Word), WarnFollowing, experimental!(ffi_pure)), - gated!(ffi_const, Normal, template!(Word), WarnFollowing, experimental!(ffi_const)), + gated!( + ffi_pure, Normal, template!(Word), WarnFollowing, + @only_local: true, experimental!(ffi_pure) + ), + gated!( + ffi_const, Normal, template!(Word), WarnFollowing, + @only_local: true, experimental!(ffi_const) + ), gated!( register_tool, CrateLevel, template!(List: "tool1, tool2, ..."), DuplicatesOk, - experimental!(register_tool), + @only_local: true, experimental!(register_tool), ), gated!( cmse_nonsecure_entry, Normal, template!(Word), WarnFollowing, - experimental!(cmse_nonsecure_entry) + @only_local: true, experimental!(cmse_nonsecure_entry) ), // RFC 2632 gated!( @@ -492,11 +507,14 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[ // `#[collapse_debuginfo]` gated!( collapse_debuginfo, Normal, template!(Word, List: "no|external|yes"), ErrorFollowing, - experimental!(collapse_debuginfo) + @only_local: true, experimental!(collapse_debuginfo) ), // RFC 2397 - gated!(do_not_recommend, Normal, template!(Word), WarnFollowing, experimental!(do_not_recommend)), + gated!( + do_not_recommend, Normal, template!(Word), WarnFollowing, + @only_local: true, experimental!(do_not_recommend) + ), // `#[cfi_encoding = ""]` gated!( @@ -528,7 +546,8 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[ ), ungated!( rustc_default_body_unstable, Normal, - template!(List: r#"feature = "name", reason = "...", issue = "N""#), DuplicatesOk + template!(List: r#"feature = "name", reason = "...", issue = "N""#), + DuplicatesOk, @only_local: true ), gated!( allow_internal_unstable, Normal, template!(Word, List: "feat1, feat2, ..."), DuplicatesOk, From 8719b740273126701a649046b39d0829776b52b0 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 27 Feb 2024 16:19:19 +0100 Subject: [PATCH 09/16] Fix link generation for locate foreign macro in jump to definition feature --- src/librustdoc/html/format.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 973036a40982c..afd5eb42d019d 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -575,7 +575,7 @@ fn generate_macro_def_id_path( ExternalLocation::Local => { // `root_path` always end with a `/`. format!( - "{root_path}{crate_name}/{path}", + "{root_path}{path}", root_path = root_path.unwrap_or(""), path = path.iter().map(|p| p.as_str()).join("/") ) From 4dd05e642403451fbe6b8db8845b2d04f0a14bbd Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 27 Feb 2024 16:20:11 +0100 Subject: [PATCH 10/16] Add regression test for link generation on foreign macro in jump to defintion feature --- tests/rustdoc/auxiliary/jump-to-def-macro.rs | 6 ++++++ tests/rustdoc/jump-to-def-macro.rs | 15 +++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 tests/rustdoc/auxiliary/jump-to-def-macro.rs create mode 100644 tests/rustdoc/jump-to-def-macro.rs diff --git a/tests/rustdoc/auxiliary/jump-to-def-macro.rs b/tests/rustdoc/auxiliary/jump-to-def-macro.rs new file mode 100644 index 0000000000000..f442b9461e892 --- /dev/null +++ b/tests/rustdoc/auxiliary/jump-to-def-macro.rs @@ -0,0 +1,6 @@ +#[macro_export] +macro_rules! symbols { + ($name:ident = $value:expr) => { + pub const $name: isize = $value; + } +} diff --git a/tests/rustdoc/jump-to-def-macro.rs b/tests/rustdoc/jump-to-def-macro.rs new file mode 100644 index 0000000000000..e8e97a442dd10 --- /dev/null +++ b/tests/rustdoc/jump-to-def-macro.rs @@ -0,0 +1,15 @@ +//@ aux-build:jump-to-def-macro.rs +//@ build-aux-docs +//@ compile-flags: -Zunstable-options --generate-link-to-definition + +#![crate_name = "foo"] + +// @has 'src/foo/jump-to-def-macro.rs.html' + +#[macro_use] +extern crate jump_to_def_macro; + +// @has - '//a[@href="../../jump_to_def_macro/macro.symbols.html"]' 'symbols!' +symbols! { + A = 12 +} From 5df9593f1ad13eac4ca1da281332c8147937cd34 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 27 Feb 2024 17:39:32 +0100 Subject: [PATCH 11/16] Prevent inclusion of whitespace character after macro_rules ident --- src/librustdoc/html/highlight.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/librustdoc/html/highlight.rs b/src/librustdoc/html/highlight.rs index 1cdc792a819a8..aa5998876d9ab 100644 --- a/src/librustdoc/html/highlight.rs +++ b/src/librustdoc/html/highlight.rs @@ -136,6 +136,7 @@ fn can_merge(class1: Option, class2: Option, text: &str) -> bool { match (class1, class2) { (Some(c1), Some(c2)) => c1.is_equal_to(c2), (Some(Class::Ident(_)), None) | (None, Some(Class::Ident(_))) => true, + (Some(Class::Macro(_)), _) => false, (Some(_), None) | (None, Some(_)) => text.trim().is_empty(), (None, None) => true, } From 1feef44daf4de5ab35c0bf1eea0f3a54c59cf49c Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Tue, 27 Feb 2024 16:02:19 +0000 Subject: [PATCH 12/16] rename RPITIT from opaque to synthetic --- compiler/rustc_hir/src/definitions.rs | 4 +++- compiler/rustc_span/src/symbol.rs | 1 + tests/ui/impl-trait/in-trait/async-and-ret-ref.stderr | 4 ++-- tests/ui/impl-trait/in-trait/doesnt-satisfy.stderr | 4 ++-- .../impl-trait/in-trait/missing-static-bound-from-impl.stderr | 4 ++-- .../ui/impl-trait/in-trait/return-dont-satisfy-bounds.stderr | 4 ++-- .../ui/rfcs/rfc-1937-termination-trait/issue-103052-2.stderr | 4 ++-- 7 files changed, 14 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_hir/src/definitions.rs b/compiler/rustc_hir/src/definitions.rs index b81ad8b194623..f538d6bcb9816 100644 --- a/compiler/rustc_hir/src/definitions.rs +++ b/compiler/rustc_hir/src/definitions.rs @@ -420,7 +420,9 @@ impl DefPathData { pub fn name(&self) -> DefPathDataName { use self::DefPathData::*; match *self { - TypeNs(name) if name == kw::Empty => DefPathDataName::Anon { namespace: sym::opaque }, + TypeNs(name) if name == kw::Empty => { + DefPathDataName::Anon { namespace: sym::synthetic } + } TypeNs(name) | ValueNs(name) | MacroNs(name) | LifetimeNs(name) => { DefPathDataName::Named(name) } diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index d472c406c4717..df8ee4cbc5ab4 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -1690,6 +1690,7 @@ symbols! { suggestion, sym, sync, + synthetic, t32, target, target_abi, diff --git a/tests/ui/impl-trait/in-trait/async-and-ret-ref.stderr b/tests/ui/impl-trait/in-trait/async-and-ret-ref.stderr index 15aa3cf54bbe5..a17653496dbd2 100644 --- a/tests/ui/impl-trait/in-trait/async-and-ret-ref.stderr +++ b/tests/ui/impl-trait/in-trait/async-and-ret-ref.stderr @@ -1,10 +1,10 @@ -error[E0310]: the associated type `::{opaque#0}` may not live long enough +error[E0310]: the associated type `::{synthetic#0}` may not live long enough --> $DIR/async-and-ret-ref.rs:7:5 | LL | async fn foo() -> &'static impl T; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | | - | the associated type `::{opaque#0}` must be valid for the static lifetime... + | the associated type `::{synthetic#0}` must be valid for the static lifetime... | ...so that the reference type `&'static impl T` does not outlive the data it points at error: aborting due to 1 previous error diff --git a/tests/ui/impl-trait/in-trait/doesnt-satisfy.stderr b/tests/ui/impl-trait/in-trait/doesnt-satisfy.stderr index baee1b5db6e9c..fc3efb44ac76b 100644 --- a/tests/ui/impl-trait/in-trait/doesnt-satisfy.stderr +++ b/tests/ui/impl-trait/in-trait/doesnt-satisfy.stderr @@ -6,11 +6,11 @@ LL | fn bar() -> () {} | = help: the trait `std::fmt::Display` is not implemented for `()` = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead -note: required by a bound in `Foo::{opaque#0}` +note: required by a bound in `Foo::{synthetic#0}` --> $DIR/doesnt-satisfy.rs:2:22 | LL | fn bar() -> impl std::fmt::Display; - | ^^^^^^^^^^^^^^^^^ required by this bound in `Foo::{opaque#0}` + | ^^^^^^^^^^^^^^^^^ required by this bound in `Foo::{synthetic#0}` error: aborting due to 1 previous error diff --git a/tests/ui/impl-trait/in-trait/missing-static-bound-from-impl.stderr b/tests/ui/impl-trait/in-trait/missing-static-bound-from-impl.stderr index 5ec0ee38347aa..7fa4b8dbd49cd 100644 --- a/tests/ui/impl-trait/in-trait/missing-static-bound-from-impl.stderr +++ b/tests/ui/impl-trait/in-trait/missing-static-bound-from-impl.stderr @@ -1,10 +1,10 @@ -error[E0310]: the associated type `::{opaque#0}` may not live long enough +error[E0310]: the associated type `::{synthetic#0}` may not live long enough --> $DIR/missing-static-bound-from-impl.rs:11:9 | LL | Box::new(::f()) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | | - | the associated type `::{opaque#0}` must be valid for the static lifetime... + | the associated type `::{synthetic#0}` must be valid for the static lifetime... | ...so that the type `impl Fn()` will meet its required lifetime bounds error: aborting due to 1 previous error diff --git a/tests/ui/impl-trait/in-trait/return-dont-satisfy-bounds.stderr b/tests/ui/impl-trait/in-trait/return-dont-satisfy-bounds.stderr index 638de01f91393..12725c3456fd5 100644 --- a/tests/ui/impl-trait/in-trait/return-dont-satisfy-bounds.stderr +++ b/tests/ui/impl-trait/in-trait/return-dont-satisfy-bounds.stderr @@ -5,11 +5,11 @@ LL | fn foo>(self) -> impl Foo { | ^^^^^^^^^^^^ the trait `Foo` is not implemented for `impl Foo` | = help: the trait `Foo` is implemented for `Bar` -note: required by a bound in `Foo::{opaque#0}` +note: required by a bound in `Foo::{synthetic#0}` --> $DIR/return-dont-satisfy-bounds.rs:2:30 | LL | fn foo(self) -> impl Foo; - | ^^^^^^ required by this bound in `Foo::{opaque#0}` + | ^^^^^^ required by this bound in `Foo::{synthetic#0}` error[E0276]: impl has stricter requirements than trait --> $DIR/return-dont-satisfy-bounds.rs:8:16 diff --git a/tests/ui/rfcs/rfc-1937-termination-trait/issue-103052-2.stderr b/tests/ui/rfcs/rfc-1937-termination-trait/issue-103052-2.stderr index 3fbbfd0fd0dbf..b17700ec6325d 100644 --- a/tests/ui/rfcs/rfc-1937-termination-trait/issue-103052-2.stderr +++ b/tests/ui/rfcs/rfc-1937-termination-trait/issue-103052-2.stderr @@ -4,11 +4,11 @@ error[E0277]: the trait bound `Something: Termination` is not satisfied LL | fn main() -> Something { | ^^^^^^^^^ the trait `Termination` is not implemented for `Something` | -note: required by a bound in `Main::{opaque#0}` +note: required by a bound in `Main::{synthetic#0}` --> $DIR/issue-103052-2.rs:5:27 | LL | fn main() -> impl std::process::Termination; - | ^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `Main::{opaque#0}` + | ^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `Main::{synthetic#0}` error: aborting due to 1 previous error From b57ddfe079cbd9974f0b1f68a50f8f1b901cfdaf Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Tue, 27 Feb 2024 16:11:35 +0000 Subject: [PATCH 13/16] Print RPITIT like an opaque --- compiler/rustc_middle/src/ty/print/pretty.rs | 20 +++++++++++-------- .../impl-trait/in-trait/async-and-ret-ref.rs | 2 +- .../in-trait/async-and-ret-ref.stderr | 4 ++-- .../missing-static-bound-from-impl.rs | 2 +- .../missing-static-bound-from-impl.stderr | 4 ++-- 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index 1910841f2684e..87acca9e945a3 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -715,13 +715,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write { p!(print_def_path(def_id, &[])); } ty::Alias(ty::Projection | ty::Inherent | ty::Weak, ref data) => { - if !(self.should_print_verbose() || with_no_queries()) - && self.tcx().is_impl_trait_in_trait(data.def_id) - { - return self.pretty_print_opaque_impl_type(data.def_id, data.args); - } else { - p!(print(data)) - } + p!(print(data)) } ty::Placeholder(placeholder) => match placeholder.bound.kind { ty::BoundTyKind::Anon => p!(write("{placeholder:?}")), @@ -3053,7 +3047,17 @@ define_print_and_forward_display! { if let DefKind::Impl { of_trait: false } = cx.tcx().def_kind(cx.tcx().parent(self.def_id)) { p!(pretty_print_inherent_projection(self)) } else { - p!(print_def_path(self.def_id, self.args)); + // If we're printing verbosely, or don't want to invoke queries + // (`is_impl_trait_in_trait`), then fall back to printing the def path. + // This is likely what you want if you're debugging the compiler anyways. + if !(cx.should_print_verbose() || with_no_queries()) + && cx.tcx().is_impl_trait_in_trait(self.def_id) + { + return cx.pretty_print_opaque_impl_type(self.def_id, self.args); + } else { + p!(print_def_path(self.def_id, self.args)); + } + } } diff --git a/tests/ui/impl-trait/in-trait/async-and-ret-ref.rs b/tests/ui/impl-trait/in-trait/async-and-ret-ref.rs index e991b74a0f81a..e0a7aef2c5f44 100644 --- a/tests/ui/impl-trait/in-trait/async-and-ret-ref.rs +++ b/tests/ui/impl-trait/in-trait/async-and-ret-ref.rs @@ -5,7 +5,7 @@ trait T {} trait MyTrait { async fn foo() -> &'static impl T; - //~^ ERROR the associated type `::{opaque#0}` may not live long enough + //~^ ERROR the associated type `impl T` may not live long enough } fn main() {} diff --git a/tests/ui/impl-trait/in-trait/async-and-ret-ref.stderr b/tests/ui/impl-trait/in-trait/async-and-ret-ref.stderr index a17653496dbd2..19ffff9d3f2bd 100644 --- a/tests/ui/impl-trait/in-trait/async-and-ret-ref.stderr +++ b/tests/ui/impl-trait/in-trait/async-and-ret-ref.stderr @@ -1,10 +1,10 @@ -error[E0310]: the associated type `::{synthetic#0}` may not live long enough +error[E0310]: the associated type `impl T` may not live long enough --> $DIR/async-and-ret-ref.rs:7:5 | LL | async fn foo() -> &'static impl T; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | | - | the associated type `::{synthetic#0}` must be valid for the static lifetime... + | the associated type `impl T` must be valid for the static lifetime... | ...so that the reference type `&'static impl T` does not outlive the data it points at error: aborting due to 1 previous error diff --git a/tests/ui/impl-trait/in-trait/missing-static-bound-from-impl.rs b/tests/ui/impl-trait/in-trait/missing-static-bound-from-impl.rs index a36799c3ebd46..ee9ecdda90289 100644 --- a/tests/ui/impl-trait/in-trait/missing-static-bound-from-impl.rs +++ b/tests/ui/impl-trait/in-trait/missing-static-bound-from-impl.rs @@ -9,7 +9,7 @@ trait Erased { impl Erased for T { fn f(&self) -> Box { Box::new(::f()) - //~^ ERROR the associated type `::{opaque#0}` may not live long enough + //~^ ERROR the associated type `impl Fn()` may not live long enough } } diff --git a/tests/ui/impl-trait/in-trait/missing-static-bound-from-impl.stderr b/tests/ui/impl-trait/in-trait/missing-static-bound-from-impl.stderr index 7fa4b8dbd49cd..1f787c1842cf9 100644 --- a/tests/ui/impl-trait/in-trait/missing-static-bound-from-impl.stderr +++ b/tests/ui/impl-trait/in-trait/missing-static-bound-from-impl.stderr @@ -1,10 +1,10 @@ -error[E0310]: the associated type `::{synthetic#0}` may not live long enough +error[E0310]: the associated type `impl Fn()` may not live long enough --> $DIR/missing-static-bound-from-impl.rs:11:9 | LL | Box::new(::f()) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | | - | the associated type `::{synthetic#0}` must be valid for the static lifetime... + | the associated type `impl Fn()` must be valid for the static lifetime... | ...so that the type `impl Fn()` will meet its required lifetime bounds error: aborting due to 1 previous error From 8a6d3535f76d17a7a31ebdd4f654f9ca5497463b Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Tue, 27 Feb 2024 18:11:23 +0000 Subject: [PATCH 14/16] Split rustc_type_ir to avoid rustc_ast from depending on it --- Cargo.lock | 15 +++- compiler/rustc_ast/Cargo.toml | 3 +- compiler/rustc_ast/src/ast.rs | 2 +- compiler/rustc_ast_ir/Cargo.toml | 22 ++++++ compiler/rustc_ast_ir/src/lib.rs | 68 +++++++++++++++++++ compiler/rustc_const_eval/src/errors.rs | 3 +- .../src/util/caller_location.rs | 3 +- compiler/rustc_hir_analysis/src/check/errs.rs | 3 +- compiler/rustc_middle/Cargo.toml | 1 + .../rustc_middle/src/mir/interpret/error.rs | 2 +- compiler/rustc_middle/src/ty/mod.rs | 1 + compiler/rustc_type_ir/Cargo.toml | 2 + compiler/rustc_type_ir/src/macros.rs | 4 +- compiler/rustc_type_ir/src/ty_kind.rs | 62 +---------------- 14 files changed, 117 insertions(+), 74 deletions(-) create mode 100644 compiler/rustc_ast_ir/Cargo.toml create mode 100644 compiler/rustc_ast_ir/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 4e00c9034bd9b..59bdce9446b15 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3432,18 +3432,29 @@ version = "0.0.0" dependencies = [ "bitflags 2.4.2", "memchr", + "rustc_ast_ir", "rustc_data_structures", "rustc_index", "rustc_lexer", "rustc_macros", "rustc_serialize", "rustc_span", - "rustc_type_ir", "smallvec", "thin-vec", "tracing", ] +[[package]] +name = "rustc_ast_ir" +version = "0.0.0" +dependencies = [ + "rustc_data_structures", + "rustc_macros", + "rustc_serialize", + "rustc_span", + "smallvec", +] + [[package]] name = "rustc_ast_lowering" version = "0.0.0" @@ -4196,6 +4207,7 @@ dependencies = [ "rustc_apfloat", "rustc_arena", "rustc_ast", + "rustc_ast_ir", "rustc_attr", "rustc_data_structures", "rustc_error_messages", @@ -4676,6 +4688,7 @@ version = "0.0.0" dependencies = [ "bitflags 2.4.2", "derivative", + "rustc_ast_ir", "rustc_data_structures", "rustc_index", "rustc_macros", diff --git a/compiler/rustc_ast/Cargo.toml b/compiler/rustc_ast/Cargo.toml index 087f6a192b5c8..d33f9666b484c 100644 --- a/compiler/rustc_ast/Cargo.toml +++ b/compiler/rustc_ast/Cargo.toml @@ -8,14 +8,13 @@ edition = "2021" # tidy-alphabetical-start bitflags = "2.4.1" memchr = "=2.5.0" +rustc_ast_ir = { path = "../rustc_ast_ir" } rustc_data_structures = { path = "../rustc_data_structures" } rustc_index = { path = "../rustc_index" } rustc_lexer = { path = "../rustc_lexer" } rustc_macros = { path = "../rustc_macros" } rustc_serialize = { path = "../rustc_serialize" } rustc_span = { path = "../rustc_span" } -# For Mutability and Movability, which could be uplifted into a common crate. -rustc_type_ir = { path = "../rustc_type_ir" } smallvec = { version = "1.8.1", features = ["union", "may_dangle"] } thin-vec = "0.2.12" tracing = "0.1" diff --git a/compiler/rustc_ast/src/ast.rs b/compiler/rustc_ast/src/ast.rs index 8ba2f222fcfaa..4ae18b4cf4854 100644 --- a/compiler/rustc_ast/src/ast.rs +++ b/compiler/rustc_ast/src/ast.rs @@ -27,6 +27,7 @@ pub use UnsafeSource::*; use crate::ptr::P; use crate::token::{self, CommentKind, Delimiter}; use crate::tokenstream::{DelimSpan, LazyAttrTokenStream, TokenStream}; +pub use rustc_ast_ir::{Movability, Mutability}; use rustc_data_structures::packed::Pu128; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::stack::ensure_sufficient_stack; @@ -35,7 +36,6 @@ use rustc_macros::HashStable_Generic; use rustc_span::source_map::{respan, Spanned}; use rustc_span::symbol::{kw, sym, Ident, Symbol}; use rustc_span::{ErrorGuaranteed, Span, DUMMY_SP}; -pub use rustc_type_ir::{Movability, Mutability}; use std::fmt; use std::mem; use thin_vec::{thin_vec, ThinVec}; diff --git a/compiler/rustc_ast_ir/Cargo.toml b/compiler/rustc_ast_ir/Cargo.toml new file mode 100644 index 0000000000000..e761b7adad3fd --- /dev/null +++ b/compiler/rustc_ast_ir/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "rustc_ast_ir" +version = "0.0.0" +edition = "2021" + +[dependencies] +# tidy-alphabetical-start +rustc_data_structures = { path = "../rustc_data_structures", optional = true } +rustc_macros = { path = "../rustc_macros", optional = true } +rustc_serialize = { path = "../rustc_serialize", optional = true } +rustc_span = { path = "../rustc_span", optional = true } +smallvec = { version = "1.8.1" } +# tidy-alphabetical-end + +[features] +default = ["nightly"] +nightly = [ + "rustc_serialize", + "rustc_data_structures", + "rustc_macros", + "rustc_span", +] diff --git a/compiler/rustc_ast_ir/src/lib.rs b/compiler/rustc_ast_ir/src/lib.rs new file mode 100644 index 0000000000000..9fe1370921345 --- /dev/null +++ b/compiler/rustc_ast_ir/src/lib.rs @@ -0,0 +1,68 @@ +#![cfg_attr(feature = "nightly", feature(rustc_attrs))] +#![cfg_attr(feature = "nightly", allow(internal_features))] + +#[cfg(feature = "nightly")] +#[macro_use] +extern crate rustc_macros; + +/// The movability of a coroutine / closure literal: +/// whether a coroutine contains self-references, causing it to be `!Unpin`. +#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Copy)] +#[cfg_attr(feature = "nightly", derive(Encodable, Decodable, HashStable_NoContext))] +pub enum Movability { + /// May contain self-references, `!Unpin`. + Static, + /// Must not contain self-references, `Unpin`. + Movable, +} + +#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Copy)] +#[cfg_attr(feature = "nightly", derive(Encodable, Decodable, HashStable_NoContext))] +pub enum Mutability { + // N.B. Order is deliberate, so that Not < Mut + Not, + Mut, +} + +impl Mutability { + pub fn invert(self) -> Self { + match self { + Mutability::Mut => Mutability::Not, + Mutability::Not => Mutability::Mut, + } + } + + /// Returns `""` (empty string) or `"mut "` depending on the mutability. + pub fn prefix_str(self) -> &'static str { + match self { + Mutability::Mut => "mut ", + Mutability::Not => "", + } + } + + /// Returns `"&"` or `"&mut "` depending on the mutability. + pub fn ref_prefix_str(self) -> &'static str { + match self { + Mutability::Not => "&", + Mutability::Mut => "&mut ", + } + } + + /// Returns `""` (empty string) or `"mutably "` depending on the mutability. + pub fn mutably_str(self) -> &'static str { + match self { + Mutability::Not => "", + Mutability::Mut => "mutably ", + } + } + + /// Return `true` if self is mutable + pub fn is_mut(self) -> bool { + matches!(self, Self::Mut) + } + + /// Return `true` if self is **not** mutable + pub fn is_not(self) -> bool { + matches!(self, Self::Not) + } +} diff --git a/compiler/rustc_const_eval/src/errors.rs b/compiler/rustc_const_eval/src/errors.rs index 267f3acaaa5b2..434101ef6b695 100644 --- a/compiler/rustc_const_eval/src/errors.rs +++ b/compiler/rustc_const_eval/src/errors.rs @@ -11,11 +11,10 @@ use rustc_middle::mir::interpret::{ PointerKind, ResourceExhaustionInfo, UndefinedBehaviorInfo, UnsupportedOpInfo, ValidationErrorInfo, }; -use rustc_middle::ty::{self, Ty}; +use rustc_middle::ty::{self, Mutability, Ty}; use rustc_span::Span; use rustc_target::abi::call::AdjustForForeignAbiError; use rustc_target::abi::{Size, WrappingRange}; -use rustc_type_ir::Mutability; use crate::interpret::InternKind; diff --git a/compiler/rustc_const_eval/src/util/caller_location.rs b/compiler/rustc_const_eval/src/util/caller_location.rs index b8e15c485f58f..af9a4a4271d74 100644 --- a/compiler/rustc_const_eval/src/util/caller_location.rs +++ b/compiler/rustc_const_eval/src/util/caller_location.rs @@ -1,10 +1,9 @@ use rustc_hir::LangItem; use rustc_middle::mir; use rustc_middle::query::TyCtxtAt; -use rustc_middle::ty; use rustc_middle::ty::layout::LayoutOf; +use rustc_middle::ty::{self, Mutability}; use rustc_span::symbol::Symbol; -use rustc_type_ir::Mutability; use crate::const_eval::{mk_eval_cx_to_read_const_val, CanAccessMutGlobal, CompileTimeEvalContext}; use crate::interpret::*; diff --git a/compiler/rustc_hir_analysis/src/check/errs.rs b/compiler/rustc_hir_analysis/src/check/errs.rs index 4a7ace274c5bb..c92320bc0fee8 100644 --- a/compiler/rustc_hir_analysis/src/check/errs.rs +++ b/compiler/rustc_hir_analysis/src/check/errs.rs @@ -1,9 +1,8 @@ use rustc_hir as hir; use rustc_hir_pretty::qpath_to_string; use rustc_lint_defs::builtin::STATIC_MUT_REFS; -use rustc_middle::ty::TyCtxt; +use rustc_middle::ty::{Mutability, TyCtxt}; use rustc_span::Span; -use rustc_type_ir::Mutability; use crate::errors; diff --git a/compiler/rustc_middle/Cargo.toml b/compiler/rustc_middle/Cargo.toml index 82e56c9cbe205..5a24a7ab0bd55 100644 --- a/compiler/rustc_middle/Cargo.toml +++ b/compiler/rustc_middle/Cargo.toml @@ -17,6 +17,7 @@ rustc-rayon-core = { version = "0.5.0", optional = true } rustc_apfloat = "0.2.0" rustc_arena = { path = "../rustc_arena" } rustc_ast = { path = "../rustc_ast" } +rustc_ast_ir = { path = "../rustc_ast_ir" } rustc_attr = { path = "../rustc_attr" } rustc_data_structures = { path = "../rustc_data_structures" } rustc_error_messages = { path = "../rustc_error_messages" } # Used for intra-doc links diff --git a/compiler/rustc_middle/src/mir/interpret/error.rs b/compiler/rustc_middle/src/mir/interpret/error.rs index 125fac48df877..9cef4cf1ac6f2 100644 --- a/compiler/rustc_middle/src/mir/interpret/error.rs +++ b/compiler/rustc_middle/src/mir/interpret/error.rs @@ -4,6 +4,7 @@ use crate::error; use crate::mir::{ConstAlloc, ConstValue}; use crate::ty::{layout, tls, Ty, TyCtxt, ValTree}; +use rustc_ast_ir::Mutability; use rustc_data_structures::sync::Lock; use rustc_errors::{ DiagnosticArgName, DiagnosticArgValue, DiagnosticMessage, ErrorGuaranteed, IntoDiagnosticArg, @@ -12,7 +13,6 @@ use rustc_macros::HashStable; use rustc_session::CtfeBacktrace; use rustc_span::{def_id::DefId, Span, DUMMY_SP}; use rustc_target::abi::{call, Align, Size, VariantIdx, WrappingRange}; -use rustc_type_ir::Mutability; use std::borrow::Cow; use std::{any::Any, backtrace::Backtrace, fmt}; diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index 30409e990e13c..eb63aa4260802 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -32,6 +32,7 @@ pub use generic_args::*; pub use generics::*; use rustc_ast as ast; use rustc_ast::node_id::NodeMap; +pub use rustc_ast_ir::{Movability, Mutability}; use rustc_attr as attr; use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet}; use rustc_data_structures::intern::Interned; diff --git a/compiler/rustc_type_ir/Cargo.toml b/compiler/rustc_type_ir/Cargo.toml index 59966423f7fb7..79ff60802d28e 100644 --- a/compiler/rustc_type_ir/Cargo.toml +++ b/compiler/rustc_type_ir/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" # tidy-alphabetical-start bitflags = "2.4.1" derivative = "2.2.0" +rustc_ast_ir = { path = "../rustc_ast_ir" } rustc_data_structures = { path = "../rustc_data_structures", optional = true } rustc_index = { path = "../rustc_index", default-features = false } rustc_macros = { path = "../rustc_macros", optional = true } @@ -25,4 +26,5 @@ nightly = [ "rustc_span", "rustc_data_structures", "rustc_macros", + "rustc_ast_ir/nightly" ] diff --git a/compiler/rustc_type_ir/src/macros.rs b/compiler/rustc_type_ir/src/macros.rs index 82bb1bf291667..231546287d027 100644 --- a/compiler/rustc_type_ir/src/macros.rs +++ b/compiler/rustc_type_ir/src/macros.rs @@ -51,6 +51,6 @@ TrivialTypeTraversalImpls! { crate::DebruijnIndex, crate::AliasRelationDirection, crate::UniverseIndex, - crate::Mutability, - crate::Movability, + rustc_ast_ir::Mutability, + rustc_ast_ir::Movability, } diff --git a/compiler/rustc_type_ir/src/ty_kind.rs b/compiler/rustc_type_ir/src/ty_kind.rs index 9f715a01d44fa..a4d67169bc75c 100644 --- a/compiler/rustc_type_ir/src/ty_kind.rs +++ b/compiler/rustc_type_ir/src/ty_kind.rs @@ -11,67 +11,7 @@ use crate::{DebruijnIndex, DebugWithInfcx, InferCtxtLike, WithInfcx}; use self::TyKind::*; -/// The movability of a coroutine / closure literal: -/// whether a coroutine contains self-references, causing it to be `!Unpin`. -#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Copy)] -#[cfg_attr(feature = "nightly", derive(Encodable, Decodable, HashStable_NoContext))] -pub enum Movability { - /// May contain self-references, `!Unpin`. - Static, - /// Must not contain self-references, `Unpin`. - Movable, -} - -#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Copy)] -#[cfg_attr(feature = "nightly", derive(Encodable, Decodable, HashStable_NoContext))] -pub enum Mutability { - // N.B. Order is deliberate, so that Not < Mut - Not, - Mut, -} - -impl Mutability { - pub fn invert(self) -> Self { - match self { - Mutability::Mut => Mutability::Not, - Mutability::Not => Mutability::Mut, - } - } - - /// Returns `""` (empty string) or `"mut "` depending on the mutability. - pub fn prefix_str(self) -> &'static str { - match self { - Mutability::Mut => "mut ", - Mutability::Not => "", - } - } - - /// Returns `"&"` or `"&mut "` depending on the mutability. - pub fn ref_prefix_str(self) -> &'static str { - match self { - Mutability::Not => "&", - Mutability::Mut => "&mut ", - } - } - - /// Returns `""` (empty string) or `"mutably "` depending on the mutability. - pub fn mutably_str(self) -> &'static str { - match self { - Mutability::Not => "", - Mutability::Mut => "mutably ", - } - } - - /// Return `true` if self is mutable - pub fn is_mut(self) -> bool { - matches!(self, Self::Mut) - } - - /// Return `true` if self is **not** mutable - pub fn is_not(self) -> bool { - matches!(self, Self::Not) - } -} +use rustc_ast_ir::Mutability; /// Specifies how a trait object is represented. #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] From e9dbf441b7751a7c0440e283ae70856da9faa9eb Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 27 Feb 2024 17:40:12 +0100 Subject: [PATCH 15/16] Add regression test for inclusion of whitespace characters in rustdoc highlighting --- .../html/highlight/fixtures/sample.html | 2 +- tests/rustdoc/source-code-highlight.rs | 29 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 tests/rustdoc/source-code-highlight.rs diff --git a/src/librustdoc/html/highlight/fixtures/sample.html b/src/librustdoc/html/highlight/fixtures/sample.html index aa735e81597c7..773afd5c2cc30 100644 --- a/src/librustdoc/html/highlight/fixtures/sample.html +++ b/src/librustdoc/html/highlight/fixtures/sample.html @@ -32,7 +32,7 @@ } } -macro_rules! bar { +macro_rules! bar { ($foo:tt) => {}; } diff --git a/tests/rustdoc/source-code-highlight.rs b/tests/rustdoc/source-code-highlight.rs new file mode 100644 index 0000000000000..8d7c7782dc5b3 --- /dev/null +++ b/tests/rustdoc/source-code-highlight.rs @@ -0,0 +1,29 @@ +// We need this option to be enabled for the `foo` macro declaration to ensure +// that the link on the ident is not including whitespace characters. + +//@ compile-flags: -Zunstable-options --generate-link-to-definition +#![crate_name = "foo"] + +// @has 'src/foo/source-code-highlight.rs.html' + +// @hasraw - 'foo' +#[macro_export] +macro_rules! foo { + () => {} +} + +// @hasraw - 'foo!' +foo! {} + +// @hasraw - 'f' +#[rustfmt::skip] +pub fn f () {} +// @hasraw - 'Bar' +// @hasraw - 'Bar' +// @hasraw - 'u32' +#[rustfmt::skip] +pub struct Bar ( u32 ); +// @hasraw - 'Foo' +pub enum Foo { + A, +} From c7476f86b5bfea32dd4eb72a672e5d308fb89604 Mon Sep 17 00:00:00 2001 From: Ramon de C Valle Date: Tue, 27 Feb 2024 11:03:02 -0800 Subject: [PATCH 16/16] CFI: Fix typo in test file names Fixes typo (i.e., saniziter) in test file names. --- ...itizer-cfi-is-incompatible-with-sanitizer-kcfi.aarch64.stderr} | 0 ...fi.rs => sanitizer-cfi-is-incompatible-with-sanitizer-kcfi.rs} | 0 ...nitizer-cfi-is-incompatible-with-sanitizer-kcfi.x86_64.stderr} | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename tests/ui/sanitize/{sanitizer-cfi-is-incompatible-with-saniziter-kcfi.aarch64.stderr => sanitizer-cfi-is-incompatible-with-sanitizer-kcfi.aarch64.stderr} (100%) rename tests/ui/sanitize/{sanitizer-cfi-is-incompatible-with-saniziter-kcfi.rs => sanitizer-cfi-is-incompatible-with-sanitizer-kcfi.rs} (100%) rename tests/ui/sanitize/{sanitizer-cfi-is-incompatible-with-saniziter-kcfi.x86_64.stderr => sanitizer-cfi-is-incompatible-with-sanitizer-kcfi.x86_64.stderr} (100%) diff --git a/tests/ui/sanitize/sanitizer-cfi-is-incompatible-with-saniziter-kcfi.aarch64.stderr b/tests/ui/sanitize/sanitizer-cfi-is-incompatible-with-sanitizer-kcfi.aarch64.stderr similarity index 100% rename from tests/ui/sanitize/sanitizer-cfi-is-incompatible-with-saniziter-kcfi.aarch64.stderr rename to tests/ui/sanitize/sanitizer-cfi-is-incompatible-with-sanitizer-kcfi.aarch64.stderr diff --git a/tests/ui/sanitize/sanitizer-cfi-is-incompatible-with-saniziter-kcfi.rs b/tests/ui/sanitize/sanitizer-cfi-is-incompatible-with-sanitizer-kcfi.rs similarity index 100% rename from tests/ui/sanitize/sanitizer-cfi-is-incompatible-with-saniziter-kcfi.rs rename to tests/ui/sanitize/sanitizer-cfi-is-incompatible-with-sanitizer-kcfi.rs diff --git a/tests/ui/sanitize/sanitizer-cfi-is-incompatible-with-saniziter-kcfi.x86_64.stderr b/tests/ui/sanitize/sanitizer-cfi-is-incompatible-with-sanitizer-kcfi.x86_64.stderr similarity index 100% rename from tests/ui/sanitize/sanitizer-cfi-is-incompatible-with-saniziter-kcfi.x86_64.stderr rename to tests/ui/sanitize/sanitizer-cfi-is-incompatible-with-sanitizer-kcfi.x86_64.stderr