Skip to content

Commit 5e8897b

Browse files
committed
Auto merge of rust-lang#68423 - Centril:rollup-bdjykrv, r=Centril
Rollup of 7 pull requests Successful merges: - rust-lang#67686 (Simplify NodeHeader by avoiding slices in BTreeMaps with shared roots) - rust-lang#68140 (Implement `?const` opt-out for trait bounds) - rust-lang#68313 (Options IP_MULTICAST_TTL and IP_MULTICAST_LOOP are 1 byte on BSD) - rust-lang#68328 (Actually pass target LLVM args to LLVM) - rust-lang#68399 (check_match: misc unifications and ICE fixes) - rust-lang#68415 (tidy: fix most clippy warnings) - rust-lang#68416 (lowering: cleanup some hofs) Failed merges: r? @ghost
2 parents 2cf24ab + c1b20b1 commit 5e8897b

File tree

95 files changed

+780
-564
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+780
-564
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3763,6 +3763,7 @@ dependencies = [
37633763
"rustc_hir",
37643764
"rustc_index",
37653765
"rustc_macros",
3766+
"rustc_session",
37663767
"rustc_span",
37673768
"rustc_target",
37683769
"serialize",

src/liballoc/collections/btree/map.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1968,7 +1968,7 @@ where
19681968
(i, false) => i,
19691969
},
19701970
(_, Unbounded) => 0,
1971-
(true, Included(_)) => min_node.keys().len(),
1971+
(true, Included(_)) => min_node.len(),
19721972
(true, Excluded(_)) => 0,
19731973
};
19741974

@@ -1987,9 +1987,9 @@ where
19871987
}
19881988
(i, false) => i,
19891989
},
1990-
(_, Unbounded) => max_node.keys().len(),
1990+
(_, Unbounded) => max_node.len(),
19911991
(true, Included(_)) => 0,
1992-
(true, Excluded(_)) => max_node.keys().len(),
1992+
(true, Excluded(_)) => max_node.len(),
19931993
};
19941994

