Skip to content

Commit 8afe9ff

Browse files
committed
Auto merge of #151911 - JonathanBrouwer:rollup-ACi3J2a, r=JonathanBrouwer
Rollup of 3 pull requests Successful merges: - #151908 (Remove unused method `DroplessArena::contains_slice`) - #151850 (refactor: add an `enum DerefAdjustKind` in favor of `Option<OverloadedDeref>`) - #151889 (Fix ICE when parsing frontmatter without newline)
2 parents 78865ca + 874c5d2 commit 8afe9ff

26 files changed

Lines changed: 110 additions & 86 deletions

File tree

compiler/rustc_arena/src/lib.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -510,19 +510,6 @@ impl DroplessArena {
510510
}
511511
}
512512

513-
/// Used by `Lift` to check whether this slice is allocated
514-
/// in this arena.
515-
#[inline]
516-
pub fn contains_slice<T>(&self, slice: &[T]) -> bool {
517-
for chunk in self.chunks.borrow_mut().iter_mut() {
518-
let ptr = slice.as_ptr().cast::<u8>().cast_mut();
519-
if chunk.start() <= ptr && chunk.end() >= ptr {
520-
return true;
521-
}
522-
}
523-
false
524-
}
525-
526513
/// Allocates a string slice that is copied into the `DroplessArena`, returning a
527514
/// reference to it. Will panic if passed an empty string.
528515
///

compiler/rustc_hir_typeck/src/autoderef.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use itertools::Itertools;
66
use rustc_hir_analysis::autoderef::{Autoderef, AutoderefKind};
77
use rustc_infer::infer::InferOk;
88
use rustc_infer::traits::PredicateObligations;
9-
use rustc_middle::ty::adjustment::{Adjust, Adjustment, OverloadedDeref};
9+
use rustc_middle::ty::adjustment::{Adjust, Adjustment, DerefAdjustKind, OverloadedDeref};
1010
use rustc_middle::ty::{self, Ty};
1111
use rustc_span::Span;
1212

