Skip to content

Commit 1ffe23e

Browse files
committed
Remove the old variants replaced by Ty::Apply
1 parent 2941f7b commit 1ffe23e

File tree

11 files changed

+247
-386
lines changed

11 files changed

+247
-386
lines changed

crates/ra_assists/src/fill_match_arms.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::fmt::Write;
22

33
use hir::{
44
AdtDef, Ty, FieldSource, source_binder,
5-
db::HirDatabase,
5+
db::HirDatabase, ApplicationTy, TypeName
66
};
77
use ra_syntax::ast::{self, AstNode};
88

@@ -26,14 +26,10 @@ pub(crate) fn fill_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<As
2626
let source_map = function.body_source_map(ctx.db);
2727
let node_expr = source_map.node_expr(expr)?;
2828
let match_expr_ty = infer_result[node_expr].clone();
29-
let enum_def = match match_expr_ty {
30-
Ty::Adt { def_id: AdtDef::Enum(e), .. } => e,
31-
Ty::Ref(adt, _) => match *adt {
32-
Ty::Adt { def_id: AdtDef::Enum(e), .. } => e,
33-
_ => return None,
34-
},
35-
_ => return None,
36-
};
29+
let enum_def = match_expr_ty.autoderef(ctx.db).find_map(|ty| match ty {
30+
Ty::Apply(ApplicationTy { name: TypeName::Adt(AdtDef::Enum(e)), .. }) => Some(e),
31+
_ => None,
32+
})?;
3733
let enum_name = enum_def.name(ctx.db)?;
3834
let db = ctx.db;
3935

