Skip to content

Commit

Permalink
Auto merge of #123230 - matthiaskrgr:rollup-4twuzj4, r=matthiaskrgr
Browse files Browse the repository at this point in the history
Rollup of 5 pull requests

Successful merges:

 - #121573 (unix_sigpipe: Add test for SIGPIPE disposition in child processes)
 - #123170 (Replace regions in const canonical vars' types with `'static` in next-solver canonicalizer)
 - #123200 (KCFI: Require -C panic=abort)
 - #123201 (Improve wording in std::any explanation)
 - #123224 (compiletest: print reason for failing to read tests)

r? `@ghost`
`@rustbot` modify labels: rollup
  • Loading branch information
bors committed Mar 30, 2024
2 parents 40116ad + 9b067df commit 1852728
Show file tree
Hide file tree
Showing 20 changed files with 234 additions and 79 deletions.
4 changes: 4 additions & 0 deletions compiler/rustc_middle/src/ty/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ impl<'tcx> rustc_type_ir::new::Region<TyCtxt<'tcx>> for Region<'tcx> {
fn new_anon_bound(tcx: TyCtxt<'tcx>, debruijn: ty::DebruijnIndex, var: ty::BoundVar) -> Self {
Region::new_bound(tcx, debruijn, ty::BoundRegion { var, kind: ty::BoundRegionKind::BrAnon })
}

fn new_static(tcx: TyCtxt<'tcx>) -> Self {
tcx.lifetimes.re_static
}
}

/// Region utilities
Expand Down
89 changes: 58 additions & 31 deletions compiler/rustc_next_trait_solver/src/canonicalizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,7 @@ impl<Infcx: InferCtxtLike<Interner = I>, I: Interner> TypeFolder<I>
Region::new_anon_bound(self.interner(), self.binder_index, var)
}

