From 7072f59fc61b8b487f301df1fe5eefbcc84672aa Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Mon, 1 Mar 2021 19:30:34 +0100 Subject: Use chalk_ir::Mutability --- crates/hir_ty/src/infer/coerce.rs | 14 +++++++------- crates/hir_ty/src/infer/expr.rs | 21 ++++++++++++--------- crates/hir_ty/src/infer/pat.rs | 11 ++++++----- 3 files changed, 25 insertions(+), 21 deletions(-) (limited to 'crates/hir_ty/src/infer') diff --git a/crates/hir_ty/src/infer/coerce.rs b/crates/hir_ty/src/infer/coerce.rs index c33d8c61e..cf0a3add4 100644 --- a/crates/hir_ty/src/infer/coerce.rs +++ b/crates/hir_ty/src/infer/coerce.rs @@ -4,8 +4,8 @@ //! //! See: https://doc.rust-lang.org/nomicon/coercions.html -use chalk_ir::TyVariableKind; -use hir_def::{lang_item::LangItemTarget, type_ref::Mutability}; +use chalk_ir::{Mutability, TyVariableKind}; +use hir_def::lang_item::LangItemTarget; use test_utils::mark; use crate::{autoderef, traits::Solution, Obligation, Substs, TraitRef, Ty}; @@ -73,20 +73,20 @@ impl<'a> InferenceContext<'a> { match (&mut from_ty, to_ty) { // `*mut T` -> `*const T` // `&mut T` -> `&T` - (Ty::Raw(m1, ..), Ty::Raw(m2 @ Mutability::Shared, ..)) - | (Ty::Ref(m1, ..), Ty::Ref(m2 @ Mutability::Shared, ..)) => { + (Ty::Raw(m1, ..), Ty::Raw(m2 @ Mutability::Not, ..)) + | (Ty::Ref(m1, ..), Ty::Ref(m2 @ Mutability::Not, ..)) => { *m1 = *m2; } // `&T` -> `*const T` // `&mut T` -> `*mut T`/`*const T` - (Ty::Ref(.., substs), &Ty::Raw(m2 @ Mutability::Shared, ..)) + (Ty::Ref(.., substs), &Ty::Raw(m2 @ Mutability::Not, ..)) | (Ty::Ref(Mutability::Mut, substs), &Ty::Raw(m2, ..)) => { from_ty = Ty::Raw(m2, substs.clone()); } // Illegal mutability conversion - (Ty::Raw(Mutability::Shared, ..), Ty::Raw(Mutability::Mut, ..)) - | (Ty::Ref(Mutability::Shared, ..), Ty::Ref(Mutability::Mut, ..)) => return false, + (Ty::Raw(Mutability::Not, ..), Ty::Raw(Mutability::Mut, ..)) + | (Ty::Ref(Mutability::Not, ..), Ty::Ref(Mutability::Mut, ..)) => return false, // `{function_type}` -> `fn()` (Ty::FnDef(..), Ty::Function { .. }) => match from_ty.callable_sig(self.db) { diff --git a/crates/hir_ty/src/infer/expr.rs b/crates/hir_ty/src/infer/expr.rs index 7852b3d23..cf1f1038a 100644 --- a/crates/hir_ty/src/infer/expr.rs +++ b/crates/hir_ty/src/infer/expr.rs @@ -3,7 +3,7 @@ use std::iter::{repeat, repeat_with}; use std::{mem, sync::Arc}; -use chalk_ir::TyVariableKind; +use chalk_ir::{Mutability, TyVariableKind}; use hir_def::{ expr::{Array, BinaryOp, Expr, ExprId, Literal, Statement, UnaryOp}, path::{GenericArg, GenericArgs}, @@ -15,12 +15,14 @@ use syntax::ast::RangeOp; use test_utils::mark; use crate::{ - autoderef, method_resolution, op, + autoderef, + lower::lower_to_chalk_mutability, + method_resolution, op, primitive::{self, UintTy}, traits::{FnTrait, InEnvironment}, utils::{generics, variant_data, Generics}, - Binders, CallableDefId, FnPointer, FnSig, Mutability, Obligation, OpaqueTyId, Rawness, Scalar, - Substs, TraitRef, Ty, + Binders, CallableDefId, FnPointer, FnSig, Obligation, OpaqueTyId, Rawness, Scalar, Substs, + TraitRef, Ty, }; use super::{ @@ -462,10 +464,11 @@ impl<'a> InferenceContext<'a> { cast_ty } Expr::Ref { expr, rawness, mutability } => { + let mutability = lower_to_chalk_mutability(*mutability); let expectation = if let Some((exp_inner, exp_rawness, exp_mutability)) = &expected.ty.as_reference_or_ptr() { - if *exp_mutability == Mutability::Mut && *mutability == Mutability::Shared { + if *exp_mutability == Mutability::Mut && mutability == Mutability::Not { // FIXME: throw type error - expected mut reference but found shared ref, // which cannot be coerced } @@ -479,8 +482,8 @@ impl<'a> InferenceContext<'a> { }; let inner_ty = self.infer_expr_inner(*expr, &expectation); match rawness { - Rawness::RawPtr => Ty::Raw(*mutability, Substs::single(inner_ty)), - Rawness::Ref => Ty::Ref(*mutability, Substs::single(inner_ty)), + Rawness::RawPtr => Ty::Raw(mutability, Substs::single(inner_ty)), + Rawness::Ref => Ty::Ref(mutability, Substs::single(inner_ty)), } } Expr::Box { expr } => { @@ -684,11 +687,11 @@ impl<'a> InferenceContext<'a> { } Expr::Literal(lit) => match lit { Literal::Bool(..) => Ty::Scalar(Scalar::Bool), - Literal::String(..) => Ty::Ref(Mutability::Shared, Substs::single(Ty::Str)), + Literal::String(..) => Ty::Ref(Mutability::Not, Substs::single(Ty::Str)), Literal::ByteString(..) => { let byte_type = Ty::Scalar(Scalar::Uint(UintTy::U8)); let array_type = Ty::Array(Substs::single(byte_type)); - Ty::Ref(Mutability::Shared, Substs::single(array_type)) + Ty::Ref(Mutability::Not, Substs::single(array_type)) } Literal::Char(..) => Ty::Scalar(Scalar::Char), Literal::Int(_v, ty) => match ty { diff --git a/crates/hir_ty/src/infer/pat.rs b/crates/hir_ty/src/infer/pat.rs index a318e47f3..eb099311c 100644 --- a/crates/hir_ty/src/infer/pat.rs +++ b/crates/hir_ty/src/infer/pat.rs @@ -3,17 +3,17 @@ use std::iter::repeat; use std::sync::Arc; +use chalk_ir::Mutability; use hir_def::{ expr::{BindingAnnotation, Expr, Literal, Pat, PatId, RecordFieldPat}, path::Path, - type_ref::Mutability, FieldId, }; use hir_expand::name::Name; use test_utils::mark; use super::{BindingMode, Expectation, InferenceContext}; -use crate::{utils::variant_data, Substs, Ty}; +use crate::{lower::lower_to_chalk_mutability, utils::variant_data, Substs, Ty}; impl<'a> InferenceContext<'a> { fn infer_tuple_struct_pat( @@ -103,7 +103,7 @@ impl<'a> InferenceContext<'a> { expected = inner; default_bm = match default_bm { BindingMode::Move => BindingMode::Ref(mutability), - BindingMode::Ref(Mutability::Shared) => BindingMode::Ref(Mutability::Shared), + BindingMode::Ref(Mutability::Not) => BindingMode::Ref(Mutability::Not), BindingMode::Ref(Mutability::Mut) => BindingMode::Ref(mutability), } } @@ -152,9 +152,10 @@ impl<'a> InferenceContext<'a> { } } Pat::Ref { pat, mutability } => { + let mutability = lower_to_chalk_mutability(*mutability); let expectation = match expected.as_reference() { Some((inner_ty, exp_mut)) => { - if *mutability != exp_mut { + if mutability != exp_mut { // FIXME: emit type error? } inner_ty @@ -162,7 +163,7 @@ impl<'a> InferenceContext<'a> { _ => &Ty::Unknown, }; let subty = self.infer_pat(*pat, expectation, default_bm); - Ty::Ref(*mutability, Substs::single(subty)) + Ty::Ref(mutability, Substs::single(subty)) } Pat::TupleStruct { path: p, args: subpats, ellipsis } => self.infer_tuple_struct_pat( p.as_ref(), -- cgit v1.2.3