Skip to content

Commit 80a9646

Browse files
committed
Auto merge of #104945 - GuillaumeGomez:rollup-ygzbpbe, r=GuillaumeGomez
Rollup of 7 pull requests Successful merges: - #104786 (Use the power of adding helper function to simplify code w/ `Mutability`) - #104788 (Do not record unresolved const vars in generator interior) - #104909 (Rename `normalize_opaque_types` to `reveal_opaque_types_in_bounds`) - #104921 (Remove unnecessary binder from `get_impl_future_output_ty`) - #104924 (jsondoclint: Accept trait alias is places where trait expected.) - #104928 (rustdoc: use flexbox CSS to align sidebar button instead of position) - #104943 (jsondoclint: Handle using enum variants and glob using enums.) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents c3a1c02 + 95e6356 commit 80a9646

File tree

45 files changed

+445
-318
lines changed

Some content is hidden

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

45 files changed

+445
-318
lines changed

compiler/rustc_ast/src/ast.rs

+30-2
Original file line numberDiff line numberDiff line change
@@ -775,8 +775,9 @@ pub enum PatKind {
775775
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Copy)]
776776
#[derive(HashStable_Generic, Encodable, Decodable)]
777777
pub enum Mutability {
778-
Mut,
778+
// N.B. Order is deliberate, so that Not < Mut
779779
Not,
780+
Mut,
780781
}
781782

782783
impl Mutability {
@@ -787,12 +788,39 @@ impl Mutability {
787788
}
788789
}
789790

790-
pub fn prefix_str(&self) -> &'static str {
791+
/// Returns `""` (empty string) or `"mut "` depending on the mutability.
792+
pub fn prefix_str(self) -> &'static str {
791793
match self {
792794
Mutability::Mut => "mut ",
793795
Mutability::Not => "",
794796
}
795797
}
798+
799+
/// Returns `"&"` or `"&mut "` depending on the mutability.
800+
pub fn ref_prefix_str(self) -> &'static str {
801+
match self {
802+
Mutability::Not => "&",
803+
Mutability::Mut => "&mut ",
804+
}
805+
}
806+
807+
/// Returns `""` (empty string) or `"mutably "` depending on the mutability.
808+
pub fn mutably_str(self) -> &'static str {
809+
match self {
810+
Mutability::Not => "",
811+
Mutability::Mut => "mutably ",
812+
}
813+
}
814+
815+
/// Return `true` if self is mutable
816+
pub fn is_mut(self) -> bool {
817+
matches!(self, Self::Mut)
818+
}
819+
820+
/// Return `true` if self is **not** mutable
821+
pub fn is_not(self) -> bool {
822+
matches!(self, Self::Not)
823+
}
796824
}
797825

798826
/// The kind of borrow in an `AddrOf` expression,

