Use ty to access most TypeRefs

This commit is contained in:
Aleksey Kladov 2020-07-30 21:02:55 +02:00
parent 2e2642efcc
commit f95f425ae4
11 changed files with 39 additions and 41 deletions

View file

@ -22,7 +22,7 @@ pub(crate) fn change_return_type_to_result(acc: &mut Assists, ctx: &AssistContex
// FIXME: extend to lambdas as well // FIXME: extend to lambdas as well
let fn_def = ret_type.syntax().parent().and_then(ast::Fn::cast)?; let fn_def = ret_type.syntax().parent().and_then(ast::Fn::cast)?;
let type_ref = &ret_type.type_ref()?; let type_ref = &ret_type.ty()?;
let ret_type_str = type_ref.syntax().text().to_string(); let ret_type_str = type_ref.syntax().text().to_string();
let first_part_ret_type = ret_type_str.splitn(2, '<').next(); let first_part_ret_type = ret_type_str.splitn(2, '<').next();
if let Some(ret_type_first_part) = first_part_ret_type { if let Some(ret_type_first_part) = first_part_ret_type {

View file

@ -32,7 +32,7 @@ pub(crate) fn generate_from_impl_for_enum(acc: &mut Assists, ctx: &AssistContext
if field_list.fields().count() != 1 { if field_list.fields().count() != 1 {
return None; return None;
} }
let field_type = field_list.fields().next()?.type_ref()?; let field_type = field_list.fields().next()?.ty()?;
let path = match field_type { let path = match field_type {
ast::TypeRef::PathType(it) => it, ast::TypeRef::PathType(it) => it,
_ => return None, _ => return None,

View file

@ -234,7 +234,7 @@ fn lower_struct(
|| Either::Left(fd.clone()), || Either::Left(fd.clone()),
|| FieldData { || FieldData {
name: Name::new_tuple_field(i), name: Name::new_tuple_field(i),
type_ref: TypeRef::from_ast_opt(&ctx, fd.type_ref()), type_ref: TypeRef::from_ast_opt(&ctx, fd.ty()),
visibility: RawVisibility::from_ast(db, ast.with_value(fd.visibility())), visibility: RawVisibility::from_ast(db, ast.with_value(fd.visibility())),
}, },
); );

View file

@ -432,7 +432,7 @@ impl ExprCollector<'_> {
} }
ast::Expr::CastExpr(e) => { ast::Expr::CastExpr(e) => {
let expr = self.collect_expr_opt(e.expr()); let expr = self.collect_expr_opt(e.expr());
let type_ref = TypeRef::from_ast_opt(&self.ctx(), e.type_ref()); let type_ref = TypeRef::from_ast_opt(&self.ctx(), e.ty());
self.alloc_expr(Expr::Cast { expr, type_ref }, syntax_ptr) self.alloc_expr(Expr::Cast { expr, type_ref }, syntax_ptr)
} }
ast::Expr::RefExpr(e) => { ast::Expr::RefExpr(e) => {
@ -471,10 +471,8 @@ impl ExprCollector<'_> {
arg_types.push(type_ref); arg_types.push(type_ref);
} }
} }
let ret_type = e let ret_type =
.ret_type() e.ret_type().and_then(|r| r.ty()).map(|it| TypeRef::from_ast(&self.ctx(), it));
.and_then(|r| r.type_ref())
.map(|it| TypeRef::from_ast(&self.ctx(), it));
let body = self.collect_expr_opt(e.body()); let body = self.collect_expr_opt(e.body());
self.alloc_expr(Expr::Lambda { args, arg_types, ret_type, body }, syntax_ptr) self.alloc_expr(Expr::Lambda { args, arg_types, ret_type, body }, syntax_ptr)
} }

View file

