Skip to content

Commit 7072f59

Browse files
committed
Use chalk_ir::Mutability
1 parent 4a9eec4 commit 7072f59

File tree

10 files changed

+78
-64
lines changed

10 files changed

+78
-64
lines changed

crates/hir/src/code_model.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use hir_def::{
1414
per_ns::PerNs,
1515
resolver::{HasResolver, Resolver},
1616
src::HasSource as _,
17-
type_ref::{Mutability, TypeRef},
17+
type_ref::TypeRef,
1818
AdtId, AssocContainerId, AssocItemId, AssocItemLoc, AttrDefId, ConstId, ConstParamId,
1919
DefWithBodyId, EnumId, FunctionId, GenericDefId, HasModule, ImplId, LifetimeParamId,
2020
LocalEnumVariantId, LocalFieldId, Lookup, ModuleId, StaticId, StructId, TraitId, TypeAliasId,
@@ -32,8 +32,8 @@ use hir_ty::{
3232
method_resolution,
3333
traits::{FnTrait, Solution, SolutionVariables},
3434
AliasTy, BoundVar, CallableDefId, CallableSig, Canonical, DebruijnIndex, GenericPredicate,
35-
InEnvironment, Obligation, ProjectionPredicate, ProjectionTy, Scalar, Substs, TraitEnvironment,
36-
Ty, TyDefId, TyVariableKind,
35+
InEnvironment, Mutability, Obligation, ProjectionPredicate, ProjectionTy, Scalar, Substs,
36+
TraitEnvironment, Ty, TyDefId, TyVariableKind,
3737
};
3838
use rustc_hash::FxHashSet;
3939
use stdx::{format_to, impl_from};
@@ -836,7 +836,7 @@ pub enum Access {
836836
impl From<Mutability> for Access {
837837
fn from(mutability: Mutability) -> Access {
838838
match mutability {
839-
Mutability::Shared => Access::Shared,
839+
Mutability::Not => Access::Shared,
840840
Mutability::Mut => Access::Exclusive,
841841
}
842842
}
@@ -865,7 +865,10 @@ impl SelfParam {
865865
.params
866866
.first()
867867
.map(|param| match *param {
868-
TypeRef::Reference(.., mutability) => mutability.into(),
868+
TypeRef::Reference(.., mutability) => match mutability {
869+
hir_def::type_ref::Mutability::Shared => Access::Shared,
870+
hir_def::type_ref::Mutability::Mut => Access::Exclusive,
871+
},
869872
_ => Access::Owned,
870873
})
871874
.unwrap_or(Access::Owned)

crates/hir_ty/src/display.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::{
88
TraitRef, Ty,
99
};
1010
use arrayvec::ArrayVec;
11+
use chalk_ir::Mutability;
1112
use hir_def::{
1213
db::DefDatabase, find_path, generics::TypeParamProvenance, item_scope::ItemInNs, AdtId,
1314
AssocContainerId, HasModule, Lookup, ModuleId, TraitId,
@@ -291,9 +292,23 @@ impl HirDisplay for Ty {
291292
t.into_displayable(f.db, f.max_size, f.omit_verbose_types, f.display_target);
292293

293294
if matches!(self, Ty::Raw(..)) {
294-
write!(f, "*{}", m.as_keyword_for_ptr())?;
295+
write!(
296+
f,
297+
"*{}",
298+
match m {
299+
Mutability::Not => "const ",
300+
Mutability::Mut => "mut ",
301+
}
302+
)?;
295303
} else {
296-
write!(f, "&{}", m.as_keyword_for_ref())?;
304+
write!(
305+
f,
306+
"&{}",
307+
match m {
308+
Mutability::Not => "",
309+
Mutability::Mut => "mut ",
310+
}
311+
)?;
297312
}
298313

299314
let datas;

crates/hir_ty/src/infer.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ use std::mem;
1818
use std::ops::Index;
1919
use std::sync::Arc;
2020

21+
use chalk_ir::Mutability;
2122
use hir_def::{
2223
body::Body,
2324
data::{ConstData, FunctionData, StaticData},
2425
expr::{ArithOp, BinaryOp, BindingAnnotation, ExprId, PatId},
2526
lang_item::LangItemTarget,
2627
path::{path, Path},
2728
resolver::{HasResolver, Resolver, TypeNs},
28-
type_ref::{Mutability, TypeRef},
29+
type_ref::TypeRef,
2930
AdtId, AssocItemId, DefWithBodyId, EnumVariantId, FieldId, FunctionId, Lookup, TraitId,
3031
TypeAliasId, VariantId,
3132
};
@@ -87,7 +88,7 @@ impl BindingMode {
8788
fn convert(annotation: BindingAnnotation) -> BindingMode {
8889
match annotation {
8990
BindingAnnotation::Unannotated | BindingAnnotation::Mutable => BindingMode::Move,
90-
BindingAnnotation::Ref => BindingMode::Ref(Mutability::Shared),
91+
BindingAnnotation::Ref => BindingMode::Ref(Mutability::Not),
9192
BindingAnnotation::RefMut => BindingMode::Ref(Mutability::Mut),
9293
}
9394
}

crates/hir_ty/src/infer/coerce.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
//!
55
//! See: https://doc.rust-lang.org/nomicon/coercions.html
66
7-
use chalk_ir::TyVariableKind;
8-
use hir_def::{lang_item::LangItemTarget, type_ref::Mutability};
7+
use chalk_ir::{Mutability, TyVariableKind};
8+
use hir_def::lang_item::LangItemTarget;
99
use test_utils::mark;
1010

1111
use crate::{autoderef, traits::Solution, Obligation, Substs, TraitRef, Ty};
@@ -73,20 +73,20 @@ impl<'a> InferenceContext<'a> {
7373
match (&mut from_ty, to_ty) {
7474
// `*mut T` -> `*const T`
7575
// `&mut T` -> `&T`
76-
(Ty::Raw(m1, ..), Ty::Raw(m2 @ Mutability::Shared, ..))
77-
| (Ty::Ref(m1, ..), Ty::Ref(m2 @ Mutability::Shared, ..)) => {
76+
(Ty::Raw(m1, ..), Ty::Raw(m2 @ Mutability::Not, ..))
77+
| (Ty::Ref(m1, ..), Ty::Ref(m2 @ Mutability::Not, ..)) => {
7878
*m1 = *m2;
7979
}
8080
// `&T` -> `*const T`
8181
// `&mut T` -> `*mut T`/`*const T`
82-
(Ty::Ref(.., substs), &Ty::Raw(m2 @ Mutability::Shared, ..))
82+
(Ty::Ref(.., substs), &Ty::Raw(m2 @ Mutability::Not, ..))
8383
| (Ty::Ref(Mutability::Mut, substs), &Ty::Raw(m2, ..)) => {
8484
from_ty = Ty::Raw(m2, substs.clone());
8585
}
8686

8787
// Illegal mutability conversion
88-
(Ty::Raw(Mutability::Shared, ..), Ty::Raw(Mutability::Mut, ..))
89-
| (Ty::Ref(Mutability::Shared, ..), Ty::Ref(Mutability::Mut, ..)) => return false,
88+
(Ty::Raw(Mutability::Not, ..), Ty::Raw(Mutability::Mut, ..))
89+
| (Ty::Ref(Mutability::Not, ..), Ty::Ref(Mutability::Mut, ..)) => return false,
9090

9191
// `{function_type}` -> `fn()`
9292
(Ty::FnDef(..), Ty::Function { .. }) => match from_ty.callable_sig(self.db) {

crates/hir_ty/src/infer/expr.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::iter::{repeat, repeat_with};
44
use std::{mem, sync::Arc};
55

6-
use chalk_ir::TyVariableKind;
6+
use chalk_ir::{Mutability, TyVariableKind};
77
use hir_def::{
88
expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp},
99
path::{GenericArg, GenericArgs},
@@ -15,12 +15,14 @@ use syntax::ast::RangeOp;
1515
use test_utils::mark;
1616

1717
use crate::{
18-
autoderef, method_resolution, op,
18+
autoderef,
19+
lower::lower_to_chalk_mutability,
20+
method_resolution, op,
1921
primitive::{self, UintTy},
2022
traits::{FnTrait, InEnvironment},
2123
utils::{generics, variant_data, Generics},
22-
Binders, CallableDefId, FnPointer, FnSig, Mutability, Obligation, OpaqueTyId, Rawness, Scalar,
23-
Substs, TraitRef, Ty,
24+
Binders, CallableDefId, FnPointer, FnSig, Obligation, OpaqueTyId, Rawness, Scalar, Substs,
25+
TraitRef, Ty,
2426
};
2527

2628
use super::{
@@ -462,10 +464,11 @@ impl<'a> InferenceContext<'a> {
462464
cast_ty
463465
}
464466
Expr::Ref { expr, rawness, mutability } => {
467+
let mutability = lower_to_chalk_mutability(*mutability);
465468
let expectation = if let Some((exp_inner, exp_rawness, exp_mutability)) =
466469
&expected.ty.as_reference_or_ptr()
467470
{
468-
if *exp_mutability == Mutability::Mut && *mutability == Mutability::Shared {
471+
if *exp_mutability == Mutability::Mut && mutability == Mutability::Not {
469472
// FIXME: throw type error - expected mut reference but found shared ref,
470473
// which cannot be coerced
471474
}
@@ -479,8 +482,8 @@ impl<'a> InferenceContext<'a> {
479482
};
480483
let inner_ty = self.infer_expr_inner(*expr, &expectation);
481484
match rawness {
482-
Rawness::RawPtr => Ty::Raw(*mutability, Substs::single(inner_ty)),
483-
Rawness::Ref => Ty::Ref(*mutability, Substs::single(inner_ty)),
485+
Rawness::RawPtr => Ty::Raw(mutability, Substs::single(inner_ty)),
486+
Rawness::Ref => Ty::Ref(mutability, Substs::single(inner_ty)),
484487
}
485488
}
486489
Expr::Box { expr } => {
@@ -684,11 +687,11 @@ impl<'a> InferenceContext<'a> {
684687
}
685688
Expr::Literal(lit) => match lit {
686689
Literal::Bool(..) => Ty::Scalar(Scalar::Bool),
687-
Literal::String(..) => Ty::Ref(Mutability::Shared, Substs::single(Ty::Str)),
690+
Literal::String(..) => Ty::Ref(Mutability::Not, Substs::single(Ty::Str)),
688691
Literal::ByteString(..) => {
689692
let byte_type = Ty::Scalar(Scalar::Uint(UintTy::U8));
690693
let array_type = Ty::Array(Substs::single(byte_type));
691-
Ty::Ref(Mutability::Shared, Substs::single(array_type))
694+
Ty::Ref(Mutability::Not, Substs::single(array_type))
692695
}
693696
Literal::Char(..) => Ty::Scalar(Scalar::Char),
694697
Literal::Int(_v, ty) => match ty {

crates/hir_ty/src/infer/pat.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
use std::iter::repeat;
44
use std::sync::Arc;
55

6+
use chalk_ir::Mutability;
67
use hir_def::{
78
expr::{BindingAnnotation, Expr, Literal, Pat, PatId, RecordFieldPat},
89
path::Path,
9-
type_ref::Mutability,
1010
FieldId,
1111
};
1212
use hir_expand::name::Name;
1313
use test_utils::mark;
1414

1515
use super::{BindingMode, Expectation, InferenceContext};
16-
use crate::{utils::variant_data, Substs, Ty};
16+
use crate::{lower::lower_to_chalk_mutability, utils::variant_data, Substs, Ty};
1717

1818
impl<'a> InferenceContext<'a> {
1919
fn infer_tuple_struct_pat(
@@ -103,7 +103,7 @@ impl<'a> InferenceContext<'a> {
103103
expected = inner;
104104
default_bm = match default_bm {
105105
BindingMode::Move => BindingMode::Ref(mutability),
106-
BindingMode::Ref(Mutability::Shared) => BindingMode::Ref(Mutability::Shared),
106+
BindingMode::Ref(Mutability::Not) => BindingMode::Ref(Mutability::Not),
107107
BindingMode::Ref(Mutability::Mut) => BindingMode::Ref(mutability),
108108
}
109109
}
@@ -152,17 +152,18 @@ impl<'a> InferenceContext<'a> {
152152
}
153153
}
154154
Pat::Ref { pat, mutability } => {
155+
let mutability = lower_to_chalk_mutability(*mutability);
155156
let expectation = match expected.as_reference() {
156157
Some((inner_ty, exp_mut)) => {
157-
if *mutability != exp_mut {
158+
if mutability != exp_mut {
158159
// FIXME: emit type error?
159160
}
160161
inner_ty
161162
}
162163
_ => &Ty::Unknown,
163164
};
164165
let subty = self.infer_pat(*pat, expectation, default_bm);
165-
Ty::Ref(*mutability, Substs::single(subty))
166+
Ty::Ref(mutability, Substs::single(subty))
166167
}
167168
Pat::TupleStruct { path: p, args: subpats, ellipsis } => self.infer_tuple_struct_pat(
168169
p.as_ref(),

crates/hir_ty/src/lib.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,9 @@ use std::{iter, mem, ops::Deref, sync::Arc};
2727

2828
use base_db::salsa;
2929
use hir_def::{
30-
builtin_type::BuiltinType,
31-
expr::ExprId,
32-
type_ref::{Mutability, Rawness},
33-
AdtId, AssocContainerId, DefWithBodyId, FunctionId, GenericDefId, HasModule, LifetimeParamId,
34-
Lookup, TraitId, TypeAliasId, TypeParamId,
30+
builtin_type::BuiltinType, expr::ExprId, type_ref::Rawness, AdtId, AssocContainerId,
31+
DefWithBodyId, FunctionId, GenericDefId, HasModule, LifetimeParamId, Lookup, TraitId,
32+
TypeAliasId, TypeParamId,
3533
};
3634
use itertools::Itertools;
3735

@@ -49,7 +47,7 @@ pub use lower::{
4947
};
5048
pub use traits::{InEnvironment, Obligation, ProjectionPredicate, TraitEnvironment};
5149

52-
pub use chalk_ir::{BoundVar, DebruijnIndex, Scalar, TyVariableKind};
50+
pub use chalk_ir::{BoundVar, DebruijnIndex, Mutability, Scalar, TyVariableKind};
5351

5452
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
5553
pub enum Lifetime {

crates/hir_ty/src/lower.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use std::{iter, sync::Arc};
99

1010
use base_db::CrateId;
11+
use chalk_ir::Mutability;
1112
use hir_def::{
1213
adt::StructKind,
1314
builtin_type::BuiltinType,
@@ -157,7 +158,7 @@ impl Ty {
157158
}
158159
TypeRef::RawPtr(inner, mutability) => {
159160
let inner_ty = Ty::from_hir(ctx, inner);
160-
Ty::Raw(*mutability, Substs::single(inner_ty))
161+
Ty::Raw(lower_to_chalk_mutability(*mutability), Substs::single(inner_ty))
161162
}
162163
TypeRef::Array(inner) => {
163164
let inner_ty = Ty::from_hir(ctx, inner);
@@ -169,7 +170,7 @@ impl Ty {
169170
}
170171
TypeRef::Reference(inner, _, mutability) => {
171172
let inner_ty = Ty::from_hir(ctx, inner);
172-
Ty::Ref(*mutability, Substs::single(inner_ty))
173+
Ty::Ref(lower_to_chalk_mutability(*mutability), Substs::single(inner_ty))
173174
}
174175
TypeRef::Placeholder => Ty::Unknown,
175176
TypeRef::Fn(params, is_varargs) => {
@@ -1259,3 +1260,10 @@ pub(crate) fn return_type_impl_traits(
12591260
Some(Arc::new(Binders::new(num_binders, return_type_impl_traits)))
12601261
}
12611262
}
1263+
1264+
pub(crate) fn lower_to_chalk_mutability(m: hir_def::type_ref::Mutability) -> Mutability {
1265+
match m {
1266+
hir_def::type_ref::Mutability::Shared => Mutability::Not,
1267+
hir_def::type_ref::Mutability::Mut => Mutability::Mut,
1268+
}
1269+
}

crates/hir_ty/src/method_resolution.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ use std::{iter, sync::Arc};
66

77
use arrayvec::ArrayVec;
88
use base_db::CrateId;
9+
use chalk_ir::Mutability;
910
use hir_def::{
10-
lang_item::LangItemTarget, type_ref::Mutability, AdtId, AssocContainerId, AssocItemId,
11-
FunctionId, GenericDefId, HasModule, ImplId, Lookup, ModuleId, TraitId, TypeAliasId,
11+
lang_item::LangItemTarget, AdtId, AssocContainerId, AssocItemId, FunctionId, GenericDefId,
12+
HasModule, ImplId, Lookup, ModuleId, TraitId, TypeAliasId,
1213
};
1314
use hir_expand::name::Name;
1415
use rustc_hash::{FxHashMap, FxHashSet};
@@ -251,7 +252,7 @@ impl Ty {
251252
}
252253
Ty::Str => lang_item_crate!("str_alloc", "str"),
253254
Ty::Slice(_) => lang_item_crate!("slice_alloc", "slice"),
254-
Ty::Raw(Mutability::Shared, _) => lang_item_crate!("const_ptr"),
255+
Ty::Raw(Mutability::Not, _) => lang_item_crate!("const_ptr"),
255256
Ty::Raw(Mutability::Mut, _) => lang_item_crate!("mut_ptr"),
256257
Ty::Dyn(_) => {
257258
return self.dyn_trait().and_then(|trait_| {
@@ -429,7 +430,7 @@ fn iterate_method_candidates_with_autoref(
429430
}
430431
let refed = Canonical {
431432
kinds: deref_chain[0].kinds.clone(),
432-
value: Ty::Ref(Mutability::Shared, Substs::single(deref_chain[0].value.clone())),
433+
value: Ty::Ref(Mutability::Not, Substs::single(deref_chain[0].value.clone())),
433434
};
434435
if iterate_method_candidates_by_receiver(
435436
&refed,

0 commit comments

Comments
 (0)