diff --git a/compiler/rustc_errors/src/diagnostic.rs b/compiler/rustc_errors/src/diagnostic.rs index 518c59dba5366..d342359ee294f 100644 --- a/compiler/rustc_errors/src/diagnostic.rs +++ b/compiler/rustc_errors/src/diagnostic.rs @@ -681,6 +681,24 @@ impl Diagnostic { msg: impl Into, suggestions: impl Iterator, applicability: Applicability, + ) -> &mut Self { + self.span_suggestions_with_style( + sp, + msg, + suggestions, + applicability, + SuggestionStyle::ShowCode, + ) + } + + /// [`Diagnostic::span_suggestions()`] but you can set the [`SuggestionStyle`]. + pub fn span_suggestions_with_style( + &mut self, + sp: Span, + msg: impl Into, + suggestions: impl Iterator, + applicability: Applicability, + style: SuggestionStyle, ) -> &mut Self { let mut suggestions: Vec<_> = suggestions.collect(); suggestions.sort(); @@ -691,14 +709,15 @@ impl Diagnostic { self.push_suggestion(CodeSuggestion { substitutions, msg: self.subdiagnostic_message_to_diagnostic_message(msg), - style: SuggestionStyle::ShowCode, + style, applicability, }); self } - /// Prints out a message with multiple suggested edits of the code. - /// See also [`Diagnostic::span_suggestion()`]. + /// Prints out a message with multiple suggested edits of the code, where each edit consists of + /// multiple parts. + /// See also [`Diagnostic::multipart_suggestion()`]. pub fn multipart_suggestions( &mut self, msg: impl Into, @@ -720,6 +739,7 @@ impl Diagnostic { }); self } + /// Prints out a message with a suggested edit of the code. If the suggestion is presented /// inline, it will only show the message and not the suggestion. /// diff --git a/compiler/rustc_macros/src/diagnostics/diagnostic_builder.rs b/compiler/rustc_macros/src/diagnostics/diagnostic_builder.rs index dcbe89251cb36..318675ba08fd8 100644 --- a/compiler/rustc_macros/src/diagnostics/diagnostic_builder.rs +++ b/compiler/rustc_macros/src/diagnostics/diagnostic_builder.rs @@ -433,7 +433,7 @@ impl<'a> DiagnosticDeriveVariantBuilder<'a> { self.formatting_init.extend(code_init); Ok(quote! { - #diag.span_suggestion_with_style( + #diag.span_suggestions_with_style( #span_field, rustc_errors::fluent::#slug, #code_field, diff --git a/compiler/rustc_macros/src/diagnostics/subdiagnostic.rs b/compiler/rustc_macros/src/diagnostics/subdiagnostic.rs index 3d4c3ab9fd7c9..ffeed99974bff 100644 --- a/compiler/rustc_macros/src/diagnostics/subdiagnostic.rs +++ b/compiler/rustc_macros/src/diagnostics/subdiagnostic.rs @@ -11,9 +11,11 @@ use crate::diagnostics::utils::{ }; use proc_macro2::TokenStream; use quote::{format_ident, quote}; -use syn::{spanned::Spanned, Attribute, Meta, MetaList, MetaNameValue, NestedMeta, Path}; +use syn::{spanned::Spanned, Attribute, Meta, MetaList, NestedMeta, Path}; use synstructure::{BindingInfo, Structure, VariantInfo}; +use super::utils::{build_suggestion_code, AllowMultipleAlternatives}; + /// The central struct for constructing the `add_to_diagnostic` method from an annotated struct. pub(crate) struct SubdiagnosticDeriveBuilder { diag: syn::Ident, @@ -393,15 +395,16 @@ impl<'parent, 'a> SubdiagnosticDeriveVariantBuilder<'parent, 'a> { let nested_name = meta.path().segments.last().unwrap().ident.to_string(); let nested_name = nested_name.as_str(); - let Meta::NameValue(MetaNameValue { lit: syn::Lit::Str(value), .. }) = meta else { - throw_invalid_nested_attr!(attr, &nested_attr); - }; - match nested_name { "code" => { - let formatted_str = self.build_format(&value.value(), value.span()); let code_field = new_code_ident(); - code.set_once((code_field, formatted_str), span); + let formatting_init = build_suggestion_code( + &code_field, + meta, + self, + AllowMultipleAlternatives::No, + ); + code.set_once((code_field, formatting_init), span); } _ => throw_invalid_nested_attr!(attr, &nested_attr, |diag| { diag.help("`code` is the only valid nested attribute") @@ -409,14 +412,14 @@ impl<'parent, 'a> SubdiagnosticDeriveVariantBuilder<'parent, 'a> { } } - let Some((code_field, formatted_str)) = code.value() else { + let Some((code_field, formatting_init)) = code.value() else { span_err(span, "`#[suggestion_part(...)]` attribute without `code = \"...\"`") .emit(); return Ok(quote! {}); }; let binding = info.binding; - self.formatting_init.extend(quote! { let #code_field = #formatted_str; }); + self.formatting_init.extend(formatting_init); let code_field = if clone_suggestion_code { quote! { #code_field.clone() } } else { diff --git a/compiler/rustc_macros/src/diagnostics/utils.rs b/compiler/rustc_macros/src/diagnostics/utils.rs index 4fd4adc511267..44f65998ebaa4 100644 --- a/compiler/rustc_macros/src/diagnostics/utils.rs +++ b/compiler/rustc_macros/src/diagnostics/utils.rs @@ -2,7 +2,7 @@ use crate::diagnostics::error::{ span_err, throw_invalid_attr, throw_invalid_nested_attr, throw_span_err, DiagnosticDeriveError, }; use proc_macro::Span; -use proc_macro2::TokenStream; +use proc_macro2::{Ident, TokenStream}; use quote::{format_ident, quote, ToTokens}; use std::cell::RefCell; use std::collections::{BTreeSet, HashMap}; @@ -395,6 +395,82 @@ pub(super) fn build_field_mapping<'v>(variant: &VariantInfo<'v>) -> HashMap TokenStream { + let values = match meta { + // `code = "foo"` + Meta::NameValue(MetaNameValue { lit: syn::Lit::Str(s), .. }) => vec![s], + // `code("foo", "bar")` + Meta::List(MetaList { nested, .. }) => { + if let AllowMultipleAlternatives::No = allow_multiple { + span_err( + meta.span().unwrap(), + "expected exactly one string literal for `code = ...`", + ) + .emit(); + vec![] + } else if nested.is_empty() { + span_err( + meta.span().unwrap(), + "expected at least one string literal for `code(...)`", + ) + .emit(); + vec![] + } else { + nested + .into_iter() + .filter_map(|item| { + if let NestedMeta::Lit(syn::Lit::Str(s)) = item { + Some(s) + } else { + span_err( + item.span().unwrap(), + "`code(...)` must contain only string literals", + ) + .emit(); + None + } + }) + .collect() + } + } + _ => { + span_err( + meta.span().unwrap(), + r#"`code = "..."`/`code(...)` must contain only string literals"#, + ) + .emit(); + vec![] + } + }; + + if let AllowMultipleAlternatives::Yes = allow_multiple { + let formatted_strings: Vec<_> = values + .into_iter() + .map(|value| fields.build_format(&value.value(), value.span())) + .collect(); + quote! { let #code_field = [#(#formatted_strings),*].into_iter(); } + } else if let [value] = values.as_slice() { + let formatted_str = fields.build_format(&value.value(), value.span()); + quote! { let #code_field = #formatted_str; } + } else { + // error handled previously + quote! { let #code_field = String::new(); } + } +} + /// Possible styles for suggestion subdiagnostics. #[derive(Clone, Copy)] pub(super) enum SuggestionKind { @@ -564,21 +640,23 @@ impl SubdiagnosticKind { let nested_name = meta.path().segments.last().unwrap().ident.to_string(); let nested_name = nested_name.as_str(); - let value = match meta { - Meta::NameValue(MetaNameValue { lit: syn::Lit::Str(value), .. }) => value, + let string_value = match meta { + Meta::NameValue(MetaNameValue { lit: syn::Lit::Str(value), .. }) => Some(value), + Meta::Path(_) => throw_invalid_nested_attr!(attr, &nested_attr, |diag| { diag.help("a diagnostic slug must be the first argument to the attribute") }), - _ => { - invalid_nested_attr(attr, &nested_attr).emit(); - continue; - } + _ => None, }; match (nested_name, &mut kind) { ("code", SubdiagnosticKind::Suggestion { code_field, .. }) => { - let formatted_str = fields.build_format(&value.value(), value.span()); - let code_init = quote! { let #code_field = #formatted_str; }; + let code_init = build_suggestion_code( + code_field, + meta, + fields, + AllowMultipleAlternatives::Yes, + ); code.set_once(code_init, span); } ( @@ -586,6 +664,11 @@ impl SubdiagnosticKind { SubdiagnosticKind::Suggestion { ref mut applicability, .. } | SubdiagnosticKind::MultipartSuggestion { ref mut applicability, .. }, ) => { + let Some(value) = string_value else { + invalid_nested_attr(attr, &nested_attr).emit(); + continue; + }; + let value = Applicability::from_str(&value.value()).unwrap_or_else(|()| { span_err(span, "invalid applicability").emit(); Applicability::Unspecified @@ -616,7 +699,7 @@ impl SubdiagnosticKind { init } else { span_err(span, "suggestion without `code = \"...\"`").emit(); - quote! { let #code_field: String = unreachable!(); } + quote! { let #code_field = std::iter::empty(); } }; } SubdiagnosticKind::Label @@ -637,7 +720,7 @@ impl quote::IdentFragment for SubdiagnosticKind { SubdiagnosticKind::Note => write!(f, "note"), SubdiagnosticKind::Help => write!(f, "help"), SubdiagnosticKind::Warn => write!(f, "warn"), - SubdiagnosticKind::Suggestion { .. } => write!(f, "suggestion_with_style"), + SubdiagnosticKind::Suggestion { .. } => write!(f, "suggestions_with_style"), SubdiagnosticKind::MultipartSuggestion { .. } => { write!(f, "multipart_suggestion_with_style") } diff --git a/compiler/rustc_passes/src/stability.rs b/compiler/rustc_passes/src/stability.rs index cfd6acd8d7cd0..9591aeb881f3d 100644 --- a/compiler/rustc_passes/src/stability.rs +++ b/compiler/rustc_passes/src/stability.rs @@ -891,8 +891,25 @@ impl<'tcx> Visitor<'tcx> for CheckTraitImplStable<'tcx> { if let TyKind::Never = t.kind { self.fully_stable = false; } + if let TyKind::BareFn(f) = t.kind { + if rustc_target::spec::abi::is_stable(f.abi.name()).is_err() { + self.fully_stable = false; + } + } intravisit::walk_ty(self, t) } + + fn visit_fn_decl(&mut self, fd: &'tcx hir::FnDecl<'tcx>) { + for ty in fd.inputs { + self.visit_ty(ty) + } + if let hir::FnRetTy::Return(output_ty) = fd.output { + match output_ty.kind { + TyKind::Never => {} // `-> !` is stable + _ => self.visit_ty(output_ty), + } + } + } } /// Given the list of enabled features that were not language features (i.e., that diff --git a/compiler/rustc_session/src/config/sigpipe.rs b/compiler/rustc_session/src/config/sigpipe.rs index a5c94118a47e2..53692ad7cc92b 100644 --- a/compiler/rustc_session/src/config/sigpipe.rs +++ b/compiler/rustc_session/src/config/sigpipe.rs @@ -1,5 +1,13 @@ //! NOTE: Keep these constants in sync with `library/std/src/sys/unix/mod.rs`! +/// The default value if `#[unix_sigpipe]` is not specified. This resolves +/// to `SIG_IGN` in `library/std/src/sys/unix/mod.rs`. +/// +/// Note that `SIG_IGN` has been the Rust default since 2014. See +/// . +#[allow(dead_code)] +pub const DEFAULT: u8 = 0; + /// Do not touch `SIGPIPE`. Use whatever the parent process uses. #[allow(dead_code)] pub const INHERIT: u8 = 1; @@ -15,8 +23,3 @@ pub const SIG_IGN: u8 = 2; /// such as `head -n 1`. #[allow(dead_code)] pub const SIG_DFL: u8 = 3; - -/// `SIG_IGN` has been the Rust default since 2014. See -/// . -#[allow(dead_code)] -pub const DEFAULT: u8 = SIG_IGN; diff --git a/compiler/rustc_target/src/spec/abi.rs b/compiler/rustc_target/src/spec/abi.rs index c915124434b2d..ce45fa13970b4 100644 --- a/compiler/rustc_target/src/spec/abi.rs +++ b/compiler/rustc_target/src/spec/abi.rs @@ -109,175 +109,125 @@ pub enum AbiDisabled { Unrecognized, } -fn gate_feature_post( +pub fn is_enabled( features: &rustc_feature::Features, - feature: Symbol, span: Span, - explain: &'static str, + name: &str, ) -> Result<(), AbiDisabled> { - if !features.enabled(feature) && !span.allows_unstable(feature) { - Err(AbiDisabled::Unstable { feature, explain }) - } else { - Ok(()) + let s = is_stable(name); + if let Err(AbiDisabled::Unstable { feature, .. }) = s { + if features.enabled(feature) || span.allows_unstable(feature) { + return Ok(()); + } } + s } -pub fn is_enabled( - features: &rustc_feature::Features, - span: Span, - name: &str, -) -> Result<(), AbiDisabled> { +pub fn is_stable(name: &str) -> Result<(), AbiDisabled> { match name { // Stable "Rust" | "C" | "cdecl" | "stdcall" | "fastcall" | "aapcs" | "win64" | "sysv64" | "system" => Ok(()), - "rust-intrinsic" => { - gate_feature_post(features, sym::intrinsics, span, "intrinsics are subject to change") - } - "platform-intrinsic" => gate_feature_post( - features, - sym::platform_intrinsics, - span, - "platform intrinsics are experimental and possibly buggy", - ), - "vectorcall" => gate_feature_post( - features, - sym::abi_vectorcall, - span, - "vectorcall is experimental and subject to change", - ), - "thiscall" => gate_feature_post( - features, - sym::abi_thiscall, - span, - "thiscall is experimental and subject to change", - ), - "rust-call" => gate_feature_post( - features, - sym::unboxed_closures, - span, - "rust-call ABI is subject to change", - ), - "rust-cold" => gate_feature_post( - features, - sym::rust_cold_cc, - span, - "rust-cold is experimental and subject to change", - ), - "ptx-kernel" => gate_feature_post( - features, - sym::abi_ptx, - span, - "PTX ABIs are experimental and subject to change", - ), - "unadjusted" => gate_feature_post( - features, - sym::abi_unadjusted, - span, - "unadjusted ABI is an implementation detail and perma-unstable", - ), - "msp430-interrupt" => gate_feature_post( - features, - sym::abi_msp430_interrupt, - span, - "msp430-interrupt ABI is experimental and subject to change", - ), - "x86-interrupt" => gate_feature_post( - features, - sym::abi_x86_interrupt, - span, - "x86-interrupt ABI is experimental and subject to change", - ), - "amdgpu-kernel" => gate_feature_post( - features, - sym::abi_amdgpu_kernel, - span, - "amdgpu-kernel ABI is experimental and subject to change", - ), - "avr-interrupt" | "avr-non-blocking-interrupt" => gate_feature_post( - features, - sym::abi_avr_interrupt, - span, - "avr-interrupt and avr-non-blocking-interrupt ABIs are experimental and subject to change", - ), - "efiapi" => gate_feature_post( - features, - sym::abi_efiapi, - span, - "efiapi ABI is experimental and subject to change", - ), - "C-cmse-nonsecure-call" => gate_feature_post( - features, - sym::abi_c_cmse_nonsecure_call, - span, - "C-cmse-nonsecure-call ABI is experimental and subject to change", - ), - "C-unwind" => gate_feature_post( - features, - sym::c_unwind, - span, - "C-unwind ABI is experimental and subject to change", - ), - "stdcall-unwind" => gate_feature_post( - features, - sym::c_unwind, - span, - "stdcall-unwind ABI is experimental and subject to change", - ), - "system-unwind" => gate_feature_post( - features, - sym::c_unwind, - span, - "system-unwind ABI is experimental and subject to change", - ), - "thiscall-unwind" => gate_feature_post( - features, - sym::c_unwind, - span, - "thiscall-unwind ABI is experimental and subject to change", - ), - "cdecl-unwind" => gate_feature_post( - features, - sym::c_unwind, - span, - "cdecl-unwind ABI is experimental and subject to change", - ), - "fastcall-unwind" => gate_feature_post( - features, - sym::c_unwind, - span, - "fastcall-unwind ABI is experimental and subject to change", - ), - "vectorcall-unwind" => gate_feature_post( - features, - sym::c_unwind, - span, - "vectorcall-unwind ABI is experimental and subject to change", - ), - "aapcs-unwind" => gate_feature_post( - features, - sym::c_unwind, - span, - "aapcs-unwind ABI is experimental and subject to change", - ), - "win64-unwind" => gate_feature_post( - features, - sym::c_unwind, - span, - "win64-unwind ABI is experimental and subject to change", - ), - "sysv64-unwind" => gate_feature_post( - features, - sym::c_unwind, - span, - "sysv64-unwind ABI is experimental and subject to change", - ), - "wasm" => gate_feature_post( - features, - sym::wasm_abi, - span, - "wasm ABI is experimental and subject to change", - ), + "rust-intrinsic" => Err(AbiDisabled::Unstable { + feature: sym::intrinsics, + explain: "intrinsics are subject to change", + }), + "platform-intrinsic" => Err(AbiDisabled::Unstable { + feature: sym::platform_intrinsics, + explain: "platform intrinsics are experimental and possibly buggy", + }), + "vectorcall" => Err(AbiDisabled::Unstable { + feature: sym::abi_vectorcall, + explain: "vectorcall is experimental and subject to change", + }), + "thiscall" => Err(AbiDisabled::Unstable { + feature: sym::abi_thiscall, + explain: "thiscall is experimental and subject to change", + }), + "rust-call" => Err(AbiDisabled::Unstable { + feature: sym::unboxed_closures, + explain: "rust-call ABI is subject to change", + }), + "rust-cold" => Err(AbiDisabled::Unstable { + feature: sym::rust_cold_cc, + explain: "rust-cold is experimental and subject to change", + }), + "ptx-kernel" => Err(AbiDisabled::Unstable { + feature: sym::abi_ptx, + explain: "PTX ABIs are experimental and subject to change", + }), + "unadjusted" => Err(AbiDisabled::Unstable { + feature: sym::abi_unadjusted, + explain: "unadjusted ABI is an implementation detail and perma-unstable", + }), + "msp430-interrupt" => Err(AbiDisabled::Unstable { + feature: sym::abi_msp430_interrupt, + explain: "msp430-interrupt ABI is experimental and subject to change", + }), + "x86-interrupt" => Err(AbiDisabled::Unstable { + feature: sym::abi_x86_interrupt, + explain: "x86-interrupt ABI is experimental and subject to change", + }), + "amdgpu-kernel" => Err(AbiDisabled::Unstable { + feature: sym::abi_amdgpu_kernel, + explain: "amdgpu-kernel ABI is experimental and subject to change", + }), + "avr-interrupt" | "avr-non-blocking-interrupt" => Err(AbiDisabled::Unstable { + feature: sym::abi_avr_interrupt, + explain: "avr-interrupt and avr-non-blocking-interrupt ABIs are experimental and subject to change", + }), + "efiapi" => Err(AbiDisabled::Unstable { + feature: sym::abi_efiapi, + explain: "efiapi ABI is experimental and subject to change", + }), + "C-cmse-nonsecure-call" => Err(AbiDisabled::Unstable { + feature: sym::abi_c_cmse_nonsecure_call, + explain: "C-cmse-nonsecure-call ABI is experimental and subject to change", + }), + "C-unwind" => Err(AbiDisabled::Unstable { + feature: sym::c_unwind, + explain: "C-unwind ABI is experimental and subject to change", + }), + "stdcall-unwind" => Err(AbiDisabled::Unstable { + feature: sym::c_unwind, + explain: "stdcall-unwind ABI is experimental and subject to change", + }), + "system-unwind" => Err(AbiDisabled::Unstable { + feature: sym::c_unwind, + explain: "system-unwind ABI is experimental and subject to change", + }), + "thiscall-unwind" => Err(AbiDisabled::Unstable { + feature: sym::c_unwind, + explain: "thiscall-unwind ABI is experimental and subject to change", + }), + "cdecl-unwind" => Err(AbiDisabled::Unstable { + feature: sym::c_unwind, + explain: "cdecl-unwind ABI is experimental and subject to change", + }), + "fastcall-unwind" => Err(AbiDisabled::Unstable { + feature: sym::c_unwind, + explain: "fastcall-unwind ABI is experimental and subject to change", + }), + "vectorcall-unwind" => Err(AbiDisabled::Unstable { + feature: sym::c_unwind, + explain: "vectorcall-unwind ABI is experimental and subject to change", + }), + "aapcs-unwind" => Err(AbiDisabled::Unstable { + feature: sym::c_unwind, + explain: "aapcs-unwind ABI is experimental and subject to change", + }), + "win64-unwind" => Err(AbiDisabled::Unstable { + feature: sym::c_unwind, + explain: "win64-unwind ABI is experimental and subject to change", + }), + "sysv64-unwind" => Err(AbiDisabled::Unstable { + feature: sym::c_unwind, + explain: "sysv64-unwind ABI is experimental and subject to change", + }), + "wasm" => Err(AbiDisabled::Unstable { + feature: sym::wasm_abi, + explain: "wasm ABI is experimental and subject to change", + }), _ => Err(AbiDisabled::Unrecognized), } } diff --git a/library/proc_macro/src/lib.rs b/library/proc_macro/src/lib.rs index f9c7d3e172cbe..8001fcff6484b 100644 --- a/library/proc_macro/src/lib.rs +++ b/library/proc_macro/src/lib.rs @@ -546,7 +546,7 @@ impl Span { /// Note: The observable result of a macro should only rely on the tokens and /// not on this source text. The result of this function is a best effort to /// be used for diagnostics only. - #[unstable(feature = "proc_macro_span", issue = "54725")] + #[stable(feature = "proc_macro_source_text", since = "CURRENT_RUSTC_VERSION")] pub fn source_text(&self) -> Option { self.0.source_text() } diff --git a/library/std/src/rt.rs b/library/std/src/rt.rs index b8bcdbece0af3..9c2f0c1dd3eb6 100644 --- a/library/std/src/rt.rs +++ b/library/std/src/rt.rs @@ -89,7 +89,7 @@ macro_rules! rtunwrap { // `src/tools/tidy/src/pal.rs` for more info. On all other platforms, `sigpipe` // has a value, but its value is ignored. // -// Even though it is an `u8`, it only ever has 3 values. These are documented in +// Even though it is an `u8`, it only ever has 4 values. These are documented in // `compiler/rustc_session/src/config/sigpipe.rs`. #[cfg_attr(test, allow(dead_code))] unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) { diff --git a/library/std/src/sys/unix/mod.rs b/library/std/src/sys/unix/mod.rs index c84e292eac152..3ae9b47d0d925 100644 --- a/library/std/src/sys/unix/mod.rs +++ b/library/std/src/sys/unix/mod.rs @@ -2,6 +2,7 @@ use crate::ffi::CStr; use crate::io::ErrorKind; +use crate::sync::atomic::{AtomicBool, Ordering}; pub use self::rand::hashmap_random_keys; @@ -163,17 +164,27 @@ pub unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) { // See the other file for docs. NOTE: Make sure to keep them in // sync! mod sigpipe { + pub const DEFAULT: u8 = 0; pub const INHERIT: u8 = 1; pub const SIG_IGN: u8 = 2; pub const SIG_DFL: u8 = 3; } - let handler = match sigpipe { - sigpipe::INHERIT => None, - sigpipe::SIG_IGN => Some(libc::SIG_IGN), - sigpipe::SIG_DFL => Some(libc::SIG_DFL), + let (sigpipe_attr_specified, handler) = match sigpipe { + sigpipe::DEFAULT => (false, Some(libc::SIG_IGN)), + sigpipe::INHERIT => (true, None), + sigpipe::SIG_IGN => (true, Some(libc::SIG_IGN)), + sigpipe::SIG_DFL => (true, Some(libc::SIG_DFL)), _ => unreachable!(), }; + // The bootstrap compiler doesn't know about sigpipe::DEFAULT, and always passes in + // SIG_IGN. This causes some tests to fail because they expect SIGPIPE to be reset to + // default on process spawning (which doesn't happen if #[unix_sigpipe] is specified). + // Since we can't differentiate between the cases here, treat SIG_IGN as DEFAULT + // unconditionally. + if sigpipe_attr_specified && !(cfg!(bootstrap) && sigpipe == sigpipe::SIG_IGN) { + UNIX_SIGPIPE_ATTR_SPECIFIED.store(true, Ordering::Relaxed); + } if let Some(handler) = handler { rtassert!(signal(libc::SIGPIPE, handler) != libc::SIG_ERR); } @@ -181,6 +192,25 @@ pub unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) { } } +// This is set (up to once) in reset_sigpipe. +#[cfg(not(any( + target_os = "espidf", + target_os = "emscripten", + target_os = "fuchsia", + target_os = "horizon" +)))] +static UNIX_SIGPIPE_ATTR_SPECIFIED: AtomicBool = AtomicBool::new(false); + +#[cfg(not(any( + target_os = "espidf", + target_os = "emscripten", + target_os = "fuchsia", + target_os = "horizon" +)))] +pub(crate) fn unix_sigpipe_attr_specified() -> bool { + UNIX_SIGPIPE_ATTR_SPECIFIED.load(Ordering::Relaxed) +} + // SAFETY: must be called only once during runtime cleanup. // NOTE: this is not guaranteed to run, for example when the program aborts. pub unsafe fn cleanup() { diff --git a/library/std/src/sys/unix/process/process_common/tests.rs b/library/std/src/sys/unix/process/process_common/tests.rs index d176b3401c03c..03631e4e33bf5 100644 --- a/library/std/src/sys/unix/process/process_common/tests.rs +++ b/library/std/src/sys/unix/process/process_common/tests.rs @@ -31,41 +31,54 @@ macro_rules! t { ignore )] fn test_process_mask() { - unsafe { - // Test to make sure that a signal mask does not get inherited. - let mut cmd = Command::new(OsStr::new("cat")); - - let mut set = mem::MaybeUninit::::uninit(); - let mut old_set = mem::MaybeUninit::::uninit(); - t!(cvt(sigemptyset(set.as_mut_ptr()))); - t!(cvt(sigaddset(set.as_mut_ptr(), libc::SIGINT))); - t!(cvt_nz(libc::pthread_sigmask(libc::SIG_SETMASK, set.as_ptr(), old_set.as_mut_ptr()))); - - cmd.stdin(Stdio::MakePipe); - cmd.stdout(Stdio::MakePipe); - - let (mut cat, mut pipes) = t!(cmd.spawn(Stdio::Null, true)); - let stdin_write = pipes.stdin.take().unwrap(); - let stdout_read = pipes.stdout.take().unwrap(); - - t!(cvt_nz(libc::pthread_sigmask(libc::SIG_SETMASK, old_set.as_ptr(), ptr::null_mut()))); - - t!(cvt(libc::kill(cat.id() as libc::pid_t, libc::SIGINT))); - // We need to wait until SIGINT is definitely delivered. The - // easiest way is to write something to cat, and try to read it - // back: if SIGINT is unmasked, it'll get delivered when cat is - // next scheduled. - let _ = stdin_write.write(b"Hello"); - drop(stdin_write); - - // Either EOF or failure (EPIPE) is okay. - let mut buf = [0; 5]; - if let Ok(ret) = stdout_read.read(&mut buf) { - assert_eq!(ret, 0); + // Test to make sure that a signal mask *does* get inherited. + fn test_inner(mut cmd: Command) { + unsafe { + let mut set = mem::MaybeUninit::::uninit(); + let mut old_set = mem::MaybeUninit::::uninit(); + t!(cvt(sigemptyset(set.as_mut_ptr()))); + t!(cvt(sigaddset(set.as_mut_ptr(), libc::SIGINT))); + t!(cvt_nz(libc::pthread_sigmask( + libc::SIG_SETMASK, + set.as_ptr(), + old_set.as_mut_ptr() + ))); + + cmd.stdin(Stdio::MakePipe); + cmd.stdout(Stdio::MakePipe); + + let (mut cat, mut pipes) = t!(cmd.spawn(Stdio::Null, true)); + let stdin_write = pipes.stdin.take().unwrap(); + let stdout_read = pipes.stdout.take().unwrap(); + + t!(cvt_nz(libc::pthread_sigmask(libc::SIG_SETMASK, old_set.as_ptr(), ptr::null_mut()))); + + t!(cvt(libc::kill(cat.id() as libc::pid_t, libc::SIGINT))); + // We need to wait until SIGINT is definitely delivered. The + // easiest way is to write something to cat, and try to read it + // back: if SIGINT is unmasked, it'll get delivered when cat is + // next scheduled. + let _ = stdin_write.write(b"Hello"); + drop(stdin_write); + + // Exactly 5 bytes should be read. + let mut buf = [0; 5]; + let ret = t!(stdout_read.read(&mut buf)); + assert_eq!(ret, 5); + assert_eq!(&buf, b"Hello"); + + t!(cat.wait()); } - - t!(cat.wait()); } + + // A plain `Command::new` uses the posix_spawn path on many platforms. + let cmd = Command::new(OsStr::new("cat")); + test_inner(cmd); + + // Specifying `pre_exec` forces the fork/exec path. + let mut cmd = Command::new(OsStr::new("cat")); + unsafe { cmd.pre_exec(Box::new(|| Ok(()))) }; + test_inner(cmd); } #[test] diff --git a/library/std/src/sys/unix/process/process_unix.rs b/library/std/src/sys/unix/process/process_unix.rs index 2ff8e600f7c52..946bd75d941b0 100644 --- a/library/std/src/sys/unix/process/process_unix.rs +++ b/library/std/src/sys/unix/process/process_unix.rs @@ -278,7 +278,7 @@ impl Command { stdio: ChildPipes, maybe_envp: Option<&CStringArray>, ) -> Result { - use crate::sys::{self, cvt_r}; + use crate::sys::{self, cvt_r, unix_sigpipe_attr_specified}; if let Some(fd) = stdio.stdin.fd() { cvt_r(|| libc::dup2(fd, libc::STDIN_FILENO))?; @@ -326,30 +326,26 @@ impl Command { // emscripten has no signal support. #[cfg(not(target_os = "emscripten"))] { - use crate::mem::MaybeUninit; - use crate::sys::cvt_nz; - // Reset signal handling so the child process starts in a - // standardized state. libstd ignores SIGPIPE, and signal-handling - // libraries often set a mask. Child processes inherit ignored - // signals and the signal mask from their parent, but most - // UNIX programs do not reset these things on their own, so we - // need to clean things up now to avoid confusing the program - // we're about to run. - let mut set = MaybeUninit::::uninit(); - cvt(sigemptyset(set.as_mut_ptr()))?; - cvt_nz(libc::pthread_sigmask(libc::SIG_SETMASK, set.as_ptr(), ptr::null_mut()))?; - - #[cfg(target_os = "android")] // see issue #88585 - { - let mut action: libc::sigaction = mem::zeroed(); - action.sa_sigaction = libc::SIG_DFL; - cvt(libc::sigaction(libc::SIGPIPE, &action, ptr::null_mut()))?; - } - #[cfg(not(target_os = "android"))] - { - let ret = sys::signal(libc::SIGPIPE, libc::SIG_DFL); - if ret == libc::SIG_ERR { - return Err(io::Error::last_os_error()); + // Inherit the signal mask from the parent rather than resetting it (i.e. do not call + // pthread_sigmask). + + // If #[unix_sigpipe] is specified, don't reset SIGPIPE to SIG_DFL. + // If #[unix_sigpipe] is not specified, reset SIGPIPE to SIG_DFL for backward compatibility. + // + // #[unix_sigpipe] is an opportunity to change the default here. + if !unix_sigpipe_attr_specified() { + #[cfg(target_os = "android")] // see issue #88585 + { + let mut action: libc::sigaction = mem::zeroed(); + action.sa_sigaction = libc::SIG_DFL; + cvt(libc::sigaction(libc::SIGPIPE, &action, ptr::null_mut()))?; + } + #[cfg(not(target_os = "android"))] + { + let ret = sys::signal(libc::SIGPIPE, libc::SIG_DFL); + if ret == libc::SIG_ERR { + return Err(io::Error::last_os_error()); + } } } } @@ -411,7 +407,7 @@ impl Command { envp: Option<&CStringArray>, ) -> io::Result> { use crate::mem::MaybeUninit; - use crate::sys::{self, cvt_nz}; + use crate::sys::{self, cvt_nz, unix_sigpipe_attr_specified}; if self.get_gid().is_some() || self.get_uid().is_some() @@ -531,13 +527,24 @@ impl Command { cvt_nz(libc::posix_spawnattr_setpgroup(attrs.0.as_mut_ptr(), pgroup))?; } - let mut set = MaybeUninit::::uninit(); - cvt(sigemptyset(set.as_mut_ptr()))?; - cvt_nz(libc::posix_spawnattr_setsigmask(attrs.0.as_mut_ptr(), set.as_ptr()))?; - cvt(sigaddset(set.as_mut_ptr(), libc::SIGPIPE))?; - cvt_nz(libc::posix_spawnattr_setsigdefault(attrs.0.as_mut_ptr(), set.as_ptr()))?; + // Inherit the signal mask from this process rather than resetting it (i.e. do not call + // posix_spawnattr_setsigmask). + + // If #[unix_sigpipe] is specified, don't reset SIGPIPE to SIG_DFL. + // If #[unix_sigpipe] is not specified, reset SIGPIPE to SIG_DFL for backward compatibility. + // + // #[unix_sigpipe] is an opportunity to change the default here. + if !unix_sigpipe_attr_specified() { + let mut default_set = MaybeUninit::::uninit(); + cvt(sigemptyset(default_set.as_mut_ptr()))?; + cvt(sigaddset(default_set.as_mut_ptr(), libc::SIGPIPE))?; + cvt_nz(libc::posix_spawnattr_setsigdefault( + attrs.0.as_mut_ptr(), + default_set.as_ptr(), + ))?; + flags |= libc::POSIX_SPAWN_SETSIGDEF; + } - flags |= libc::POSIX_SPAWN_SETSIGDEF | libc::POSIX_SPAWN_SETSIGMASK; cvt_nz(libc::posix_spawnattr_setflags(attrs.0.as_mut_ptr(), flags as _))?; // Make sure we synchronize access to the global `environ` resource diff --git a/src/ci/docker/host-x86_64/x86_64-gnu-tools/browser-ui-test.version b/src/ci/docker/host-x86_64/x86_64-gnu-tools/browser-ui-test.version index 0ec9201397fe4..cc96715b28572 100644 --- a/src/ci/docker/host-x86_64/x86_64-gnu-tools/browser-ui-test.version +++ b/src/ci/docker/host-x86_64/x86_64-gnu-tools/browser-ui-test.version @@ -1 +1 @@ -0.12.6 \ No newline at end of file +0.12.7 \ No newline at end of file diff --git a/src/doc/unstable-book/src/language-features/unix-sigpipe.md b/src/doc/unstable-book/src/language-features/unix-sigpipe.md index aa39b6eb2886f..7ed6a7de895c1 100644 --- a/src/doc/unstable-book/src/language-features/unix-sigpipe.md +++ b/src/doc/unstable-book/src/language-features/unix-sigpipe.md @@ -36,7 +36,7 @@ hello world Set the `SIGPIPE` handler to `SIG_IGN` before invoking `fn main()`. This will result in `ErrorKind::BrokenPipe` errors if you program tries to write to a closed pipe. This is normally what you want if you for example write socket servers, socket clients, or pipe peers. -This is what libstd has done by default since 2014. Omitting `#[unix_sigpipe = "..."]` is the same as having `#[unix_sigpipe = "sig_ign"]`. +This is what libstd has done by default since 2014. (However, see the note on child processes below.) ### Example @@ -52,3 +52,11 @@ hello world thread 'main' panicked at 'failed printing to stdout: Broken pipe (os error 32)', library/std/src/io/stdio.rs:1016:9 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace ``` + +### Note on child processes + +When spawning child processes, the legacy Rust behavior if `#[unix_sigpipe]` is not specified is to +reset `SIGPIPE` to `SIG_DFL`. + +If `#[unix_sigpipe = "..."]` is specified, no matter what its value is, the signal disposition of +`SIGPIPE` is no longer reset. This means that the child inherits the parent's `SIGPIPE` behavior. diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index cd56d73e7d47b..eeec6f8fee778 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -1884,7 +1884,7 @@ fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut Buffer) { if !it.is_mod() { let path: String = cx.current.iter().map(|s| s.as_str()).intersperse("::").collect(); - write!(buffer, "