@ -228,7 +228,7 @@ impl Ctx {
fn lower_tuple_field(&mut self, idx: usize, field: &ast::TupleField) -> Field { fn lower_tuple_field(&mut self, idx: usize, field: &ast::TupleField) -> Field {
let name = Name::new_tuple_field(idx); let name = Name::new_tuple_field(idx);
let visibility = self.lower_visibility(field); let visibility = self.lower_visibility(field);
let type_ref = self.lower_type_ref_opt(field.type_ref()); let type_ref = self.lower_type_ref_opt(field.ty());
let res = Field { name, type_ref, visibility }; let res = Field { name, type_ref, visibility };
res res
} }
@ -317,7 +317,7 @@ impl Ctx {
} }
} }
let ret_type = match func.ret_type().and_then(|rt| rt.type_ref()) { let ret_type = match func.ret_type().and_then(|rt| rt.ty()) {
Some(type_ref) => TypeRef::from_ast(&self.body_ctx, type_ref), Some(type_ref) => TypeRef::from_ast(&self.body_ctx, type_ref),
_ => TypeRef::unit(), _ => TypeRef::unit(),
}; };
@ -352,7 +352,7 @@ impl Ctx {
type_alias: &ast::TypeAlias, type_alias: &ast::TypeAlias,
) -> Option<FileItemTreeId<TypeAlias>> { ) -> Option<FileItemTreeId<TypeAlias>> {
let name = type_alias.name()?.as_name(); let name = type_alias.name()?.as_name();
let type_ref = type_alias.type_ref().map(|it| self.lower_type_ref(&it)); let type_ref = type_alias.ty().map(|it| self.lower_type_ref(&it));
let visibility = self.lower_visibility(type_alias); let visibility = self.lower_visibility(type_alias);
let bounds = self.lower_type_bounds(type_alias); let bounds = self.lower_type_bounds(type_alias);
let generic_params = self.lower_generic_params(GenericsOwner::TypeAlias, type_alias); let generic_params = self.lower_generic_params(GenericsOwner::TypeAlias, type_alias);

View file

@ -196,7 +196,7 @@ fn lower_generic_args_from_fn_path(
args.push(arg); args.push(arg);
} }
if let Some(ret_type) = ret_type { if let Some(ret_type) = ret_type {
let type_ref = TypeRef::from_ast_opt(&ctx, ret_type.type_ref()); let type_ref = TypeRef::from_ast_opt(&ctx, ret_type.ty());
bindings.push(AssociatedTypeBinding { bindings.push(AssociatedTypeBinding {
name: name![Output], name: name![Output],
type_ref: Some(type_ref), type_ref: Some(type_ref),

View file

@ -82,7 +82,7 @@ impl TypeRef {
/// Converts an `ast::TypeRef` to a `hir::TypeRef`. /// Converts an `ast::TypeRef` to a `hir::TypeRef`.
pub(crate) fn from_ast(ctx: &LowerCtx, node: ast::TypeRef) -> Self { pub(crate) fn from_ast(ctx: &LowerCtx, node: ast::TypeRef) -> Self {
match node { match node {
ast::TypeRef::ParenType(inner) => TypeRef::from_ast_opt(&ctx, inner.type_ref()), ast::TypeRef::ParenType(inner) => TypeRef::from_ast_opt(&ctx, inner.ty()),
ast::TypeRef::TupleType(inner) => { ast::TypeRef::TupleType(inner) => {
TypeRef::Tuple(inner.fields().map(|it| TypeRef::from_ast(ctx, it)).collect()) TypeRef::Tuple(inner.fields().map(|it| TypeRef::from_ast(ctx, it)).collect())
} }
@ -96,18 +96,18 @@ impl TypeRef {
.unwrap_or(TypeRef::Error) .unwrap_or(TypeRef::Error)
} }
ast::TypeRef::PointerType(inner) => { ast::TypeRef::PointerType(inner) => {
let inner_ty = TypeRef::from_ast_opt(&ctx, inner.type_ref()); let inner_ty = TypeRef::from_ast_opt(&ctx, inner.ty());
let mutability = Mutability::from_mutable(inner.mut_token().is_some()); let mutability = Mutability::from_mutable(inner.mut_token().is_some());
TypeRef::RawPtr(Box::new(inner_ty), mutability) TypeRef::RawPtr(Box::new(inner_ty), mutability)
} }
ast::TypeRef::ArrayType(inner) => { ast::TypeRef::ArrayType(inner) => {
TypeRef::Array(Box::new(TypeRef::from_ast_opt(&ctx, inner.type_ref()))) TypeRef::Array(Box::new(TypeRef::from_ast_opt(&ctx, inner.ty())))
} }
ast::TypeRef::SliceType(inner) => { ast::TypeRef::SliceType(inner) => {
TypeRef::Slice(Box::new(TypeRef::from_ast_opt(&ctx, inner.type_ref()))) TypeRef::Slice(Box::new(TypeRef::from_ast_opt(&ctx, inner.ty())))
} }
ast::TypeRef::ReferenceType(inner) => { ast::TypeRef::ReferenceType(inner) => {
let inner_ty = TypeRef::from_ast_opt(&ctx, inner.type_ref()); let inner_ty = TypeRef::from_ast_opt(&ctx, inner.ty());
let mutability = Mutability::from_mutable(inner.mut_token().is_some()); let mutability = Mutability::from_mutable(inner.mut_token().is_some());
TypeRef::Reference(Box::new(inner_ty), mutability) TypeRef::Reference(Box::new(inner_ty), mutability)
} }
@ -115,7 +115,7 @@ impl TypeRef {
ast::TypeRef::FnPointerType(inner) => { ast::TypeRef::FnPointerType(inner) => {
let ret_ty = inner let ret_ty = inner
.ret_type() .ret_type()
.and_then(|rt| rt.type_ref()) .and_then(|rt| rt.ty())
.map(|it| TypeRef::from_ast(ctx, it)) .map(|it| TypeRef::from_ast(ctx, it))
.unwrap_or_else(|| TypeRef::Tuple(Vec::new())); .unwrap_or_else(|| TypeRef::Tuple(Vec::new()));
let mut is_varargs = false; let mut is_varargs = false;
@ -132,7 +132,7 @@ impl TypeRef {
TypeRef::Fn(params, is_varargs) TypeRef::Fn(params, is_varargs)
} }
// for types are close enough for our purposes to the inner type for now... // for types are close enough for our purposes to the inner type for now...
ast::TypeRef::ForType(inner) => TypeRef::from_ast_opt(&ctx, inner.type_ref()), ast::TypeRef::ForType(inner) => TypeRef::from_ast_opt(&ctx, inner.ty()),
ast::TypeRef::ImplTraitType(inner) => { ast::TypeRef::ImplTraitType(inner) => {
TypeRef::ImplTrait(type_bounds_from_ast(ctx, inner.type_bound_list())) TypeRef::ImplTrait(type_bounds_from_ast(ctx, inner.type_bound_list()))
} }

View file

@ -44,7 +44,7 @@ pub(crate) fn function_declaration(node: &ast::Fn) -> String {
format_to!(buf, "{}", param_list); format_to!(buf, "{}", param_list);
} }
if let Some(ret_type) = node.ret_type() { if let Some(ret_type) = node.ret_type() {
if ret_type.type_ref().is_some() { if ret_type.ty().is_some() {
format_to!(buf, " {}", ret_type); format_to!(buf, " {}", ret_type);
} }
} }

View file

@ -125,7 +125,7 @@ fn structure_node(node: &SyntaxNode) -> Option<StructureNode> {
ast::Variant(it) => decl(it), ast::Variant(it) => decl(it),
ast::Trait(it) => decl(it), ast::Trait(it) => decl(it),
ast::Module(it) => decl(it), ast::Module(it) => decl(it),
ast::TypeAlias(it) => decl_with_type_ref(&it, it.type_ref()), ast::TypeAlias(it) => decl_with_type_ref(&it, it.ty()),
ast::RecordField(it) => decl_with_type_ref(&it, it.ty()), ast::RecordField(it) => decl_with_type_ref(&it, it.ty()),
ast::Const(it) => decl_with_type_ref(&it, it.ty()), ast::Const(it) => decl_with_type_ref(&it, it.ty()),
ast::Static(it) => decl_with_type_ref(&it, it.ty()), ast::Static(it) => decl_with_type_ref(&it, it.ty()),

View file

@ -198,7 +198,7 @@ impl TypeAlias {
pub fn default_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![default]) } pub fn default_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![default]) }
pub fn type_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![type]) } pub fn type_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![type]) }
pub fn eq_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=]) } pub fn eq_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![=]) }
pub fn type_ref(&self) -> Option<TypeRef> { support::child(&self.syntax) } pub fn ty(&self) -> Option<TypeRef> { support::child(&self.syntax) }
pub fn semicolon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![;]) } pub fn semicolon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![;]) }
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
@ -333,7 +333,7 @@ pub struct RetType {
} }
impl RetType { impl RetType {
pub fn thin_arrow_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![->]) } pub fn thin_arrow_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![->]) }
pub fn type_ref(&self) -> Option<TypeRef> { support::child(&self.syntax) } pub fn ty(&self) -> Option<TypeRef> { support::child(&self.syntax) }
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct WhereClause { pub struct WhereClause {
@ -425,7 +425,7 @@ pub struct TupleField {
impl ast::AttrsOwner for TupleField {} impl ast::AttrsOwner for TupleField {}
impl ast::VisibilityOwner for TupleField {} impl ast::VisibilityOwner for TupleField {}
impl TupleField { impl TupleField {
pub fn type_ref(&self) -> Option<TypeRef> { support::child(&self.syntax) } pub fn ty(&self) -> Option<TypeRef> { support::child(&self.syntax) }
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct VariantList { pub struct VariantList {
@ -525,7 +525,7 @@ pub struct ParenType {
} }
impl ParenType { impl ParenType {
pub fn l_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['(']) } pub fn l_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['(']) }
pub fn type_ref(&self) -> Option<TypeRef> { support::child(&self.syntax) } pub fn ty(&self) -> Option<TypeRef> { support::child(&self.syntax) }
pub fn r_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![')']) } pub fn r_paren_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![')']) }
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
@ -559,7 +559,7 @@ impl PointerType {
pub fn star_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![*]) } pub fn star_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![*]) }
pub fn const_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![const]) } pub fn const_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![const]) }
pub fn mut_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![mut]) } pub fn mut_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![mut]) }
pub fn type_ref(&self) -> Option<TypeRef> { support::child(&self.syntax) } pub fn ty(&self) -> Option<TypeRef> { support::child(&self.syntax) }
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ArrayType { pub struct ArrayType {
@ -567,7 +567,7 @@ pub struct ArrayType {
} }
impl ArrayType { impl ArrayType {
pub fn l_brack_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['[']) } pub fn l_brack_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['[']) }
pub fn type_ref(&self) -> Option<TypeRef> { support::child(&self.syntax) } pub fn ty(&self) -> Option<TypeRef> { support::child(&self.syntax) }
pub fn semicolon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![;]) } pub fn semicolon_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![;]) }
pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) } pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) }
pub fn r_brack_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![']']) } pub fn r_brack_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![']']) }
@ -578,7 +578,7 @@ pub struct SliceType {
} }
impl SliceType { impl SliceType {
pub fn l_brack_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['[']) } pub fn l_brack_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T!['[']) }
pub fn type_ref(&self) -> Option<TypeRef> { support::child(&self.syntax) } pub fn ty(&self) -> Option<TypeRef> { support::child(&self.syntax) }
pub fn r_brack_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![']']) } pub fn r_brack_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![']']) }
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
@ -591,7 +591,7 @@ impl ReferenceType {
support::token(&self.syntax, T![lifetime]) support::token(&self.syntax, T![lifetime])
} }
pub fn mut_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![mut]) } pub fn mut_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![mut]) }
pub fn type_ref(&self) -> Option<TypeRef> { support::child(&self.syntax) } pub fn ty(&self) -> Option<TypeRef> { support::child(&self.syntax) }
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct PlaceholderType { pub struct PlaceholderType {
@ -618,7 +618,7 @@ pub struct ForType {
impl ForType { impl ForType {
pub fn for_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![for]) } pub fn for_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![for]) }
pub fn generic_param_list(&self) -> Option<GenericParamList> { support::child(&self.syntax) } pub fn generic_param_list(&self) -> Option<GenericParamList> { support::child(&self.syntax) }
pub fn type_ref(&self) -> Option<TypeRef> { support::child(&self.syntax) } pub fn ty(&self) -> Option<TypeRef> { support::child(&self.syntax) }
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ImplTraitType { pub struct ImplTraitType {
@ -882,7 +882,7 @@ impl ast::AttrsOwner for CastExpr {}
impl CastExpr { impl CastExpr {
pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) } pub fn expr(&self) -> Option<Expr> { support::child(&self.syntax) }
pub fn as_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![as]) } pub fn as_token(&self) -> Option<SyntaxToken> { support::token(&self.syntax, T![as]) }
pub fn type_ref(&self) -> Option<TypeRef> { support::child(&self.syntax) } pub fn ty(&self) -> Option<TypeRef> { support::child(&self.syntax) }
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct RefExpr { pub struct RefExpr {

View file

@ -72,11 +72,11 @@ Param =
) )
RetType = RetType =
'->' TypeRef '->' ty:TypeRef
TypeAlias = TypeAlias =
Attr* Visibility? 'default'? 'type' Name GenericParamList? (':' TypeBoundList?)? WhereClause? Attr* Visibility? 'default'? 'type' Name GenericParamList? (':' TypeBoundList?)? WhereClause?
'=' TypeRef ';' '=' ty:TypeRef ';'
Struct = Struct =
Attr* Visibility? 'struct' Name GenericParamList? ( Attr* Visibility? 'struct' Name GenericParamList? (
@ -94,7 +94,7 @@ TupleFieldList =
'(' fields:(TupleField (',' TupleField)* ','?)? ')' '(' fields:(TupleField (',' TupleField)* ','?)? ')'
TupleField = TupleField =
Attr* Visibility? TypeRef Attr* Visibility? ty:TypeRef
FieldList = FieldList =
RecordFieldList RecordFieldList
@ -184,7 +184,7 @@ Attr =
'#' '!'? '[' Path ('=' Literal | TokenTree)? ']' '#' '!'? '[' Path ('=' Literal | TokenTree)? ']'
ParenType = ParenType =
'(' TypeRef ')' '(' ty:TypeRef ')'
TupleType = TupleType =
'(' fields:TypeRef* ')' '(' fields:TypeRef* ')'
@ -196,16 +196,16 @@ PathType =
Path Path
PointerType = PointerType =
'*' ('const' | 'mut') TypeRef '*' ('const' | 'mut') ty:TypeRef
ArrayType = ArrayType =
'[' TypeRef ';' Expr ']' '[' ty:TypeRef ';' Expr ']'
SliceType = SliceType =
'[' TypeRef ']' '[' ty:TypeRef ']'
ReferenceType = ReferenceType =
'&' 'lifetime'? 'mut'? TypeRef '&' 'lifetime'? 'mut'? ty:TypeRef
PlaceholderType = PlaceholderType =
'_' '_'
@ -214,7 +214,7 @@ FnPointerType =
Abi 'unsafe'? 'fn' ParamList RetType? Abi 'unsafe'? 'fn' ParamList RetType?
ForType = ForType =
'for' GenericParamList TypeRef 'for' GenericParamList ty:TypeRef
ImplTraitType = ImplTraitType =
'impl' TypeBoundList 'impl' TypeBoundList
@ -302,7 +302,7 @@ TryExpr =
Attr* Expr '?' Attr* Expr '?'
CastExpr = CastExpr =
Attr* Expr 'as' TypeRef Attr* Expr 'as' ty:TypeRef
RefExpr = RefExpr =
Attr* '&' ('raw' | 'mut' | 'const') Expr Attr* '&' ('raw' | 'mut' | 'const') Expr