From 628b530e92c1579f3924c37290ad59ac0512d2a0 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 16 Mar 2019 16:50:31 +0100 Subject: Some more Ty displaying cleanup --- crates/ra_hir/src/ty.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'crates/ra_hir/src/ty.rs') diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index f64877f3b..884cea52a 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -304,8 +304,8 @@ impl HirDisplay for Ty { match self { Ty::Bool => write!(f, "bool")?, Ty::Char => write!(f, "char")?, - Ty::Int(t) => write!(f, "{}", t.ty_to_string())?, - Ty::Float(t) => write!(f, "{}", t.ty_to_string())?, + Ty::Int(t) => write!(f, "{}", t)?, + Ty::Float(t) => write!(f, "{}", t)?, Ty::Str => write!(f, "str")?, Ty::Slice(t) | Ty::Array(t) => { write!(f, "[{}]", t.display(f.db))?; -- cgit v1.2.3 From a9ddaba905348897606948658798f9f46854acf7 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 16 Mar 2019 17:21:32 +0100 Subject: Refactor FnSig a bit --- crates/ra_hir/src/ty.rs | 81 ++++++++++++++++++++++++++++--------------------- 1 file changed, 47 insertions(+), 34 deletions(-) (limited to 'crates/ra_hir/src/ty.rs') diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index 884cea52a..31f726f35 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -78,7 +78,7 @@ pub enum Ty { /// The definition of the function / constructor. def: CallableDef, /// Parameters and return type - sig: Arc, + sig: FnSig, /// Substitutions for the generic parameters of the type substs: Substs, }, @@ -91,7 +91,7 @@ pub enum Ty { /// fn foo() -> i32 { 1 } /// let bar: fn() -> i32 = foo; /// ``` - FnPtr(Arc), + FnPtr(FnSig), /// The never type `!`. Never, @@ -128,13 +128,44 @@ impl Substs { pub fn empty() -> Substs { Substs(Arc::new([])) } + + pub fn walk_mut(&mut self, f: &mut impl FnMut(&mut Ty)) { + // Without an Arc::make_mut_slice, we can't avoid the clone here: + let mut v: Vec<_> = self.0.iter().cloned().collect(); + for t in &mut v { + t.walk_mut(f); + } + self.0 = v.into(); + } } /// A function signature. #[derive(Clone, PartialEq, Eq, Debug)] pub struct FnSig { - input: Vec, - output: Ty, + params_and_return: Arc<[Ty]>, +} + +impl FnSig { + pub fn from_params_and_return(mut params: Vec, ret: Ty) -> FnSig { + params.push(ret); + FnSig { params_and_return: params.into() } + } + pub fn params(&self) -> &[Ty] { + &self.params_and_return[0..self.params_and_return.len() - 1] + } + + pub fn ret(&self) -> &Ty { + &self.params_and_return[self.params_and_return.len() - 1] + } + + pub fn walk_mut(&mut self, f: &mut impl FnMut(&mut Ty)) { + // Without an Arc::make_mut_slice, we can't avoid the clone here: + let mut v: Vec<_> = self.params_and_return.iter().cloned().collect(); + for t in &mut v { + t.walk_mut(f); + } + self.params_and_return = v.into(); + } } impl Ty { @@ -153,16 +184,16 @@ impl Ty { } } Ty::FnPtr(sig) => { - for input in &sig.input { + for input in sig.params() { input.walk(f); } - sig.output.walk(f); + sig.ret().walk(f); } Ty::FnDef { substs, sig, .. } => { - for input in &sig.input { + for input in sig.params() { input.walk(f); } - sig.output.walk(f); + sig.ret().walk(f); for t in substs.0.iter() { t.walk(f); } @@ -199,32 +230,14 @@ impl Ty { *ts = v.into(); } Ty::FnPtr(sig) => { - let sig_mut = Arc::make_mut(sig); - for input in &mut sig_mut.input { - input.walk_mut(f); - } - sig_mut.output.walk_mut(f); + sig.walk_mut(f); } Ty::FnDef { substs, sig, .. } => { - let sig_mut = Arc::make_mut(sig); - for input in &mut sig_mut.input { - input.walk_mut(f); - } - sig_mut.output.walk_mut(f); - // Without an Arc::make_mut_slice, we can't avoid the clone here: - let mut v: Vec<_> = substs.0.iter().cloned().collect(); - for t in &mut v { - t.walk_mut(f); - } - substs.0 = v.into(); + sig.walk_mut(f); + substs.walk_mut(f); } Ty::Adt { substs, .. } => { - // Without an Arc::make_mut_slice, we can't avoid the clone here: - let mut v: Vec<_> = substs.0.iter().cloned().collect(); - for t in &mut v { - t.walk_mut(f); - } - substs.0 = v.into(); + substs.walk_mut(f); } Ty::Bool | Ty::Char @@ -328,8 +341,8 @@ impl HirDisplay for Ty { } Ty::FnPtr(sig) => { write!(f, "fn(")?; - f.write_joined(&sig.input, ", ")?; - write!(f, ") -> {}", sig.output.display(f.db))?; + f.write_joined(sig.params(), ", ")?; + write!(f, ") -> {}", sig.ret().display(f.db))?; } Ty::FnDef { def, substs, sig, .. } => { let name = match def { @@ -347,8 +360,8 @@ impl HirDisplay for Ty { write!(f, ">")?; } write!(f, "(")?; - f.write_joined(&sig.input, ", ")?; - write!(f, ") -> {}", sig.output.display(f.db))?; + f.write_joined(sig.params(), ", ")?; + write!(f, ") -> {}", sig.ret().display(f.db))?; } Ty::Adt { def_id, substs, .. } => { let name = match def_id { -- cgit v1.2.3 From 7faae12311895b20b4dec47825708d15f3aaf034 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 16 Mar 2019 17:29:55 +0100 Subject: Remove FnSig from FnDef type It doesn't need to be in there since it's just information from the def. Another step towards aligning Ty with Chalk's representation. --- crates/ra_hir/src/ty.rs | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) (limited to 'crates/ra_hir/src/ty.rs') diff --git a/crates/ra_hir/src/ty.rs b/crates/ra_hir/src/ty.rs index 31f726f35..2ea3b341f 100644 --- a/crates/ra_hir/src/ty.rs +++ b/crates/ra_hir/src/ty.rs @@ -16,7 +16,7 @@ use std::{fmt, mem}; use crate::{Name, AdtDef, type_ref::Mutability, db::HirDatabase}; -pub(crate) use lower::{TypableDef, CallableDef, type_for_def, type_for_field}; +pub(crate) use lower::{TypableDef, CallableDef, type_for_def, type_for_field, callable_item_sig}; pub(crate) use infer::{infer, InferenceResult, InferTy}; use display::{HirDisplay, HirFormatter}; @@ -77,8 +77,6 @@ pub enum Ty { FnDef { /// The definition of the function / constructor. def: CallableDef, - /// Parameters and return type - sig: FnSig, /// Substitutions for the generic parameters of the type substs: Substs, }, @@ -189,11 +187,7 @@ impl Ty { } sig.ret().walk(f); } - Ty::FnDef { substs, sig, .. } => { - for input in sig.params() { - input.walk(f); - } - sig.ret().walk(f); + Ty::FnDef { substs, .. } => { for t in substs.0.iter() { t.walk(f); } @@ -232,8 +226,7 @@ impl Ty { Ty::FnPtr(sig) => { sig.walk_mut(f); } - Ty::FnDef { substs, sig, .. } => { - sig.walk_mut(f); + Ty::FnDef { substs, .. } => { substs.walk_mut(f); } Ty::Adt { substs, .. } => { @@ -275,7 +268,7 @@ impl Ty { pub fn apply_substs(self, substs: Substs) -> Ty { match self { Ty::Adt { def_id, .. } => Ty::Adt { def_id, substs }, - Ty::FnDef { def, sig, .. } => Ty::FnDef { def, sig, substs }, + Ty::FnDef { def, .. } => Ty::FnDef { def, substs }, _ => self, } } @@ -344,7 +337,8 @@ impl HirDisplay for Ty { f.write_joined(sig.params(), ", ")?; write!(f, ") -> {}", sig.ret().display(f.db))?; } - Ty::FnDef { def, substs, sig, .. } => { + Ty::FnDef { def, substs, .. } => { + let sig = f.db.callable_item_signature(*def); let name = match def { CallableDef::Function(ff) => ff.name(f.db), CallableDef::Struct(s) => s.name(f.db).unwrap_or_else(Name::missing), -- cgit v1.2.3