19951995
if !diverged {

src/liballoc/collections/btree/node.rs

+11-54
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,8 @@ pub const CAPACITY: usize = 2 * B - 1;
5454
/// `NodeHeader` because we do not want unnecessary padding between `len` and the keys.
5555
/// Crucially, `NodeHeader` can be safely transmuted to different K and V. (This is exploited
5656
/// by `as_header`.)
57-
/// See `into_key_slice` for an explanation of K2. K2 cannot be safely transmuted around
58-
/// because the size of `NodeHeader` depends on its alignment!
5957
#[repr(C)]
60-
struct NodeHeader<K, V, K2 = ()> {
58+
struct NodeHeader<K, V> {
6159
/// We use `*const` as opposed to `*mut` so as to be covariant in `K` and `V`.
6260
/// This either points to an actual node or is null.
6361
parent: *const InternalNode<K, V>,
@@ -72,9 +70,6 @@ struct NodeHeader<K, V, K2 = ()> {
7270
/// This next to `parent_idx` to encourage the compiler to join `len` and
7371
/// `parent_idx` into the same 32-bit word, reducing space overhead.
7472
len: u16,
75-
76-
/// See `into_key_slice`.
77-
keys_start: [K2; 0],
7873
}
7974
#[repr(C)]
8075
struct LeafNode<K, V> {
@@ -128,7 +123,7 @@ unsafe impl Sync for NodeHeader<(), ()> {}
128123
// We use just a header in order to save space, since no operation on an empty tree will
129124
// ever take a pointer past the first key.
130125
static EMPTY_ROOT_NODE: NodeHeader<(), ()> =
131-
NodeHeader { parent: ptr::null(), parent_idx: MaybeUninit::uninit(), len: 0, keys_start: [] };
126+
NodeHeader { parent: ptr::null(), parent_idx: MaybeUninit::uninit(), len: 0 };
132127

133128
/// The underlying representation of internal nodes. As with `LeafNode`s, these should be hidden
134129
/// behind `BoxedNode`s to prevent dropping uninitialized keys and values. Any pointer to an
@@ -390,14 +385,13 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
390385
}
391386

392387
/// Borrows a view into the keys stored in the node.
393-
/// Works on all possible nodes, including the shared root.
394-
pub fn keys(&self) -> &[K] {
388+
/// The caller must ensure that the node is not the shared root.
389+
pub unsafe fn keys(&self) -> &[K] {
395390
self.reborrow().into_key_slice()
396391
}
397392

398393
/// Borrows a view into the values stored in the node.
399394
/// The caller must ensure that the node is not the shared root.
400-
/// This function is not public, so doesn't have to support shared roots like `keys` does.
401395
fn vals(&self) -> &[V] {
402396
self.reborrow().into_val_slice()
403397
}
@@ -515,7 +509,6 @@ impl<'a, K, V, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
515509
}
516510

517511
/// The caller must ensure that the node is not the shared root.
518-
/// This function is not public, so doesn't have to support shared roots like `keys` does.
519512
fn keys_mut(&mut self) -> &mut [K] {
520513
unsafe { self.reborrow_mut().into_key_slice_mut() }
521514
}
@@ -527,48 +520,11 @@ impl<'a, K, V, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
527520
}
528521

529522
impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Immut<'a>, K, V, Type> {
530-
fn into_key_slice(self) -> &'a [K] {
531-
// We have to be careful here because we might be pointing to the shared root.
532-
// In that case, we must not create an `&LeafNode`. We could just return
533-
// an empty slice whenever the length is 0 (this includes the shared root),
534-
// but we want to avoid that run-time check.
535-
// Instead, we create a slice pointing into the node whenever possible.
536-
// We can sometimes do this even for the shared root, as the slice will be
537-
// empty and `NodeHeader` contains an empty `keys_start` array.
538-
// We cannot *always* do this because:
539-
// - `keys_start` is not correctly typed because we want `NodeHeader`'s size to
540-
// not depend on the alignment of `K` (needed because `as_header` should be safe).
541-
// For this reason, `NodeHeader` has this `K2` parameter (that's usually `()`
542-
// and hence just adds a size-0-align-1 field, not affecting layout).
543-
// If the correctly typed header is more highly aligned than the allocated header,
544-
// we cannot transmute safely.
545-
// - Even if we can transmute, the offset of a correctly typed `keys_start` might
546-
// be different and outside the bounds of the allocated header!
547-
// So we do an alignment check and a size check first, that will be evaluated
548-
// at compile-time, and only do any run-time check in the rare case that
549-
// the compile-time checks signal danger.
550-
if (mem::align_of::<NodeHeader<K, V, K>>() > mem::align_of::<NodeHeader<K, V>>()
551-
|| mem::size_of::<NodeHeader<K, V, K>>() != mem::size_of::<NodeHeader<K, V>>())
552-
&& self.is_shared_root()
553-
{
554-
&[]
555-
} else {
556-
// If we are a `LeafNode<K, V>`, we can always transmute to
557-
// `NodeHeader<K, V, K>` and `keys_start` always has the same offset
558-
// as the actual `keys`.
559-
// Thanks to the checks above, we know that we can transmute to
560-
// `NodeHeader<K, V, K>` and that `keys_start` will be
561-
// in-bounds of some allocation even if this is the shared root!
562-
// (We might be one-past-the-end, but that is allowed by LLVM.)
563-
// Thus we can use `NodeHeader<K, V, K>`
564-
// to compute the pointer where the keys start.
565-
// This entire hack will become unnecessary once
566-
// <https://github.com/rust-lang/rfcs/pull/2582> lands, then we can just take a raw
567-
// pointer to the `keys` field of `*const InternalNode<K, V>`.
568-
let header = self.as_header() as *const _ as *const NodeHeader<K, V, K>;
569-
let keys = unsafe { &(*header).keys_start as *const _ as *const K };
570-
unsafe { slice::from_raw_parts(keys, self.len()) }
571-
}
523+
/// The caller must ensure that the node is not the shared root.
524+
unsafe fn into_key_slice(self) -> &'a [K] {
525+
debug_assert!(!self.is_shared_root());
526+
// We cannot be the shared root, so `as_leaf` is okay.
527+
slice::from_raw_parts(MaybeUninit::first_ptr(&self.as_leaf().keys), self.len())
572528
}
573529

574530
/// The caller must ensure that the node is not the shared root.
@@ -578,9 +534,10 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Immut<'a>, K, V, Type> {
578534
unsafe { slice::from_raw_parts(MaybeUninit::first_ptr(&self.as_leaf().vals), self.len()) }
579535
}
580536