fn fold_ty(&mut self, t: I::Ty) -> I::Ty
where
I::Ty: TypeSuperFoldable<I>,
{
fn fold_ty(&mut self, t: I::Ty) -> I::Ty {
let kind = match t.kind() {
ty::Infer(i) => match i {
ty::TyVar(vid) => {
Expand Down Expand Up @@ -378,47 +375,48 @@ impl<Infcx: InferCtxtLike<Interner = I>, I: Interner> TypeFolder<I>
Ty::new_anon_bound(self.interner(), self.binder_index, var)
}

fn fold_const(&mut self, c: I::Const) -> I::Const
where
I::Const: TypeSuperFoldable<I>,
{
fn fold_const(&mut self, c: I::Const) -> I::Const {
// We could canonicalize all consts with static types, but the only ones we
// *really* need to worry about are the ones that we end up putting into `CanonicalVarKind`
// since canonical vars can't reference other canonical vars.
let ty = c
.ty()
.fold_with(&mut RegionsToStatic { interner: self.interner(), binder: ty::INNERMOST });
let kind = match c.kind() {
ty::ConstKind::Infer(i) => {
// FIXME: we should fold the ty too eventually
match i {
ty::InferConst::Var(vid) => {
assert_eq!(
self.infcx.root_ct_var(vid),
vid,
"region vid should have been resolved fully before canonicalization"
);
assert_eq!(
self.infcx.probe_ct_var(vid),
None,
"region vid should have been resolved fully before canonicalization"
);
CanonicalVarKind::Const(self.infcx.universe_of_ct(vid).unwrap(), c.ty())
}
ty::InferConst::EffectVar(_) => CanonicalVarKind::Effect,
ty::InferConst::Fresh(_) => todo!(),
ty::ConstKind::Infer(i) => match i {
ty::InferConst::Var(vid) => {
assert_eq!(
self.infcx.root_ct_var(vid),
vid,
"region vid should have been resolved fully before canonicalization"
);
assert_eq!(
self.infcx.probe_ct_var(vid),
None,
"region vid should have been resolved fully before canonicalization"
);
CanonicalVarKind::Const(self.infcx.universe_of_ct(vid).unwrap(), ty)
}
}
ty::InferConst::EffectVar(_) => CanonicalVarKind::Effect,
ty::InferConst::Fresh(_) => todo!(),
},
ty::ConstKind::Placeholder(placeholder) => match self.canonicalize_mode {
CanonicalizeMode::Input => CanonicalVarKind::PlaceholderConst(
PlaceholderLike::new(placeholder.universe(), self.variables.len().into()),
c.ty(),
ty,
),
CanonicalizeMode::Response { .. } => {
CanonicalVarKind::PlaceholderConst(placeholder, c.ty())
CanonicalVarKind::PlaceholderConst(placeholder, ty)
}
},
ty::ConstKind::Param(_) => match self.canonicalize_mode {
CanonicalizeMode::Input => CanonicalVarKind::PlaceholderConst(
PlaceholderLike::new(ty::UniverseIndex::ROOT, self.variables.len().into()),
c.ty(),
ty,
),
CanonicalizeMode::Response { .. } => panic!("param ty in response: {c:?}"),
},
// FIXME: See comment above -- we could fold the region separately or something.
ty::ConstKind::Bound(_, _)
| ty::ConstKind::Unevaluated(_)
| ty::ConstKind::Value(_)
Expand All @@ -435,6 +433,35 @@ impl<Infcx: InferCtxtLike<Interner = I>, I: Interner> TypeFolder<I>
}),
);

Const::new_anon_bound(self.interner(), self.binder_index, var, c.ty())
Const::new_anon_bound(self.interner(), self.binder_index, var, ty)
}
}

struct RegionsToStatic<I> {
interner: I,
binder: ty::DebruijnIndex,
}

impl<I: Interner> TypeFolder<I> for RegionsToStatic<I> {
fn interner(&self) -> I {
self.interner
}

fn fold_binder<T>(&mut self, t: I::Binder<T>) -> I::Binder<T>
where
T: TypeFoldable<I>,
I::Binder<T>: TypeSuperFoldable<I>,
{
self.binder.shift_in(1);
let t = t.fold_with(self);
self.binder.shift_out(1);
t
}

fn fold_region(&mut self, r: I::Region) -> I::Region {
match r.kind() {
ty::ReBound(db, _) if self.binder > db => r,
_ => Region::new_static(self.interner()),
}
}
}
2 changes: 2 additions & 0 deletions compiler/rustc_session/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ session_sanitizer_cfi_requires_lto = `-Zsanitizer=cfi` requires `-Clto` or `-Cli
session_sanitizer_cfi_requires_single_codegen_unit = `-Zsanitizer=cfi` with `-Clto` requires `-Ccodegen-units=1`
session_sanitizer_kcfi_requires_panic_abort = `-Z sanitizer=kcfi` requires `-C panic=abort`
session_sanitizer_not_supported = {$us} sanitizer is not supported for this target
session_sanitizers_not_supported = {$us} sanitizers are not supported for this target
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_session/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ pub(crate) struct SanitizerCfiGeneralizePointersRequiresCfi;
#[diag(session_sanitizer_cfi_normalize_integers_requires_cfi)]
pub(crate) struct SanitizerCfiNormalizeIntegersRequiresCfi;

#[derive(Diagnostic)]
#[diag(session_sanitizer_kcfi_requires_panic_abort)]
pub(crate) struct SanitizerKcfiRequiresPanicAbort;

#[derive(Diagnostic)]
#[diag(session_split_lto_unit_requires_lto)]
pub(crate) struct SplitLtoUnitRequiresLto;
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_session/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1211,6 +1211,11 @@ fn validate_commandline_args_with_session_available(sess: &Session) {
sess.dcx().emit_err(errors::SanitizerCfiRequiresLto);
}

// KCFI requires panic=abort
if sess.is_sanitizer_kcfi_enabled() && sess.panic_strategy() != PanicStrategy::Abort {
sess.dcx().emit_err(errors::SanitizerKcfiRequiresPanicAbort);
}

// LLVM CFI using rustc LTO requires a single codegen unit.
if sess.is_sanitizer_cfi_enabled()
&& sess.lto() == config::Lto::Fat
Expand Down
51 changes: 11 additions & 40 deletions compiler/rustc_type_ir/src/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,31 +136,21 @@ pub trait TypeFolder<I: Interner>: FallibleTypeFolder<I, Error = Never> {
t.super_fold_with(self)
}

fn fold_ty(&mut self, t: I::Ty) -> I::Ty
where
I::Ty: TypeSuperFoldable<I>,
{
fn fold_ty(&mut self, t: I::Ty) -> I::Ty {
t.super_fold_with(self)
}

// The default region folder is a no-op because `Region` is non-recursive
// and has no `super_fold_with` method to call. That also explains the
// lack of `I::Region: TypeSuperFoldable<I>` bound on this method.
// and has no `super_fold_with` method to call.
fn fold_region(&mut self, r: I::Region) -> I::Region {
r
}

fn fold_const(&mut self, c: I::Const) -> I::Const
where
I::Const: TypeSuperFoldable<I>,
{
fn fold_const(&mut self, c: I::Const) -> I::Const {
c.super_fold_with(self)
}

fn fold_predicate(&mut self, p: I::Predicate) -> I::Predicate
where
I::Predicate: TypeSuperFoldable<I>,
{
fn fold_predicate(&mut self, p: I::Predicate) -> I::Predicate {
p.super_fold_with(self)
}
}
Expand All @@ -185,31 +175,21 @@ pub trait FallibleTypeFolder<I: Interner>: Sized {
t.try_super_fold_with(self)
}

fn try_fold_ty(&mut self, t: I::Ty) -> Result<I::Ty, Self::Error>
where
I::Ty: TypeSuperFoldable<I>,
{
fn try_fold_ty(&mut self, t: I::Ty) -> Result<I::Ty, Self::Error> {
t.try_super_fold_with(self)
}

// The default region folder is a no-op because `Region` is non-recursive
// and has no `super_fold_with` method to call. That also explains the
// lack of `I::Region: TypeSuperFoldable<I>` bound on this method.
// and has no `super_fold_with` method to call.
fn try_fold_region(&mut self, r: I::Region) -> Result<I::Region, Self::Error> {
Ok(r)
}

fn try_fold_const(&mut self, c: I::Const) -> Result<I::Const, Self::Error>
where
I::Const: TypeSuperFoldable<I>,
{
fn try_fold_const(&mut self, c: I::Const) -> Result<I::Const, Self::Error> {
c.try_super_fold_with(self)
}

fn try_fold_predicate(&mut self, p: I::Predicate) -> Result<I::Predicate, Self::Error>
where
I::Predicate: TypeSuperFoldable<I>,
{
fn try_fold_predicate(&mut self, p: I::Predicate) -> Result<I::Predicate, Self::Error> {
p.try_super_fold_with(self)
}
}
Expand All @@ -234,28 +214,19 @@ where
Ok(self.fold_binder(t))
}

fn try_fold_ty(&mut self, t: I::Ty) -> Result<I::Ty, Never>
where
I::Ty: TypeSuperFoldable<I>,
{
fn try_fold_ty(&mut self, t: I::Ty) -> Result<I::Ty, Never> {
Ok(self.fold_ty(t))
}

fn try_fold_region(&mut self, r: I::Region) -> Result<I::Region, Never> {
Ok(self.fold_region(r))
}

fn try_fold_const(&mut self, c: I::Const) -> Result<I::Const, Never>
where
I::Const: TypeSuperFoldable<I>,
{
fn try_fold_const(&mut self, c: I::Const) -> Result<I::Const, Never> {
Ok(self.fold_const(c))
}

fn try_fold_predicate(&mut self, p: I::Predicate) -> Result<I::Predicate, Never>
where
I::Predicate: TypeSuperFoldable<I>,
{
fn try_fold_predicate(&mut self, p: I::Predicate) -> Result<I::Predicate, Never> {
Ok(self.fold_predicate(p))
}
}
Expand Down
13 changes: 11 additions & 2 deletions compiler/rustc_type_ir/src/interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ use smallvec::SmallVec;
use std::fmt::Debug;
use std::hash::Hash;

use crate::fold::TypeSuperFoldable;
use crate::visit::{Flags, TypeSuperVisitable, TypeVisitable};
use crate::{
new, BoundVar, BoundVars, CanonicalVarInfo, ConstKind, DebugWithInfcx, RegionKind, TyKind,
UniverseIndex,
};

pub trait Interner: Sized {
pub trait Interner: Sized + Copy {
type DefId: Copy + Debug + Hash + Eq;
type AdtDef: Copy + Debug + Hash + Eq;

Expand All @@ -34,6 +35,7 @@ pub trait Interner: Sized {
+ Into<Self::GenericArg>
+ IntoKind<Kind = TyKind<Self>>
+ TypeSuperVisitable<Self>
+ TypeSuperFoldable<Self>
+ Flags
+ new::Ty<Self>;
type Tys: Copy + Debug + Hash + Eq + IntoIterator<Item = Self::Ty>;
Expand All @@ -57,6 +59,7 @@ pub trait Interner: Sized {
+ IntoKind<Kind = ConstKind<Self>>
+ ConstTy<Self>
+ TypeSuperVisitable<Self>
+ TypeSuperFoldable<Self>
+ Flags
+ new::Const<Self>;
type AliasConst: Copy + DebugWithInfcx<Self> + Hash + Eq;
Expand All @@ -82,7 +85,13 @@ pub trait Interner: Sized {
type PlaceholderRegion: Copy + Debug + Hash + Eq + PlaceholderLike;

// Predicates
type Predicate: Copy + Debug + Hash + Eq + TypeSuperVisitable<Self> + Flags;
type Predicate: Copy
+ Debug
+ Hash
+ Eq
+ TypeSuperVisitable<Self>
+ TypeSuperFoldable<Self>
+ Flags;
type TraitPredicate: Copy + Debug + Hash + Eq;
type RegionOutlivesPredicate: Copy + Debug + Hash + Eq;
type TypeOutlivesPredicate: Copy + Debug + Hash + Eq;
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_type_ir/src/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ pub trait Ty<I: Interner<Ty = Self>> {

pub trait Region<I: Interner<Region = Self>> {
fn new_anon_bound(interner: I, debruijn: DebruijnIndex, var: BoundVar) -> Self;

fn new_static(interner: I) -> Self;
}

pub trait Const<I: Interner<Const = Self>> {
Expand Down
8 changes: 4 additions & 4 deletions library/core/src/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,18 @@
//!
//! ## Examples
//!
//! Consider a situation where we want to log out a value passed to a function.
//! We know the value we're working on implements Debug, but we don't know its
//! Consider a situation where we want to log a value passed to a function.
//! We know the value we're working on implements `Debug`, but we don't know its
//! concrete type. We want to give special treatment to certain types: in this
//! case printing out the length of String values prior to their value.
//! case printing out the length of `String` values prior to their value.
//! We don't know the concrete type of our value at compile time, so we need to
//! use runtime reflection instead.
//!
//! ```rust
//! use std::fmt::Debug;
//! use std::any::Any;
//!
//! // Logger function for any type that implements Debug.
//! // Logger function for any type that implements `Debug`.
//! fn log<T: Any + Debug>(value: &T) {
//! let value_any = value as &dyn Any;
//!
Expand Down
4 changes: 3 additions & 1 deletion src/tools/compiletest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,9 @@ pub fn make_tests(
&modified_tests,
&mut poisoned,
)
.unwrap_or_else(|_| panic!("Could not read tests from {}", config.src_base.display()));
.unwrap_or_else(|reason| {
panic!("Could not read tests from {}: {reason}", config.src_base.display())
});

if poisoned {
eprintln!();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// It is UB to unwind out of `fn start()` according to
// https://doc.rust-lang.org/beta/unstable-book/language-features/start.html so
// panic with abort to avoid UB:
//@ compile-flags: -Cpanic=abort
//@ no-prefer-dynamic so panic=abort works

#![feature(start, rustc_private)]

extern crate libc;

// Use #[start] so we don't have a runtime that messes with SIGPIPE.
#[start]
fn start(argc: isize, argv: *const *const u8) -> isize {
assert_eq!(argc, 2, "Must pass SIG_IGN or SIG_DFL as first arg");
let arg1 = unsafe { std::ffi::CStr::from_ptr(*argv.offset(1) as *const libc::c_char) }
.to_str()
.unwrap();

let expected = match arg1 {
"SIG_IGN" => libc::SIG_IGN,
"SIG_DFL" => libc::SIG_DFL,
arg => panic!("Must pass SIG_IGN or SIG_DFL as first arg. Got: {}", arg),
};

let actual = unsafe {
let mut actual: libc::sigaction = std::mem::zeroed();
libc::sigaction(libc::SIGPIPE, std::ptr::null(), &mut actual);
actual.sa_sigaction
};

assert_eq!(actual, expected, "actual and expected SIGPIPE disposition in child differs");

0
}
Loading

0 comments on commit 1852728

Please sign in to comment.