Skip to content

Uplift mutability to rustc_type_ir #116919

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
// If we're in pattern, we do nothing in favor of the previous suggestion (#80913).
// Same for if we're in a loop, see #101119.
if is_loop_move & !in_pattern && !matches!(use_spans, UseSpans::ClosureUse { .. }) {
if let ty::Ref(_, _, hir::Mutability::Mut) = ty.kind() {
if let ty::Ref(_, _, ty::Mutability::Mut) = ty.kind() {
// We have a `&mut` ref, we need to reborrow on each iteration (#62112).
err.span_suggestion_verbose(
span.shrink_to_lo(),
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_borrowck/src/diagnostics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
// If the moved place was a `&mut` ref, then we can
// suggest to reborrow it where it was moved, so it
// will still be valid by the time we get to the usage.
if let ty::Ref(_, _, hir::Mutability::Mut) =
if let ty::Ref(_, _, ty::Mutability::Mut) =
moved_place.ty(self.body, self.infcx.tcx).ty.kind()
{
// If we are in a loop this will be suggested later.
Expand Down Expand Up @@ -1107,7 +1107,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {

if let ty::Adt(def, args) = ty.kind()
&& Some(def.did()) == tcx.lang_items().pin_type()
&& let ty::Ref(_, _, hir::Mutability::Mut) = args.type_at(0).kind()
&& let ty::Ref(_, _, ty::Mutability::Mut) = args.type_at(0).kind()
&& let self_ty = self.infcx.instantiate_binder_with_fresh_vars(
fn_call_span,
LateBoundRegionConversionTime::FnCall,
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
.place
.place
.deref_tys()
.any(|ty| matches!(ty.kind(), ty::Ref(.., hir::Mutability::Not)));
.any(|ty| matches!(ty.kind(), ty::Ref(.., Mutability::Not)));

// If the place is immutable then:
//
Expand Down Expand Up @@ -918,7 +918,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
);
}
}
AddrOf(BorrowKind::Ref, Mutability::Not, expr) => {
AddrOf(BorrowKind::Ref, hir::Mutability::Not, expr) => {
// We have `for _ in &i`, suggest `for _ in &mut i`.
err.span_suggestion_verbose(
expr.span.shrink_to_lo(),
Expand Down Expand Up @@ -1246,7 +1246,7 @@ pub fn mut_borrow_of_mutable_ref(local_decl: &LocalDecl<'_>, local_name: Option<
LocalInfo::User(mir::BindingForm::Var(mir::VarBindingForm {
binding_mode: ty::BindingMode::BindByValue(Mutability::Not),
..
})) => matches!(local_decl.ty.kind(), ty::Ref(_, _, hir::Mutability::Mut)),
})) => matches!(local_decl.ty.kind(), ty::Ref(_, _, Mutability::Mut)),
LocalInfo::User(mir::BindingForm::ImplicitSelf(kind)) => {
// Check if the user variable is a `&mut self` and we can therefore
// suggest removing the `&mut`.
Expand All @@ -1259,7 +1259,7 @@ pub fn mut_borrow_of_mutable_ref(local_decl: &LocalDecl<'_>, local_name: Option<
// Otherwise, check if the name is the `self` keyword - in which case
// we have an explicit self. Do the same thing in this case and check
// for a `self: &mut Self` to suggest removing the `&mut`.
matches!(local_decl.ty.kind(), ty::Ref(_, _, hir::Mutability::Mut))
matches!(local_decl.ty.kind(), ty::Ref(_, _, Mutability::Mut))
}
_ => false,
}
Expand Down Expand Up @@ -1371,7 +1371,7 @@ fn suggest_ampmut<'tcx>(
// otherwise, suggest that the user annotates the binding; we provide the
// type of the local.
let ty_mut = decl_ty.builtin_deref(true).unwrap();
assert_eq!(ty_mut.mutbl, hir::Mutability::Not);
assert_eq!(ty_mut.mutbl, Mutability::Not);

(false, span, format!("{}mut {}", if decl_ty.is_ref() { "&" } else { "*" }, ty_mut.ty))
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_borrowck/src/diagnostics/region_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,14 +488,14 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
ty::VarianceDiagInfo::Invariant { ty, param_index } => {
let (desc, note) = match ty.kind() {
ty::RawPtr(ty_mut) => {
assert_eq!(ty_mut.mutbl, rustc_hir::Mutability::Mut);
assert_eq!(ty_mut.mutbl, ty::Mutability::Mut);
(
format!("a mutable pointer to `{}`", ty_mut.ty),
"mutable pointers are invariant over their type parameter".to_string(),
)
}
ty::Ref(_, inner_ty, mutbl) => {
assert_eq!(*mutbl, rustc_hir::Mutability::Mut);
assert_eq!(*mutbl, ty::Mutability::Mut);
(
format!("a mutable reference to `{inner_ty}`"),
"mutable references are invariant over their type parameter"
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_borrowck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1366,7 +1366,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
if proj == ProjectionElem::Deref {
match place_ref.ty(this.body(), this.infcx.tcx).ty.kind() {
// We aren't modifying a variable directly
ty::Ref(_, _, hir::Mutability::Mut) => return,
ty::Ref(_, _, Mutability::Mut) => return,

_ => {}
}
Expand Down Expand Up @@ -2154,10 +2154,10 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
ty::Ref(_, _, mutbl) => {
match mutbl {
// Shared borrowed data is never mutable
hir::Mutability::Not => Err(place),
Mutability::Not => Err(place),
// Mutably borrowed data is mutable, but only if we have a
// unique path to the `&mut`
hir::Mutability::Mut => {
Mutability::Mut => {
let mode = match self.is_upvar_field_projection(place) {
Some(field) if self.upvars[field.index()].by_ref => {
is_local_mutation_allowed
Expand All @@ -2172,10 +2172,10 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
ty::RawPtr(tnm) => {
match tnm.mutbl {
// `*const` raw pointers are not mutable
hir::Mutability::Not => Err(place),
Mutability::Not => Err(place),
// `*mut` raw pointers are always mutable, regardless of
// context. The users have to check by themselves.
hir::Mutability::Mut => Ok(RootPlace {
Mutability::Mut => Ok(RootPlace {
place_local: place.local,
place_projection: place.projection,
is_local_mutation_allowed,
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_borrowck/src/place_ext.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![deny(rustc::untranslatable_diagnostic)]
#![deny(rustc::diagnostic_outside_of_impl)]
use crate::borrow_set::LocalsStateAtExit;
use rustc_hir as hir;
use rustc_middle::mir::ProjectionElem;
use rustc_middle::mir::{Body, Mutability, Place};
use rustc_middle::ty::{self, TyCtxt};
Expand Down Expand Up @@ -50,15 +49,15 @@ impl<'tcx> PlaceExt<'tcx> for Place<'tcx> {
if elem == ProjectionElem::Deref {
let ty = proj_base.ty(body, tcx).ty;
match ty.kind() {
ty::Ref(_, _, hir::Mutability::Not) if i == 0 => {
ty::Ref(_, _, Mutability::Not) if i == 0 => {
// For references to thread-local statics, we do need
// to track the borrow.
if body.local_decls[self.local].is_ref_to_thread_local() {
continue;
}
return true;
}
ty::RawPtr(..) | ty::Ref(_, _, hir::Mutability::Not) => {
ty::RawPtr(..) | ty::Ref(_, _, Mutability::Not) => {
// For both derefs of raw pointers and `&T`
// references, the original path is `Copy` and
// therefore not significant. In particular,
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_borrowck/src/places_conflict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
use crate::ArtificialField;
use crate::Overlap;
use crate::{AccessDepth, Deep, Shallow};
use rustc_hir as hir;
use rustc_middle::mir::{
Body, BorrowKind, MutBorrowKind, Place, PlaceElem, PlaceRef, ProjectionElem,
};
Expand Down Expand Up @@ -223,11 +222,11 @@ fn place_components_conflict<'tcx>(
debug!("borrow_conflicts_with_place: shallow access behind ptr");
return false;
}
(ProjectionElem::Deref, ty::Ref(_, _, hir::Mutability::Not), _) => {
(ProjectionElem::Deref, ty::Ref(_, _, ty::Mutability::Not), _) => {
// Shouldn't be tracked
bug!("Tracking borrow behind shared reference.");
}
(ProjectionElem::Deref, ty::Ref(_, _, hir::Mutability::Mut), AccessDepth::Drop) => {
(ProjectionElem::Deref, ty::Ref(_, _, ty::Mutability::Mut), AccessDepth::Drop) => {
// Values behind a mutable reference are not access either by dropping a
// value, or by StorageDead
debug!("borrow_conflicts_with_place: drop access behind ptr");
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_borrowck/src/prefixes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

use super::MirBorrowckCtxt;

use rustc_hir as hir;
use rustc_middle::mir::{Body, PlaceRef, ProjectionElem};
use rustc_middle::ty::{self, TyCtxt};

Expand Down Expand Up @@ -125,14 +124,14 @@ impl<'cx, 'tcx> Iterator for Prefixes<'cx, 'tcx> {

let ty = cursor_base.ty(self.body, self.tcx).ty;
match ty.kind() {
ty::RawPtr(_) | ty::Ref(_ /*rgn*/, _ /*ty*/, hir::Mutability::Not) => {
ty::RawPtr(_) | ty::Ref(_ /*rgn*/, _ /*ty*/, ty::Mutability::Not) => {
// don't continue traversing over derefs of raw pointers or shared
// borrows.
self.next = None;
return Some(cursor);
}

ty::Ref(_ /*rgn*/, _ /*ty*/, hir::Mutability::Mut) => {
ty::Ref(_ /*rgn*/, _ /*ty*/, ty::Mutability::Mut) => {
self.next = Some(cursor_base);
return Some(cursor);
}
Expand Down
9 changes: 4 additions & 5 deletions compiler/rustc_borrowck/src/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use either::Either;
use rustc_data_structures::frozen::Frozen;
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
use rustc_errors::ErrorGuaranteed;
use rustc_hir as hir;
use rustc_hir::def::DefKind;
use rustc_hir::def_id::LocalDefId;
use rustc_hir::lang_items::LangItem;
Expand Down Expand Up @@ -2047,13 +2046,13 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
}

CastKind::PointerCoercion(PointerCoercion::MutToConstPointer) => {
let ty::RawPtr(ty::TypeAndMut { ty: ty_from, mutbl: hir::Mutability::Mut }) =
let ty::RawPtr(ty::TypeAndMut { ty: ty_from, mutbl: Mutability::Mut }) =
op.ty(body, tcx).kind()
else {
span_mirbug!(self, rvalue, "unexpected base type for cast {:?}", ty,);
return;
};
let ty::RawPtr(ty::TypeAndMut { ty: ty_to, mutbl: hir::Mutability::Not }) =
let ty::RawPtr(ty::TypeAndMut { ty: ty_to, mutbl: Mutability::Not }) =
ty.kind()
else {
span_mirbug!(self, rvalue, "unexpected target type for cast {:?}", ty,);
Expand Down Expand Up @@ -2533,13 +2532,13 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
});

match mutbl {
hir::Mutability::Not => {
Mutability::Not => {
// Immutable reference. We don't need the base
// to be valid for the entire lifetime of
// the borrow.
break;
}
hir::Mutability::Mut => {
Mutability::Mut => {
// Mutable reference. We *do* need the base
// to be valid, because after the base becomes
// invalid, someone else can use our mutable deref.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_cranelift/src/abi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ pub(crate) fn codegen_drop<'tcx>(
fx.layout_of(Ty::new_ref(
fx.tcx,
fx.tcx.lifetimes.re_erased,
TypeAndMut { ty, mutbl: crate::rustc_hir::Mutability::Mut },
TypeAndMut { ty, mutbl: Mutability::Mut },
)),
);
let arg_value = adjust_arg_for_abi(fx, arg_value, &fn_abi.args[0], true);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_cranelift/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ fn clif_pair_type_from_ty<'tcx>(

/// Is a pointer to this type a fat ptr?
pub(crate) fn has_ptr_meta<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool {
let ptr_ty = Ty::new_ptr(tcx, TypeAndMut { ty, mutbl: rustc_hir::Mutability::Not });
let ptr_ty = Ty::new_ptr(tcx, TypeAndMut { ty, mutbl: Mutability::Not });
match &tcx.layout_of(ParamEnv::reveal_all().and(ptr_ty)).unwrap().abi {
Abi::Scalar(_) => false,
Abi::ScalarPair(_, _) => true,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_cranelift/src/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ pub(crate) fn data_id_for_alloc_id(
cx: &mut ConstantCx,
module: &mut dyn Module,
alloc_id: AllocId,
mutability: rustc_hir::Mutability,
mutability: ty::Mutability,
) -> DataId {
cx.todo.push(TodoItem::Alloc(alloc_id));
*cx.anon_allocs
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_gcc/src/intrinsic/simd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ pub fn generic_simd_intrinsic<'a, 'gcc, '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 (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 == ty::Mutability::Mut => {
(ptr_count(element_ty1), non_ptr(element_ty1))
}
_ => {
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_codegen_llvm/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ use crate::llvm::{self, BasicBlock, Bool, ConstantInt, False, OperandBundleDef,
use crate::type_::Type;
use crate::value::Value;

use rustc_ast::Mutability;
use rustc_codegen_ssa::traits::*;
use rustc_data_structures::stable_hasher::{Hash128, HashStable, StableHasher};
use rustc_hir::def_id::DefId;
use rustc_middle::bug;
use rustc_middle::mir::interpret::{ConstAllocation, GlobalAlloc, Scalar};
use rustc_middle::ty::TyCtxt;
use rustc_middle::ty::{Mutability, TyCtxt};
use rustc_session::cstore::{DllCallingConvention, DllImport, PeImportNameType};
use rustc_target::abi::{self, AddressSpace, HasDataLayout, Pointer};
use rustc_target::spec::Target;
Expand Down
14 changes: 7 additions & 7 deletions compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::stable_hasher::{Hash64, HashStable, StableHasher};
use rustc_hir::def_id::DefId;
use rustc_hir::definitions::{DefPathData, DefPathDataName, DisambiguatedDefPathData};
use rustc_hir::{AsyncGeneratorKind, GeneratorKind, Mutability};
use rustc_hir::{AsyncGeneratorKind, GeneratorKind};
use rustc_middle::ty::layout::{IntegerExt, TyAndLayout};
use rustc_middle::ty::{self, ExistentialProjection, ParamEnv, Ty, TyCtxt};
use rustc_middle::ty::{GenericArgKind, GenericArgsRef};
Expand Down Expand Up @@ -140,14 +140,14 @@ fn push_debuginfo_type_name<'tcx>(
ty::RawPtr(ty::TypeAndMut { ty: inner_type, mutbl }) => {
if cpp_like_debuginfo {
match mutbl {
Mutability::Not => output.push_str("ptr_const$<"),
Mutability::Mut => output.push_str("ptr_mut$<"),
ty::Mutability::Not => output.push_str("ptr_const$<"),
ty::Mutability::Mut => output.push_str("ptr_mut$<"),
}
} else {
output.push('*');
match mutbl {
Mutability::Not => output.push_str("const "),
Mutability::Mut => output.push_str("mut "),
ty::Mutability::Not => output.push_str("const "),
ty::Mutability::Mut => output.push_str("mut "),
}
}

Expand All @@ -160,8 +160,8 @@ fn push_debuginfo_type_name<'tcx>(
ty::Ref(_, inner_type, mutbl) => {
if cpp_like_debuginfo {
match mutbl {
Mutability::Not => output.push_str("ref$<"),
Mutability::Mut => output.push_str("ref_mut$<"),
ty::Mutability::Not => output.push_str("ref$<"),
ty::Mutability::Mut => output.push_str("ref_mut$<"),
}
} else {
output.push('&');
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_const_eval/src/const_eval/eval_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::mem;

use either::{Left, Right};

use rustc_hir as hir;
use rustc_hir::def::DefKind;
use rustc_middle::mir::interpret::{ErrorHandled, InterpErrorInfo};
use rustc_middle::mir::pretty::write_allocation_bytes;
Expand Down Expand Up @@ -71,7 +72,8 @@ fn eval_body_using_ecx<'mir, 'tcx>(
InternKind::Promoted
} else {
match tcx.static_mutability(cid.instance.def_id()) {
Some(m) => InternKind::Static(m),
Some(hir::Mutability::Mut) => InternKind::Static(ty::Mutability::Mut),
Some(hir::Mutability::Not) => InternKind::Static(ty::Mutability::Not),
None => InternKind::Constant,
}
};
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_const_eval/src/const_eval/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use rustc_hir::LangItem;
use rustc_middle::mir;
use rustc_middle::mir::interpret::PointerArithmetic;
use rustc_middle::ty::layout::{FnAbiOf, TyAndLayout};
use rustc_middle::ty::{self, TyCtxt};
use rustc_middle::ty::{self, Mutability, TyCtxt};
use std::borrow::Borrow;
use std::hash::Hash;
use std::ops::ControlFlow;
Expand All @@ -12,7 +12,6 @@ use rustc_data_structures::fx::FxIndexMap;
use rustc_data_structures::fx::IndexEntry;
use std::fmt;

use rustc_ast::Mutability;
use rustc_hir::def_id::DefId;
use rustc_middle::mir::AssertMessage;
use rustc_span::symbol::{sym, Symbol};
Expand Down
Loading