In {}

", path); + write!(buffer, "

In {}

", path); } // Closes sidebar-elems div. diff --git a/src/librustdoc/html/static/css/rustdoc.css b/src/librustdoc/html/static/css/rustdoc.css index 7b6f52b0acf69..b4408e64740bc 100644 --- a/src/librustdoc/html/static/css/rustdoc.css +++ b/src/librustdoc/html/static/css/rustdoc.css @@ -171,7 +171,7 @@ h1.fqn { Rustdoc-generated h2 section headings (e.g. "Implementations", "Required Methods", etc) Underlines elsewhere in the documentation break up visual flow and tend to invert section hierarchies. */ -h2, +.content h2, .top-doc .docblock > h3, .top-doc .docblock > h4 { border-bottom: 1px solid var(--headings-border-bottom-color); @@ -397,15 +397,6 @@ img { left: 0; } -.sidebar-elems, -.sidebar > .location { - padding-left: 24px; -} - -.sidebar .location { - overflow-wrap: anywhere; -} - .rustdoc.source .sidebar { width: 50px; min-width: 0px; @@ -505,8 +496,8 @@ ul.block, .block li { } .block a, -.sidebar h3 a, -h2.location a { +.sidebar h2 a, +.sidebar h3 a { display: block; padding: 0.25rem; margin-left: -0.25rem; @@ -516,8 +507,7 @@ h2.location a { } .sidebar h2 { - border-bottom: none; - font-weight: 500; + overflow-wrap: anywhere; padding: 0; margin: 0; margin-top: 0.7rem; @@ -526,11 +516,15 @@ h2.location a { .sidebar h3 { font-size: 1.125rem; /* 18px */ - font-weight: 500; padding: 0; margin: 0; } +.sidebar-elems, +.sidebar > h2 { + padding-left: 24px; +} + .sidebar a, .sidebar .current { color: var(--sidebar-link-color); } @@ -1788,18 +1782,10 @@ in storage.js width: 0; } - .mobile-topbar .location a { - padding: 0; - margin: 0; - } - - .mobile-topbar .location { - border: none; - padding: 0; + .mobile-topbar h2 { + padding-bottom: 0; margin: auto 0.5em auto auto; - text-overflow: ellipsis; overflow: hidden; - white-space: nowrap; /* Rare exception to specifying font sizes in rem. Since the topbar height is specified in pixels, this also has to be specified in pixels to avoid overflowing the topbar when the user sets a bigger @@ -1807,6 +1793,13 @@ in storage.js font-size: 24px; } + .mobile-topbar h2 a { + display: block; + text-overflow: ellipsis; + overflow: hidden; + white-space: nowrap; + } + .mobile-topbar .logo-container { max-height: 45px; } diff --git a/src/librustdoc/html/static/js/main.js b/src/librustdoc/html/static/js/main.js index 1251d6bfda352..d8f4bc2db22e1 100644 --- a/src/librustdoc/html/static/js/main.js +++ b/src/librustdoc/html/static/js/main.js @@ -55,7 +55,7 @@ function blurHandler(event, parentElem, hideCallback) { function setMobileTopbar() { // FIXME: It would be nicer to generate this text content directly in HTML, // but with the current code it's hard to get the right information in the right place. - const mobileLocationTitle = document.querySelector(".mobile-topbar h2.location"); + const mobileLocationTitle = document.querySelector(".mobile-topbar h2"); const locationTitle = document.querySelector(".sidebar h2.location"); if (mobileLocationTitle && locationTitle) { mobileLocationTitle.innerHTML = locationTitle.innerHTML; diff --git a/src/librustdoc/html/templates/page.html b/src/librustdoc/html/templates/page.html index 123bd576d6463..20a314a1c00e3 100644 --- a/src/librustdoc/html/templates/page.html +++ b/src/librustdoc/html/templates/page.html @@ -85,7 +85,7 @@ {%- endif -%} {#- -#} {#- -#} -

{#- -#} +

{#- -#} {#- -#} {%- endif -%}