compiler/rustc_ast_lowering/src/lib.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -1781,14 +1781,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
17811781
// Given we are only considering `ImplicitSelf` types, we needn't consider
17821782
// the case where we have a mutable pattern to a reference as that would
17831783
// no longer be an `ImplicitSelf`.
1784-
TyKind::Rptr(_, mt)
1785-
if mt.ty.kind.is_implicit_self() && mt.mutbl == ast::Mutability::Mut =>
1786-
{
1787-
hir::ImplicitSelfKind::MutRef
1788-
}
1789-
TyKind::Rptr(_, mt) if mt.ty.kind.is_implicit_self() => {
1790-
hir::ImplicitSelfKind::ImmRef
1791-
}
1784+
TyKind::Rptr(_, mt) if mt.ty.kind.is_implicit_self() => match mt.mutbl {
1785+
hir::Mutability::Not => hir::ImplicitSelfKind::ImmRef,
1786+
hir::Mutability::Mut => hir::ImplicitSelfKind::MutRef,
1787+
},
17921788
_ => hir::ImplicitSelfKind::None,
17931789
}
17941790
}),

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -716,19 +716,12 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
716716
let moved_place = &self.move_data.move_paths[move_out.path].place;
717717
let move_spans = self.move_spans(moved_place.as_ref(), move_out.source);
718718
let move_span = move_spans.args_or_use();
719-
let suggestion = if borrow_level == hir::Mutability::Mut {
720-
"&mut ".to_string()
721-
} else {
722-
"&".to_string()
723-
};
719+
let suggestion = borrow_level.ref_prefix_str().to_owned();
724720
(move_span.shrink_to_lo(), suggestion)
725721
})
726722
.collect();
727723
err.multipart_suggestion_verbose(
728-
&format!(
729-
"consider {}borrowing {value_name}",
730-
if borrow_level == hir::Mutability::Mut { "mutably " } else { "" }
731-
),
724+
format!("consider {}borrowing {value_name}", borrow_level.mutably_str()),
732725
sugg,
733726
Applicability::MaybeIncorrect,
734727
);

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -389,13 +389,13 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
389389
// diagnostic: if the span starts with a mutable borrow of
390390
// a local variable, then just suggest the user remove it.
391391
PlaceRef { local: _, projection: [] }
392-
if {
393-
if let Ok(snippet) = self.infcx.tcx.sess.source_map().span_to_snippet(span) {
394-
snippet.starts_with("&mut ")
395-
} else {
396-
false
397-
}
398-
} =>
392+
if self
393+
.infcx
394+
.tcx
395+
.sess
396+
.source_map()
397+
.span_to_snippet(span)
398+
.map_or(false, |snippet| snippet.starts_with("&mut ")) =>
399399
{
400400
err.span_label(span, format!("cannot {ACT}", ACT = act));
401401
err.span_suggestion(

compiler/rustc_codegen_cranelift/src/constant.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,9 @@ pub(crate) fn data_id_for_alloc_id(
257257
mutability: rustc_hir::Mutability,
258258
) -> DataId {
259259
cx.todo.push(TodoItem::Alloc(alloc_id));
260-
*cx.anon_allocs.entry(alloc_id).or_insert_with(|| {
261-
module.declare_anonymous_data(mutability == rustc_hir::Mutability::Mut, false).unwrap()
262-
})
260+
*cx.anon_allocs
261+
.entry(alloc_id)
262+
.or_insert_with(|| module.declare_anonymous_data(mutability.is_mut(), false).unwrap())
263263
}
264264

265265
fn data_id_for_static(
@@ -343,12 +343,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
343343
}
344344
};
345345
let data_id = *cx.anon_allocs.entry(alloc_id).or_insert_with(|| {
346-
module
347-
.declare_anonymous_data(
348-
alloc.inner().mutability == rustc_hir::Mutability::Mut,
349-
false,
350-
)
351-
.unwrap()
346+
module.declare_anonymous_data(alloc.inner().mutability.is_mut(), false).unwrap()
352347
});
353348
(data_id, alloc, None)
354349
}