crates/ra_hir/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub use self::{
5353
name::Name,
5454
ids::{HirFileId, MacroCallId, MacroCallLoc, HirInterner},
5555
nameres::{PerNs, Namespace},
56-
ty::{Ty, Substs, display::HirDisplay},
56+
ty::{Ty, ApplicationTy, TypeName, Substs, display::HirDisplay},
5757
impl_block::{ImplBlock, ImplItem},
5858
docs::{Docs, Documentation},
5959
adt::AdtDef,

crates/ra_hir/src/ty.rs

Lines changed: 16 additions & 197 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub(crate) use lower::{TypableDef, CallableDef, type_for_def, type_for_field, ca
2020
pub(crate) use infer::{infer, InferenceResult, InferTy};
2121
use display::{HirDisplay, HirFormatter};
2222

23-
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
23+
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
2424
pub enum TypeName {
2525
/// The primitive boolean type. Written as `bool`.
2626
Bool,
@@ -96,80 +96,11 @@ pub struct ApplicationTy {
9696
/// This should be cheap to clone.
9797
#[derive(Clone, PartialEq, Eq, Debug)]
9898
pub enum Ty {
99+
/// A nominal type with (maybe 0) type parameters. This might be a primitive
100+
/// type like `bool`, a struct, tuple, function pointer, reference or
101+
/// several other things.
99102
Apply(ApplicationTy),
100103

101-
/// The primitive boolean type. Written as `bool`.
102-
Bool,
103-
104-
/// The primitive character type; holds a Unicode scalar value
105-
/// (a non-surrogate code point). Written as `char`.
106-
Char,
107-
108-
/// A primitive integer type. For example, `i32`.
109-
Int(primitive::UncertainIntTy),
110-
111-
/// A primitive floating-point type. For example, `f64`.
112-
Float(primitive::UncertainFloatTy),
113-
114-
/// Structures, enumerations and unions.
115-
Adt {
116-
/// The definition of the struct/enum.
117-
def_id: AdtDef,
118-
/// Substitutions for the generic parameters of the type.
119-
substs: Substs,
120-
},
121-
122-
/// The pointee of a string slice. Written as `str`.
123-
Str,
124-
125-
/// The pointee of an array slice. Written as `[T]`.
126-
Slice(Arc<Ty>),
127-
128-
/// An array with the given length. Written as `[T; n]`.
129-
Array(Arc<Ty>),
130-
131-
/// A raw pointer. Written as `*mut T` or `*const T`
132-
RawPtr(Arc<Ty>, Mutability),
133-
134-
/// A reference; a pointer with an associated lifetime. Written as
135-
/// `&'a mut T` or `&'a T`.
136-
Ref(Arc<Ty>, Mutability),
137-
138-
/// The anonymous type of a function declaration/definition. Each
139-
/// function has a unique type, which is output (for a function
140-
/// named `foo` returning an `i32`) as `fn() -> i32 {foo}`.
141-
///
142-
/// This includes tuple struct / enum variant constructors as well.
143-
///
144-
/// For example the type of `bar` here:
145-
///
146-
/// ```rust
147-
/// fn foo() -> i32 { 1 }
148-
/// let bar = foo; // bar: fn() -> i32 {foo}
149-
/// ```
150-
FnDef {
151-
/// The definition of the function / constructor.
152-
def: CallableDef,
153-
/// Substitutions for the generic parameters of the type
154-
substs: Substs,
155-
},
156-
157-
/// A pointer to a function. Written as `fn() -> i32`.
158-
///
159-
/// For example the type of `bar` here:
160-
///
161-
/// ```rust
162-
/// fn foo() -> i32 { 1 }
163-
/// let bar: fn() -> i32 = foo;
164-
/// ```
165-
FnPtr(Substs),
166-
167-
/// The never type `!`.
168-
Never,
169-
170-
/// A tuple type. For example, `(i32, bool)`.
171-
Tuple(Substs),
172-
173104
/// A type parameter; for example, `T` in `fn f<T>(x: T) {}
174105
Param {
175106
/// The index of the parameter (starting with parameters from the
@@ -200,6 +131,10 @@ impl Substs {
200131
Substs(Arc::new([]))
201132
}
202133

134+
pub fn single(ty: Ty) -> Substs {
135+
Substs(Arc::new([ty]))
136+
}
137+
203138
pub fn iter(&self) -> impl Iterator<Item = &Ty> {
204139
self.0.iter()
205140
}
@@ -256,6 +191,12 @@ impl FnSig {
256191
}
257192

258193
impl Ty {
194+
pub fn simple(name: TypeName) -> Ty {
195+
Ty::Apply(ApplicationTy { name, parameters: Substs::empty() })
196+
}
197+
pub fn apply_one(name: TypeName, param: Ty) -> Ty {
198+
Ty::Apply(ApplicationTy { name, parameters: Substs::single(param) })
199+
}
259200
pub fn apply(name: TypeName, parameters: Substs) -> Ty {
260201
Ty::Apply(ApplicationTy { name, parameters })
261202
}
@@ -270,38 +211,7 @@ impl Ty {
270211
t.walk(f);
271212
}
272213
}
273-
Ty::Slice(t) | Ty::Array(t) => t.walk(f),
274-
Ty::RawPtr(t, _) => t.walk(f),
275-
Ty::Ref(t, _) => t.walk(f),
276-
Ty::Tuple(ts) => {
277-
for t in ts.iter() {
278-
t.walk(f);
279-
}
280-
}
281-
Ty::FnPtr(sig) => {
282-
for t in sig.iter() {
283-
t.walk(f);
284-
}
285-
}
286-
Ty::FnDef { substs, .. } => {
287-
for t in substs.0.iter() {
288-
t.walk(f);
289-
}
290-
}
291-
Ty::Adt { substs, .. } => {
292-
for t in substs.0.iter() {
293-
t.walk(f);
294-
}
295-
}
296-
Ty::Bool
297-
| Ty::Char
298-
| Ty::Int(_)
299-
| Ty::Float(_)
300-
| Ty::Str
301-
| Ty::Never
302-
| Ty::Param { .. }
303-
| Ty::Infer(_)
304-
| Ty::Unknown => {}
214+
Ty::Param { .. } | Ty::Infer(_) | Ty::Unknown => {}
305215
}
306216
f(self);
307217
}
@@ -311,30 +221,7 @@ impl Ty {
311221
Ty::Apply(a_ty) => {
312222
a_ty.parameters.walk_mut(f);
313223
}
314-
Ty::Slice(t) | Ty::Array(t) => Arc::make_mut(t).walk_mut(f),
315-
Ty::RawPtr(t, _) => Arc::make_mut(t).walk_mut(f),
316-
Ty::Ref(t, _) => Arc::make_mut(t).walk_mut(f),
317-
Ty::Tuple(ts) => {
318-
ts.walk_mut(f);
319-
}
320-
Ty::FnPtr(sig) => {
321-
sig.walk_mut(f);
322-
}
323-
Ty::FnDef { substs, .. } => {
324-
substs.walk_mut(f);
325-
}
326-
Ty::Adt { substs, .. } => {
327-
substs.walk_mut(f);
328-
}
329-
Ty::Bool
330-
| Ty::Char
331-
| Ty::Int(_)
332-
| Ty::Float(_)
333-
| Ty::Str
334-
| Ty::Never
335-
| Ty::Param { .. }
336-
| Ty::Infer(_)
337-
| Ty::Unknown => {}
224+
Ty::Param { .. } | Ty::Infer(_) | Ty::Unknown => {}
338225
}
339226
f(self);
340227
}
@@ -354,8 +241,6 @@ impl Ty {
354241
TypeName::RawPtr(..) => Some(Ty::clone(a_ty.parameters.as_single())),
355242
_ => None,
356243
},
357-
Ty::Ref(t, _) => Some(Ty::clone(t)),
358-
Ty::RawPtr(t, _) => Some(Ty::clone(t)),
359244
_ => None,
360245
}
361246
}
@@ -369,8 +254,6 @@ impl Ty {
369254
Ty::Apply(ApplicationTy { name, .. }) => {
370255
Ty::Apply(ApplicationTy { name, parameters: substs })
371256
}
372-
Ty::Adt { def_id, .. } => Ty::Adt { def_id, substs },
373-
Ty::FnDef { def, .. } => Ty::FnDef { def, substs },
374257
_ => self,
375258
}
376259
}
@@ -396,7 +279,6 @@ impl Ty {
396279
fn substs(&self) -> Option<Substs> {
397280
match self {
398281
Ty::Apply(ApplicationTy { parameters, .. }) => Some(parameters.clone()),
399-
Ty::Adt { substs, .. } | Ty::FnDef { substs, .. } => Some(substs.clone()),
400282
_ => None,
401283
}
402284
}
@@ -487,69 +369,6 @@ impl HirDisplay for Ty {
487369
fn hir_fmt(&self, f: &mut HirFormatter<impl HirDatabase>) -> fmt::Result {
488370
match self {
489371
Ty::Apply(a_ty) => a_ty.hir_fmt(f)?,
490-
Ty::Bool => write!(f, "bool")?,
491-
Ty::Char => write!(f, "char")?,
492-
Ty::Int(t) => write!(f, "{}", t)?,
493-
Ty::Float(t) => write!(f, "{}", t)?,
494-
Ty::Str => write!(f, "str")?,
495-
Ty::Slice(t) | Ty::Array(t) => {
496-
write!(f, "[{}]", t.display(f.db))?;
497-
}
498-
Ty::RawPtr(t, m) => {
499-
write!(f, "*{}{}", m.as_keyword_for_ptr(), t.display(f.db))?;
500-
}
501-
Ty::Ref(t, m) => {
502-
write!(f, "&{}{}", m.as_keyword_for_ref(), t.display(f.db))?;
503-
}
504-
Ty::Never => write!(f, "!")?,
505-
Ty::Tuple(ts) => {
506-
if ts.0.len() == 1 {
507-
write!(f, "({},)", ts.0[0].display(f.db))?;
508-
} else {
509-
write!(f, "(")?;
510-
f.write_joined(&*ts.0, ", ")?;
511-
write!(f, ")")?;
512-
}
513-
}
514-
Ty::FnPtr(sig) => {
515-
let sig = FnSig::from_fn_ptr_substs(sig);
516-
write!(f, "fn(")?;
517-
f.write_joined(sig.params(), ", ")?;
518-
write!(f, ") -> {}", sig.ret().display(f.db))?;
519-
}
520-
Ty::FnDef { def, substs, .. } => {
521-
let sig = f.db.callable_item_signature(*def);
522-
let name = match def {
523-
CallableDef::Function(ff) => ff.name(f.db),
524-
CallableDef::Struct(s) => s.name(f.db).unwrap_or_else(Name::missing),
525-
CallableDef::EnumVariant(e) => e.name(f.db).unwrap_or_else(Name::missing),
526-
};
527-
match def {
528-
CallableDef::Function(_) => write!(f, "fn {}", name)?,
529-
CallableDef::Struct(_) | CallableDef::EnumVariant(_) => write!(f, "{}", name)?,
530-
}
531-
if substs.0.len() > 0 {
532-
write!(f, "<")?;
533-
f.write_joined(&*substs.0, ", ")?;
534-
write!(f, ">")?;
535-
}
536-
write!(f, "(")?;
537-
f.write_joined(sig.params(), ", ")?;
538-
write!(f, ") -> {}", sig.ret().display(f.db))?;
539-
}
540-
Ty::Adt { def_id, substs, .. } => {
541-
let name = match def_id {
542-
AdtDef::Struct(s) => s.name(f.db),
543-
AdtDef::Enum(e) => e.name(f.db),
544-
}
545-
.unwrap_or_else(Name::missing);
546-
write!(f, "{}", name)?;
547-
if substs.0.len() > 0 {
548-
write!(f, "<")?;
549-
f.write_joined(&*substs.0, ", ")?;
550-
write!(f, ">")?;
551-
}
552-
}
553372
Ty::Param { name, .. } => write!(f, "{}", name)?,
554373
Ty::Unknown => write!(f, "{{unknown}}")?,
555374
Ty::Infer(..) => write!(f, "_")?,

0 commit comments

Comments
 (0)