537+
/// The caller must ensure that the node is not the shared root.
581538
fn into_slices(self) -> (&'a [K], &'a [V]) {
582539
let k = unsafe { ptr::read(&self) };
583-
(k.into_key_slice(), self.into_val_slice())
540+
(unsafe { k.into_key_slice() }, self.into_val_slice())
584541
}
585542
}
586543

src/liballoc/collections/btree/search.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,18 @@ where
6161
{
6262
// This function is defined over all borrow types (immutable, mutable, owned),
6363
// and may be called on the shared root in each case.
64-
// Crucially, we use `keys()` here, i.e., we work with immutable data.
65-
// `keys_mut()` does not support the shared root, so we cannot use it.
6664
// Using `keys()` is fine here even if BorrowType is mutable, as all we return
6765
// is an index -- not a reference.
68-
for (i, k) in node.keys().iter().enumerate() {
69-
match key.cmp(k.borrow()) {
70-
Ordering::Greater => {}
71-
Ordering::Equal => return (i, true),
72-
Ordering::Less => return (i, false),
66+
let len = node.len();
67+
if len > 0 {
68+
let keys = unsafe { node.keys() }; // safe because a non-empty node cannot be the shared root
69+
for (i, k) in keys.iter().enumerate() {
70+
match key.cmp(k.borrow()) {
71+
Ordering::Greater => {}
72+
Ordering::Equal => return (i, true),
73+
Ordering::Less => return (i, false),
74+
}
7375
}
7476
}
75-
(node.keys().len(), false)
77+
(len, false)
7678
}

src/librustc/traits/auto_trait.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,10 @@ impl AutoTraitFinder<'tcx> {
337337
&Err(SelectionError::Unimplemented) => {
338338
if self.is_param_no_infer(pred.skip_binder().trait_ref.substs) {
339339
already_visited.remove(&pred);
340-
self.add_user_pred(&mut user_computed_preds, ty::Predicate::Trait(pred));
340+
self.add_user_pred(
341+
&mut user_computed_preds,
342+
ty::Predicate::Trait(pred, ast::Constness::NotConst),
343+
);
341344
predicates.push_back(pred);
342345
} else {
343346
debug!(
@@ -405,7 +408,7 @@ impl AutoTraitFinder<'tcx> {
405408
let mut should_add_new = true;
406409
user_computed_preds.retain(|&old_pred| {
407410
match (&new_pred, old_pred) {
408-
(&ty::Predicate::Trait(new_trait), ty::Predicate::Trait(old_trait)) => {
411+
(&ty::Predicate::Trait(new_trait, _), ty::Predicate::Trait(old_trait, _)) => {
409412
if new_trait.def_id() == old_trait.def_id() {
410413
let new_substs = new_trait.skip_binder().trait_ref.substs;
411414
let old_substs = old_trait.skip_binder().trait_ref.substs;
@@ -627,7 +630,7 @@ impl AutoTraitFinder<'tcx> {
627630
// We check this by calling is_of_param on the relevant types
628631
// from the various possible predicates
629632
match &predicate {
630-
&ty::Predicate::Trait(p) => {
633+
&ty::Predicate::Trait(p, _) => {
631634
if self.is_param_no_infer(p.skip_binder().trait_ref.substs)
632635
&& !only_projections
633636
&& is_new_pred

src/librustc/traits/engine.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::infer::InferCtxt;
22
use crate::traits::Obligation;
3-
use crate::ty::{self, ToPredicate, Ty, TyCtxt};
3+
use crate::ty::{self, ToPredicate, Ty, TyCtxt, WithConstness};
44
use rustc_hir::def_id::DefId;
55

66
use super::{ChalkFulfillmentContext, FulfillmentContext, FulfillmentError};
@@ -33,7 +33,7 @@ pub trait TraitEngine<'tcx>: 'tcx {
3333
cause,
3434
recursion_depth: 0,
3535
param_env,
36-
predicate: trait_ref.to_predicate(),
36+
predicate: trait_ref.without_const().to_predicate(),
3737
},
3838
);
3939
}

src/librustc/traits/error_reporting/mod.rs

+18-10
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ use crate::ty::error::ExpectedFound;
1919
use crate::ty::fast_reject;
2020
use crate::ty::fold::TypeFolder;
2121
use crate::ty::SubtypePredicate;
22-
use crate::ty::{self, AdtKind, ToPolyTraitRef, ToPredicate, Ty, TyCtxt, TypeFoldable};
22+
use crate::ty::{
23+
self, AdtKind, ToPolyTraitRef, ToPredicate, Ty, TyCtxt, TypeFoldable, WithConstness,
24+
};
2325

2426
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
2527
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder};
@@ -128,15 +130,15 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
128130
}
129131

130132
let (cond, error) = match (cond, error) {
131-
(&ty::Predicate::Trait(..), &ty::Predicate::Trait(ref error)) => (cond, error),
133+
(&ty::Predicate::Trait(..), &ty::Predicate::Trait(ref error, _)) => (cond, error),
132134
_ => {
133135
// FIXME: make this work in other cases too.
134136
return false;
135137
}
136138
};
137139

138140
for implication in super::elaborate_predicates(self.tcx, vec![cond.clone()]) {
139-
if let ty::Predicate::Trait(implication) = implication {
141+
if let ty::Predicate::Trait(implication, _) = implication {
140142
let error = error.to_poly_trait_ref();
141143
let implication = implication.to_poly_trait_ref();
142144
// FIXME: I'm just not taking associated types at all here.
@@ -528,7 +530,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
528530
return;
529531
}
530532
match obligation.predicate {
531-
ty::Predicate::Trait(ref trait_predicate) => {
533+
ty::Predicate::Trait(ref trait_predicate, _) => {
532534
let trait_predicate = self.resolve_vars_if_possible(trait_predicate);
533535

534536
if self.tcx.sess.has_errors() && trait_predicate.references_error() {
@@ -581,7 +583,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
581583
"{}",
582584
message.unwrap_or_else(|| format!(
583585
"the trait bound `{}` is not satisfied{}",
584-
trait_ref.to_predicate(),
586+
trait_ref.without_const().to_predicate(),
585587
post_message,
586588
))
587589
);
@@ -693,7 +695,10 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
693695
trait_pred
694696
});
695697
let unit_obligation = Obligation {
696-
predicate: ty::Predicate::Trait(predicate),
698+
predicate: ty::Predicate::Trait(
699+
predicate,
700+
ast::Constness::NotConst,
701+
),
697702
..obligation.clone()
698703
};
699704
if self.predicate_may_hold(&unit_obligation) {
@@ -986,7 +991,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
986991
) -> PredicateObligation<'tcx> {
987992
let new_trait_ref =
988993
ty::TraitRef { def_id, substs: self.tcx.mk_substs_trait(output_ty, &[]) };
989-
Obligation::new(cause, param_env, new_trait_ref.to_predicate())
994+
Obligation::new(cause, param_env, new_trait_ref.without_const().to_predicate())
990995
}
991996
}
992997

@@ -1074,7 +1079,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
10741079
}
10751080

10761081
let mut err = match predicate {
1077-
ty::Predicate::Trait(ref data) => {
1082+
ty::Predicate::Trait(ref data, _) => {
10781083
let trait_ref = data.to_poly_trait_ref();
10791084
let self_ty = trait_ref.self_ty();
10801085
debug!("self_ty {:?} {:?} trait_ref {:?}", self_ty, self_ty.kind, trait_ref);
@@ -1267,8 +1272,11 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
12671272
)
12681273
.value;
12691274

1270-
let obligation =
1271-
Obligation::new(ObligationCause::dummy(), param_env, cleaned_pred.to_predicate());
1275+
let obligation = Obligation::new(
1276+
ObligationCause::dummy(),
1277+
param_env,
1278+
cleaned_pred.without_const().to_predicate(),
1279+
);
12721280

12731281
self.predicate_may_hold(&obligation)
12741282
})

src/librustc/traits/error_reporting/suggestions.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use super::{
66
use crate::infer::InferCtxt;
77
use crate::traits::object_safety::object_safety_violations;
88
use crate::ty::TypeckTables;
9-
use crate::ty::{self, AdtKind, DefIdTree, ToPredicate, Ty, TyCtxt, TypeFoldable};
9+
use crate::ty::{self, AdtKind, DefIdTree, ToPredicate, Ty, TyCtxt, TypeFoldable, WithConstness};
1010

1111
use rustc_errors::{
1212
error_code, pluralize, struct_span_err, Applicability, DiagnosticBuilder, Style,
@@ -48,7 +48,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
4848
} else {
4949
" where"
5050
},
51-
trait_ref.to_predicate(),
51+
trait_ref.without_const().to_predicate(),
5252
),
5353
Applicability::MachineApplicable,
5454
);
@@ -338,8 +338,11 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
338338
let new_self_ty = self.tcx.mk_imm_ref(self.tcx.lifetimes.re_static, self_ty);
339339
let substs = self.tcx.mk_substs_trait(new_self_ty, &[]);
340340
let new_trait_ref = ty::TraitRef::new(obligation.parent_trait_ref.def_id(), substs);
341-
let new_obligation =
342-
Obligation::new(ObligationCause::dummy(), param_env, new_trait_ref.to_predicate());
341+
let new_obligation = Obligation::new(
342+
ObligationCause::dummy(),
343+
param_env,
344+
new_trait_ref.without_const().to_predicate(),
345+
);
343346
if self.predicate_must_hold_modulo_regions(&new_obligation) {
344347
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
345348
// We have a very specific type of error, where just borrowing this argument
@@ -1120,7 +1123,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
11201123
// the type. The last generator has information about where the bound was introduced. At
11211124
// least one generator should be present for this diagnostic to be modified.
11221125
let (mut trait_ref, mut target_ty) = match obligation.predicate {
1123-
ty::Predicate::Trait(p) => {
1126+
ty::Predicate::Trait(p, _) => {
11241127
(Some(p.skip_binder().trait_ref), Some(p.skip_binder().self_ty()))
11251128
}
11261129
_ => (None, None),
@@ -1543,7 +1546,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
15431546
err.note(&format!("required because it appears within the type `{}`", ty));
15441547
obligated_types.push(ty);
15451548

1546-
let parent_predicate = parent_trait_ref.to_predicate();
1549+
let parent_predicate = parent_trait_ref.without_const().to_predicate();
15471550
if !self.is_recursive_obligation(obligated_types, &data.parent_code) {
15481551
self.note_obligation_cause_code(
15491552
err,
@@ -1560,7 +1563,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
15601563
parent_trait_ref.print_only_trait_path(),
15611564
parent_trait_ref.skip_binder().self_ty()
15621565
));
1563-
let parent_predicate = parent_trait_ref.to_predicate();
1566+
let parent_predicate = parent_trait_ref.without_const().to_predicate();
15641567
self.note_obligation_cause_code(
15651568
err,
15661569
&parent_predicate,

src/librustc/traits/fulfill.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ impl<'a, 'b, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'b, 'tcx> {
311311
}
312312

313313
match obligation.predicate {
314-
ty::Predicate::Trait(ref data) => {
314+
ty::Predicate::Trait(ref data, _) => {
315315
let trait_obligation = obligation.with(data.clone());
316316

317317
if data.is_global() {

0 commit comments

Comments
 (0)