Add Mutability::{is_mut,is_not}

This commit is contained in:
Maybe Waffle 2022-11-23 18:22:51 +00:00
parent 9b9c7d0ecc
commit da40965300
14 changed files with 29 additions and 26 deletions

View file

@ -803,6 +803,16 @@ impl Mutability {
Mutability::Mut => "&mut ", Mutability::Mut => "&mut ",
} }
} }
/// Return `true` if self is mutable
pub fn is_mut(self) -> bool {
matches!(self, Self::Mut)
}
/// Return `true` if self is **not** mutable
pub fn is_not(self) -> bool {
matches!(self, Self::Not)
}
} }
/// The kind of borrow in an `AddrOf` expression, /// The kind of borrow in an `AddrOf` expression,

View file

@ -1781,9 +1781,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
// Given we are only considering `ImplicitSelf` types, we needn't consider // Given we are only considering `ImplicitSelf` types, we needn't consider
// the case where we have a mutable pattern to a reference as that would // the case where we have a mutable pattern to a reference as that would
// no longer be an `ImplicitSelf`. // no longer be an `ImplicitSelf`.
TyKind::Rptr(_, mt) TyKind::Rptr(_, mt) if mt.ty.kind.is_implicit_self() && mt.mutbl.is_mut() => {
if mt.ty.kind.is_implicit_self() && mt.mutbl == ast::Mutability::Mut =>
{
hir::ImplicitSelfKind::MutRef hir::ImplicitSelfKind::MutRef
} }
TyKind::Rptr(_, mt) if mt.ty.kind.is_implicit_self() => { TyKind::Rptr(_, mt) if mt.ty.kind.is_implicit_self() => {

View file

@ -578,7 +578,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
err.multipart_suggestion_verbose( err.multipart_suggestion_verbose(
&format!( &format!(
"consider {}borrowing {value_name}", "consider {}borrowing {value_name}",
if borrow_level == hir::Mutability::Mut { "mutably " } else { "" } if borrow_level.is_mut() { "mutably " } else { "" }
), ),
sugg, sugg,
Applicability::MaybeIncorrect, Applicability::MaybeIncorrect,

View file

@ -257,9 +257,9 @@ pub(crate) fn data_id_for_alloc_id(
mutability: rustc_hir::Mutability, mutability: rustc_hir::Mutability,
) -> DataId { ) -> DataId {
cx.todo.push(TodoItem::Alloc(alloc_id)); cx.todo.push(TodoItem::Alloc(alloc_id));
*cx.anon_allocs.entry(alloc_id).or_insert_with(|| { *cx.anon_allocs
module.declare_anonymous_data(mutability == rustc_hir::Mutability::Mut, false).unwrap() .entry(alloc_id)
}) .or_insert_with(|| module.declare_anonymous_data(mutability.is_mut(), false).unwrap())
} }
fn data_id_for_static( fn data_id_for_static(
@ -343,12 +343,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
} }
}; };
let data_id = *cx.anon_allocs.entry(alloc_id).or_insert_with(|| { let data_id = *cx.anon_allocs.entry(alloc_id).or_insert_with(|| {
module module.declare_anonymous_data(alloc.inner().mutability.is_mut(), false).unwrap()
.declare_anonymous_data(
alloc.inner().mutability == rustc_hir::Mutability::Mut,
false,
)
.unwrap()
}); });
(data_id, alloc, None) (data_id, alloc, None)
} }

View file

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

View file

