From b6f22400002f7921feed13e35852e3041cf2b145 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 25 Feb 2025 20:26:12 +0100 Subject: [PATCH 01/15] rustdoc: disable forbidden #[target_feature] check --- compiler/rustc_codegen_ssa/src/target_features.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/target_features.rs b/compiler/rustc_codegen_ssa/src/target_features.rs index d8b9bdb55da69..28c6932dd5b9c 100644 --- a/compiler/rustc_codegen_ssa/src/target_features.rs +++ b/compiler/rustc_codegen_ssa/src/target_features.rs @@ -87,7 +87,10 @@ pub(crate) fn from_target_feature_attr( // But ensure the ABI does not forbid enabling this. // Here we do assume that LLVM doesn't add even more implied features // we don't know about, at least no features that would have ABI effects! - if abi_feature_constraints.incompatible.contains(&name.as_str()) { + // We skip this check in rustdoc, like we skip all target feature related checks. + if !tcx.sess.opts.actually_rustdoc + && abi_feature_constraints.incompatible.contains(&name.as_str()) + { tcx.dcx().emit_err(errors::ForbiddenTargetFeatureAttr { span: item.span(), feature: name.as_str(), @@ -142,8 +145,11 @@ pub(crate) fn provide(providers: &mut Providers) { rust_target_features: |tcx, cnum| { assert_eq!(cnum, LOCAL_CRATE); if tcx.sess.opts.actually_rustdoc { - // rustdoc needs to be able to document functions that use all the features, so - // whitelist them all + // HACK: rustdoc would like to pretend that we have all the target features, so we + // have to merge all the lists into one. The result has a "random" stability + // (depending on the order in which we consider features); all places that check + // target stability are expected to check `actually_rustdoc` and do nothing when + // that is set. rustc_target::target_features::all_rust_features() .map(|(a, b)| (a.to_string(), b)) .collect() From 039af88e09f4f4beb47406f4771bffc2e61d800a Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 25 Feb 2025 20:38:13 +0100 Subject: [PATCH 02/15] also fix potential issues with mixed stable/unstable target features in rustdoc --- .../rustc_codegen_ssa/src/target_features.rs | 45 ++++++++++++++----- 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/target_features.rs b/compiler/rustc_codegen_ssa/src/target_features.rs index 28c6932dd5b9c..a63e1877e4533 100644 --- a/compiler/rustc_codegen_ssa/src/target_features.rs +++ b/compiler/rustc_codegen_ssa/src/target_features.rs @@ -10,7 +10,7 @@ use rustc_middle::query::Providers; use rustc_middle::ty::TyCtxt; use rustc_session::parse::feature_err; use rustc_span::{Span, Symbol, sym}; -use rustc_target::target_features; +use rustc_target::target_features::{self, Stability}; use crate::errors; @@ -87,10 +87,7 @@ pub(crate) fn from_target_feature_attr( // But ensure the ABI does not forbid enabling this. // Here we do assume that LLVM doesn't add even more implied features // we don't know about, at least no features that would have ABI effects! - // We skip this check in rustdoc, like we skip all target feature related checks. - if !tcx.sess.opts.actually_rustdoc - && abi_feature_constraints.incompatible.contains(&name.as_str()) - { + if abi_feature_constraints.incompatible.contains(&name.as_str()) { tcx.dcx().emit_err(errors::ForbiddenTargetFeatureAttr { span: item.span(), feature: name.as_str(), @@ -146,13 +143,37 @@ pub(crate) fn provide(providers: &mut Providers) { assert_eq!(cnum, LOCAL_CRATE); if tcx.sess.opts.actually_rustdoc { // HACK: rustdoc would like to pretend that we have all the target features, so we - // have to merge all the lists into one. The result has a "random" stability - // (depending on the order in which we consider features); all places that check - // target stability are expected to check `actually_rustdoc` and do nothing when - // that is set. - rustc_target::target_features::all_rust_features() - .map(|(a, b)| (a.to_string(), b)) - .collect() + // have to merge all the lists into one. To ensure an unstable target never prevents + // a stable one from working, we merge the stability info of all instances of the + // same target feature name, with the "most stable" taking precedence. And then we + // hope that this doesn't cause issues anywhere else in the compiler... + let mut result: UnordMap = Default::default(); + for (name, stability) in rustc_target::target_features::all_rust_features() { + use std::collections::hash_map::Entry; + match result.entry(name.to_owned()) { + Entry::Vacant(vacant_entry) => { + vacant_entry.insert(stability); + } + Entry::Occupied(mut occupied_entry) => { + // Merge the two stabilities, "more stable" taking precedence. + match (occupied_entry.get(), stability) { + (Stability::Stable, _) + | ( + Stability::Unstable { .. }, + Stability::Unstable { .. } | Stability::Forbidden { .. }, + ) + | (Stability::Forbidden { .. }, Stability::Forbidden { .. }) => { + // The stability in the entry is at least as good as the new one, just keep it. + } + _ => { + // Overwrite stabilite. + occupied_entry.insert(stability); + } + } + } + } + } + result } else { tcx.sess .target From 745297e7aa714ea4a657adb7403a18d9766ce2bd Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 26 Feb 2025 17:08:35 +0100 Subject: [PATCH 03/15] Use helper function instead of reimplementing the logic to check if rustdoc should emit crate --- src/librustdoc/html/render/write_shared.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustdoc/html/render/write_shared.rs b/src/librustdoc/html/render/write_shared.rs index a4dec013fc040..adb77653efdcb 100644 --- a/src/librustdoc/html/render/write_shared.rs +++ b/src/librustdoc/html/render/write_shared.rs @@ -153,7 +153,7 @@ fn write_rendered_cross_crate_info( include_sources: bool, ) -> Result<(), Error> { let m = &opt.should_merge; - if opt.emit.is_empty() || opt.emit.contains(&EmitType::InvocationSpecific) { + if opt.should_emit_crate() { if include_sources { write_rendered_cci::(SourcesPart::blank, dst, crates, m)?; } From 98eb2e3e5ac1c2de47c63d71cb8d4244f8174b16 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 26 Feb 2025 17:13:30 +0100 Subject: [PATCH 04/15] Add rustdoc support for `--emit=dep-info[=path]` --- src/librustdoc/config.rs | 28 ++++++++++++++++++++++------ src/librustdoc/core.rs | 24 ++++++++++++++++++------ src/librustdoc/lib.rs | 10 ++++++++-- 3 files changed, 48 insertions(+), 14 deletions(-) diff --git a/src/librustdoc/config.rs b/src/librustdoc/config.rs index fd4d9845c8920..e67fe6c88ea41 100644 --- a/src/librustdoc/config.rs +++ b/src/librustdoc/config.rs @@ -315,23 +315,30 @@ pub(crate) enum ModuleSorting { Alphabetical, } -#[derive(Copy, Clone, Debug, PartialEq, Eq)] +#[derive(Clone, Debug, PartialEq, Eq)] pub(crate) enum EmitType { Unversioned, Toolchain, InvocationSpecific, + DepInfo(Option), } impl FromStr for EmitType { type Err = (); fn from_str(s: &str) -> Result { - use EmitType::*; match s { - "unversioned-shared-resources" => Ok(Unversioned), - "toolchain-shared-resources" => Ok(Toolchain), - "invocation-specific" => Ok(InvocationSpecific), - _ => Err(()), + "unversioned-shared-resources" => Ok(Self::Unversioned), + "toolchain-shared-resources" => Ok(Self::Toolchain), + "invocation-specific" => Ok(Self::InvocationSpecific), + "dep-info" => Ok(Self::DepInfo(None)), + option => { + if let Some(file) = option.strip_prefix("dep-info=") { + Ok(Self::DepInfo(Some(Path::new(file).into()))) + } else { + Err(()) + } + } } } } @@ -340,6 +347,15 @@ impl RenderOptions { pub(crate) fn should_emit_crate(&self) -> bool { self.emit.is_empty() || self.emit.contains(&EmitType::InvocationSpecific) } + + pub(crate) fn dep_info(&self) -> Option> { + for emit in &self.emit { + if let EmitType::DepInfo(file) = emit { + return Some(file.as_deref()); + } + } + None + } } /// Create the input (string or file path) diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index 757a2a6e0dd06..679921c32693f 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -15,11 +15,12 @@ use rustc_hir::def::Res; use rustc_hir::def_id::{DefId, DefIdMap, DefIdSet, LocalDefId}; use rustc_hir::intravisit::{self, Visitor}; use rustc_hir::{HirId, Path}; -use rustc_interface::interface; use rustc_lint::{MissingDoc, late_lint_mod}; use rustc_middle::hir::nested_filter; use rustc_middle::ty::{self, ParamEnv, Ty, TyCtxt}; -use rustc_session::config::{self, CrateType, ErrorOutputType, Input, ResolveDocLinks}; +use rustc_session::config::{ + self, CrateType, ErrorOutputType, Input, OutFileName, OutputType, OutputTypes, ResolveDocLinks, +}; pub(crate) use rustc_session::config::{Options, UnstableOptions}; use rustc_session::{Session, lint}; use rustc_span::source_map; @@ -219,7 +220,7 @@ pub(crate) fn create_config( remap_path_prefix, .. }: RustdocOptions, - RenderOptions { document_private, .. }: &RenderOptions, + render_options: &RenderOptions, ) -> rustc_interface::Config { // Add the doc cfg into the doc build. cfgs.push("doc".to_string()); @@ -245,8 +246,11 @@ pub(crate) fn create_config( let crate_types = if proc_macro_crate { vec![CrateType::ProcMacro] } else { vec![CrateType::Rlib] }; - let resolve_doc_links = - if *document_private { ResolveDocLinks::All } else { ResolveDocLinks::Exported }; + let resolve_doc_links = if render_options.document_private { + ResolveDocLinks::All + } else { + ResolveDocLinks::Exported + }; let test = scrape_examples_options.map(|opts| opts.scrape_tests).unwrap_or(false); // plays with error output here! let sessopts = config::Options { @@ -269,10 +273,18 @@ pub(crate) fn create_config( crate_name, test, remap_path_prefix, + output_types: if let Some(file) = render_options.dep_info() { + OutputTypes::new(&[( + OutputType::DepInfo, + file.map(|f| OutFileName::Real(f.to_path_buf())), + )]) + } else { + OutputTypes::new(&[]) + }, ..Options::default() }; - interface::Config { + rustc_interface::Config { opts: sessopts, crate_cfg: cfgs, crate_check_cfg: check_cfgs, diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index 1f2f8f7d33a4a..c1e6d324d5a1a 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -561,7 +561,7 @@ fn opts() -> Vec { "", "emit", "Comma separated list of types of output for rustdoc to emit", - "[unversioned-shared-resources,toolchain-shared-resources,invocation-specific]", + "[unversioned-shared-resources,toolchain-shared-resources,invocation-specific,dep-info]", ), opt(Unstable, FlagMulti, "", "no-run", "Compile doctests without running them", ""), opt( @@ -890,7 +890,13 @@ fn main_args(early_dcx: &mut EarlyDiagCtxt, at_args: &[String]) { // if we ran coverage, bail early, we don't need to also generate docs at this point // (also we didn't load in any of the useful passes) return; - } else if run_check { + } + + if render_opts.dep_info().is_some() { + rustc_interface::passes::write_dep_info(tcx); + } + + if run_check { // Since we're in "check" mode, no need to generate anything beyond this point. return; } From 46a39f0b27f20848757b60e6d8caf274de7aefaa Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 26 Feb 2025 17:13:48 +0100 Subject: [PATCH 05/15] Add new `Rustdoc::emit` method in `run-make-support` --- src/tools/run-make-support/src/external_deps/rustdoc.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/tools/run-make-support/src/external_deps/rustdoc.rs b/src/tools/run-make-support/src/external_deps/rustdoc.rs index 3c0e9c82f0bbc..8a659cd3d8ab5 100644 --- a/src/tools/run-make-support/src/external_deps/rustdoc.rs +++ b/src/tools/run-make-support/src/external_deps/rustdoc.rs @@ -132,4 +132,11 @@ impl Rustdoc { self.cmd.arg(format); self } + + /// Specify type(s) of output files to generate. + pub fn emit>(&mut self, kinds: S) -> &mut Self { + let kinds = kinds.as_ref(); + self.cmd.arg(format!("--emit={kinds}")); + self + } } From b97310c4491c989e74d42b48483626b988d1f5a6 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 26 Feb 2025 17:14:10 +0100 Subject: [PATCH 06/15] Add run-make test for rustdoc `--emit=dep-info` option --- .../output-default.stdout | 2 +- tests/run-make/rustdoc-dep-info/bar.rs | 1 + tests/run-make/rustdoc-dep-info/doc.md | 1 + tests/run-make/rustdoc-dep-info/foo.rs | 1 + tests/run-make/rustdoc-dep-info/lib.rs | 6 ++++++ tests/run-make/rustdoc-dep-info/rmake.rs | 21 +++++++++++++++++++ 6 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 tests/run-make/rustdoc-dep-info/bar.rs create mode 100644 tests/run-make/rustdoc-dep-info/doc.md create mode 100644 tests/run-make/rustdoc-dep-info/foo.rs create mode 100644 tests/run-make/rustdoc-dep-info/lib.rs create mode 100644 tests/run-make/rustdoc-dep-info/rmake.rs diff --git a/tests/run-make/rustdoc-default-output/output-default.stdout b/tests/run-make/rustdoc-default-output/output-default.stdout index c1b246e849ce4..01f470f6e162b 100644 --- a/tests/run-make/rustdoc-default-output/output-default.stdout +++ b/tests/run-make/rustdoc-default-output/output-default.stdout @@ -153,7 +153,7 @@ Options: --generate-redirect-map Generate JSON file at the top level instead of generating HTML redirection files - --emit [unversioned-shared-resources,toolchain-shared-resources,invocation-specific] + --emit [unversioned-shared-resources,toolchain-shared-resources,invocation-specific,dep-info] Comma separated list of types of output for rustdoc to emit --no-run Compile doctests without running them diff --git a/tests/run-make/rustdoc-dep-info/bar.rs b/tests/run-make/rustdoc-dep-info/bar.rs new file mode 100644 index 0000000000000..76b8dcd05c8fc --- /dev/null +++ b/tests/run-make/rustdoc-dep-info/bar.rs @@ -0,0 +1 @@ +include!("foo.rs"); diff --git a/tests/run-make/rustdoc-dep-info/doc.md b/tests/run-make/rustdoc-dep-info/doc.md new file mode 100644 index 0000000000000..6a4238fc8b610 --- /dev/null +++ b/tests/run-make/rustdoc-dep-info/doc.md @@ -0,0 +1 @@ +blablabla diff --git a/tests/run-make/rustdoc-dep-info/foo.rs b/tests/run-make/rustdoc-dep-info/foo.rs new file mode 100644 index 0000000000000..b76b4321d62aa --- /dev/null +++ b/tests/run-make/rustdoc-dep-info/foo.rs @@ -0,0 +1 @@ +pub fn foo() {} diff --git a/tests/run-make/rustdoc-dep-info/lib.rs b/tests/run-make/rustdoc-dep-info/lib.rs new file mode 100644 index 0000000000000..1f003f79b1f44 --- /dev/null +++ b/tests/run-make/rustdoc-dep-info/lib.rs @@ -0,0 +1,6 @@ +#![crate_name = "foo"] + +#[cfg_attr(doc, doc = include_str!("doc.md"))] +pub struct Bar; + +mod bar; diff --git a/tests/run-make/rustdoc-dep-info/rmake.rs b/tests/run-make/rustdoc-dep-info/rmake.rs new file mode 100644 index 0000000000000..6902bfc21ca20 --- /dev/null +++ b/tests/run-make/rustdoc-dep-info/rmake.rs @@ -0,0 +1,21 @@ +// This is a simple smoke test for rustdoc's `--emit dep-info` feature. It prints out +// information about dependencies in a Makefile-compatible format, as a `.d` file. + +use run_make_support::assertion_helpers::assert_contains; +use run_make_support::{path, rfs, rustdoc}; + +fn main() { + // We're only emitting dep info, so we shouldn't be running static analysis to + // figure out that this program is erroneous. + rustdoc().input("lib.rs").arg("-Zunstable-options").emit("dep-info").run(); + + let content = rfs::read_to_string("foo.d"); + assert_contains(&content, "lib.rs:"); + assert_contains(&content, "foo.rs:"); + assert_contains(&content, "bar.rs:"); + assert_contains(&content, "doc.md:"); + + // Now we check that we can provide a file name to the `dep-info` argument. + rustdoc().input("lib.rs").arg("-Zunstable-options").emit("dep-info=bla.d").run(); + assert!(path("bla.d").exists()); +} From e66bf08f93de71125765d0e7e1a5f5d1550406ea Mon Sep 17 00:00:00 2001 From: Tshepang Mbambo Date: Fri, 28 Feb 2025 16:10:38 +0200 Subject: [PATCH 07/15] sort list --- .../ui/attributes/auxiliary/used_pre_main_constructor.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/ui/attributes/auxiliary/used_pre_main_constructor.rs b/tests/ui/attributes/auxiliary/used_pre_main_constructor.rs index 686dcb277f89e..192594d2d1049 100644 --- a/tests/ui/attributes/auxiliary/used_pre_main_constructor.rs +++ b/tests/ui/attributes/auxiliary/used_pre_main_constructor.rs @@ -10,14 +10,14 @@ #[cfg_attr( any( - target_os = "linux", target_os = "android", + target_os = "dragonfly", target_os = "freebsd", + target_os = "haiku", + target_os = "illumos", + target_os = "linux", target_os = "netbsd", target_os = "openbsd", - target_os = "dragonfly", - target_os = "illumos", - target_os = "haiku" ), link_section = ".init_array" )] From f8091c49ffc2bccce89972b0b4058b92a62ee260 Mon Sep 17 00:00:00 2001 From: Tshepang Mbambo Date: Fri, 28 Feb 2025 16:10:14 +0200 Subject: [PATCH 08/15] qnx: avoid test failure Fixes tests/ui/attributes/used_with_archive.rs fail --- tests/ui/attributes/auxiliary/used_pre_main_constructor.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/ui/attributes/auxiliary/used_pre_main_constructor.rs b/tests/ui/attributes/auxiliary/used_pre_main_constructor.rs index 192594d2d1049..d94572ef5d6cd 100644 --- a/tests/ui/attributes/auxiliary/used_pre_main_constructor.rs +++ b/tests/ui/attributes/auxiliary/used_pre_main_constructor.rs @@ -17,6 +17,7 @@ target_os = "illumos", target_os = "linux", target_os = "netbsd", + target_os = "nto", target_os = "openbsd", ), link_section = ".init_array" From 7fdd193e23c2c1afaa071ee167232c07bd2a4be8 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Fri, 28 Feb 2025 16:48:58 +0300 Subject: [PATCH 09/15] tests: Unignore target modifier tests on all platforms These tests can be `check-pass` and do not need dynamic libraries. Also remove other unnecessary stuff from them. --- .../auxiliary/default_reg_struct_return.rs | 21 +++------------ .../auxiliary/wrong_regparm.rs | 21 +++------------ .../auxiliary/wrong_regparm_and_ret.rs | 21 +++------------ .../defaults_check.error.stderr | 4 +-- tests/ui/target_modifiers/defaults_check.rs | 27 +++++++------------ ...ncompatible_regparm.error_generated.stderr | 4 +-- .../target_modifiers/incompatible_regparm.rs | 23 ++++++---------- tests/ui/target_modifiers/two_flags.rs | 22 +++++---------- .../two_flags.unknown_allowed.stderr | 4 +-- 9 files changed, 43 insertions(+), 104 deletions(-) diff --git a/tests/ui/target_modifiers/auxiliary/default_reg_struct_return.rs b/tests/ui/target_modifiers/auxiliary/default_reg_struct_return.rs index 355e7c56e9462..2bbdc83921105 100644 --- a/tests/ui/target_modifiers/auxiliary/default_reg_struct_return.rs +++ b/tests/ui/target_modifiers/auxiliary/default_reg_struct_return.rs @@ -1,20 +1,7 @@ -//@ compile-flags: --target i686-unknown-linux-gnu -Cpanic=abort -// Auxiliary build problems with aarch64-apple: -// Shared library linking cc seems to convert "-m32" flag into -arch armv4t -// Auxiliary build problems with i686-mingw: linker `cc` not found -//@ only-x86 -//@ ignore-windows -//@ ignore-apple +//@ no-prefer-dynamic +//@ compile-flags: --target i686-unknown-linux-gnu //@ needs-llvm-components: x86 + +#![feature(no_core)] #![crate_type = "rlib"] #![no_core] -#![feature(no_core, lang_items, repr_simd)] - -#[lang = "sized"] -trait Sized {} -#[lang = "copy"] -trait Copy {} - -pub fn somefun() {} - -pub struct S; diff --git a/tests/ui/target_modifiers/auxiliary/wrong_regparm.rs b/tests/ui/target_modifiers/auxiliary/wrong_regparm.rs index 2e16f1ee747c8..267292faecd5a 100644 --- a/tests/ui/target_modifiers/auxiliary/wrong_regparm.rs +++ b/tests/ui/target_modifiers/auxiliary/wrong_regparm.rs @@ -1,20 +1,7 @@ -//@ compile-flags: --target i686-unknown-linux-gnu -Zregparm=2 -Cpanic=abort -// Auxiliary build problems with aarch64-apple: -// Shared library linking cc seems to convert "-m32" flag into -arch armv4t -// Auxiliary build problems with i686-mingw: linker `cc` not found -//@ only-x86 -//@ ignore-windows -//@ ignore-apple +//@ no-prefer-dynamic +//@ compile-flags: --target i686-unknown-linux-gnu -Zregparm=2 //@ needs-llvm-components: x86 + +#![feature(no_core)] #![crate_type = "rlib"] #![no_core] -#![feature(no_core, lang_items, repr_simd)] - -#[lang = "sized"] -trait Sized {} -#[lang = "copy"] -trait Copy {} - -pub fn somefun() {} - -pub struct S; diff --git a/tests/ui/target_modifiers/auxiliary/wrong_regparm_and_ret.rs b/tests/ui/target_modifiers/auxiliary/wrong_regparm_and_ret.rs index 39c6be9d5892e..82ee3e71d16a8 100644 --- a/tests/ui/target_modifiers/auxiliary/wrong_regparm_and_ret.rs +++ b/tests/ui/target_modifiers/auxiliary/wrong_regparm_and_ret.rs @@ -1,20 +1,7 @@ -//@ compile-flags: --target i686-unknown-linux-gnu -Zregparm=2 -Zreg-struct-return=true -Cpanic=abort -// Auxiliary build problems with aarch64-apple: -// Shared library linking cc seems to convert "-m32" flag into -arch armv4t -// Auxiliary build problems with i686-mingw: linker `cc` not found -//@ only-x86 -//@ ignore-windows -//@ ignore-apple +//@ no-prefer-dynamic +//@ compile-flags: --target i686-unknown-linux-gnu -Zregparm=2 -Zreg-struct-return=true //@ needs-llvm-components: x86 + +#![feature(no_core)] #![crate_type = "rlib"] #![no_core] -#![feature(no_core, lang_items, repr_simd)] - -#[lang = "sized"] -trait Sized {} -#[lang = "copy"] -trait Copy {} - -pub fn somefun() {} - -pub struct S; diff --git a/tests/ui/target_modifiers/defaults_check.error.stderr b/tests/ui/target_modifiers/defaults_check.error.stderr index c545dd710690a..4833fe906775c 100644 --- a/tests/ui/target_modifiers/defaults_check.error.stderr +++ b/tests/ui/target_modifiers/defaults_check.error.stderr @@ -1,7 +1,7 @@ error: mixing `-Zreg-struct-return` will cause an ABI mismatch in crate `defaults_check` - --> $DIR/defaults_check.rs:20:1 + --> $DIR/defaults_check.rs:15:1 | -LL | #![crate_type = "rlib"] +LL | #![feature(no_core)] | ^ | = help: the `-Zreg-struct-return` flag modifies the ABI so Rust crates compiled with different values of this flag cannot be used together safely diff --git a/tests/ui/target_modifiers/defaults_check.rs b/tests/ui/target_modifiers/defaults_check.rs index b8f4848d3a422..de72acd32bc18 100644 --- a/tests/ui/target_modifiers/defaults_check.rs +++ b/tests/ui/target_modifiers/defaults_check.rs @@ -1,27 +1,20 @@ // Tests that default unspecified target modifier value in dependency crate is ok linked // with the same value, explicitly specified -//@ aux-crate:default_reg_struct_return=default_reg_struct_return.rs + +//@ aux-build:default_reg_struct_return.rs //@ compile-flags: --target i686-unknown-linux-gnu -Cpanic=abort -//@ revisions:error ok ok_explicit +//@ needs-llvm-components: x86 + +//@ revisions: ok ok_explicit error //@[ok] compile-flags: //@[ok_explicit] compile-flags: -Zreg-struct-return=false //@[error] compile-flags: -Zreg-struct-return=true +//@[ok] check-pass +//@[ok_explicit] check-pass -// Auxiliary build problems with aarch64-apple: -// Shared library linking cc seems to convert "-m32" flag into -arch armv4t -// Auxiliary build problems with i686-mingw: linker `cc` not found -//@ only-x86 -//@ ignore-windows -//@ ignore-apple -//@ needs-llvm-components: x86 -//@[ok] build-pass -//@[ok_explicit] build-pass - -#![crate_type = "rlib"] +#![feature(no_core)] //[error]~^ ERROR mixing `-Zreg-struct-return` will cause an ABI mismatch in crate `defaults_check` +#![crate_type = "rlib"] #![no_core] -#![feature(no_core, lang_items, repr_simd)] -fn foo() { - default_reg_struct_return::somefun(); -} +extern crate default_reg_struct_return; diff --git a/tests/ui/target_modifiers/incompatible_regparm.error_generated.stderr b/tests/ui/target_modifiers/incompatible_regparm.error_generated.stderr index 692fc7a4e3fa7..81015694d05eb 100644 --- a/tests/ui/target_modifiers/incompatible_regparm.error_generated.stderr +++ b/tests/ui/target_modifiers/incompatible_regparm.error_generated.stderr @@ -1,7 +1,7 @@ error: mixing `-Zregparm` will cause an ABI mismatch in crate `incompatible_regparm` - --> $DIR/incompatible_regparm.rs:16:1 + --> $DIR/incompatible_regparm.rs:11:1 | -LL | #![crate_type = "rlib"] +LL | #![feature(no_core)] | ^ | = help: the `-Zregparm` flag modifies the ABI so Rust crates compiled with different values of this flag cannot be used together safely diff --git a/tests/ui/target_modifiers/incompatible_regparm.rs b/tests/ui/target_modifiers/incompatible_regparm.rs index e866c5aa891b0..befe573b276ca 100644 --- a/tests/ui/target_modifiers/incompatible_regparm.rs +++ b/tests/ui/target_modifiers/incompatible_regparm.rs @@ -1,23 +1,16 @@ -//@ aux-crate:wrong_regparm=wrong_regparm.rs -//@ compile-flags: --target i686-unknown-linux-gnu -Zregparm=1 -Cpanic=abort -// Auxiliary build problems with aarch64-apple: -// Shared library linking cc seems to convert "-m32" flag into -arch armv4t -// Auxiliary build problems with i686-mingw: linker `cc` not found -//@ only-x86 -//@ ignore-windows -//@ ignore-apple +//@ aux-build:wrong_regparm.rs +//@ compile-flags: --target i686-unknown-linux-gnu -Zregparm=1 //@ needs-llvm-components: x86 -//@ revisions:error_generated allow_regparm_mismatch allow_no_value +//@ revisions:allow_regparm_mismatch allow_no_value error_generated //@[allow_regparm_mismatch] compile-flags: -Cunsafe-allow-abi-mismatch=regparm -//@[allow_regparm_mismatch] build-pass //@[allow_no_value] compile-flags: -Cunsafe-allow-abi-mismatch +//@[error_generated] compile-flags: +//@[allow_regparm_mismatch] check-pass -#![crate_type = "rlib"] +#![feature(no_core)] //[error_generated]~^ ERROR mixing `-Zregparm` will cause an ABI mismatch in crate `incompatible_regparm` +#![crate_type = "rlib"] #![no_core] -#![feature(no_core, lang_items, repr_simd)] -fn foo() { - wrong_regparm::somefun(); -} +extern crate wrong_regparm; diff --git a/tests/ui/target_modifiers/two_flags.rs b/tests/ui/target_modifiers/two_flags.rs index ca17117a267b1..d5b5a923ca09d 100644 --- a/tests/ui/target_modifiers/two_flags.rs +++ b/tests/ui/target_modifiers/two_flags.rs @@ -1,23 +1,15 @@ -//@ aux-crate:wrong_regparm_and_ret=wrong_regparm_and_ret.rs -//@ compile-flags: --target i686-unknown-linux-gnu -Cpanic=abort -// Auxiliary build problems with aarch64-apple: -// Shared library linking cc seems to convert "-m32" flag into -arch armv4t -// Auxiliary build problems with i686-mingw: linker `cc` not found -//@ only-x86 -//@ ignore-windows -//@ ignore-apple +//@ aux-build:wrong_regparm_and_ret.rs +//@ compile-flags: --target i686-unknown-linux-gnu //@ needs-llvm-components: x86 -//@ revisions:two_allowed unknown_allowed +//@ revisions:two_allowed unknown_allowed //@[two_allowed] compile-flags: -Cunsafe-allow-abi-mismatch=regparm,reg-struct-return -//@[two_allowed] build-pass //@[unknown_allowed] compile-flags: -Cunsafe-allow-abi-mismatch=unknown_flag -Zregparm=2 -Zreg-struct-return=true +//@[two_allowed] check-pass -#![crate_type = "rlib"] +#![feature(no_core)] //[unknown_allowed]~^ ERROR unknown target modifier `unknown_flag`, requested by `-Cunsafe-allow-abi-mismatch=unknown_flag` +#![crate_type = "rlib"] #![no_core] -#![feature(no_core, lang_items, repr_simd)] -fn foo() { - wrong_regparm_and_ret::somefun(); -} +extern crate wrong_regparm_and_ret; diff --git a/tests/ui/target_modifiers/two_flags.unknown_allowed.stderr b/tests/ui/target_modifiers/two_flags.unknown_allowed.stderr index c8040c6e389b5..248bde452f221 100644 --- a/tests/ui/target_modifiers/two_flags.unknown_allowed.stderr +++ b/tests/ui/target_modifiers/two_flags.unknown_allowed.stderr @@ -1,7 +1,7 @@ error: unknown target modifier `unknown_flag`, requested by `-Cunsafe-allow-abi-mismatch=unknown_flag` - --> $DIR/two_flags.rs:16:1 + --> $DIR/two_flags.rs:10:1 | -LL | #![crate_type = "rlib"] +LL | #![feature(no_core)] | ^ error: aborting due to 1 previous error From dc04c0ca48c7285d74a0489354ed7d013dc25799 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 28 Feb 2025 16:51:53 +0100 Subject: [PATCH 10/15] add test --- tests/rustdoc-ui/target-feature-stability.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 tests/rustdoc-ui/target-feature-stability.rs diff --git a/tests/rustdoc-ui/target-feature-stability.rs b/tests/rustdoc-ui/target-feature-stability.rs new file mode 100644 index 0000000000000..4ade9690310e3 --- /dev/null +++ b/tests/rustdoc-ui/target-feature-stability.rs @@ -0,0 +1,18 @@ +//! This is a regression test for , ensuring +//! that we can use the `neon` target feature on ARM-32 targets in rustdoc despite there +//! being a "forbidden" feature of the same name for aarch64, and rustdoc merging the +//! target features of all targets. +//@ check-pass +//@ compile-flags: --target armv7-unknown-linux-gnueabihf + +#![crate_type = "lib"] +#![feature(no_core, lang_items)] +#![feature(arm_target_feature)] +#![no_core] + +#[lang = "sized"] +pub trait Sized {} + +// `fp-armv8` is "forbidden" on aarch64 as we tie it to `neon`. +#[target_feature(enable = "fp-armv8")] +pub fn fun() {} From 107d7ef767b1d4585515cc17db103a03d7a9364e Mon Sep 17 00:00:00 2001 From: Karol Zwolak Date: Fri, 28 Feb 2025 22:37:15 +0100 Subject: [PATCH 11/15] test(codegen): add looping_over_ne_bytes test for #133528 --- .../issues/looping-over-ne-bytes-133528.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 tests/codegen/issues/looping-over-ne-bytes-133528.rs diff --git a/tests/codegen/issues/looping-over-ne-bytes-133528.rs b/tests/codegen/issues/looping-over-ne-bytes-133528.rs new file mode 100644 index 0000000000000..35acf765d690c --- /dev/null +++ b/tests/codegen/issues/looping-over-ne-bytes-133528.rs @@ -0,0 +1,17 @@ +//@ compile-flags: -Copt-level=3 +//@ min-llvm-version: 20 +#![crate_type = "lib"] + +/// Ensure the function is properly optimized +/// In the issue #133528, the function was not getting optimized +/// whereas, a version with `bytes` wrapped into a `black_box` was optimized +/// It was probably a LLVM bug that was fixed in LLVM 20 + +// CHECK-LABEL: @looping_over_ne_bytes +// CHECK: icmp eq i64 %input, -1 +// CHECK-NEXT: ret i1 +#[no_mangle] +fn looping_over_ne_bytes(input: u64) -> bool { + let bytes = input.to_ne_bytes(); + bytes.iter().all(|x| *x == !0) +} From 11e7aaf6e4841274a8023020943e9458700ae3a3 Mon Sep 17 00:00:00 2001 From: Yotam Ofek Date: Sat, 15 Feb 2025 22:36:11 +0000 Subject: [PATCH 12/15] jsondocck: minor cleanups - replace `OnceLock` with `LazyLock` - use `let..else` where applicable --- src/tools/jsondocck/src/main.rs | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/src/tools/jsondocck/src/main.rs b/src/tools/jsondocck/src/main.rs index 7bfa7e3355d3f..3f3a524217a09 100644 --- a/src/tools/jsondocck/src/main.rs +++ b/src/tools/jsondocck/src/main.rs @@ -1,6 +1,6 @@ use std::borrow::Cow; use std::process::ExitCode; -use std::sync::OnceLock; +use std::sync::LazyLock; use std::{env, fs}; use regex::{Regex, RegexBuilder}; @@ -151,8 +151,7 @@ impl CommandKind { } } -static LINE_PATTERN: OnceLock = OnceLock::new(); -fn line_pattern() -> Regex { +static LINE_PATTERN: LazyLock = LazyLock::new(|| { RegexBuilder::new( r#" //@\s+ @@ -165,7 +164,7 @@ fn line_pattern() -> Regex { .unicode(true) .build() .unwrap() -} +}); fn print_err(msg: &str, lineno: usize) { eprintln!("Invalid command: {} on line {}", msg, lineno) @@ -184,21 +183,17 @@ fn get_commands(template: &str) -> Result, ()> { for (lineno, line) in file.split('\n').enumerate() { let lineno = lineno + 1; - let cap = match LINE_PATTERN.get_or_init(line_pattern).captures(line) { - Some(c) => c, - None => continue, + let Some(cap) = LINE_PATTERN.captures(line) else { + continue; }; - let negated = cap.name("negated").unwrap().as_str() == "!"; + let negated = &cap["negated"] == "!"; let args_str = &cap["args"]; - let args = match shlex::split(args_str) { - Some(args) => args, - None => { - print_err(&format!("Invalid arguments to shlex::split: `{args_str}`",), lineno); - errors = true; - continue; - } + let Some(args) = shlex::split(args_str) else { + print_err(&format!("Invalid arguments to shlex::split: `{args_str}`",), lineno); + errors = true; + continue; }; if let Some((kind, path)) = CommandKind::parse(&cap["cmd"], negated, &args) { From 94645f6d102b2e7e720ffa1c759069c378bfd219 Mon Sep 17 00:00:00 2001 From: Yotam Ofek Date: Sat, 15 Feb 2025 22:49:00 +0000 Subject: [PATCH 13/15] jsondocck: catch and error on deprecated syntax --- src/tools/jsondocck/src/main.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/tools/jsondocck/src/main.rs b/src/tools/jsondocck/src/main.rs index 3f3a524217a09..54249fbd9ae71 100644 --- a/src/tools/jsondocck/src/main.rs +++ b/src/tools/jsondocck/src/main.rs @@ -166,6 +166,18 @@ static LINE_PATTERN: LazyLock = LazyLock::new(|| { .unwrap() }); +static DEPRECATED_LINE_PATTERN: LazyLock = LazyLock::new(|| { + RegexBuilder::new( + r#" + //\s+@ + "#, + ) + .ignore_whitespace(true) + .unicode(true) + .build() + .unwrap() +}); + fn print_err(msg: &str, lineno: usize) { eprintln!("Invalid command: {} on line {}", msg, lineno) } @@ -183,6 +195,12 @@ fn get_commands(template: &str) -> Result, ()> { for (lineno, line) in file.split('\n').enumerate() { let lineno = lineno + 1; + if DEPRECATED_LINE_PATTERN.is_match(line) { + print_err("Deprecated command syntax, replace `// @` with `//@ `", lineno); + errors = true; + continue; + } + let Some(cap) = LINE_PATTERN.captures(line) else { continue; }; From 797ef6455e782ec0ec4b6dd725c8ec70746e3e2d Mon Sep 17 00:00:00 2001 From: Yotam Ofek Date: Sat, 15 Feb 2025 22:49:21 +0000 Subject: [PATCH 14/15] htmldocck: catch and error on deprecated syntax --- src/etc/htmldocck.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/etc/htmldocck.py b/src/etc/htmldocck.py index d6b594aca71a2..06fc6518e3b1b 100755 --- a/src/etc/htmldocck.py +++ b/src/etc/htmldocck.py @@ -297,10 +297,24 @@ def filter_line(line): re.X | re.UNICODE, ) +DEPRECATED_LINE_PATTERN = re.compile( + r""" + //\s+@ +""", + re.X | re.UNICODE, +) + def get_commands(template): with io.open(template, encoding="utf-8") as f: for lineno, line in concat_multi_lines(f): + if DEPRECATED_LINE_PATTERN.search(line): + print_err( + lineno, + line, + "Deprecated command syntax, replace `// @` with `//@ `", + ) + continue m = LINE_PATTERN.search(line) if not m: continue From 4c939db0e775df21a0b409b7603eaaf0056e8f86 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 28 Feb 2025 16:56:36 +0100 Subject: [PATCH 15/15] also skip abi_required_features check in rustdoc --- .../rustc_codegen_ssa/src/target_features.rs | 17 +++++++++++------ tests/rustdoc-ui/target-feature-stability.rs | 16 +++++++++++++--- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/target_features.rs b/compiler/rustc_codegen_ssa/src/target_features.rs index a63e1877e4533..95a5e96fe46e4 100644 --- a/compiler/rustc_codegen_ssa/src/target_features.rs +++ b/compiler/rustc_codegen_ssa/src/target_features.rs @@ -87,12 +87,17 @@ pub(crate) fn from_target_feature_attr( // But ensure the ABI does not forbid enabling this. // Here we do assume that LLVM doesn't add even more implied features // we don't know about, at least no features that would have ABI effects! - if abi_feature_constraints.incompatible.contains(&name.as_str()) { - tcx.dcx().emit_err(errors::ForbiddenTargetFeatureAttr { - span: item.span(), - feature: name.as_str(), - reason: "this feature is incompatible with the target ABI", - }); + // We skip this logic in rustdoc, where we want to allow all target features of + // all targets, so we can't check their ABI compatibility and anyway we are not + // generating code so "it's fine". + if !tcx.sess.opts.actually_rustdoc { + if abi_feature_constraints.incompatible.contains(&name.as_str()) { + tcx.dcx().emit_err(errors::ForbiddenTargetFeatureAttr { + span: item.span(), + feature: name.as_str(), + reason: "this feature is incompatible with the target ABI", + }); + } } target_features.push(TargetFeature { name, implied: name != feature_sym }) } diff --git a/tests/rustdoc-ui/target-feature-stability.rs b/tests/rustdoc-ui/target-feature-stability.rs index 4ade9690310e3..17fa3ccfe3e89 100644 --- a/tests/rustdoc-ui/target-feature-stability.rs +++ b/tests/rustdoc-ui/target-feature-stability.rs @@ -1,9 +1,13 @@ //! This is a regression test for , ensuring -//! that we can use the `neon` target feature on ARM-32 targets in rustdoc despite there +//! that we can use the `neon` target feature on ARM32 targets in rustdoc despite there //! being a "forbidden" feature of the same name for aarch64, and rustdoc merging the //! target features of all targets. //@ check-pass -//@ compile-flags: --target armv7-unknown-linux-gnueabihf +//@ revisions: arm aarch64 +//@[arm] compile-flags: --target armv7-unknown-linux-gnueabihf +//@[arm] needs-llvm-components: arm +//@[aarch64] compile-flags: --target aarch64-unknown-none-softfloat +//@[aarch64] needs-llvm-components: aarch64 #![crate_type = "lib"] #![feature(no_core, lang_items)] @@ -15,4 +19,10 @@ pub trait Sized {} // `fp-armv8` is "forbidden" on aarch64 as we tie it to `neon`. #[target_feature(enable = "fp-armv8")] -pub fn fun() {} +pub fn fun1() {} + +// This would usually be rejected as it changes the ABI. +// But we disable that check in rustdoc since we are building "for all targets" and the +// check can't really handle that. +#[target_feature(enable = "soft-float")] +pub fn fun2() {}