compiler/rustc_codegen_llvm/src/intrinsic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1500,7 +1500,7 @@ fn generic_simd_intrinsic<'ll, 'tcx>(
15001500
let (_, element_ty1) = arg_tys[1].simd_size_and_type(bx.tcx());
15011501
let (_, element_ty2) = arg_tys[2].simd_size_and_type(bx.tcx());
15021502
let (pointer_count, underlying_ty) = match element_ty1.kind() {
1503-
ty::RawPtr(p) if p.ty == in_elem && p.mutbl == hir::Mutability::Mut => {
1503+
ty::RawPtr(p) if p.ty == in_elem && p.mutbl.is_mut() => {
15041504
(ptr_count(element_ty1), non_ptr(element_ty1))
15051505
}
15061506
_ => {

compiler/rustc_hir/src/pat_util.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,7 @@ impl hir::Pat<'_> {
130130
pub fn contains_explicit_ref_binding(&self) -> Option<hir::Mutability> {
131131
let mut result = None;
132132
self.each_binding(|annotation, _, _, _| match annotation {
133-
hir::BindingAnnotation::REF => match result {
134-
None | Some(hir::Mutability::Not) => result = Some(hir::Mutability::Not),
135-
_ => {}
136-
},
133+
hir::BindingAnnotation::REF if result.is_none() => result = Some(hir::Mutability::Not),
137134
hir::BindingAnnotation::REF_MUT => result = Some(hir::Mutability::Mut),
138135
_ => {}
139136
});

compiler/rustc_hir_analysis/src/coherence/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ pub fn coerce_unsized_info<'tcx>(tcx: TyCtxt<'tcx>, impl_did: DefId) -> CoerceUn
370370
let check_mutbl = |mt_a: ty::TypeAndMut<'tcx>,
371371
mt_b: ty::TypeAndMut<'tcx>,
372372
mk_ptr: &dyn Fn(Ty<'tcx>) -> Ty<'tcx>| {
373-
if (mt_a.mutbl, mt_b.mutbl) == (hir::Mutability::Not, hir::Mutability::Mut) {
373+
if mt_a.mutbl < mt_b.mutbl {
374374
infcx
375375
.err_ctxt()
376376
.report_mismatched_types(

compiler/rustc_hir_analysis/src/coherence/orphan.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ fn emit_newtype_suggestion_for_raw_ptr(
292292
diag: &mut Diagnostic,
293293
) {
294294
if !self_ty.needs_subst() {
295-
let mut_key = if ptr_ty.mutbl == rustc_middle::mir::Mutability::Mut { "mut " } else { "" };
295+
let mut_key = ptr_ty.mutbl.prefix_str();
296296
let msg_sugg = "consider introducing a new wrapper type".to_owned();
297297
let sugg = vec![
298298
(

compiler/rustc_hir_pretty/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ impl<'a> State<'a> {
398398
}
399399
hir::ForeignItemKind::Static(t, m) => {
400400
self.head("static");
401-
if m == hir::Mutability::Mut {
401+
if m.is_mut() {
402402
self.word_space("mut");
403403
}
404404
self.print_ident(item.ident);
@@ -519,7 +519,7 @@ impl<'a> State<'a> {
519519
}
520520
hir::ItemKind::Static(ty, m, expr) => {
521521
self.head("static");
522-
if m == hir::Mutability::Mut {
522+
if m.is_mut() {
523523
self.word_space("mut");
524524
}
525525
self.print_ident(item.ident);

compiler/rustc_hir_typeck/src/_match.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -574,8 +574,5 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
574574
}
575575

576576
fn arms_contain_ref_bindings<'tcx>(arms: &'tcx [hir::Arm<'tcx>]) -> Option<hir::Mutability> {
577-
arms.iter().filter_map(|a| a.pat.contains_explicit_ref_binding()).max_by_key(|m| match *m {
578-
hir::Mutability::Mut => 1,
579-
hir::Mutability::Not => 0,
580-
})
577+
arms.iter().filter_map(|a| a.pat.contains_explicit_ref_binding()).max()
581578
}

compiler/rustc_hir_typeck/src/callee.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -257,15 +257,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
257257
return None;
258258
};
259259

260-
let mutbl = match mutbl {
261-
hir::Mutability::Not => AutoBorrowMutability::Not,
262-
hir::Mutability::Mut => AutoBorrowMutability::Mut {
263-
// For initial two-phase borrow
264-
// deployment, conservatively omit
265-
// overloaded function call ops.
266-
allow_two_phase_borrow: AllowTwoPhase::No,
267-
},
268-
};
260+
// For initial two-phase borrow
261+
// deployment, conservatively omit
262+
// overloaded function call ops.
263+
let mutbl = AutoBorrowMutability::new(*mutbl, AllowTwoPhase::No);
264+
269265
autoref = Some(Adjustment {
270266
kind: Adjust::Borrow(AutoBorrow::Ref(*region, mutbl)),
271267
target: method.sig.inputs()[0],

compiler/rustc_hir_typeck/src/cast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
951951
m_cast: ty::TypeAndMut<'tcx>,
952952
) -> Result<CastKind, CastError> {
953953
// array-ptr-cast: allow mut-to-mut, mut-to-const, const-to-const
954-
if m_expr.mutbl == hir::Mutability::Mut || m_cast.mutbl == hir::Mutability::Not {
954+
if m_expr.mutbl >= m_cast.mutbl {
955955
if let ty::Array(ety, _) = m_expr.ty.kind() {
956956
// Due to the limitations of LLVM global constants,
957957
// region pointers end up pointing at copies of

compiler/rustc_hir_typeck/src/check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ fn check_panic_info_fn(
202202
let arg_is_panic_info = match *inputs[0].kind() {
203203
ty::Ref(region, ty, mutbl) => match *ty.kind() {
204204
ty::Adt(ref adt, _) => {
205-
adt.did() == panic_info_did && mutbl == hir::Mutability::Not && !region.is_static()
205+
adt.did() == panic_info_did && mutbl.is_not() && !region.is_static()
206206
}
207207
_ => false,
208208
},

compiler/rustc_hir_typeck/src/coercion.rs

+10-22
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,7 @@ fn coerce_mutbls<'tcx>(
108108
from_mutbl: hir::Mutability,
109109
to_mutbl: hir::Mutability,
110110
) -> RelateResult<'tcx, ()> {
111-
match (from_mutbl, to_mutbl) {
112-
(hir::Mutability::Mut, hir::Mutability::Mut | hir::Mutability::Not)
113-
| (hir::Mutability::Not, hir::Mutability::Not) => Ok(()),
114-
(hir::Mutability::Not, hir::Mutability::Mut) => Err(TypeError::Mutability),
115-
}
111+
if from_mutbl >= to_mutbl { Ok(()) } else { Err(TypeError::Mutability) }
116112
}
117113

118114
/// Do not require any adjustments, i.e. coerce `x -> x`.
@@ -456,7 +452,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
456452
return Err(err);
457453
};
458454

459-
if ty == a && mt_a.mutbl == hir::Mutability::Not && autoderef.step_count() == 1 {
455+
if ty == a && mt_a.mutbl.is_not() && autoderef.step_count() == 1 {
460456
// As a special case, if we would produce `&'a *x`, that's
461457
// a total no-op. We end up with the type `&'a T` just as
462458
// we started with. In that case, just skip it
@@ -468,7 +464,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
468464
// `self.x` both have `&mut `type would be a move of
469465
// `self.x`, but we auto-coerce it to `foo(&mut *self.x)`,
470466
// which is a borrow.
471-
assert_eq!(mutbl_b, hir::Mutability::Not); // can only coerce &T -> &U
467+
assert!(mutbl_b.is_not()); // can only coerce &T -> &U
472468
return success(vec![], ty, obligations);
473469
}
474470

@@ -482,12 +478,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
482478
let ty::Ref(r_borrow, _, _) = ty.kind() else {
483479
span_bug!(span, "expected a ref type, got {:?}", ty);
484480
};
485-
let mutbl = match mutbl_b {
486-
hir::Mutability::Not => AutoBorrowMutability::Not,
487-
hir::Mutability::Mut => {
488-
AutoBorrowMutability::Mut { allow_two_phase_borrow: self.allow_two_phase }
489-
}
490-
};
481+
let mutbl = AutoBorrowMutability::new(mutbl_b, self.allow_two_phase);
491482
adjustments.push(Adjustment {
492483
kind: Adjust::Borrow(AutoBorrow::Ref(*r_borrow, mutbl)),
493484
target: ty,
@@ -556,15 +547,12 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
556547

557548
let coercion = Coercion(self.cause.span);
558549
let r_borrow = self.next_region_var(coercion);
559-
let mutbl = match mutbl_b {
560-
hir::Mutability::Not => AutoBorrowMutability::Not,
561-
hir::Mutability::Mut => AutoBorrowMutability::Mut {
562-
// We don't allow two-phase borrows here, at least for initial
563-
// implementation. If it happens that this coercion is a function argument,
564-
// the reborrow in coerce_borrowed_ptr will pick it up.
565-
allow_two_phase_borrow: AllowTwoPhase::No,
566-
},
567-
};
550+
551+
// We don't allow two-phase borrows here, at least for initial
552+
// implementation. If it happens that this coercion is a function argument,
553+
// the reborrow in coerce_borrowed_ptr will pick it up.
554+
let mutbl = AutoBorrowMutability::new(mutbl_b, AllowTwoPhase::No);
555+
568556
Some((
569557
Adjustment { kind: Adjust::Deref(None), target: ty_a },
570558
Adjustment {

0 commit comments

Comments
 (0)