@ -292,7 +292,7 @@ fn emit_newtype_suggestion_for_raw_ptr(
diag: &mut Diagnostic, diag: &mut Diagnostic,
) { ) {
if !self_ty.needs_subst() { if !self_ty.needs_subst() {
let mut_key = if ptr_ty.mutbl == rustc_middle::mir::Mutability::Mut { "mut " } else { "" }; let mut_key = ptr_ty.mutbl.prefix_str();
let msg_sugg = "consider introducing a new wrapper type".to_owned(); let msg_sugg = "consider introducing a new wrapper type".to_owned();
let sugg = vec![ let sugg = vec![
( (

View file

@ -398,7 +398,7 @@ impl<'a> State<'a> {
} }
hir::ForeignItemKind::Static(t, m) => { hir::ForeignItemKind::Static(t, m) => {
self.head("static"); self.head("static");
if m == hir::Mutability::Mut { if m.is_mut() {
self.word_space("mut"); self.word_space("mut");
} }
self.print_ident(item.ident); self.print_ident(item.ident);
@ -519,7 +519,7 @@ impl<'a> State<'a> {
} }
hir::ItemKind::Static(ty, m, expr) => { hir::ItemKind::Static(ty, m, expr) => {
self.head("static"); self.head("static");
if m == hir::Mutability::Mut { if m.is_mut() {
self.word_space("mut"); self.word_space("mut");
} }
self.print_ident(item.ident); self.print_ident(item.ident);

View file

@ -197,7 +197,7 @@ fn check_panic_info_fn(
let arg_is_panic_info = match *inputs[0].kind() { let arg_is_panic_info = match *inputs[0].kind() {
ty::Ref(region, ty, mutbl) => match *ty.kind() { ty::Ref(region, ty, mutbl) => match *ty.kind() {
ty::Adt(ref adt, _) => { ty::Adt(ref adt, _) => {
adt.did() == panic_info_did && mutbl == hir::Mutability::Not && !region.is_static() adt.did() == panic_info_did && mutbl.is_not() && !region.is_static()
} }
_ => false, _ => false,
}, },

View file

@ -452,7 +452,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
return Err(err); return Err(err);
}; };
if ty == a && mt_a.mutbl == hir::Mutability::Not && autoderef.step_count() == 1 { if ty == a && mt_a.mutbl.is_not() && autoderef.step_count() == 1 {
// As a special case, if we would produce `&'a *x`, that's // As a special case, if we would produce `&'a *x`, that's
// a total no-op. We end up with the type `&'a T` just as // a total no-op. We end up with the type `&'a T` just as
// we started with. In that case, just skip it // we started with. In that case, just skip it

View file

@ -856,7 +856,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.. ..
})) = self.tcx.hir().find(self.tcx.hir().get_parent_node(expr.hir_id)) })) = self.tcx.hir().find(self.tcx.hir().get_parent_node(expr.hir_id))
{ {
if mutability == hir::Mutability::Mut { if mutability.is_mut() {
// Suppressing this diagnostic, we'll properly print it in `check_expr_assign` // Suppressing this diagnostic, we'll properly print it in `check_expr_assign`
return None; return None;
} }

View file

@ -702,7 +702,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}; };
let mut_var_suggestion = 'block: { let mut_var_suggestion = 'block: {
if !matches!(mutbl, ast::Mutability::Mut) { if mutbl.is_not() {
break 'block None; break 'block None;
} }

View file

@ -169,7 +169,7 @@ impl<'hir> Sig for hir::Ty<'hir> {
let mut prefix = "&".to_owned(); let mut prefix = "&".to_owned();
prefix.push_str(&lifetime.name.ident().to_string()); prefix.push_str(&lifetime.name.ident().to_string());
prefix.push(' '); prefix.push(' ');
if let hir::Mutability::Mut = mt.mutbl { if mt.mutbl.is_mut() {
prefix.push_str("mut "); prefix.push_str("mut ");
}; };
@ -332,7 +332,7 @@ impl<'hir> Sig for hir::Item<'hir> {
match self.kind { match self.kind {
hir::ItemKind::Static(ref ty, m, ref body) => { hir::ItemKind::Static(ref ty, m, ref body) => {
let mut text = "static ".to_owned(); let mut text = "static ".to_owned();
if m == hir::Mutability::Mut { if m.is_mut() {
text.push_str("mut "); text.push_str("mut ");
} }
let name = self.ident.to_string(); let name = self.ident.to_string();

View file

@ -621,7 +621,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> {
}); });
match inner_ty.kind() { match inner_ty.kind() {
ty::Str if *mutbl == hir::Mutability::Not => { ty::Str if mutbl.is_not() => {
match ct.kind() { match ct.kind() {
ty::ConstKind::Value(valtree) => { ty::ConstKind::Value(valtree) => {
let slice = let slice =

View file

@ -1059,7 +1059,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
{ {
( (
mk_result(old_pred.map_bound(|trait_pred| (trait_pred, *ty))), mk_result(old_pred.map_bound(|trait_pred| (trait_pred, *ty))),
matches!(mutability, hir::Mutability::Mut), mutability.is_mut(),
) )
} else { } else {
(false, false) (false, false)
@ -1348,7 +1348,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
.sess .sess
.source_map() .source_map()
.span_take_while(span, |c| c.is_whitespace() || *c == '&'); .span_take_while(span, |c| c.is_whitespace() || *c == '&');
if points_at_arg && mutability == hir::Mutability::Not && refs_number > 0 { if points_at_arg && mutability.is_not() && refs_number > 0 {
err.span_suggestion_verbose( err.span_suggestion_verbose(
sp, sp,
"consider changing this borrow's mutability", "consider changing this borrow's mutability",