@@ -45,22 +45,24 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
4545
steps.iter().skip(1).map(|&(ty, _)| ty).chain(iter::once(autoderef.final_ty()));
4646
let steps: Vec<_> = steps
4747
.iter()
48-
.map(|&(source, kind)| {
49-
if let AutoderefKind::Overloaded = kind {
50-
self.try_overloaded_deref(autoderef.span(), source).and_then(
51-
|InferOk { value: method, obligations: o }| {
48+
.map(|&(source, kind)| match kind {
49+
AutoderefKind::Overloaded => {
50+
self.try_overloaded_deref(autoderef.span(), source)
51+
.and_then(|InferOk { value: method, obligations: o }| {
5252
obligations.extend(o);
5353
// FIXME: we should assert the sig is &T here... there's no reason for this to be fallible.
5454
if let ty::Ref(_, _, mutbl) = *method.sig.output().kind() {
55-
Some(OverloadedDeref { mutbl, span: autoderef.span() })
55+
Some(DerefAdjustKind::Overloaded(OverloadedDeref {
56+
mutbl,
57+
span: autoderef.span(),
58+
}))
5659
} else {
5760
None
5861
}
59-
},
60-
)
61-
} else {
62-
None
62+
})
63+
.unwrap_or(DerefAdjustKind::Builtin)
6364
}
65+
AutoderefKind::Builtin => DerefAdjustKind::Builtin,
6466
})
6567
.zip_eq(targets)
6668
.map(|(autoderef, target)| Adjustment { kind: Adjust::Deref(autoderef), target })

compiler/rustc_hir_typeck/src/coercion.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ use rustc_infer::traits::{
5050
};
5151
use rustc_middle::span_bug;
5252
use rustc_middle::ty::adjustment::{
53-
Adjust, Adjustment, AllowTwoPhase, AutoBorrow, AutoBorrowMutability, PointerCoercion,
53+
Adjust, Adjustment, AllowTwoPhase, AutoBorrow, AutoBorrowMutability, DerefAdjustKind,
54+
PointerCoercion,
5455
};
5556
use rustc_middle::ty::error::TypeError;
5657
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt};
@@ -595,7 +596,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
595596
let mutbl = AutoBorrowMutability::new(mutbl_b, AllowTwoPhase::No);
596597

597598
Some((
598-
Adjustment { kind: Adjust::Deref(None), target: ty_a },
599+
Adjustment { kind: Adjust::Deref(DerefAdjustKind::Builtin), target: ty_a },
599600
Adjustment {
600601
kind: Adjust::Borrow(AutoBorrow::Ref(mutbl)),
601602
target: Ty::new_ref(self.tcx, r_borrow, ty_a, mutbl_b),
@@ -606,7 +607,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
606607
coerce_mutbls(mt_a, mt_b)?;
607608

608609
Some((
609-
Adjustment { kind: Adjust::Deref(None), target: ty_a },
610+
Adjustment { kind: Adjust::Deref(DerefAdjustKind::Builtin), target: ty_a },
610611
Adjustment {
611612
kind: Adjust::Borrow(AutoBorrow::RawPtr(mt_b)),
612613
target: Ty::new_ptr(self.tcx, ty_a, mt_b),
@@ -936,7 +937,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
936937
self.unify_and(
937938
a_raw,
938939
b,
939-
[Adjustment { kind: Adjust::Deref(None), target: mt_a.ty }],
940+
[Adjustment { kind: Adjust::Deref(DerefAdjustKind::Builtin), target: mt_a.ty }],
940941
Adjust::Borrow(AutoBorrow::RawPtr(mutbl_b)),
941942
ForceLeakCheck::No,
942943
)

compiler/rustc_hir_typeck/src/expr_use_visitor.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use rustc_middle::hir::place::ProjectionKind;
2222
pub use rustc_middle::hir::place::{Place, PlaceBase, PlaceWithHirId, Projection};
2323
use rustc_middle::mir::FakeReadCause;
2424
use rustc_middle::thir::DerefPatBorrowMode;
25+
use rustc_middle::ty::adjustment::DerefAdjustKind;
2526
use rustc_middle::ty::{
2627
self, BorrowKind, Ty, TyCtxt, TypeFoldable, TypeVisitableExt as _, adjustment,
2728
};
@@ -733,14 +734,14 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
733734
self.consume_or_copy(&place_with_id, place_with_id.hir_id);
734735
}
735736

736-
adjustment::Adjust::Deref(None) => {}
737+
adjustment::Adjust::Deref(DerefAdjustKind::Builtin) => {}
737738

738739
// Autoderefs for overloaded Deref calls in fact reference
739740
// their receiver. That is, if we have `(*x)` where `x`
740741
// is of type `Rc<T>`, then this in fact is equivalent to
741742
// `x.deref()`. Since `deref()` is declared with `&self`,
742743
// this is an autoref of `x`.
743-
adjustment::Adjust::Deref(Some(ref deref)) => {
744+
adjustment::Adjust::Deref(DerefAdjustKind::Overloaded(deref)) => {
744745
let bk = ty::BorrowKind::from_mutbl(deref.mutbl);
745746
self.delegate.borrow_mut().borrow(&place_with_id, place_with_id.hir_id, bk);
746747
}
@@ -1272,9 +1273,9 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
12721273
{
12731274
let target = self.cx.resolve_vars_if_possible(adjustment.target);
12741275
match adjustment.kind {
1275-
adjustment::Adjust::Deref(overloaded) => {
1276+
adjustment::Adjust::Deref(deref_kind) => {
12761277
// Equivalent to *expr or something similar.
1277-
let base = if let Some(deref) = overloaded {
1278+
let base = if let DerefAdjustKind::Overloaded(deref) = deref_kind {
12781279
let ref_ty = Ty::new_ref(
12791280
self.cx.tcx(),
12801281
self.cx.tcx().lifetimes.re_erased,

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ use rustc_hir_analysis::hir_ty_lowering::{
2020
use rustc_infer::infer::canonical::{Canonical, OriginalQueryValues, QueryResponse};
2121
use rustc_infer::infer::{DefineOpaqueTypes, InferResult};
2222
use rustc_lint::builtin::SELF_CONSTRUCTOR_FROM_OUTER_ITEM;
23-
use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow, AutoBorrowMutability};
23+
use rustc_middle::ty::adjustment::{
24+
Adjust, Adjustment, AutoBorrow, AutoBorrowMutability, DerefAdjustKind,
25+
};
2426
use rustc_middle::ty::{
2527
self, AdtKind, CanonicalUserType, GenericArgsRef, GenericParamDefKind, IsIdentity,
2628
SizedTraitKind, Ty, TyCtxt, TypeFoldable, TypeVisitable, TypeVisitableExt, UserArgs,
@@ -266,15 +268,15 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
266268
debug!("apply_adjustments: adding `{:?}` as diverging type var", a.target);
267269
}
268270
}
269-
Adjust::Deref(Some(overloaded_deref)) => {
271+
Adjust::Deref(DerefAdjustKind::Overloaded(overloaded_deref)) => {
270272
self.enforce_context_effects(
271273
None,
272274
expr.span,
273275
overloaded_deref.method_call(self.tcx),
274276
self.tcx.mk_args(&[expr_ty.into()]),
275277
);
276278
}
277-
Adjust::Deref(None) => {
279+
Adjust::Deref(DerefAdjustKind::Builtin) => {
278280
// FIXME(const_trait_impl): We *could* enforce `&T: [const] Deref` here.
279281
}
280282
Adjust::Pointer(_pointer_coercion) => {

compiler/rustc_hir_typeck/src/place_op.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use rustc_infer::infer::InferOk;
44
use rustc_infer::traits::{Obligation, ObligationCauseCode};
55
use rustc_middle::span_bug;
66
use rustc_middle::ty::adjustment::{
7-
Adjust, Adjustment, AllowTwoPhase, AutoBorrow, AutoBorrowMutability, OverloadedDeref,
8-
PointerCoercion,
7+
Adjust, Adjustment, AllowTwoPhase, AutoBorrow, AutoBorrowMutability, DerefAdjustKind,
8+
OverloadedDeref, PointerCoercion,
99
};
1010
use rustc_middle::ty::{self, Ty};
1111
use rustc_span::{Span, sym};
@@ -298,7 +298,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
298298
self.typeck_results.borrow_mut().adjustments_mut().remove(expr.hir_id);
299299
if let Some(mut adjustments) = previous_adjustments {
300300
for adjustment in &mut adjustments {
301-
if let Adjust::Deref(Some(ref mut deref)) = adjustment.kind
301+
if let Adjust::Deref(DerefAdjustKind::Overloaded(ref mut deref)) =
302+
adjustment.kind
302303
&& let Some(ok) = self.try_mutable_overloaded_place_op(
303304
expr.span,
304305
source,

compiler/rustc_lint/src/autorefs.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use rustc_ast::{BorrowKind, UnOp};
22
use rustc_hir::attrs::AttributeKind;
33
use rustc_hir::{Expr, ExprKind, Mutability, find_attr};
4-
use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow, OverloadedDeref};
4+
use rustc_middle::ty::adjustment::{
5+
Adjust, Adjustment, AutoBorrow, DerefAdjustKind, OverloadedDeref,
6+
};
57
use rustc_session::{declare_lint, declare_lint_pass};
68

79
use crate::lints::{
@@ -165,12 +167,14 @@ fn peel_derefs_adjustments<'a>(mut adjs: &'a [Adjustment<'a>]) -> &'a [Adjustmen
165167
/// an implicit borrow (or has an implicit borrow via an overloaded deref).
166168
fn has_implicit_borrow(Adjustment { kind, .. }: &Adjustment<'_>) -> Option<(Mutability, bool)> {
167169
match kind {
168-
&Adjust::Deref(Some(OverloadedDeref { mutbl, .. })) => Some((mutbl, true)),
170+
&Adjust::Deref(DerefAdjustKind::Overloaded(OverloadedDeref { mutbl, .. })) => {
171+
Some((mutbl, true))
172+
}
169173
&Adjust::Borrow(AutoBorrow::Ref(mutbl)) => Some((mutbl.into(), false)),
170174
Adjust::NeverToAny
171175
| Adjust::Pointer(..)
172176
| Adjust::ReborrowPin(..)
173-
| Adjust::Deref(None)
177+
| Adjust::Deref(DerefAdjustKind::Builtin)
174178
| Adjust::Borrow(AutoBorrow::RawPtr(..)) => None,
175179
}
176180
}

compiler/rustc_lint/src/noop_method_call.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_hir::def::DefKind;
22
use rustc_hir::{Expr, ExprKind};
33
use rustc_middle::ty;
4-
use rustc_middle::ty::adjustment::Adjust;
4+
use rustc_middle::ty::adjustment::{Adjust, DerefAdjustKind};
55
use rustc_session::{declare_lint, declare_lint_pass};
66
use rustc_span::sym;
77

@@ -114,7 +114,10 @@ impl<'tcx> LateLintPass<'tcx> for NoopMethodCall {
114114

115115
// If there is any user defined auto-deref step, then we don't want to warn.
116116
// https://github.com/rust-lang/rust-clippy/issues/9272
117-
if arg_adjustments.iter().any(|adj| matches!(adj.kind, Adjust::Deref(Some(_)))) {
117+
if arg_adjustments
118+
.iter()
119+
.any(|adj| matches!(adj.kind, Adjust::Deref(DerefAdjustKind::Overloaded(_))))
120+
{
118121
return;
119122
}
120123

compiler/rustc_middle/src/ty/adjustment.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ pub enum Adjust {
9797
NeverToAny,
9898

9999
/// Dereference once, producing a place.
100-
Deref(Option<OverloadedDeref>),
100+
Deref(DerefAdjustKind),
101101

102102
/// Take the address and produce either a `&` or `*` pointer.
103103
Borrow(AutoBorrow),
@@ -108,6 +108,12 @@ pub enum Adjust {
108108
ReborrowPin(hir::Mutability),
109109
}
110110

111+
#[derive(Copy, Clone, Debug, TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable)]
112+
pub enum DerefAdjustKind {
113+
Builtin,
114+
Overloaded(OverloadedDeref),
115+
}
116+
111117
/// An overloaded autoderef step, representing a `Deref(Mut)::deref(_mut)`
112118
/// call, with the signature `&'a T -> &'a U` or `&'a mut T -> &'a mut U`.
113119
/// The target type is `U` in both cases, with the region and mutability

compiler/rustc_mir_build/src/thir/cx/expr.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_middle::middle::region;
1414
use rustc_middle::mir::{self, AssignOp, BinOp, BorrowKind, UnOp};
1515
use rustc_middle::thir::*;
1616
use rustc_middle::ty::adjustment::{
17-
Adjust, Adjustment, AutoBorrow, AutoBorrowMutability, PointerCoercion,
17+
Adjust, Adjustment, AutoBorrow, AutoBorrowMutability, DerefAdjustKind, PointerCoercion,
1818
};
1919
use rustc_middle::ty::{
2020
self, AdtKind, GenericArgs, InlineConstArgs, InlineConstArgsParts, ScalarInt, Ty, UpvarArgs,
@@ -140,11 +140,11 @@ impl<'tcx> ThirBuildCx<'tcx> {
140140
}
141141
Adjust::NeverToAny if adjustment.target.is_never() => return expr,
142142
Adjust::NeverToAny => ExprKind::NeverToAny { source: self.thir.exprs.push(expr) },
143-
Adjust::Deref(None) => {
143+
Adjust::Deref(DerefAdjustKind::Builtin) => {
144144
adjust_span(&mut expr);
145145
ExprKind::Deref { arg: self.thir.exprs.push(expr) }
146146
}
147-
Adjust::Deref(Some(deref)) => {
147+
Adjust::Deref(DerefAdjustKind::Overloaded(deref)) => {
148148
// We don't need to do call adjust_span here since
149149
// deref coercions always start with a built-in deref.
150150
let call_def_id = deref.method_call(self.tcx);

0 commit comments

Comments
 (0)